00001 /************************************************************************ 00002 Starts the InnoDB database server 00003 00004 (c) 1996-2000 Innobase Oy 00005 00006 Created 2/16/1996 Heikki Tuuri 00007 *************************************************************************/ 00008 00009 #include "os0proc.h" 00010 #include "sync0sync.h" 00011 #include "ut0mem.h" 00012 #include "mem0mem.h" 00013 #include "mem0pool.h" 00014 #include "data0data.h" 00015 #include "data0type.h" 00016 #include "dict0dict.h" 00017 #include "buf0buf.h" 00018 #include "buf0flu.h" 00019 #include "buf0rea.h" 00020 #include "os0file.h" 00021 #include "os0thread.h" 00022 #include "fil0fil.h" 00023 #include "fsp0fsp.h" 00024 #include "rem0rec.h" 00025 #include "rem0cmp.h" 00026 #include "mtr0mtr.h" 00027 #include "log0log.h" 00028 #include "log0recv.h" 00029 #include "page0page.h" 00030 #include "page0cur.h" 00031 #include "trx0trx.h" 00032 #include "dict0boot.h" 00033 #include "dict0load.h" 00034 #include "trx0sys.h" 00035 #include "dict0crea.h" 00036 #include "btr0btr.h" 00037 #include "btr0pcur.h" 00038 #include "btr0cur.h" 00039 #include "btr0sea.h" 00040 #include "rem0rec.h" 00041 #include "srv0srv.h" 00042 #include "que0que.h" 00043 #include "usr0sess.h" 00044 #include "lock0lock.h" 00045 #include "trx0roll.h" 00046 #include "trx0purge.h" 00047 #include "row0ins.h" 00048 #include "row0sel.h" 00049 #include "row0upd.h" 00050 #include "row0row.h" 00051 #include "row0mysql.h" 00052 #include "lock0lock.h" 00053 #include "ibuf0ibuf.h" 00054 #include "pars0pars.h" 00055 #include "btr0sea.h" 00056 #include "srv0start.h" 00057 #include "que0que.h" 00058 00059 /* Log sequence number immediately after startup */ 00060 dulint srv_start_lsn; 00061 /* Log sequence number at shutdown */ 00062 dulint srv_shutdown_lsn; 00063 00064 #ifdef HAVE_DARWIN_THREADS 00065 # include <sys/utsname.h> 00066 ibool srv_have_fullfsync = FALSE; 00067 #endif 00068 00069 ibool srv_start_raw_disk_in_use = FALSE; 00070 00071 ulint srv_sizeof_trx_t_in_ha_innodb_cc; 00072 00073 ibool srv_startup_is_before_trx_rollback_phase = FALSE; 00074 ibool srv_is_being_started = FALSE; 00075 #ifndef UNIV_HOTBACKUP 00076 static ibool srv_start_has_been_called = FALSE; 00077 static ibool srv_was_started = FALSE; 00078 #endif /* !UNIV_HOTBACKUP */ 00079 00080 /* At a shutdown the value first climbs to SRV_SHUTDOWN_CLEANUP 00081 and then to SRV_SHUTDOWN_LAST_PHASE */ 00082 ulint srv_shutdown_state = 0; 00083 00084 #ifndef UNIV_HOTBACKUP 00085 static os_file_t files[1000]; 00086 00087 static mutex_t ios_mutex; 00088 static ulint ios; 00089 00090 static ulint n[SRV_MAX_N_IO_THREADS + 5]; 00091 static os_thread_id_t thread_ids[SRV_MAX_N_IO_THREADS + 5]; 00092 00093 /* We use this mutex to test the return value of pthread_mutex_trylock 00094 on successful locking. HP-UX does NOT return 0, though Linux et al do. */ 00095 static os_fast_mutex_t srv_os_test_mutex; 00096 00097 /* Name of srv_monitor_file */ 00098 static char* srv_monitor_file_name; 00099 #endif /* !UNIV_HOTBACKUP */ 00100 00101 #define SRV_N_PENDING_IOS_PER_THREAD OS_AIO_N_PENDING_IOS_PER_THREAD 00102 #define SRV_MAX_N_PENDING_SYNC_IOS 100 00103 00104 00105 /* Avoid warnings when using purify */ 00106 00107 #ifdef HAVE_purify 00108 static int inno_bcmp(register const char *s1, register const char *s2, 00109 register uint len) 00110 { 00111 while ((len-- != 0) && (*s1++ == *s2++)) 00112 ; 00113 00114 return(len + 1); 00115 } 00116 #define memcmp(A,B,C) inno_bcmp((A),(B),(C)) 00117 #endif 00118 00119 static 00120 char* 00121 srv_parse_megabytes( 00122 /*================*/ 00123 /* out: next character in string */ 00124 char* str, /* in: string containing a quantity in bytes */ 00125 ulint* megs) /* out: the number in megabytes */ 00126 { 00127 char* endp; 00128 ulint size; 00129 00130 size = strtoul(str, &endp, 10); 00131 00132 str = endp; 00133 00134 switch (*str) { 00135 case 'G': case 'g': 00136 size *= 1024; 00137 /* fall through */ 00138 case 'M': case 'm': 00139 str++; 00140 break; 00141 default: 00142 size /= 1024 * 1024; 00143 break; 00144 } 00145 00146 *megs = size; 00147 return(str); 00148 } 00149 00150 /************************************************************************* 00151 Reads the data files and their sizes from a character string given in 00152 the .cnf file. */ 00153 00154 ibool 00155 srv_parse_data_file_paths_and_sizes( 00156 /*================================*/ 00157 /* out: TRUE if ok, FALSE if parsing 00158 error */ 00159 char* str, /* in: the data file path string */ 00160 char*** data_file_names, /* out, own: array of data file 00161 names */ 00162 ulint** data_file_sizes, /* out, own: array of data file sizes 00163 in megabytes */ 00164 ulint** data_file_is_raw_partition,/* out, own: array of flags 00165 showing which data files are raw 00166 partitions */ 00167 ulint* n_data_files, /* out: number of data files */ 00168 ibool* is_auto_extending, /* out: TRUE if the last data file is 00169 auto-extending */ 00170 ulint* max_auto_extend_size) /* out: max auto extend size for the 00171 last file if specified, 0 if not */ 00172 { 00173 char* input_str; 00174 char* path; 00175 ulint size; 00176 ulint i = 0; 00177 00178 *is_auto_extending = FALSE; 00179 *max_auto_extend_size = 0; 00180 00181 input_str = str; 00182 00183 /* First calculate the number of data files and check syntax: 00184 path:size[M | G];path:size[M | G]... . Note that a Windows path may 00185 contain a drive name and a ':'. */ 00186 00187 while (*str != '\0') { 00188 path = str; 00189 00190 while ((*str != ':' && *str != '\0') 00191 || (*str == ':' 00192 && (*(str + 1) == '\\' || *(str + 1) == '/' 00193 || *(str + 1) == ':'))) { 00194 str++; 00195 } 00196 00197 if (*str == '\0') { 00198 return(FALSE); 00199 } 00200 00201 str++; 00202 00203 str = srv_parse_megabytes(str, &size); 00204 00205 if (0 == memcmp(str, ":autoextend", (sizeof ":autoextend") - 1)) { 00206 00207 str += (sizeof ":autoextend") - 1; 00208 00209 if (0 == memcmp(str, ":max:", (sizeof ":max:") - 1)) { 00210 00211 str += (sizeof ":max:") - 1; 00212 00213 str = srv_parse_megabytes(str, &size); 00214 } 00215 00216 if (*str != '\0') { 00217 00218 return(FALSE); 00219 } 00220 } 00221 00222 if (strlen(str) >= 6 00223 && *str == 'n' 00224 && *(str + 1) == 'e' 00225 && *(str + 2) == 'w') { 00226 str += 3; 00227 } 00228 00229 if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') { 00230 str += 3; 00231 } 00232 00233 if (size == 0) { 00234 return(FALSE); 00235 } 00236 00237 i++; 00238 00239 if (*str == ';') { 00240 str++; 00241 } else if (*str != '\0') { 00242 00243 return(FALSE); 00244 } 00245 } 00246 00247 if (i == 0) { 00248 /* If innodb_data_file_path was defined it must contain 00249 at least one data file definition */ 00250 00251 return(FALSE); 00252 } 00253 00254 *data_file_names = (char**)ut_malloc(i * sizeof(void*)); 00255 *data_file_sizes = (ulint*)ut_malloc(i * sizeof(ulint)); 00256 *data_file_is_raw_partition = (ulint*)ut_malloc(i * sizeof(ulint)); 00257 00258 *n_data_files = i; 00259 00260 /* Then store the actual values to our arrays */ 00261 00262 str = input_str; 00263 i = 0; 00264 00265 while (*str != '\0') { 00266 path = str; 00267 00268 /* Note that we must step over the ':' in a Windows path; 00269 a Windows path normally looks like C:\ibdata\ibdata1:1G, but 00270 a Windows raw partition may have a specification like 00271 \\.\C::1Gnewraw or \\.\PHYSICALDRIVE2:1Gnewraw */ 00272 00273 while ((*str != ':' && *str != '\0') 00274 || (*str == ':' 00275 && (*(str + 1) == '\\' || *(str + 1) == '/' 00276 || *(str + 1) == ':'))) { 00277 str++; 00278 } 00279 00280 if (*str == ':') { 00281 /* Make path a null-terminated string */ 00282 *str = '\0'; 00283 str++; 00284 } 00285 00286 str = srv_parse_megabytes(str, &size); 00287 00288 (*data_file_names)[i] = path; 00289 (*data_file_sizes)[i] = size; 00290 00291 if (0 == memcmp(str, ":autoextend", (sizeof ":autoextend") - 1)) { 00292 00293 *is_auto_extending = TRUE; 00294 00295 str += (sizeof ":autoextend") - 1; 00296 00297 if (0 == memcmp(str, ":max:", (sizeof ":max:") - 1)) { 00298 00299 str += (sizeof ":max:") - 1; 00300 00301 str = srv_parse_megabytes(str, 00302 max_auto_extend_size); 00303 } 00304 00305 if (*str != '\0') { 00306 00307 return(FALSE); 00308 } 00309 } 00310 00311 (*data_file_is_raw_partition)[i] = 0; 00312 00313 if (strlen(str) >= 6 00314 && *str == 'n' 00315 && *(str + 1) == 'e' 00316 && *(str + 2) == 'w') { 00317 str += 3; 00318 (*data_file_is_raw_partition)[i] = SRV_NEW_RAW; 00319 } 00320 00321 if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') { 00322 str += 3; 00323 00324 if ((*data_file_is_raw_partition)[i] == 0) { 00325 (*data_file_is_raw_partition)[i] = SRV_OLD_RAW; 00326 } 00327 } 00328 00329 i++; 00330 00331 if (*str == ';') { 00332 str++; 00333 } 00334 } 00335 00336 return(TRUE); 00337 } 00338 00339 /************************************************************************* 00340 Reads log group home directories from a character string given in 00341 the .cnf file. */ 00342 00343 ibool 00344 srv_parse_log_group_home_dirs( 00345 /*==========================*/ 00346 /* out: TRUE if ok, FALSE if parsing 00347 error */ 00348 char* str, /* in: character string */ 00349 char*** log_group_home_dirs) /* out, own: log group home dirs */ 00350 { 00351 char* input_str; 00352 char* path; 00353 ulint i = 0; 00354 00355 input_str = str; 00356 00357 /* First calculate the number of directories and check syntax: 00358 path;path;... */ 00359 00360 while (*str != '\0') { 00361 path = str; 00362 00363 while (*str != ';' && *str != '\0') { 00364 str++; 00365 } 00366 00367 i++; 00368 00369 if (*str == ';') { 00370 str++; 00371 } else if (*str != '\0') { 00372 00373 return(FALSE); 00374 } 00375 } 00376 00377 if (i != 1) { 00378 /* If innodb_log_group_home_dir was defined it must 00379 contain exactly one path definition under current MySQL */ 00380 00381 return(FALSE); 00382 } 00383 00384 *log_group_home_dirs = (char**) ut_malloc(i * sizeof(void*)); 00385 00386 /* Then store the actual values to our array */ 00387 00388 str = input_str; 00389 i = 0; 00390 00391 while (*str != '\0') { 00392 path = str; 00393 00394 while (*str != ';' && *str != '\0') { 00395 str++; 00396 } 00397 00398 if (*str == ';') { 00399 *str = '\0'; 00400 str++; 00401 } 00402 00403 (*log_group_home_dirs)[i] = path; 00404 00405 i++; 00406 } 00407 00408 return(TRUE); 00409 } 00410 00411 #ifndef UNIV_HOTBACKUP 00412 /************************************************************************ 00413 I/o-handler thread function. */ 00414 static 00415 00416 os_thread_ret_t 00417 io_handler_thread( 00418 /*==============*/ 00419 void* arg) 00420 { 00421 ulint segment; 00422 ulint i; 00423 00424 segment = *((ulint*)arg); 00425 00426 #ifdef UNIV_DEBUG_THREAD_CREATION 00427 fprintf(stderr, "Io handler thread %lu starts, id %lu\n", segment, 00428 os_thread_pf(os_thread_get_curr_id())); 00429 #endif 00430 for (i = 0;; i++) { 00431 fil_aio_wait(segment); 00432 00433 mutex_enter(&ios_mutex); 00434 ios++; 00435 mutex_exit(&ios_mutex); 00436 } 00437 00438 /* We count the number of threads in os_thread_exit(). A created 00439 thread should always use that to exit and not use return() to exit. 00440 The thread actually never comes here because it is exited in an 00441 os_event_wait(). */ 00442 00443 os_thread_exit(NULL); 00444 00445 OS_THREAD_DUMMY_RETURN; 00446 } 00447 #endif /* !UNIV_HOTBACKUP */ 00448 00449 #ifdef __WIN__ 00450 #define SRV_PATH_SEPARATOR '\\' 00451 #else 00452 #define SRV_PATH_SEPARATOR '/' 00453 #endif 00454 00455 /************************************************************************* 00456 Normalizes a directory path for Windows: converts slashes to backslashes. */ 00457 00458 void 00459 srv_normalize_path_for_win( 00460 /*=======================*/ 00461 char* str __attribute__((unused))) /* in/out: null-terminated 00462 character string */ 00463 { 00464 #ifdef __WIN__ 00465 for (; *str; str++) { 00466 00467 if (*str == '/') { 00468 *str = '\\'; 00469 } 00470 } 00471 #endif 00472 } 00473 00474 /************************************************************************* 00475 Adds a slash or a backslash to the end of a string if it is missing 00476 and the string is not empty. */ 00477 00478 char* 00479 srv_add_path_separator_if_needed( 00480 /*=============================*/ 00481 /* out: string which has the separator if the 00482 string is not empty */ 00483 char* str) /* in: null-terminated character string */ 00484 { 00485 char* out_str; 00486 ulint len = ut_strlen(str); 00487 00488 if (len == 0 || str[len - 1] == SRV_PATH_SEPARATOR) { 00489 00490 return(str); 00491 } 00492 00493 out_str = ut_malloc(len + 2); 00494 memcpy(out_str, str, len); 00495 out_str[len] = SRV_PATH_SEPARATOR; 00496 out_str[len + 1] = 0; 00497 00498 return(out_str); 00499 } 00500 00501 #ifndef UNIV_HOTBACKUP 00502 /************************************************************************* 00503 Calculates the low 32 bits when a file size which is given as a number 00504 database pages is converted to the number of bytes. */ 00505 static 00506 ulint 00507 srv_calc_low32( 00508 /*===========*/ 00509 /* out: low 32 bytes of file size when 00510 expressed in bytes */ 00511 ulint file_size) /* in: file size in database pages */ 00512 { 00513 return(0xFFFFFFFFUL & (file_size << UNIV_PAGE_SIZE_SHIFT)); 00514 } 00515 00516 /************************************************************************* 00517 Calculates the high 32 bits when a file size which is given as a number 00518 database pages is converted to the number of bytes. */ 00519 static 00520 ulint 00521 srv_calc_high32( 00522 /*============*/ 00523 /* out: high 32 bytes of file size when 00524 expressed in bytes */ 00525 ulint file_size) /* in: file size in database pages */ 00526 { 00527 return(file_size >> (32 - UNIV_PAGE_SIZE_SHIFT)); 00528 } 00529 00530 /************************************************************************* 00531 Creates or opens the log files and closes them. */ 00532 static 00533 ulint 00534 open_or_create_log_file( 00535 /*====================*/ 00536 /* out: DB_SUCCESS or error code */ 00537 ibool create_new_db, /* in: TRUE if we should create a 00538 new database */ 00539 ibool* log_file_created, /* out: TRUE if new log file 00540 created */ 00541 ibool log_file_has_been_opened,/* in: TRUE if a log file has been 00542 opened before: then it is an error 00543 to try to create another log file */ 00544 ulint k, /* in: log group number */ 00545 ulint i) /* in: log file number in group */ 00546 { 00547 ibool ret; 00548 ulint size; 00549 ulint size_high; 00550 char name[10000]; 00551 00552 UT_NOT_USED(create_new_db); 00553 00554 *log_file_created = FALSE; 00555 00556 srv_normalize_path_for_win(srv_log_group_home_dirs[k]); 00557 srv_log_group_home_dirs[k] = srv_add_path_separator_if_needed( 00558 srv_log_group_home_dirs[k]); 00559 00560 ut_a(strlen(srv_log_group_home_dirs[k]) < 00561 (sizeof name) - 10 - sizeof "ib_logfile"); 00562 sprintf(name, "%s%s%lu", srv_log_group_home_dirs[k], "ib_logfile", (ulong) i); 00563 00564 files[i] = os_file_create(name, OS_FILE_CREATE, OS_FILE_NORMAL, 00565 OS_LOG_FILE, &ret); 00566 if (ret == FALSE) { 00567 if (os_file_get_last_error(FALSE) != OS_FILE_ALREADY_EXISTS 00568 #ifdef UNIV_AIX 00569 /* AIX 5.1 after security patch ML7 may have errno set 00570 to 0 here, which causes our function to return 100; 00571 work around that AIX problem */ 00572 && os_file_get_last_error(FALSE) != 100 00573 #endif 00574 ) { 00575 fprintf(stderr, 00576 "InnoDB: Error in creating or opening %s\n", name); 00577 00578 return(DB_ERROR); 00579 } 00580 00581 files[i] = os_file_create(name, OS_FILE_OPEN, OS_FILE_AIO, 00582 OS_LOG_FILE, &ret); 00583 if (!ret) { 00584 fprintf(stderr, 00585 "InnoDB: Error in opening %s\n", name); 00586 00587 return(DB_ERROR); 00588 } 00589 00590 ret = os_file_get_size(files[i], &size, &size_high); 00591 ut_a(ret); 00592 00593 if (size != srv_calc_low32(srv_log_file_size) 00594 || size_high != srv_calc_high32(srv_log_file_size)) { 00595 00596 fprintf(stderr, 00597 "InnoDB: Error: log file %s is of different size %lu %lu bytes\n" 00598 "InnoDB: than specified in the .cnf file %lu %lu bytes!\n", 00599 name, (ulong) size_high, (ulong) size, 00600 (ulong) srv_calc_high32(srv_log_file_size), 00601 (ulong) srv_calc_low32(srv_log_file_size)); 00602 00603 return(DB_ERROR); 00604 } 00605 } else { 00606 *log_file_created = TRUE; 00607 00608 ut_print_timestamp(stderr); 00609 00610 fprintf(stderr, 00611 " InnoDB: Log file %s did not exist: new to be created\n", 00612 name); 00613 if (log_file_has_been_opened) { 00614 00615 return(DB_ERROR); 00616 } 00617 00618 fprintf(stderr, "InnoDB: Setting log file %s size to %lu MB\n", 00619 name, (ulong) srv_log_file_size 00620 >> (20 - UNIV_PAGE_SIZE_SHIFT)); 00621 00622 fprintf(stderr, 00623 "InnoDB: Database physically writes the file full: wait...\n"); 00624 00625 ret = os_file_set_size(name, files[i], 00626 srv_calc_low32(srv_log_file_size), 00627 srv_calc_high32(srv_log_file_size)); 00628 if (!ret) { 00629 fprintf(stderr, 00630 "InnoDB: Error in creating %s: probably out of disk space\n", 00631 name); 00632 00633 return(DB_ERROR); 00634 } 00635 } 00636 00637 ret = os_file_close(files[i]); 00638 ut_a(ret); 00639 00640 if (i == 0) { 00641 /* Create in memory the file space object 00642 which is for this log group */ 00643 00644 fil_space_create(name, 00645 2 * k + SRV_LOG_SPACE_FIRST_ID, FIL_LOG); 00646 } 00647 00648 ut_a(fil_validate()); 00649 00650 fil_node_create(name, srv_log_file_size, 00651 2 * k + SRV_LOG_SPACE_FIRST_ID, FALSE); 00652 #ifdef UNIV_LOG_ARCHIVE 00653 /* If this is the first log group, create the file space object 00654 for archived logs. 00655 Under MySQL, no archiving ever done. */ 00656 00657 if (k == 0 && i == 0) { 00658 arch_space_id = 2 * k + 1 + SRV_LOG_SPACE_FIRST_ID; 00659 00660 fil_space_create("arch_log_space", arch_space_id, FIL_LOG); 00661 } else { 00662 arch_space_id = ULINT_UNDEFINED; 00663 } 00664 #endif /* UNIV_LOG_ARCHIVE */ 00665 if (i == 0) { 00666 log_group_init(k, srv_n_log_files, 00667 srv_log_file_size * UNIV_PAGE_SIZE, 00668 2 * k + SRV_LOG_SPACE_FIRST_ID, 00669 SRV_LOG_SPACE_FIRST_ID + 1); /* dummy arch 00670 space id */ 00671 } 00672 00673 return(DB_SUCCESS); 00674 } 00675 00676 /************************************************************************* 00677 Creates or opens database data files and closes them. */ 00678 static 00679 ulint 00680 open_or_create_data_files( 00681 /*======================*/ 00682 /* out: DB_SUCCESS or error code */ 00683 ibool* create_new_db, /* out: TRUE if new database should be 00684 created */ 00685 #ifdef UNIV_LOG_ARCHIVE 00686 ulint* min_arch_log_no,/* out: min of archived log numbers in data 00687 files */ 00688 ulint* max_arch_log_no,/* out: */ 00689 #endif /* UNIV_LOG_ARCHIVE */ 00690 dulint* min_flushed_lsn,/* out: min of flushed lsn values in data 00691 files */ 00692 dulint* max_flushed_lsn,/* out: */ 00693 ulint* sum_of_new_sizes)/* out: sum of sizes of the new files added */ 00694 { 00695 ibool ret; 00696 ulint i; 00697 ibool one_opened = FALSE; 00698 ibool one_created = FALSE; 00699 ulint size; 00700 ulint size_high; 00701 ulint rounded_size_pages; 00702 char name[10000]; 00703 00704 if (srv_n_data_files >= 1000) { 00705 fprintf(stderr, "InnoDB: can only have < 1000 data files\n" 00706 "InnoDB: you have defined %lu\n", 00707 (ulong) srv_n_data_files); 00708 return(DB_ERROR); 00709 } 00710 00711 *sum_of_new_sizes = 0; 00712 00713 *create_new_db = FALSE; 00714 00715 srv_normalize_path_for_win(srv_data_home); 00716 srv_data_home = srv_add_path_separator_if_needed(srv_data_home); 00717 00718 for (i = 0; i < srv_n_data_files; i++) { 00719 srv_normalize_path_for_win(srv_data_file_names[i]); 00720 00721 ut_a(strlen(srv_data_home) + strlen(srv_data_file_names[i]) 00722 < (sizeof name) - 1); 00723 sprintf(name, "%s%s", srv_data_home, srv_data_file_names[i]); 00724 00725 if (srv_data_file_is_raw_partition[i] == 0) { 00726 00727 /* First we try to create the file: if it already 00728 exists, ret will get value FALSE */ 00729 00730 files[i] = os_file_create(name, OS_FILE_CREATE, 00731 OS_FILE_NORMAL, OS_DATA_FILE, &ret); 00732 00733 if (ret == FALSE && os_file_get_last_error(FALSE) != 00734 OS_FILE_ALREADY_EXISTS 00735 #ifdef UNIV_AIX 00736 /* AIX 5.1 after security patch ML7 may have 00737 errno set to 0 here, which causes our function 00738 to return 100; work around that AIX problem */ 00739 && os_file_get_last_error(FALSE) != 100 00740 #endif 00741 ) { 00742 fprintf(stderr, 00743 "InnoDB: Error in creating or opening %s\n", 00744 name); 00745 00746 return(DB_ERROR); 00747 } 00748 } else if (srv_data_file_is_raw_partition[i] == SRV_NEW_RAW) { 00749 /* The partition is opened, not created; then it is 00750 written over */ 00751 00752 srv_start_raw_disk_in_use = TRUE; 00753 srv_created_new_raw = TRUE; 00754 00755 files[i] = os_file_create( 00756 name, OS_FILE_OPEN_RAW, OS_FILE_NORMAL, 00757 OS_DATA_FILE, &ret); 00758 if (!ret) { 00759 fprintf(stderr, 00760 "InnoDB: Error in opening %s\n", name); 00761 00762 return(DB_ERROR); 00763 } 00764 } else if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) { 00765 srv_start_raw_disk_in_use = TRUE; 00766 00767 ret = FALSE; 00768 } else { 00769 ut_a(0); 00770 } 00771 00772 if (ret == FALSE) { 00773 /* We open the data file */ 00774 00775 if (one_created) { 00776 fprintf(stderr, 00777 "InnoDB: Error: data files can only be added at the end\n"); 00778 fprintf(stderr, 00779 "InnoDB: of a tablespace, but data file %s existed beforehand.\n", 00780 name); 00781 return(DB_ERROR); 00782 } 00783 00784 if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) { 00785 files[i] = os_file_create( 00786 name, OS_FILE_OPEN_RAW, OS_FILE_NORMAL, 00787 OS_DATA_FILE, &ret); 00788 } else if (i == 0) { 00789 files[i] = os_file_create( 00790 name, OS_FILE_OPEN_RETRY, 00791 OS_FILE_NORMAL, 00792 OS_DATA_FILE, &ret); 00793 } else { 00794 files[i] = os_file_create( 00795 name, OS_FILE_OPEN, OS_FILE_NORMAL, 00796 OS_DATA_FILE, &ret); 00797 } 00798 00799 if (!ret) { 00800 fprintf(stderr, 00801 "InnoDB: Error in opening %s\n", name); 00802 os_file_get_last_error(TRUE); 00803 00804 return(DB_ERROR); 00805 } 00806 00807 if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) { 00808 00809 goto skip_size_check; 00810 } 00811 00812 ret = os_file_get_size(files[i], &size, &size_high); 00813 ut_a(ret); 00814 /* Round size downward to megabytes */ 00815 00816 rounded_size_pages = (size / (1024 * 1024) 00817 + 4096 * size_high) 00818 << (20 - UNIV_PAGE_SIZE_SHIFT); 00819 00820 if (i == srv_n_data_files - 1 00821 && srv_auto_extend_last_data_file) { 00822 00823 if (srv_data_file_sizes[i] > 00824 rounded_size_pages 00825 || (srv_last_file_size_max > 0 00826 && srv_last_file_size_max < 00827 rounded_size_pages)) { 00828 00829 fprintf(stderr, 00830 "InnoDB: Error: auto-extending data file %s is of a different size\n" 00831 "InnoDB: %lu pages (rounded down to MB) than specified in the .cnf file:\n" 00832 "InnoDB: initial %lu pages, max %lu (relevant if non-zero) pages!\n", 00833 name, (ulong) rounded_size_pages, 00834 (ulong) srv_data_file_sizes[i], 00835 (ulong) srv_last_file_size_max); 00836 00837 return(DB_ERROR); 00838 } 00839 00840 srv_data_file_sizes[i] = rounded_size_pages; 00841 } 00842 00843 if (rounded_size_pages != srv_data_file_sizes[i]) { 00844 00845 fprintf(stderr, 00846 "InnoDB: Error: data file %s is of a different size\n" 00847 "InnoDB: %lu pages (rounded down to MB)\n" 00848 "InnoDB: than specified in the .cnf file %lu pages!\n", name, 00849 (ulong) rounded_size_pages, 00850 (ulong) srv_data_file_sizes[i]); 00851 00852 return(DB_ERROR); 00853 } 00854 skip_size_check: 00855 fil_read_flushed_lsn_and_arch_log_no(files[i], 00856 one_opened, 00857 #ifdef UNIV_LOG_ARCHIVE 00858 min_arch_log_no, max_arch_log_no, 00859 #endif /* UNIV_LOG_ARCHIVE */ 00860 min_flushed_lsn, max_flushed_lsn); 00861 one_opened = TRUE; 00862 } else { 00863 /* We created the data file and now write it full of 00864 zeros */ 00865 00866 one_created = TRUE; 00867 00868 if (i > 0) { 00869 ut_print_timestamp(stderr); 00870 fprintf(stderr, 00871 " InnoDB: Data file %s did not exist: new to be created\n", 00872 name); 00873 } else { 00874 fprintf(stderr, 00875 "InnoDB: The first specified data file %s did not exist:\n" 00876 "InnoDB: a new database to be created!\n", name); 00877 *create_new_db = TRUE; 00878 } 00879 00880 ut_print_timestamp(stderr); 00881 fprintf(stderr, 00882 " InnoDB: Setting file %s size to %lu MB\n", 00883 name, (ulong) (srv_data_file_sizes[i] 00884 >> (20 - UNIV_PAGE_SIZE_SHIFT))); 00885 00886 fprintf(stderr, 00887 "InnoDB: Database physically writes the file full: wait...\n"); 00888 00889 ret = os_file_set_size(name, files[i], 00890 srv_calc_low32(srv_data_file_sizes[i]), 00891 srv_calc_high32(srv_data_file_sizes[i])); 00892 00893 if (!ret) { 00894 fprintf(stderr, 00895 "InnoDB: Error in creating %s: probably out of disk space\n", name); 00896 00897 return(DB_ERROR); 00898 } 00899 00900 *sum_of_new_sizes = *sum_of_new_sizes 00901 + srv_data_file_sizes[i]; 00902 } 00903 00904 ret = os_file_close(files[i]); 00905 ut_a(ret); 00906 00907 if (i == 0) { 00908 fil_space_create(name, 0, FIL_TABLESPACE); 00909 } 00910 00911 ut_a(fil_validate()); 00912 00913 if (srv_data_file_is_raw_partition[i]) { 00914 00915 fil_node_create(name, srv_data_file_sizes[i], 0, TRUE); 00916 } else { 00917 fil_node_create(name, srv_data_file_sizes[i], 0, 00918 FALSE); 00919 } 00920 } 00921 00922 ios = 0; 00923 00924 mutex_create(&ios_mutex, SYNC_NO_ORDER_CHECK); 00925 00926 return(DB_SUCCESS); 00927 } 00928 00929 /******************************************************************** 00930 Starts InnoDB and creates a new database if database files 00931 are not found and the user wants. Server parameters are 00932 read from a file of name "srv_init" in the ib_home directory. */ 00933 00934 int 00935 innobase_start_or_create_for_mysql(void) 00936 /*====================================*/ 00937 /* out: DB_SUCCESS or error code */ 00938 { 00939 buf_pool_t* ret; 00940 ibool create_new_db; 00941 ibool log_file_created; 00942 ibool log_created = FALSE; 00943 ibool log_opened = FALSE; 00944 dulint min_flushed_lsn; 00945 dulint max_flushed_lsn; 00946 #ifdef UNIV_LOG_ARCHIVE 00947 ulint min_arch_log_no; 00948 ulint max_arch_log_no; 00949 #endif /* UNIV_LOG_ARCHIVE */ 00950 ulint sum_of_new_sizes; 00951 ulint sum_of_data_file_sizes; 00952 ulint tablespace_size_in_header; 00953 ulint err; 00954 ulint i; 00955 ibool srv_file_per_table_original_value = srv_file_per_table; 00956 mtr_t mtr; 00957 #ifdef HAVE_DARWIN_THREADS 00958 # ifdef F_FULLFSYNC 00959 /* This executable has been compiled on Mac OS X 10.3 or later. 00960 Assume that F_FULLFSYNC is available at run-time. */ 00961 srv_have_fullfsync = TRUE; 00962 # else /* F_FULLFSYNC */ 00963 /* This executable has been compiled on Mac OS X 10.2 00964 or earlier. Determine if the executable is running 00965 on Mac OS X 10.3 or later. */ 00966 struct utsname utsname; 00967 if (uname(&utsname)) { 00968 fputs("InnoDB: cannot determine Mac OS X version!\n", stderr); 00969 } else { 00970 srv_have_fullfsync = strcmp(utsname.release, "7.") >= 0; 00971 } 00972 if (!srv_have_fullfsync) { 00973 fputs( 00974 "InnoDB: On Mac OS X, fsync() may be broken on internal drives,\n" 00975 "InnoDB: making transactions unsafe!\n", stderr); 00976 } 00977 # endif /* F_FULLFSYNC */ 00978 #endif /* HAVE_DARWIN_THREADS */ 00979 00980 if (sizeof(ulint) != sizeof(void*)) { 00981 fprintf(stderr, 00982 "InnoDB: Error: size of InnoDB's ulint is %lu, but size of void* is %lu.\n" 00983 "InnoDB: The sizes should be the same so that on a 64-bit platform you can\n" 00984 "InnoDB: allocate more than 4 GB of memory.", 00985 (ulong)sizeof(ulint), (ulong)sizeof(void*)); 00986 } 00987 00988 srv_file_per_table = FALSE; /* system tables are created in tablespace 00989 0 */ 00990 #ifdef UNIV_DEBUG 00991 fprintf(stderr, 00992 "InnoDB: !!!!!!!!!!!!!! UNIV_DEBUG switched on !!!!!!!!!!!!!!!\n"); 00993 #endif 00994 00995 #ifdef UNIV_SYNC_DEBUG 00996 fprintf(stderr, 00997 "InnoDB: !!!!!!!!!!!!!! UNIV_SYNC_DEBUG switched on !!!!!!!!!!!!!!!\n"); 00998 #endif 00999 01000 #ifdef UNIV_SEARCH_DEBUG 01001 fprintf(stderr, 01002 "InnoDB: !!!!!!!!!!!!!! UNIV_SEARCH_DEBUG switched on !!!!!!!!!!!!!!!\n"); 01003 #endif 01004 01005 #ifdef UNIV_MEM_DEBUG 01006 fprintf(stderr, 01007 "InnoDB: !!!!!!!!!!!!!! UNIV_MEM_DEBUG switched on !!!!!!!!!!!!!!!\n"); 01008 #endif 01009 01010 #ifdef UNIV_SIMULATE_AWE 01011 fprintf(stderr, 01012 "InnoDB: !!!!!!!!!!!!!! UNIV_SIMULATE_AWE switched on !!!!!!!!!!!!!!!!!\n"); 01013 #endif 01014 if (srv_sizeof_trx_t_in_ha_innodb_cc != (ulint)sizeof(trx_t)) { 01015 fprintf(stderr, 01016 "InnoDB: Error: trx_t size is %lu in ha_innodb.cc but %lu in srv0start.c\n" 01017 "InnoDB: Check that pthread_mutex_t is defined in the same way in these\n" 01018 "InnoDB: compilation modules. Cannot continue.\n", 01019 (ulong) srv_sizeof_trx_t_in_ha_innodb_cc, 01020 (ulong) sizeof(trx_t)); 01021 return(DB_ERROR); 01022 } 01023 01024 /* Since InnoDB does not currently clean up all its internal data 01025 structures in MySQL Embedded Server Library server_end(), we 01026 print an error message if someone tries to start up InnoDB a 01027 second time during the process lifetime. */ 01028 01029 if (srv_start_has_been_called) { 01030 fprintf(stderr, 01031 "InnoDB: Error:startup called second time during the process lifetime.\n" 01032 "InnoDB: In the MySQL Embedded Server Library you cannot call server_init()\n" 01033 "InnoDB: more than once during the process lifetime.\n"); 01034 } 01035 01036 srv_start_has_been_called = TRUE; 01037 01038 #ifdef UNIV_DEBUG 01039 log_do_write = TRUE; 01040 #endif /* UNIV_DEBUG */ 01041 /* yydebug = TRUE; */ 01042 01043 srv_is_being_started = TRUE; 01044 srv_startup_is_before_trx_rollback_phase = TRUE; 01045 os_aio_use_native_aio = FALSE; 01046 01047 #if !defined(__WIN2000__) && !defined(UNIV_SIMULATE_AWE) 01048 if (srv_use_awe) { 01049 01050 fprintf(stderr, 01051 "InnoDB: Error: You have specified innodb_buffer_pool_awe_mem_mb\n" 01052 "InnoDB: in my.cnf, but AWE can only be used in Windows 2000 and later.\n" 01053 "InnoDB: To use AWE, InnoDB must be compiled with __WIN2000__ defined.\n"); 01054 01055 return(DB_ERROR); 01056 } 01057 #endif 01058 01059 #ifdef __WIN__ 01060 if (os_get_os_version() == OS_WIN95 01061 || os_get_os_version() == OS_WIN31 01062 || os_get_os_version() == OS_WINNT) { 01063 01064 /* On Win 95, 98, ME, Win32 subsystem for Windows 3.1, 01065 and NT use simulated aio. In NT Windows provides async i/o, 01066 but when run in conjunction with InnoDB Hot Backup, it seemed 01067 to corrupt the data files. */ 01068 01069 os_aio_use_native_aio = FALSE; 01070 } else { 01071 /* On Win 2000 and XP use async i/o */ 01072 os_aio_use_native_aio = TRUE; 01073 } 01074 #endif 01075 if (srv_file_flush_method_str == NULL) { 01076 /* These are the default options */ 01077 01078 srv_unix_file_flush_method = SRV_UNIX_FDATASYNC; 01079 01080 srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED; 01081 #ifndef __WIN__ 01082 } else if (0 == ut_strcmp(srv_file_flush_method_str, "fdatasync")) { 01083 srv_unix_file_flush_method = SRV_UNIX_FDATASYNC; 01084 01085 } else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DSYNC")) { 01086 srv_unix_file_flush_method = SRV_UNIX_O_DSYNC; 01087 01088 } else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DIRECT")) { 01089 srv_unix_file_flush_method = SRV_UNIX_O_DIRECT; 01090 01091 } else if (0 == ut_strcmp(srv_file_flush_method_str, "littlesync")) { 01092 srv_unix_file_flush_method = SRV_UNIX_LITTLESYNC; 01093 01094 } else if (0 == ut_strcmp(srv_file_flush_method_str, "nosync")) { 01095 srv_unix_file_flush_method = SRV_UNIX_NOSYNC; 01096 #else 01097 } else if (0 == ut_strcmp(srv_file_flush_method_str, "normal")) { 01098 srv_win_file_flush_method = SRV_WIN_IO_NORMAL; 01099 os_aio_use_native_aio = FALSE; 01100 01101 } else if (0 == ut_strcmp(srv_file_flush_method_str, "unbuffered")) { 01102 srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED; 01103 os_aio_use_native_aio = FALSE; 01104 01105 } else if (0 == ut_strcmp(srv_file_flush_method_str, 01106 "async_unbuffered")) { 01107 srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED; 01108 #endif 01109 } else { 01110 fprintf(stderr, 01111 "InnoDB: Unrecognized value %s for innodb_flush_method\n", 01112 srv_file_flush_method_str); 01113 return(DB_ERROR); 01114 } 01115 01116 /* Note that the call srv_boot() also changes the values of 01117 srv_pool_size etc. to the units used by InnoDB internally */ 01118 01119 /* Set the maximum number of threads which can wait for a semaphore 01120 inside InnoDB: this is the 'sync wait array' size, as well as the 01121 maximum number of threads that can wait in the 'srv_conc array' for 01122 their time to enter InnoDB. */ 01123 01124 #if defined(__WIN__) || defined(__NETWARE__) 01125 01126 /* Create less event semaphores because Win 98/ME had difficulty creating 01127 40000 event semaphores. 01128 Comment from Novell, Inc.: also, these just take a lot of memory on 01129 NetWare. */ 01130 srv_max_n_threads = 1000; 01131 #else 01132 if (srv_pool_size >= 1000 * 1024) { 01133 /* Here we still have srv_pool_size counted 01134 in kilobytes (in 4.0 this was in bytes) 01135 srv_boot() converts the value to 01136 pages; if buffer pool is less than 1000 MB, 01137 assume fewer threads. */ 01138 srv_max_n_threads = 50000; 01139 01140 } else if (srv_pool_size >= 8 * 1024) { 01141 01142 srv_max_n_threads = 10000; 01143 } else { 01144 srv_max_n_threads = 1000; /* saves several MB of memory, 01145 especially in 64-bit 01146 computers */ 01147 } 01148 #endif 01149 err = srv_boot(); /* This changes srv_pool_size to units of a page */ 01150 01151 if (err != DB_SUCCESS) { 01152 01153 return((int) err); 01154 } 01155 01156 mutex_create(&srv_monitor_file_mutex, SYNC_NO_ORDER_CHECK); 01157 01158 if (srv_innodb_status) { 01159 srv_monitor_file_name = mem_alloc( 01160 strlen(fil_path_to_mysql_datadir) + 01161 20 + sizeof "/innodb_status."); 01162 sprintf(srv_monitor_file_name, "%s/innodb_status.%lu", 01163 fil_path_to_mysql_datadir, os_proc_get_number()); 01164 srv_monitor_file = fopen(srv_monitor_file_name, "w+"); 01165 if (!srv_monitor_file) { 01166 fprintf(stderr, "InnoDB: unable to create %s: %s\n", 01167 srv_monitor_file_name, strerror(errno)); 01168 return(DB_ERROR); 01169 } 01170 } else { 01171 srv_monitor_file_name = NULL; 01172 srv_monitor_file = os_file_create_tmpfile(); 01173 if (!srv_monitor_file) { 01174 return(DB_ERROR); 01175 } 01176 } 01177 01178 mutex_create(&srv_dict_tmpfile_mutex, SYNC_DICT_OPERATION); 01179 01180 srv_dict_tmpfile = os_file_create_tmpfile(); 01181 if (!srv_dict_tmpfile) { 01182 return(DB_ERROR); 01183 } 01184 01185 mutex_create(&srv_misc_tmpfile_mutex, SYNC_ANY_LATCH); 01186 01187 srv_misc_tmpfile = os_file_create_tmpfile(); 01188 if (!srv_misc_tmpfile) { 01189 return(DB_ERROR); 01190 } 01191 01192 /* Restrict the maximum number of file i/o threads */ 01193 if (srv_n_file_io_threads > SRV_MAX_N_IO_THREADS) { 01194 01195 srv_n_file_io_threads = SRV_MAX_N_IO_THREADS; 01196 } 01197 01198 if (!os_aio_use_native_aio) { 01199 /* In simulated aio we currently have use only for 4 threads */ 01200 srv_n_file_io_threads = 4; 01201 01202 os_aio_init(8 * SRV_N_PENDING_IOS_PER_THREAD 01203 * srv_n_file_io_threads, 01204 srv_n_file_io_threads, 01205 SRV_MAX_N_PENDING_SYNC_IOS); 01206 } else { 01207 os_aio_init(SRV_N_PENDING_IOS_PER_THREAD 01208 * srv_n_file_io_threads, 01209 srv_n_file_io_threads, 01210 SRV_MAX_N_PENDING_SYNC_IOS); 01211 } 01212 01213 fil_init(srv_max_n_open_files); 01214 01215 if (srv_use_awe) { 01216 fprintf(stderr, 01217 "InnoDB: Using AWE: Memory window is %lu MB and AWE memory is %lu MB\n", 01218 (ulong) (srv_awe_window_size / ((1024 * 1024) / UNIV_PAGE_SIZE)), 01219 (ulong) (srv_pool_size / ((1024 * 1024) / UNIV_PAGE_SIZE))); 01220 01221 /* We must disable adaptive hash indexes because they do not 01222 tolerate remapping of pages in AWE */ 01223 01224 srv_use_adaptive_hash_indexes = FALSE; 01225 ret = buf_pool_init(srv_pool_size, srv_pool_size, 01226 srv_awe_window_size); 01227 } else { 01228 ret = buf_pool_init(srv_pool_size, srv_pool_size, 01229 srv_pool_size); 01230 } 01231 01232 if (ret == NULL) { 01233 fprintf(stderr, 01234 "InnoDB: Fatal error: cannot allocate the memory for the buffer pool\n"); 01235 01236 return(DB_ERROR); 01237 } 01238 01239 fsp_init(); 01240 log_init(); 01241 01242 lock_sys_create(srv_lock_table_size); 01243 01244 /* Create i/o-handler threads: */ 01245 01246 for (i = 0; i < srv_n_file_io_threads; i++) { 01247 n[i] = i; 01248 01249 os_thread_create(io_handler_thread, n + i, thread_ids + i); 01250 } 01251 01252 #ifdef UNIV_LOG_ARCHIVE 01253 if (0 != ut_strcmp(srv_log_group_home_dirs[0], srv_arch_dir)) { 01254 fprintf(stderr, 01255 "InnoDB: Error: you must set the log group home dir in my.cnf the\n" 01256 "InnoDB: same as log arch dir.\n"); 01257 01258 return(DB_ERROR); 01259 } 01260 #endif /* UNIV_LOG_ARCHIVE */ 01261 01262 if (srv_n_log_files * srv_log_file_size >= 262144) { 01263 fprintf(stderr, 01264 "InnoDB: Error: combined size of log files must be < 4 GB\n"); 01265 01266 return(DB_ERROR); 01267 } 01268 01269 sum_of_new_sizes = 0; 01270 01271 for (i = 0; i < srv_n_data_files; i++) { 01272 #ifndef __WIN__ 01273 if (sizeof(off_t) < 5 && srv_data_file_sizes[i] >= 262144) { 01274 fprintf(stderr, 01275 "InnoDB: Error: file size must be < 4 GB with this MySQL binary\n" 01276 "InnoDB: and operating system combination, in some OS's < 2 GB\n"); 01277 01278 return(DB_ERROR); 01279 } 01280 #endif 01281 sum_of_new_sizes += srv_data_file_sizes[i]; 01282 } 01283 01284 if (sum_of_new_sizes < 640) { 01285 fprintf(stderr, 01286 "InnoDB: Error: tablespace size must be at least 10 MB\n"); 01287 01288 return(DB_ERROR); 01289 } 01290 01291 err = open_or_create_data_files(&create_new_db, 01292 #ifdef UNIV_LOG_ARCHIVE 01293 &min_arch_log_no, &max_arch_log_no, 01294 #endif /* UNIV_LOG_ARCHIVE */ 01295 &min_flushed_lsn, &max_flushed_lsn, 01296 &sum_of_new_sizes); 01297 if (err != DB_SUCCESS) { 01298 fprintf(stderr, 01299 "InnoDB: Could not open or create data files.\n" 01300 "InnoDB: If you tried to add new data files, and it failed here,\n" 01301 "InnoDB: you should now edit innodb_data_file_path in my.cnf back\n" 01302 "InnoDB: to what it was, and remove the new ibdata files InnoDB created\n" 01303 "InnoDB: in this failed attempt. InnoDB only wrote those files full of\n" 01304 "InnoDB: zeros, but did not yet use them in any way. But be careful: do not\n" 01305 "InnoDB: remove old data files which contain your precious data!\n"); 01306 01307 return((int) err); 01308 } 01309 01310 #ifdef UNIV_LOG_ARCHIVE 01311 srv_normalize_path_for_win(srv_arch_dir); 01312 srv_arch_dir = srv_add_path_separator_if_needed(srv_arch_dir); 01313 #endif /* UNIV_LOG_ARCHIVE */ 01314 01315 for (i = 0; i < srv_n_log_files; i++) { 01316 err = open_or_create_log_file(create_new_db, &log_file_created, 01317 log_opened, 0, i); 01318 if (err != DB_SUCCESS) { 01319 01320 return((int) err); 01321 } 01322 01323 if (log_file_created) { 01324 log_created = TRUE; 01325 } else { 01326 log_opened = TRUE; 01327 } 01328 if ((log_opened && create_new_db) 01329 || (log_opened && log_created)) { 01330 fprintf(stderr, 01331 "InnoDB: Error: all log files must be created at the same time.\n" 01332 "InnoDB: All log files must be created also in database creation.\n" 01333 "InnoDB: If you want bigger or smaller log files, shut down the\n" 01334 "InnoDB: database and make sure there were no errors in shutdown.\n" 01335 "InnoDB: Then delete the existing log files. Edit the .cnf file\n" 01336 "InnoDB: and start the database again.\n"); 01337 01338 return(DB_ERROR); 01339 } 01340 } 01341 01342 /* Open all log files and data files in the system tablespace: we 01343 keep them open until database shutdown */ 01344 01345 fil_open_log_and_system_tablespace_files(); 01346 01347 if (log_created && !create_new_db 01348 #ifdef UNIV_LOG_ARCHIVE 01349 && !srv_archive_recovery 01350 #endif /* UNIV_LOG_ARCHIVE */ 01351 ) { 01352 if (ut_dulint_cmp(max_flushed_lsn, min_flushed_lsn) != 0 01353 #ifdef UNIV_LOG_ARCHIVE 01354 || max_arch_log_no != min_arch_log_no 01355 #endif /* UNIV_LOG_ARCHIVE */ 01356 ) { 01357 fprintf(stderr, 01358 "InnoDB: Cannot initialize created log files because\n" 01359 "InnoDB: data files were not in sync with each other\n" 01360 "InnoDB: or the data files are corrupt.\n"); 01361 01362 return(DB_ERROR); 01363 } 01364 01365 if (ut_dulint_cmp(max_flushed_lsn, ut_dulint_create(0, 1000)) 01366 < 0) { 01367 fprintf(stderr, 01368 "InnoDB: Cannot initialize created log files because\n" 01369 "InnoDB: data files are corrupt, or new data files were\n" 01370 "InnoDB: created when the database was started previous\n" 01371 "InnoDB: time but the database was not shut down\n" 01372 "InnoDB: normally after that.\n"); 01373 01374 return(DB_ERROR); 01375 } 01376 01377 mutex_enter(&(log_sys->mutex)); 01378 01379 #ifdef UNIV_LOG_ARCHIVE 01380 /* Do not + 1 arch_log_no because we do not use log 01381 archiving */ 01382 recv_reset_logs(max_flushed_lsn, max_arch_log_no, TRUE); 01383 #else 01384 recv_reset_logs(max_flushed_lsn, TRUE); 01385 #endif /* UNIV_LOG_ARCHIVE */ 01386 01387 mutex_exit(&(log_sys->mutex)); 01388 } 01389 01390 if (create_new_db) { 01391 mtr_start(&mtr); 01392 01393 fsp_header_init(0, sum_of_new_sizes, &mtr); 01394 01395 mtr_commit(&mtr); 01396 01397 trx_sys_create(); 01398 dict_create(); 01399 srv_startup_is_before_trx_rollback_phase = FALSE; 01400 01401 #ifdef UNIV_LOG_ARCHIVE 01402 } else if (srv_archive_recovery) { 01403 fprintf(stderr, 01404 "InnoDB: Starting archive recovery from a backup...\n"); 01405 err = recv_recovery_from_archive_start( 01406 min_flushed_lsn, 01407 srv_archive_recovery_limit_lsn, 01408 min_arch_log_no); 01409 if (err != DB_SUCCESS) { 01410 01411 return(DB_ERROR); 01412 } 01413 /* Since ibuf init is in dict_boot, and ibuf is needed 01414 in any disk i/o, first call dict_boot */ 01415 01416 dict_boot(); 01417 trx_sys_init_at_db_start(); 01418 srv_startup_is_before_trx_rollback_phase = FALSE; 01419 01420 /* Initialize the fsp free limit global variable in the log 01421 system */ 01422 fsp_header_get_free_limit(0); 01423 01424 recv_recovery_from_archive_finish(); 01425 #endif /* UNIV_LOG_ARCHIVE */ 01426 } else { 01427 /* We always try to do a recovery, even if the database had 01428 been shut down normally: this is the normal startup path */ 01429 01430 err = recv_recovery_from_checkpoint_start(LOG_CHECKPOINT, 01431 ut_dulint_max, 01432 min_flushed_lsn, 01433 max_flushed_lsn); 01434 if (err != DB_SUCCESS) { 01435 01436 return(DB_ERROR); 01437 } 01438 01439 /* Since the insert buffer init is in dict_boot, and the 01440 insert buffer is needed in any disk i/o, first we call 01441 dict_boot(). Note that trx_sys_init_at_db_start() only needs 01442 to access space 0, and the insert buffer at this stage already 01443 works for space 0. */ 01444 01445 dict_boot(); 01446 trx_sys_init_at_db_start(); 01447 01448 if (srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE) { 01449 /* The following call is necessary for the insert 01450 buffer to work with multiple tablespaces. We must 01451 know the mapping between space id's and .ibd file 01452 names. 01453 01454 In a crash recovery, we check that the info in data 01455 dictionary is consistent with what we already know 01456 about space id's from the call of 01457 fil_load_single_table_tablespaces(). 01458 01459 In a normal startup, we create the space objects for 01460 every table in the InnoDB data dictionary that has 01461 an .ibd file. 01462 01463 We also determine the maximum tablespace id used. 01464 01465 TODO: We may have incomplete transactions in the 01466 data dictionary tables. Does that harm the scanning of 01467 the data dictionary below? */ 01468 01469 dict_check_tablespaces_and_store_max_id( 01470 recv_needed_recovery); 01471 } 01472 01473 srv_startup_is_before_trx_rollback_phase = FALSE; 01474 01475 /* Initialize the fsp free limit global variable in the log 01476 system */ 01477 fsp_header_get_free_limit(0); 01478 01479 /* recv_recovery_from_checkpoint_finish needs trx lists which 01480 are initialized in trx_sys_init_at_db_start(). */ 01481 01482 recv_recovery_from_checkpoint_finish(); 01483 } 01484 01485 if (!create_new_db && sum_of_new_sizes > 0) { 01486 /* New data file(s) were added */ 01487 mtr_start(&mtr); 01488 01489 fsp_header_inc_size(0, sum_of_new_sizes, &mtr); 01490 01491 mtr_commit(&mtr); 01492 01493 /* Immediately write the log record about increased tablespace 01494 size to disk, so that it is durable even if mysqld would crash 01495 quickly */ 01496 01497 log_buffer_flush_to_disk(); 01498 } 01499 01500 #ifdef UNIV_LOG_ARCHIVE 01501 /* Archiving is always off under MySQL */ 01502 if (!srv_log_archive_on) { 01503 ut_a(DB_SUCCESS == log_archive_noarchivelog()); 01504 } else { 01505 mutex_enter(&(log_sys->mutex)); 01506 01507 start_archive = FALSE; 01508 01509 if (log_sys->archiving_state == LOG_ARCH_OFF) { 01510 start_archive = TRUE; 01511 } 01512 01513 mutex_exit(&(log_sys->mutex)); 01514 01515 if (start_archive) { 01516 ut_a(DB_SUCCESS == log_archive_archivelog()); 01517 } 01518 } 01519 #endif /* UNIV_LOG_ARCHIVE */ 01520 01521 /* fprintf(stderr, "Max allowed record size %lu\n", 01522 page_get_free_space_of_empty() / 2); */ 01523 01524 /* Create the thread which watches the timeouts for lock waits 01525 and prints InnoDB monitor info */ 01526 01527 os_thread_create(&srv_lock_timeout_and_monitor_thread, NULL, 01528 thread_ids + 2 + SRV_MAX_N_IO_THREADS); 01529 01530 /* Create the thread which warns of long semaphore waits */ 01531 os_thread_create(&srv_error_monitor_thread, NULL, 01532 thread_ids + 3 + SRV_MAX_N_IO_THREADS); 01533 srv_was_started = TRUE; 01534 srv_is_being_started = FALSE; 01535 01536 #ifdef UNIV_DEBUG 01537 /* Wait a while so that the created threads have time to suspend 01538 themselves before we switch sync debugging on; otherwise a thread may 01539 execute mutex_enter() before the checks are on, and mutex_exit() after 01540 the checks are on, which will cause an assertion failure in sync 01541 debug. */ 01542 01543 os_thread_sleep(3000000); 01544 #endif 01545 sync_order_checks_on = TRUE; 01546 01547 if (trx_doublewrite == NULL) { 01548 /* Create the doublewrite buffer to a new tablespace */ 01549 01550 trx_sys_create_doublewrite_buf(); 01551 } 01552 01553 err = dict_create_or_check_foreign_constraint_tables(); 01554 01555 if (err != DB_SUCCESS) { 01556 return((int)DB_ERROR); 01557 } 01558 01559 /* Create the master thread which does purge and other utility 01560 operations */ 01561 01562 os_thread_create(&srv_master_thread, NULL, thread_ids + 1 + 01563 SRV_MAX_N_IO_THREADS); 01564 #ifdef UNIV_DEBUG 01565 /* buf_debug_prints = TRUE; */ 01566 #endif /* UNIV_DEBUG */ 01567 sum_of_data_file_sizes = 0; 01568 01569 for (i = 0; i < srv_n_data_files; i++) { 01570 sum_of_data_file_sizes += srv_data_file_sizes[i]; 01571 } 01572 01573 tablespace_size_in_header = fsp_header_get_tablespace_size(0); 01574 01575 if (!srv_auto_extend_last_data_file 01576 && sum_of_data_file_sizes != tablespace_size_in_header) { 01577 01578 fprintf(stderr, 01579 "InnoDB: Error: tablespace size stored in header is %lu pages, but\n" 01580 "InnoDB: the sum of data file sizes is %lu pages\n", 01581 (ulong) tablespace_size_in_header, 01582 (ulong) sum_of_data_file_sizes); 01583 01584 if (srv_force_recovery == 0 01585 && sum_of_data_file_sizes < tablespace_size_in_header) { 01586 /* This is a fatal error, the tail of a tablespace is 01587 missing */ 01588 01589 fprintf(stderr, 01590 "InnoDB: Cannot start InnoDB. The tail of the system tablespace is\n" 01591 "InnoDB: missing. Have you edited innodb_data_file_path in my.cnf in an\n" 01592 "InnoDB: inappropriate way, removing ibdata files from there?\n" 01593 "InnoDB: You can set innodb_force_recovery=1 in my.cnf to force\n" 01594 "InnoDB: a startup if you are trying to recover a badly corrupt database.\n"); 01595 01596 return(DB_ERROR); 01597 } 01598 } 01599 01600 if (srv_auto_extend_last_data_file 01601 && sum_of_data_file_sizes < tablespace_size_in_header) { 01602 01603 fprintf(stderr, 01604 "InnoDB: Error: tablespace size stored in header is %lu pages, but\n" 01605 "InnoDB: the sum of data file sizes is only %lu pages\n", 01606 (ulong) tablespace_size_in_header, 01607 (ulong) sum_of_data_file_sizes); 01608 01609 if (srv_force_recovery == 0) { 01610 01611 fprintf(stderr, 01612 "InnoDB: Cannot start InnoDB. The tail of the system tablespace is\n" 01613 "InnoDB: missing. Have you edited innodb_data_file_path in my.cnf in an\n" 01614 "InnoDB: inappropriate way, removing ibdata files from there?\n" 01615 "InnoDB: You can set innodb_force_recovery=1 in my.cnf to force\n" 01616 "InnoDB: a startup if you are trying to recover a badly corrupt database.\n"); 01617 01618 return(DB_ERROR); 01619 } 01620 } 01621 01622 /* Check that os_fast_mutexes work as expected */ 01623 os_fast_mutex_init(&srv_os_test_mutex); 01624 01625 if (0 != os_fast_mutex_trylock(&srv_os_test_mutex)) { 01626 fprintf(stderr, 01627 "InnoDB: Error: pthread_mutex_trylock returns an unexpected value on\n" 01628 "InnoDB: success! Cannot continue.\n"); 01629 exit(1); 01630 } 01631 01632 os_fast_mutex_unlock(&srv_os_test_mutex); 01633 01634 os_fast_mutex_lock(&srv_os_test_mutex); 01635 01636 os_fast_mutex_unlock(&srv_os_test_mutex); 01637 01638 os_fast_mutex_free(&srv_os_test_mutex); 01639 01640 if (srv_print_verbose_log) { 01641 ut_print_timestamp(stderr); 01642 fprintf(stderr, 01643 " InnoDB: Started; log sequence number %lu %lu\n", 01644 (ulong) ut_dulint_get_high(srv_start_lsn), 01645 (ulong) ut_dulint_get_low(srv_start_lsn)); 01646 } 01647 01648 if (srv_force_recovery > 0) { 01649 fprintf(stderr, 01650 "InnoDB: !!! innodb_force_recovery is set to %lu !!!\n", 01651 (ulong) srv_force_recovery); 01652 } 01653 01654 fflush(stderr); 01655 01656 if (trx_doublewrite_must_reset_space_ids) { 01657 /* Actually, we did not change the undo log format between 01658 4.0 and 4.1.1, and we would not need to run purge to 01659 completion. Note also that the purge algorithm in 4.1.1 01660 can process the the history list again even after a full 01661 purge, because our algorithm does not cut the end of the 01662 history list in all cases so that it would become empty 01663 after a full purge. That mean that we may purge 4.0 type 01664 undo log even after this phase. 01665 01666 The insert buffer record format changed between 4.0 and 01667 4.1.1. It is essential that the insert buffer is emptied 01668 here! */ 01669 01670 fprintf(stderr, 01671 "InnoDB: You are upgrading to an InnoDB version which allows multiple\n" 01672 "InnoDB: tablespaces. Wait that purge and insert buffer merge run to\n" 01673 "InnoDB: completion...\n"); 01674 for (;;) { 01675 os_thread_sleep(1000000); 01676 01677 if (0 == strcmp(srv_main_thread_op_info, 01678 "waiting for server activity")) { 01679 01680 ut_a(ibuf_is_empty()); 01681 01682 break; 01683 } 01684 } 01685 fprintf(stderr, 01686 "InnoDB: Full purge and insert buffer merge completed.\n"); 01687 01688 trx_sys_mark_upgraded_to_multiple_tablespaces(); 01689 01690 fprintf(stderr, 01691 "InnoDB: You have now successfully upgraded to the multiple tablespaces\n" 01692 "InnoDB: format. You should NOT DOWNGRADE to an earlier version of\n" 01693 "InnoDB: InnoDB! But if you absolutely need to downgrade, see\n" 01694 "InnoDB: http://dev.mysql.com/doc/mysql/en/Multiple_tablespaces.html\n" 01695 "InnoDB: for instructions.\n"); 01696 } 01697 01698 if (srv_force_recovery == 0) { 01699 /* In the insert buffer we may have even bigger tablespace 01700 id's, because we may have dropped those tablespaces, but 01701 insert buffer merge has not had time to clean the records from 01702 the ibuf tree. */ 01703 01704 ibuf_update_max_tablespace_id(); 01705 } 01706 01707 srv_file_per_table = srv_file_per_table_original_value; 01708 01709 return((int) DB_SUCCESS); 01710 } 01711 01712 /******************************************************************** 01713 Shuts down the InnoDB database. */ 01714 01715 int 01716 innobase_shutdown_for_mysql(void) 01717 /*=============================*/ 01718 /* out: DB_SUCCESS or error code */ 01719 { 01720 ulint i; 01721 #ifdef __NETWARE__ 01722 extern ibool panic_shutdown; 01723 #endif 01724 if (!srv_was_started) { 01725 if (srv_is_being_started) { 01726 ut_print_timestamp(stderr); 01727 fprintf(stderr, 01728 " InnoDB: Warning: shutting down a not properly started\n" 01729 " InnoDB: or created database!\n"); 01730 } 01731 01732 return(DB_SUCCESS); 01733 } 01734 01735 /* 1. Flush the buffer pool to disk, write the current lsn to 01736 the tablespace header(s), and copy all log data to archive. 01737 The step 1 is the real InnoDB shutdown. The remaining steps 2 - ... 01738 just free data structures after the shutdown. */ 01739 01740 01741 if (srv_fast_shutdown == 2) { 01742 ut_print_timestamp(stderr); 01743 fprintf(stderr, 01744 " InnoDB: MySQL has requested a very fast shutdown without flushing " 01745 "the InnoDB buffer pool to data files. At the next mysqld startup " 01746 "InnoDB will do a crash recovery!\n"); 01747 } 01748 01749 #ifdef __NETWARE__ 01750 if(!panic_shutdown) 01751 #endif 01752 logs_empty_and_mark_files_at_shutdown(); 01753 01754 if (srv_conc_n_threads != 0) { 01755 fprintf(stderr, 01756 "InnoDB: Warning: query counter shows %ld queries still\n" 01757 "InnoDB: inside InnoDB at shutdown\n", 01758 srv_conc_n_threads); 01759 } 01760 01761 /* 2. Make all threads created by InnoDB to exit */ 01762 01763 srv_shutdown_state = SRV_SHUTDOWN_EXIT_THREADS; 01764 01765 /* In a 'very fast' shutdown, we do not need to wait for these threads 01766 to die; all which counts is that we flushed the log; a 'very fast' 01767 shutdown is essentially a crash. */ 01768 01769 if (srv_fast_shutdown == 2) { 01770 return(DB_SUCCESS); 01771 } 01772 01773 /* All threads end up waiting for certain events. Put those events 01774 to the signaled state. Then the threads will exit themselves in 01775 os_thread_event_wait(). */ 01776 01777 for (i = 0; i < 1000; i++) { 01778 /* NOTE: IF YOU CREATE THREADS IN INNODB, YOU MUST EXIT THEM 01779 HERE OR EARLIER */ 01780 01781 /* a. Let the lock timeout thread exit */ 01782 os_event_set(srv_lock_timeout_thread_event); 01783 01784 /* b. srv error monitor thread exits automatically, no need 01785 to do anything here */ 01786 01787 /* c. We wake the master thread so that it exits */ 01788 srv_wake_master_thread(); 01789 01790 /* d. Exit the i/o threads */ 01791 01792 os_aio_wake_all_threads_at_shutdown(); 01793 01794 os_mutex_enter(os_sync_mutex); 01795 01796 if (os_thread_count == 0) { 01797 /* All the threads have exited or are just exiting; 01798 NOTE that the threads may not have completed their 01799 exit yet. Should we use pthread_join() to make sure 01800 they have exited? Now we just sleep 0.1 seconds and 01801 hope that is enough! */ 01802 01803 os_mutex_exit(os_sync_mutex); 01804 01805 os_thread_sleep(100000); 01806 01807 break; 01808 } 01809 01810 os_mutex_exit(os_sync_mutex); 01811 01812 os_thread_sleep(100000); 01813 } 01814 01815 if (i == 1000) { 01816 fprintf(stderr, 01817 "InnoDB: Warning: %lu threads created by InnoDB had not exited at shutdown!\n", 01818 (ulong) os_thread_count); 01819 } 01820 01821 if (srv_monitor_file) { 01822 fclose(srv_monitor_file); 01823 srv_monitor_file = 0; 01824 if (srv_monitor_file_name) { 01825 unlink(srv_monitor_file_name); 01826 mem_free(srv_monitor_file_name); 01827 } 01828 } 01829 if (srv_dict_tmpfile) { 01830 fclose(srv_dict_tmpfile); 01831 srv_dict_tmpfile = 0; 01832 } 01833 01834 if (srv_misc_tmpfile) { 01835 fclose(srv_misc_tmpfile); 01836 srv_misc_tmpfile = 0; 01837 } 01838 01839 mutex_free(&srv_monitor_file_mutex); 01840 mutex_free(&srv_dict_tmpfile_mutex); 01841 mutex_free(&srv_misc_tmpfile_mutex); 01842 01843 /* 3. Free all InnoDB's own mutexes and the os_fast_mutexes inside 01844 them */ 01845 sync_close(); 01846 01847 /* 4. Free the os_conc_mutex and all os_events and os_mutexes */ 01848 01849 srv_free(); 01850 os_sync_free(); 01851 01852 /* Check that all read views are closed except read view owned 01853 by a purge. */ 01854 01855 if (UT_LIST_GET_LEN(trx_sys->view_list) > 1) { 01856 fprintf(stderr, 01857 "InnoDB: Error: all read views were not closed before shutdown:\n" 01858 "InnoDB: %lu read views open \n", 01859 UT_LIST_GET_LEN(trx_sys->view_list) - 1); 01860 } 01861 01862 /* 5. Free all allocated memory and the os_fast_mutex created in 01863 ut0mem.c */ 01864 01865 ut_free_all_mem(); 01866 01867 if (os_thread_count != 0 01868 || os_event_count != 0 01869 || os_mutex_count != 0 01870 || os_fast_mutex_count != 0) { 01871 fprintf(stderr, 01872 "InnoDB: Warning: some resources were not cleaned up in shutdown:\n" 01873 "InnoDB: threads %lu, events %lu, os_mutexes %lu, os_fast_mutexes %lu\n", 01874 (ulong) os_thread_count, (ulong) os_event_count, 01875 (ulong) os_mutex_count, (ulong) os_fast_mutex_count); 01876 } 01877 01878 if (dict_foreign_err_file) { 01879 fclose(dict_foreign_err_file); 01880 } 01881 if (lock_latest_err_file) { 01882 fclose(lock_latest_err_file); 01883 } 01884 01885 if (srv_print_verbose_log) { 01886 ut_print_timestamp(stderr); 01887 fprintf(stderr, 01888 " InnoDB: Shutdown completed; log sequence number %lu %lu\n", 01889 (ulong) ut_dulint_get_high(srv_shutdown_lsn), 01890 (ulong) ut_dulint_get_low(srv_shutdown_lsn)); 01891 } 01892 01893 return((int) DB_SUCCESS); 01894 } 01895 01896 #ifdef __NETWARE__ 01897 void set_panic_flag_for_netware() 01898 { 01899 extern ibool panic_shutdown; 01900 panic_shutdown = TRUE; 01901 } 01902 #endif /* __NETWARE__ */ 01903 #endif /* !UNIV_HOTBACKUP */
1.4.7

