MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
srv0start.cc
Go to the documentation of this file.
1 /*****************************************************************************
2 
3 Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
4 Copyright (c) 2008, Google Inc.
5 Copyright (c) 2009, Percona Inc.
6 
7 Portions of this file contain modifications contributed and copyrighted by
8 Google, Inc. Those modifications are gratefully acknowledged and are described
9 briefly in the InnoDB documentation. The contributions by Google are
10 incorporated with their permission, and subject to the conditions contained in
11 the file COPYING.Google.
12 
13 Portions of this file contain modifications contributed and copyrighted
14 by Percona Inc.. Those modifications are
15 gratefully acknowledged and are described briefly in the InnoDB
16 documentation. The contributions by Percona Inc. are incorporated with
17 their permission, and subject to the conditions contained in the file
18 COPYING.Percona.
19 
20 This program is free software; you can redistribute it and/or modify it under
21 the terms of the GNU General Public License as published by the Free Software
22 Foundation; version 2 of the License.
23 
24 This program is distributed in the hope that it will be useful, but WITHOUT
25 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
26 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
27 
28 You should have received a copy of the GNU General Public License along with
29 this program; if not, write to the Free Software Foundation, Inc.,
30 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
31 
32 *****************************************************************************/
33 
34 /********************************************************************/
41 #include "ut0mem.h"
42 #include "mem0mem.h"
43 #include "data0data.h"
44 #include "data0type.h"
45 #include "dict0dict.h"
46 #include "buf0buf.h"
47 #include "buf0dump.h"
48 #include "os0file.h"
49 #include "os0thread.h"
50 #include "fil0fil.h"
51 #include "fsp0fsp.h"
52 #include "rem0rec.h"
53 #include "mtr0mtr.h"
54 #include "log0log.h"
55 #include "log0recv.h"
56 #include "page0page.h"
57 #include "page0cur.h"
58 #include "trx0trx.h"
59 #include "trx0sys.h"
60 #include "btr0btr.h"
61 #include "btr0cur.h"
62 #include "rem0rec.h"
63 #include "ibuf0ibuf.h"
64 #include "srv0start.h"
65 #include "srv0srv.h"
66 #ifndef UNIV_HOTBACKUP
67 # include "trx0rseg.h"
68 # include "os0proc.h"
69 # include "sync0sync.h"
70 # include "buf0flu.h"
71 # include "buf0rea.h"
72 # include "dict0boot.h"
73 # include "dict0load.h"
74 # include "dict0stats_bg.h"
75 # include "que0que.h"
76 # include "usr0sess.h"
77 # include "lock0lock.h"
78 # include "trx0roll.h"
79 # include "trx0purge.h"
80 # include "lock0lock.h"
81 # include "pars0pars.h"
82 # include "btr0sea.h"
83 # include "rem0cmp.h"
84 # include "dict0crea.h"
85 # include "row0ins.h"
86 # include "row0sel.h"
87 # include "row0upd.h"
88 # include "row0row.h"
89 # include "row0mysql.h"
90 # include "btr0pcur.h"
91 # include "os0sync.h"
92 # include "zlib.h"
93 # include "ut0crc32.h"
94 
96 UNIV_INTERN lsn_t srv_start_lsn;
98 UNIV_INTERN lsn_t srv_shutdown_lsn;
99 
100 #ifdef HAVE_DARWIN_THREADS
101 # include <sys/utsname.h>
103 UNIV_INTERN ibool srv_have_fullfsync = FALSE;
104 #endif
105 
107 UNIV_INTERN ibool srv_start_raw_disk_in_use = FALSE;
108 
113 UNIV_INTERN ibool srv_is_being_started = FALSE;
115 UNIV_INTERN ibool srv_was_started = FALSE;
117 static ibool srv_start_has_been_called = FALSE;
118 
122 
124 static os_file_t files[1000];
125 
127 static ulint n[SRV_MAX_N_IO_THREADS + 6];
129 static os_thread_id_t thread_ids[SRV_MAX_N_IO_THREADS + 6 + 32];
130 
133 static os_fast_mutex_t srv_os_test_mutex;
134 
136 static char* srv_monitor_file_name;
137 #endif /* !UNIV_HOTBACKUP */
138 
140 static const ulint SRV_UNDO_TABLESPACE_SIZE_IN_PAGES =
141  ((1024 * 1024) * 10) / UNIV_PAGE_SIZE_DEF;
142 
144 #define SRV_N_PENDING_IOS_PER_THREAD OS_AIO_N_PENDING_IOS_PER_THREAD
145 #define SRV_MAX_N_PENDING_SYNC_IOS 100
146 
147 #ifdef UNIV_PFS_THREAD
148 /* Keys to register InnoDB threads with performance schema */
149 UNIV_INTERN mysql_pfs_key_t io_handler_thread_key;
150 UNIV_INTERN mysql_pfs_key_t srv_lock_timeout_thread_key;
151 UNIV_INTERN mysql_pfs_key_t srv_error_monitor_thread_key;
152 UNIV_INTERN mysql_pfs_key_t srv_monitor_thread_key;
153 UNIV_INTERN mysql_pfs_key_t srv_master_thread_key;
154 UNIV_INTERN mysql_pfs_key_t srv_purge_thread_key;
155 #endif /* UNIV_PFS_THREAD */
156 
157 /*********************************************************************/
161 static
162 char*
163 srv_parse_megabytes(
164 /*================*/
165  char* str,
166  ulint* megs)
167 {
168  char* endp;
169  ulint size;
170 
171  size = strtoul(str, &endp, 10);
172 
173  str = endp;
174 
175  switch (*str) {
176  case 'G': case 'g':
177  size *= 1024;
178  /* fall through */
179  case 'M': case 'm':
180  str++;
181  break;
182  default:
183  size /= 1024 * 1024;
184  break;
185  }
186 
187  *megs = size;
188  return(str);
189 }
190 
191 /*********************************************************************/
194 static
195 bool
196 srv_file_check_mode(
197 /*================*/
198  const char* name)
199 {
200  os_file_stat_t stat;
201 
202  memset(&stat, 0x0, sizeof(stat));
203 
204  dberr_t err = os_file_get_status(name, &stat, true);
205 
206  if (err == DB_FAIL) {
207 
208  ib_logf(IB_LOG_LEVEL_ERROR,
209  "os_file_get_status() failed on '%s'. Can't determine "
210  "file permissions", name);
211 
212  return(false);
213 
214  } else if (err == DB_SUCCESS) {
215 
216  /* Note: stat.rw_perm is only valid of files */
217 
218  if (stat.type == OS_FILE_TYPE_FILE) {
219  if (!stat.rw_perm) {
220 
221  ib_logf(IB_LOG_LEVEL_ERROR,
222  "%s can't be opened in %s mode",
223  name,
225  ? "read" : "read-write");
226 
227  return(false);
228  }
229  } else {
230  /* Not a regular file, bail out. */
231 
232  ib_logf(IB_LOG_LEVEL_ERROR,
233  "'%s' not a regular file.", name);
234 
235  return(false);
236  }
237  } else {
238 
239  /* This is OK. If the file create fails on RO media, there
240  is nothing we can do. */
241 
242  ut_a(err == DB_NOT_FOUND);
243  }
244 
245  return(true);
246 }
247 
248 /*********************************************************************/
252 UNIV_INTERN
253 ibool
255 /*================================*/
256  char* str)
257 {
258  char* input_str;
259  char* path;
260  ulint size;
261  ulint i = 0;
262 
263  srv_auto_extend_last_data_file = FALSE;
264  srv_last_file_size_max = 0;
265  srv_data_file_names = NULL;
266  srv_data_file_sizes = NULL;
267  srv_data_file_is_raw_partition = NULL;
268 
269  input_str = str;
270 
271  /* First calculate the number of data files and check syntax:
272  path:size[M | G];path:size[M | G]... . Note that a Windows path may
273  contain a drive name and a ':'. */
274 
275  while (*str != '\0') {
276  path = str;
277 
278  while ((*str != ':' && *str != '\0')
279  || (*str == ':'
280  && (*(str + 1) == '\\' || *(str + 1) == '/'
281  || *(str + 1) == ':'))) {
282  str++;
283  }
284 
285  if (*str == '\0') {
286  return(FALSE);
287  }
288 
289  str++;
290 
291  str = srv_parse_megabytes(str, &size);
292 
293  if (0 == strncmp(str, ":autoextend",
294  (sizeof ":autoextend") - 1)) {
295 
296  str += (sizeof ":autoextend") - 1;
297 
298  if (0 == strncmp(str, ":max:",
299  (sizeof ":max:") - 1)) {
300 
301  str += (sizeof ":max:") - 1;
302 
303  str = srv_parse_megabytes(str, &size);
304  }
305 
306  if (*str != '\0') {
307 
308  return(FALSE);
309  }
310  }
311 
312  if (strlen(str) >= 6
313  && *str == 'n'
314  && *(str + 1) == 'e'
315  && *(str + 2) == 'w') {
316  str += 3;
317  }
318 
319  if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
320  str += 3;
321  }
322 
323  if (size == 0) {
324  return(FALSE);
325  }
326 
327  i++;
328 
329  if (*str == ';') {
330  str++;
331  } else if (*str != '\0') {
332 
333  return(FALSE);
334  }
335  }
336 
337  if (i == 0) {
338  /* If innodb_data_file_path was defined it must contain
339  at least one data file definition */
340 
341  return(FALSE);
342  }
343 
344  srv_data_file_names = static_cast<char**>(
345  malloc(i * sizeof *srv_data_file_names));
346 
347  srv_data_file_sizes = static_cast<ulint*>(
348  malloc(i * sizeof *srv_data_file_sizes));
349 
350  srv_data_file_is_raw_partition = static_cast<ulint*>(
351  malloc(i * sizeof *srv_data_file_is_raw_partition));
352 
353  srv_n_data_files = i;
354 
355  /* Then store the actual values to our arrays */
356 
357  str = input_str;
358  i = 0;
359 
360  while (*str != '\0') {
361  path = str;
362 
363  /* Note that we must step over the ':' in a Windows path;
364  a Windows path normally looks like C:\ibdata\ibdata1:1G, but
365  a Windows raw partition may have a specification like
366  \\.\C::1Gnewraw or \\.\PHYSICALDRIVE2:1Gnewraw */
367 
368  while ((*str != ':' && *str != '\0')
369  || (*str == ':'
370  && (*(str + 1) == '\\' || *(str + 1) == '/'
371  || *(str + 1) == ':'))) {
372  str++;
373  }
374 
375  if (*str == ':') {
376  /* Make path a null-terminated string */
377  *str = '\0';
378  str++;
379  }
380 
381  str = srv_parse_megabytes(str, &size);
382 
383  srv_data_file_names[i] = path;
384  srv_data_file_sizes[i] = size;
385 
386  if (0 == strncmp(str, ":autoextend",
387  (sizeof ":autoextend") - 1)) {
388 
389  srv_auto_extend_last_data_file = TRUE;
390 
391  str += (sizeof ":autoextend") - 1;
392 
393  if (0 == strncmp(str, ":max:",
394  (sizeof ":max:") - 1)) {
395 
396  str += (sizeof ":max:") - 1;
397 
398  str = srv_parse_megabytes(
399  str, &srv_last_file_size_max);
400  }
401 
402  if (*str != '\0') {
403 
404  return(FALSE);
405  }
406  }
407 
408  (srv_data_file_is_raw_partition)[i] = 0;
409 
410  if (strlen(str) >= 6
411  && *str == 'n'
412  && *(str + 1) == 'e'
413  && *(str + 2) == 'w') {
414  str += 3;
415  (srv_data_file_is_raw_partition)[i] = SRV_NEW_RAW;
416  }
417 
418  if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
419  str += 3;
420 
421  if ((srv_data_file_is_raw_partition)[i] == 0) {
422  (srv_data_file_is_raw_partition)[i] = SRV_OLD_RAW;
423  }
424  }
425 
426  i++;
427 
428  if (*str == ';') {
429  str++;
430  }
431  }
432 
433  return(TRUE);
434 }
435 
436 /*********************************************************************/
439 UNIV_INTERN
440 void
442 /*==========================*/
443 {
444  free(srv_data_file_names);
445  srv_data_file_names = NULL;
446  free(srv_data_file_sizes);
447  srv_data_file_sizes = NULL;
448  free(srv_data_file_is_raw_partition);
449  srv_data_file_is_raw_partition = NULL;
450 }
451 
452 #ifndef UNIV_HOTBACKUP
453 /********************************************************************/
456 extern "C" UNIV_INTERN
457 os_thread_ret_t
459 /*==============================*/
460  void* arg)
462 {
463  ulint segment;
464 
465  segment = *((ulint*) arg);
466 
467 #ifdef UNIV_DEBUG_THREAD_CREATION
468  fprintf(stderr, "Io handler thread %lu starts, id %lu\n", segment,
470 #endif
471 
472 #ifdef UNIV_PFS_THREAD
473  pfs_register_thread(io_handler_thread_key);
474 #endif /* UNIV_PFS_THREAD */
475 
477  fil_aio_wait(segment);
478  }
479 
480  /* We count the number of threads in os_thread_exit(). A created
481  thread should always use that to exit and not use return() to exit.
482  The thread actually never comes here because it is exited in an
483  os_event_wait(). */
484 
485  os_thread_exit(NULL);
486 
487  OS_THREAD_DUMMY_RETURN;
488 }
489 #endif /* !UNIV_HOTBACKUP */
490 
491 /*********************************************************************/
493 UNIV_INTERN
494 void
496 /*=======================*/
497  char* str __attribute__((unused)))
499 {
500 #ifdef __WIN__
501  for (; *str; str++) {
502 
503  if (*str == '/') {
504  *str = '\\';
505  }
506  }
507 #endif
508 }
509 
510 #ifndef UNIV_HOTBACKUP
511 /*********************************************************************/
514 static __attribute__((nonnull, warn_unused_result))
515 dberr_t
516 create_log_file(
517 /*============*/
518  os_file_t* file,
519  const char* name)
520 {
521  ibool ret;
522 
523  *file = os_file_create(
524  innodb_file_log_key, name,
526  OS_LOG_FILE, &ret);
527 
528  if (!ret) {
529  ib_logf(IB_LOG_LEVEL_ERROR, "Cannot create %s", name);
530  return(DB_ERROR);
531  }
532 
533  ib_logf(IB_LOG_LEVEL_INFO,
534  "Setting log file %s size to %lu MB",
535  name, (ulong) srv_log_file_size
536  >> (20 - UNIV_PAGE_SIZE_SHIFT));
537 
538  ret = os_file_set_size(name, *file,
539  (os_offset_t) srv_log_file_size
540  << UNIV_PAGE_SIZE_SHIFT);
541  if (!ret) {
542  ib_logf(IB_LOG_LEVEL_ERROR, "Cannot set log file"
543  " %s to size %lu MB", name, (ulong) srv_log_file_size
544  >> (20 - UNIV_PAGE_SIZE_SHIFT));
545  return(DB_ERROR);
546  }
547 
548  ret = os_file_close(*file);
549  ut_a(ret);
550 
551  return(DB_SUCCESS);
552 }
553 
555 #define INIT_LOG_FILE0 (SRV_N_LOG_FILES_MAX + 1)
556 
557 #ifdef DBUG_OFF
558 # define RECOVERY_CRASH(x) do {} while(0)
559 #else
560 # define RECOVERY_CRASH(x) do { \
561  if (srv_force_recovery_crash == x) { \
562  fprintf(stderr, "innodb_force_recovery_crash=%lu\n", \
563  srv_force_recovery_crash); \
564  fflush(stderr); \
565  exit(3); \
566  } \
567 } while (0)
568 #endif
569 
570 /*********************************************************************/
573 static
574 dberr_t
575 create_log_files(
576 /*=============*/
577  bool create_new_db,
579  char* logfilename,
580  size_t dirnamelen,
581  lsn_t lsn,
582  char*& logfile0)
583 {
584  if (srv_read_only_mode) {
585  ib_logf(IB_LOG_LEVEL_ERROR,
586  "Cannot create log files in read-only mode");
587  return(DB_READ_ONLY);
588  }
589 
590  /* We prevent system tablespace creation with existing files in
591  data directory. So we do not delete log files when creating new system
592  tablespace */
593  if (!create_new_db) {
594  /* Remove any old log files. */
595  for (unsigned i = 0; i <= INIT_LOG_FILE0; i++) {
596  sprintf(logfilename + dirnamelen, "ib_logfile%u", i);
597 
598  /* Ignore errors about non-existent files or files
599  that cannot be removed. The create_log_file() will
600  return an error when the file exists. */
601 #ifdef __WIN__
602  DeleteFile((LPCTSTR) logfilename);
603 #else
604  unlink(logfilename);
605 #endif
606  /* Crashing after deleting the first
607  file should be recoverable. The buffer
608  pool was clean, and we can simply create
609  all log files from the scratch. */
610  RECOVERY_CRASH(6);
611  }
612  }
613 
615 
616  RECOVERY_CRASH(7);
617 
618  for (unsigned i = 0; i < srv_n_log_files; i++) {
619  sprintf(logfilename + dirnamelen,
620  "ib_logfile%u", i ? i : INIT_LOG_FILE0);
621 
622  dberr_t err = create_log_file(&files[i], logfilename);
623 
624  if (err != DB_SUCCESS) {
625  return(err);
626  }
627  }
628 
629  RECOVERY_CRASH(8);
630 
631  /* We did not create the first log file initially as
632  ib_logfile0, so that crash recovery cannot find it until it
633  has been completed and renamed. */
634  sprintf(logfilename + dirnamelen, "ib_logfile%u", INIT_LOG_FILE0);
635 
637  logfilename, SRV_LOG_SPACE_FIRST_ID,
638  fsp_flags_set_page_size(0, UNIV_PAGE_SIZE),
639  FIL_LOG);
640  ut_a(fil_validate());
641 
642  logfile0 = fil_node_create(
643  logfilename, (ulint) srv_log_file_size,
644  SRV_LOG_SPACE_FIRST_ID, FALSE);
645  ut_a(logfile0);
646 
647  for (unsigned i = 1; i < srv_n_log_files; i++) {
648  sprintf(logfilename + dirnamelen, "ib_logfile%u", i);
649 
650  if (!fil_node_create(
651  logfilename,
652  (ulint) srv_log_file_size,
653  SRV_LOG_SPACE_FIRST_ID, FALSE)) {
654  ut_error;
655  }
656  }
657 
658  log_group_init(0, srv_n_log_files,
659  srv_log_file_size * UNIV_PAGE_SIZE,
662 
664 
665  /* Create a log checkpoint. */
666  mutex_enter(&log_sys->mutex);
667  ut_d(recv_no_log_write = FALSE);
668  recv_reset_logs(lsn);
669  mutex_exit(&log_sys->mutex);
670 
671  return(DB_SUCCESS);
672 }
673 
674 /*********************************************************************/
676 static
677 void
678 create_log_files_rename(
679 /*====================*/
680  char* logfilename,
681  size_t dirnamelen,
682  lsn_t lsn,
683  char* logfile0)
684 {
685  /* If innodb_flush_method=O_DSYNC,
686  we need to explicitly flush the log buffers. */
688  /* Close the log files, so that we can rename
689  the first one. */
690  fil_close_log_files(false);
691 
692  /* Rename the first log file, now that a log
693  checkpoint has been created. */
694  sprintf(logfilename + dirnamelen, "ib_logfile%u", 0);
695 
696  RECOVERY_CRASH(9);
697 
698  ib_logf(IB_LOG_LEVEL_INFO,
699  "Renaming log file %s to %s", logfile0, logfilename);
700 
701  mutex_enter(&log_sys->mutex);
702  ut_ad(strlen(logfile0) == 2 + strlen(logfilename));
703  ibool success = os_file_rename(
704  innodb_file_log_key, logfile0, logfilename);
705  ut_a(success);
706 
707  RECOVERY_CRASH(10);
708 
709  /* Replace the first file with ib_logfile0. */
710  strcpy(logfile0, logfilename);
711  mutex_exit(&log_sys->mutex);
712 
714 
715  ib_logf(IB_LOG_LEVEL_WARN, "New log files created, LSN=" LSN_PF, lsn);
716 }
717 
718 /*********************************************************************/
721 static __attribute__((nonnull, warn_unused_result))
722 dberr_t
723 open_log_file(
724 /*==========*/
725  os_file_t* file,
726  const char* name,
727  os_offset_t* size)
728 {
729  ibool ret;
730 
731  *file = os_file_create(innodb_file_log_key, name,
732  OS_FILE_OPEN, OS_FILE_AIO,
733  OS_LOG_FILE, &ret);
734  if (!ret) {
735  ib_logf(IB_LOG_LEVEL_ERROR, "Unable to open '%s'", name);
736  return(DB_ERROR);
737  }
738 
739  *size = os_file_get_size(*file);
740 
741  ret = os_file_close(*file);
742  ut_a(ret);
743  return(DB_SUCCESS);
744 }
745 
746 /*********************************************************************/
749 static __attribute__((nonnull, warn_unused_result))
750 dberr_t
751 open_or_create_data_files(
752 /*======================*/
753  ibool* create_new_db,
755 #ifdef UNIV_LOG_ARCHIVE
756  ulint* min_arch_log_no,
758  ulint* max_arch_log_no,
760 #endif /* UNIV_LOG_ARCHIVE */
761  lsn_t* min_flushed_lsn,
763  lsn_t* max_flushed_lsn,
765  ulint* sum_of_new_sizes)
767 {
768  ibool ret;
769  ulint i;
770  ibool one_opened = FALSE;
771  ibool one_created = FALSE;
773  ulint flags;
774  ulint space;
775  ulint rounded_size_pages;
776  char name[10000];
777 
778  if (srv_n_data_files >= 1000) {
779 
780  ib_logf(IB_LOG_LEVEL_ERROR,
781  "Can only have < 1000 data files, you have "
782  "defined %lu", (ulong) srv_n_data_files);
783 
784  return(DB_ERROR);
785  }
786 
787  *sum_of_new_sizes = 0;
788 
789  *create_new_db = FALSE;
790 
791  srv_normalize_path_for_win(srv_data_home);
792 
793  for (i = 0; i < srv_n_data_files; i++) {
794  ulint dirnamelen;
795 
796  srv_normalize_path_for_win(srv_data_file_names[i]);
797  dirnamelen = strlen(srv_data_home);
798 
799  ut_a(dirnamelen + strlen(srv_data_file_names[i])
800  < (sizeof name) - 1);
801 
802  memcpy(name, srv_data_home, dirnamelen);
803 
804  /* Add a path separator if needed. */
805  if (dirnamelen && name[dirnamelen - 1] != SRV_PATH_SEPARATOR) {
806  name[dirnamelen++] = SRV_PATH_SEPARATOR;
807  }
808 
809  strcpy(name + dirnamelen, srv_data_file_names[i]);
810 
811  /* Note: It will return true if the file doesn' exist. */
812 
813  if (!srv_file_check_mode(name)) {
814 
815  return(DB_FAIL);
816 
817  } else if (srv_data_file_is_raw_partition[i] == 0) {
818 
819  /* First we try to create the file: if it already
820  exists, ret will get value FALSE */
821 
822  files[i] = os_file_create(
823  innodb_file_data_key, name, OS_FILE_CREATE,
824  OS_FILE_NORMAL, OS_DATA_FILE, &ret);
825 
826  if (srv_read_only_mode) {
827 
828  if (ret) {
829  goto size_check;
830  }
831 
832  ib_logf(IB_LOG_LEVEL_ERROR,
833  "Opening %s failed!", name);
834 
835  return(DB_ERROR);
836 
837  } else if (!ret
838  && os_file_get_last_error(false)
839  != OS_FILE_ALREADY_EXISTS
840 #ifdef UNIV_AIX
841  /* AIX 5.1 after security patch ML7 may have
842  errno set to 0 here, which causes our
843  function to return 100; work around that
844  AIX problem */
845  && os_file_get_last_error(false) != 100
846 #endif /* UNIV_AIX */
847  ) {
848  ib_logf(IB_LOG_LEVEL_ERROR,
849  "Creating or opening %s failed!",
850  name);
851 
852  return(DB_ERROR);
853  }
854 
855  } else if (srv_data_file_is_raw_partition[i] == SRV_NEW_RAW) {
856 
858 
859  /* The partition is opened, not created; then it is
860  written over */
861 
863  srv_created_new_raw = TRUE;
864 
865  files[i] = os_file_create(
866  innodb_file_data_key, name, OS_FILE_OPEN_RAW,
867  OS_FILE_NORMAL, OS_DATA_FILE, &ret);
868 
869  if (!ret) {
870  ib_logf(IB_LOG_LEVEL_ERROR,
871  "Error in opening %s", name);
872 
873  return(DB_ERROR);
874  }
875  } else if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
877 
878  ret = FALSE;
879  } else {
880  ut_a(0);
881  }
882 
883  if (ret == FALSE) {
884  const char* check_msg;
885  /* We open the data file */
886 
887  if (one_created) {
888  ib_logf(IB_LOG_LEVEL_ERROR,
889  "Data files can only be added at "
890  "the end of a tablespace, but "
891  "data file %s existed beforehand.",
892  name);
893  return(DB_ERROR);
894  }
895  if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
897  files[i] = os_file_create(
898  innodb_file_data_key,
899  name, OS_FILE_OPEN_RAW,
900  OS_FILE_NORMAL, OS_DATA_FILE, &ret);
901  } else if (i == 0) {
902  files[i] = os_file_create(
903  innodb_file_data_key,
904  name, OS_FILE_OPEN_RETRY,
905  OS_FILE_NORMAL, OS_DATA_FILE, &ret);
906  } else {
907  files[i] = os_file_create(
908  innodb_file_data_key,
909  name, OS_FILE_OPEN, OS_FILE_NORMAL,
910  OS_DATA_FILE, &ret);
911  }
912 
913  if (!ret) {
914 
916 
917  ib_logf(IB_LOG_LEVEL_ERROR,
918  "Can't open '%s'", name);
919 
920  return(DB_ERROR);
921  }
922 
923  if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
924 
925  goto skip_size_check;
926  }
927 
928 size_check:
929  size = os_file_get_size(files[i]);
930  ut_a(size != (os_offset_t) -1);
931  /* Round size downward to megabytes */
932 
933  rounded_size_pages = (ulint)
934  (size >> UNIV_PAGE_SIZE_SHIFT);
935 
936  if (i == srv_n_data_files - 1
937  && srv_auto_extend_last_data_file) {
938 
939  if (srv_data_file_sizes[i] > rounded_size_pages
940  || (srv_last_file_size_max > 0
941  && srv_last_file_size_max
942  < rounded_size_pages)) {
943 
944  ib_logf(IB_LOG_LEVEL_ERROR,
945  "auto-extending "
946  "data file %s is "
947  "of a different size "
948  "%lu pages (rounded "
949  "down to MB) than specified "
950  "in the .cnf file: "
951  "initial %lu pages, "
952  "max %lu (relevant if "
953  "non-zero) pages!",
954  name,
955  (ulong) rounded_size_pages,
956  (ulong) srv_data_file_sizes[i],
957  (ulong)
958  srv_last_file_size_max);
959 
960  return(DB_ERROR);
961  }
962 
963  srv_data_file_sizes[i] = rounded_size_pages;
964  }
965 
966  if (rounded_size_pages != srv_data_file_sizes[i]) {
967 
968  ib_logf(IB_LOG_LEVEL_ERROR,
969  "Data file %s is of a different "
970  "size %lu pages (rounded down to MB) "
971  "than specified in the .cnf file "
972  "%lu pages!",
973  name,
974  (ulong) rounded_size_pages,
975  (ulong) srv_data_file_sizes[i]);
976 
977  return(DB_ERROR);
978  }
979 skip_size_check:
980  check_msg = fil_read_first_page(
981  files[i], one_opened, &flags, &space,
982 #ifdef UNIV_LOG_ARCHIVE
983  min_arch_log_no, max_arch_log_no,
984 #endif /* UNIV_LOG_ARCHIVE */
985  min_flushed_lsn, max_flushed_lsn);
986 
987  if (check_msg) {
988  ib_logf(IB_LOG_LEVEL_ERROR,
989  "%s in data file %s",
990  check_msg, name);
991  return(DB_ERROR);
992  }
993 
994  /* The first file of the system tablespace must
995  have space ID = TRX_SYS_SPACE. The FSP_SPACE_ID
996  field in files greater than ibdata1 are unreliable. */
997  ut_a(one_opened || space == TRX_SYS_SPACE);
998 
999  /* Check the flags for the first system tablespace
1000  file only. */
1001  if (!one_opened
1002  && UNIV_PAGE_SIZE
1003  != fsp_flags_get_page_size(flags)) {
1004 
1005  ib_logf(IB_LOG_LEVEL_ERROR,
1006  "Data file \"%s\" uses page size %lu,"
1007  "but the start-up parameter "
1008  "is --innodb-page-size=%lu",
1009  name,
1010  fsp_flags_get_page_size(flags),
1011  UNIV_PAGE_SIZE);
1012 
1013  return(DB_ERROR);
1014  }
1015 
1016  one_opened = TRUE;
1017  } else if (!srv_read_only_mode) {
1018  /* We created the data file and now write it full of
1019  zeros */
1020 
1021  one_created = TRUE;
1022 
1023  if (i > 0) {
1024  ib_logf(IB_LOG_LEVEL_INFO,
1025  "Data file %s did not"
1026  " exist: new to be created",
1027  name);
1028  } else {
1029  ib_logf(IB_LOG_LEVEL_INFO,
1030  "The first specified "
1031  "data file %s did not exist: "
1032  "a new database to be created!",
1033  name);
1034 
1035  *create_new_db = TRUE;
1036  }
1037 
1038  ib_logf(IB_LOG_LEVEL_INFO,
1039  "Setting file %s size to %lu MB",
1040  name,
1041  (ulong) (srv_data_file_sizes[i]
1042  >> (20 - UNIV_PAGE_SIZE_SHIFT)));
1043 
1044  ib_logf(IB_LOG_LEVEL_INFO,
1045  "Database physically writes the"
1046  " file full: wait...");
1047 
1048  ret = os_file_set_size(
1049  name, files[i],
1050  (os_offset_t) srv_data_file_sizes[i]
1051  << UNIV_PAGE_SIZE_SHIFT);
1052 
1053  if (!ret) {
1054  ib_logf(IB_LOG_LEVEL_ERROR,
1055  "Error in creating %s: "
1056  "probably out of disk space",
1057  name);
1058 
1059  return(DB_ERROR);
1060  }
1061 
1062  *sum_of_new_sizes += srv_data_file_sizes[i];
1063  }
1064 
1065  ret = os_file_close(files[i]);
1066  ut_a(ret);
1067 
1068  if (i == 0) {
1069  flags = fsp_flags_set_page_size(0, UNIV_PAGE_SIZE);
1070  fil_space_create(name, 0, flags, FIL_TABLESPACE);
1071  }
1072 
1073  ut_a(fil_validate());
1074 
1075  if (!fil_node_create(name, srv_data_file_sizes[i], 0,
1076  srv_data_file_is_raw_partition[i] != 0)) {
1077  return(DB_ERROR);
1078  }
1079  }
1080 
1081  return(DB_SUCCESS);
1082 }
1083 
1084 /*********************************************************************/
1087 static
1088 dberr_t
1089 srv_undo_tablespace_create(
1090 /*=======================*/
1091  const char* name,
1092  ulint size)
1093 {
1094  os_file_t fh;
1095  ibool ret;
1096  dberr_t err = DB_SUCCESS;
1097 
1099 
1100  fh = os_file_create(
1101  innodb_file_data_key,
1102  name,
1104  OS_FILE_NORMAL, OS_DATA_FILE, &ret);
1105 
1106  if (srv_read_only_mode && ret) {
1107  ib_logf(IB_LOG_LEVEL_INFO,
1108  "%s opened in read-only mode", name);
1109  } else if (ret == FALSE) {
1110  if (os_file_get_last_error(false) != OS_FILE_ALREADY_EXISTS
1111 #ifdef UNIV_AIX
1112  /* AIX 5.1 after security patch ML7 may have
1113  errno set to 0 here, which causes our function
1114  to return 100; work around that AIX problem */
1115  && os_file_get_last_error(false) != 100
1116 #endif /* UNIV_AIX */
1117  ) {
1118  ib_logf(IB_LOG_LEVEL_ERROR,
1119  "Can't create UNDO tablespace %s", name);
1120  } else {
1121  ib_logf(IB_LOG_LEVEL_ERROR,
1122  "Creating system tablespace with"
1123  " existing undo tablespaces is not"
1124  " supported. Please delete all undo"
1125  " tablespaces before creating new"
1126  " system tablespace.");
1127  }
1128  err = DB_ERROR;
1129  } else {
1131 
1132  /* We created the data file and now write it full of zeros */
1133 
1134  ib_logf(IB_LOG_LEVEL_INFO,
1135  "Data file %s did not exist: new to be created",
1136  name);
1137 
1138  ib_logf(IB_LOG_LEVEL_INFO,
1139  "Setting file %s size to %lu MB",
1140  name, size >> (20 - UNIV_PAGE_SIZE_SHIFT));
1141 
1142  ib_logf(IB_LOG_LEVEL_INFO,
1143  "Database physically writes the file full: wait...");
1144 
1145  ret = os_file_set_size(name, fh, size << UNIV_PAGE_SIZE_SHIFT);
1146 
1147  if (!ret) {
1148  ib_logf(IB_LOG_LEVEL_INFO,
1149  "Error in creating %s: probably out of "
1150  "disk space", name);
1151 
1152  err = DB_ERROR;
1153  }
1154 
1155  os_file_close(fh);
1156  }
1157 
1158  return(err);
1159 }
1160 
1161 /*********************************************************************/
1164 static
1165 dberr_t
1166 srv_undo_tablespace_open(
1167 /*=====================*/
1168  const char* name,
1169  ulint space)
1170 {
1171  os_file_t fh;
1172  dberr_t err = DB_ERROR;
1173  ibool ret;
1174  ulint flags;
1175 
1176  if (!srv_file_check_mode(name)) {
1177  ib_logf(IB_LOG_LEVEL_ERROR,
1178  "UNDO tablespaces must be %s!",
1179  srv_read_only_mode ? "writable" : "readable");
1180 
1181  return(DB_ERROR);
1182  }
1183 
1184  fh = os_file_create(
1185  innodb_file_data_key, name,
1189  OS_FILE_NORMAL,
1190  OS_DATA_FILE,
1191  &ret);
1192 
1193  /* If the file open was successful then load the tablespace. */
1194 
1195  if (ret) {
1196  os_offset_t size;
1197 
1198  size = os_file_get_size(fh);
1199  ut_a(size != (os_offset_t) -1);
1200 
1201  ret = os_file_close(fh);
1202  ut_a(ret);
1203 
1204  /* Load the tablespace into InnoDB's internal
1205  data structures. */
1206 
1207  /* We set the biggest space id to the undo tablespace
1208  because InnoDB hasn't opened any other tablespace apart
1209  from the system tablespace. */
1210 
1212 
1213  /* Set the compressed page size to 0 (non-compressed) */
1214  flags = fsp_flags_set_page_size(0, UNIV_PAGE_SIZE);
1215  fil_space_create(name, space, flags, FIL_TABLESPACE);
1216 
1217  ut_a(fil_validate());
1218 
1219  os_offset_t n_pages = size / UNIV_PAGE_SIZE;
1220 
1221  /* On 64 bit Windows ulint can be 32 bit and os_offset_t
1222  is 64 bit. It is OK to cast the n_pages to ulint because
1223  the unit has been scaled to pages and they are always
1224  32 bit. */
1225  if (fil_node_create(name, (ulint) n_pages, space, FALSE)) {
1226  err = DB_SUCCESS;
1227  }
1228  }
1229 
1230  return(err);
1231 }
1232 
1233 /********************************************************************
1234 Opens the configured number of undo tablespaces.
1235 @return DB_SUCCESS or error code */
1236 static
1237 dberr_t
1238 srv_undo_tablespaces_init(
1239 /*======================*/
1240  ibool create_new_db,
1242  const ulint n_conf_tablespaces,
1244  ulint* n_opened)
1247 {
1248  ulint i;
1249  dberr_t err = DB_SUCCESS;
1250  ulint prev_space_id = 0;
1251  ulint n_undo_tablespaces;
1252  ulint undo_tablespace_ids[TRX_SYS_N_RSEGS + 1];
1253 
1254  *n_opened = 0;
1255 
1256  ut_a(n_conf_tablespaces <= TRX_SYS_N_RSEGS);
1257 
1258  memset(undo_tablespace_ids, 0x0, sizeof(undo_tablespace_ids));
1259 
1260  /* Create the undo spaces only if we are creating a new
1261  instance. We don't allow creating of new undo tablespaces
1262  in an existing instance (yet). This restriction exists because
1263  we check in several places for SYSTEM tablespaces to be less than
1264  the min of user defined tablespace ids. Once we implement saving
1265  the location of the undo tablespaces and their space ids this
1266  restriction will/should be lifted. */
1267 
1268  for (i = 0; create_new_db && i < n_conf_tablespaces; ++i) {
1269  char name[OS_FILE_MAX_PATH];
1270 
1271  ut_snprintf(
1272  name, sizeof(name),
1273  "%s%cundo%03lu",
1274  srv_undo_dir, SRV_PATH_SEPARATOR, i + 1);
1275 
1276  /* Undo space ids start from 1. */
1277  err = srv_undo_tablespace_create(
1278  name, SRV_UNDO_TABLESPACE_SIZE_IN_PAGES);
1279 
1280  if (err != DB_SUCCESS) {
1281 
1282  ib_logf(IB_LOG_LEVEL_ERROR,
1283  "Could not create undo tablespace '%s'.",
1284  name);
1285 
1286  return(err);
1287  }
1288  }
1289 
1290  /* Get the tablespace ids of all the undo segments excluding
1291  the system tablespace (0). If we are creating a new instance then
1292  we build the undo_tablespace_ids ourselves since they don't
1293  already exist. */
1294 
1295  if (!create_new_db) {
1296  n_undo_tablespaces = trx_rseg_get_n_undo_tablespaces(
1297  undo_tablespace_ids);
1298  } else {
1299  n_undo_tablespaces = n_conf_tablespaces;
1300 
1301  for (i = 1; i <= n_undo_tablespaces; ++i) {
1302  undo_tablespace_ids[i - 1] = i;
1303  }
1304 
1305  undo_tablespace_ids[i] = ULINT_UNDEFINED;
1306  }
1307 
1308  /* Open all the undo tablespaces that are currently in use. If we
1309  fail to open any of these it is a fatal error. The tablespace ids
1310  should be contiguous. It is a fatal error because they are required
1311  for recovery and are referenced by the UNDO logs (a.k.a RBS). */
1312 
1313  for (i = 0; i < n_undo_tablespaces; ++i) {
1314  char name[OS_FILE_MAX_PATH];
1315 
1316  ut_snprintf(
1317  name, sizeof(name),
1318  "%s%cundo%03lu",
1319  srv_undo_dir, SRV_PATH_SEPARATOR,
1320  undo_tablespace_ids[i]);
1321 
1322  /* Should be no gaps in undo tablespace ids. */
1323  ut_a(prev_space_id + 1 == undo_tablespace_ids[i]);
1324 
1325  /* The system space id should not be in this array. */
1326  ut_a(undo_tablespace_ids[i] != 0);
1327  ut_a(undo_tablespace_ids[i] != ULINT_UNDEFINED);
1328 
1329  /* Undo space ids start from 1. */
1330 
1331  err = srv_undo_tablespace_open(name, undo_tablespace_ids[i]);
1332 
1333  if (err != DB_SUCCESS) {
1334 
1335  ib_logf(IB_LOG_LEVEL_ERROR,
1336  "Unable to open undo tablespace '%s'.", name);
1337 
1338  return(err);
1339  }
1340 
1341  prev_space_id = undo_tablespace_ids[i];
1342 
1343  ++*n_opened;
1344  }
1345 
1346  /* Open any extra unused undo tablespaces. These must be contiguous.
1347  We stop at the first failure. These are undo tablespaces that are
1348  not in use and therefore not required by recovery. We only check
1349  that there are no gaps. */
1350 
1351  for (i = prev_space_id + 1; i < TRX_SYS_N_RSEGS; ++i) {
1352  char name[OS_FILE_MAX_PATH];
1353 
1354  ut_snprintf(
1355  name, sizeof(name),
1356  "%s%cundo%03lu", srv_undo_dir, SRV_PATH_SEPARATOR, i);
1357 
1358  /* Undo space ids start from 1. */
1359  err = srv_undo_tablespace_open(name, i);
1360 
1361  if (err != DB_SUCCESS) {
1362  break;
1363  }
1364 
1365  ++n_undo_tablespaces;
1366 
1367  ++*n_opened;
1368  }
1369 
1370  /* If the user says that there are fewer than what we find we
1371  tolerate that discrepancy but not the inverse. Because there could
1372  be unused undo tablespaces for future use. */
1373 
1374  if (n_conf_tablespaces > n_undo_tablespaces) {
1375  ut_print_timestamp(stderr);
1376  fprintf(stderr,
1377  " InnoDB: Expected to open %lu undo "
1378  "tablespaces but was able\n",
1379  n_conf_tablespaces);
1380  ut_print_timestamp(stderr);
1381  fprintf(stderr,
1382  " InnoDB: to find only %lu undo "
1383  "tablespaces.\n", n_undo_tablespaces);
1384  ut_print_timestamp(stderr);
1385  fprintf(stderr,
1386  " InnoDB: Set the "
1387  "innodb_undo_tablespaces parameter to "
1388  "the\n");
1389  ut_print_timestamp(stderr);
1390  fprintf(stderr,
1391  " InnoDB: correct value and retry. Suggested "
1392  "value is %lu\n", n_undo_tablespaces);
1393 
1394  return(err != DB_SUCCESS ? err : DB_ERROR);
1395 
1396  } else if (n_undo_tablespaces > 0) {
1397 
1398  ib_logf(IB_LOG_LEVEL_INFO, "Opened %lu undo tablespaces",
1399  n_undo_tablespaces);
1400 
1401  if (n_conf_tablespaces == 0) {
1402  ib_logf(IB_LOG_LEVEL_WARN,
1403  "Using the system tablespace for all UNDO "
1404  "logging because innodb_undo_tablespaces=0");
1405  }
1406  }
1407 
1408  if (create_new_db) {
1409  mtr_t mtr;
1410 
1411  mtr_start(&mtr);
1412 
1413  /* The undo log tablespace */
1414  for (i = 1; i <= n_undo_tablespaces; ++i) {
1415 
1417  i, SRV_UNDO_TABLESPACE_SIZE_IN_PAGES, &mtr);
1418  }
1419 
1420  mtr_commit(&mtr);
1421  }
1422 
1423  return(DB_SUCCESS);
1424 }
1425 
1426 /********************************************************************
1427 Wait for the purge thread(s) to start up. */
1428 static
1429 void
1430 srv_start_wait_for_purge_to_start()
1431 /*===============================*/
1432 {
1433  /* Wait for the purge coordinator and master thread to startup. */
1434 
1435  purge_state_t state = trx_purge_state();
1436 
1437  ut_a(state != PURGE_STATE_DISABLED);
1438 
1441  && state == PURGE_STATE_INIT) {
1442 
1443  switch (state = trx_purge_state()) {
1444  case PURGE_STATE_RUN:
1445  case PURGE_STATE_STOP:
1446  break;
1447 
1448  case PURGE_STATE_INIT:
1449  ib_logf(IB_LOG_LEVEL_INFO,
1450  "Waiting for purge to start");
1451 
1452  os_thread_sleep(50000);
1453  break;
1454 
1455  case PURGE_STATE_EXIT:
1456  case PURGE_STATE_DISABLED:
1457  ut_error;
1458  }
1459  }
1460 }
1461 
1462 /********************************************************************
1463 Starts InnoDB and creates a new database if database files
1464 are not found and the user wants.
1465 @return DB_SUCCESS or error code */
1466 UNIV_INTERN
1467 dberr_t
1469 /*====================================*/
1470 {
1471  ibool create_new_db;
1472  lsn_t min_flushed_lsn;
1473  lsn_t max_flushed_lsn;
1474 #ifdef UNIV_LOG_ARCHIVE
1475  ulint min_arch_log_no;
1476  ulint max_arch_log_no;
1477 #endif /* UNIV_LOG_ARCHIVE */
1478  ulint sum_of_new_sizes;
1479  ulint sum_of_data_file_sizes;
1480  ulint tablespace_size_in_header;
1481  dberr_t err;
1482  unsigned i;
1483  ulint srv_n_log_files_found = srv_n_log_files;
1484  ulint io_limit;
1485  mtr_t mtr;
1486  ib_bh_t* ib_bh;
1487  ulint n_recovered_trx;
1488  char logfilename[10000];
1489  char* logfile0 = NULL;
1490  size_t dirnamelen;
1491 
1492  if (srv_read_only_mode) {
1493  ib_logf(IB_LOG_LEVEL_INFO, "Started in read only mode");
1494  }
1495 
1496 #ifdef HAVE_DARWIN_THREADS
1497 # ifdef F_FULLFSYNC
1498  /* This executable has been compiled on Mac OS X 10.3 or later.
1499  Assume that F_FULLFSYNC is available at run-time. */
1500  srv_have_fullfsync = TRUE;
1501 # else /* F_FULLFSYNC */
1502  /* This executable has been compiled on Mac OS X 10.2
1503  or earlier. Determine if the executable is running
1504  on Mac OS X 10.3 or later. */
1505  struct utsname utsname;
1506  if (uname(&utsname)) {
1507  ut_print_timestamp(stderr);
1508  fputs(" InnoDB: cannot determine Mac OS X version!\n", stderr);
1509  } else {
1510  srv_have_fullfsync = strcmp(utsname.release, "7.") >= 0;
1511  }
1512  if (!srv_have_fullfsync) {
1513  ut_print_timestamp(stderr);
1514  fputs(" InnoDB: On Mac OS X, fsync() may be "
1515  "broken on internal drives,\n", stderr);
1516  ut_print_timestamp(stderr);
1517  fputs(" InnoDB: making transactions unsafe!\n", stderr);
1518  }
1519 # endif /* F_FULLFSYNC */
1520 #endif /* HAVE_DARWIN_THREADS */
1521 
1522  if (sizeof(ulint) != sizeof(void*)) {
1523  ut_print_timestamp(stderr);
1524  fprintf(stderr,
1525  " InnoDB: Error: size of InnoDB's ulint is %lu, "
1526  "but size of void*\n", (ulong) sizeof(ulint));
1527  ut_print_timestamp(stderr);
1528  fprintf(stderr,
1529  " InnoDB: is %lu. The sizes should be the same "
1530  "so that on a 64-bit\n",
1531  (ulong) sizeof(void*));
1532  ut_print_timestamp(stderr);
1533  fprintf(stderr,
1534  " InnoDB: platforms you can allocate more than 4 GB "
1535  "of memory.\n");
1536  }
1537 
1538 #ifdef UNIV_DEBUG
1539  ut_print_timestamp(stderr);
1540  fprintf(stderr,
1541  " InnoDB: !!!!!!!! UNIV_DEBUG switched on !!!!!!!!!\n");
1542 #endif
1543 
1544 #ifdef UNIV_IBUF_DEBUG
1545  ut_print_timestamp(stderr);
1546  fprintf(stderr,
1547  " InnoDB: !!!!!!!! UNIV_IBUF_DEBUG switched on !!!!!!!!!\n");
1548 # ifdef UNIV_IBUF_COUNT_DEBUG
1549  ut_print_timestamp(stderr);
1550  fprintf(stderr,
1551  " InnoDB: !!!!!!!! UNIV_IBUF_COUNT_DEBUG switched on "
1552  "!!!!!!!!!\n");
1553  ut_print_timestamp(stderr);
1554  fprintf(stderr,
1555  " InnoDB: Crash recovery will fail with UNIV_IBUF_COUNT_DEBUG\n");
1556 # endif
1557 #endif
1558 
1559 #ifdef UNIV_BLOB_DEBUG
1560  fprintf(stderr,
1561  "InnoDB: !!!!!!!! UNIV_BLOB_DEBUG switched on !!!!!!!!!\n"
1562  "InnoDB: Server restart may fail with UNIV_BLOB_DEBUG\n");
1563 #endif /* UNIV_BLOB_DEBUG */
1564 
1565 #ifdef UNIV_SYNC_DEBUG
1566  ut_print_timestamp(stderr);
1567  fprintf(stderr,
1568  " InnoDB: !!!!!!!! UNIV_SYNC_DEBUG switched on !!!!!!!!!\n");
1569 #endif
1570 
1571 #ifdef UNIV_SEARCH_DEBUG
1572  ut_print_timestamp(stderr);
1573  fprintf(stderr,
1574  " InnoDB: !!!!!!!! UNIV_SEARCH_DEBUG switched on !!!!!!!!!\n");
1575 #endif
1576 
1577 #ifdef UNIV_LOG_LSN_DEBUG
1578  ut_print_timestamp(stderr);
1579  fprintf(stderr,
1580  " InnoDB: !!!!!!!! UNIV_LOG_LSN_DEBUG switched on !!!!!!!!!\n");
1581 #endif /* UNIV_LOG_LSN_DEBUG */
1582 #ifdef UNIV_MEM_DEBUG
1583  ut_print_timestamp(stderr);
1584  fprintf(stderr,
1585  " InnoDB: !!!!!!!! UNIV_MEM_DEBUG switched on !!!!!!!!!\n");
1586 #endif
1587 
1588  if (srv_use_sys_malloc) {
1589  ib_logf(IB_LOG_LEVEL_INFO,
1590  "The InnoDB memory heap is disabled");
1591  }
1592 
1593 #if defined(COMPILER_HINTS_ENABLED)
1594  ib_logf(IB_LOG_LEVEL_INFO,
1595  " InnoDB: Compiler hints enabled.");
1596 #endif /* defined(COMPILER_HINTS_ENABLED) */
1597 
1598  ib_logf(IB_LOG_LEVEL_INFO,
1599  "" IB_ATOMICS_STARTUP_MSG "");
1600 
1601  ib_logf(IB_LOG_LEVEL_INFO,
1602  "Compressed tables use zlib " ZLIB_VERSION
1603 #ifdef UNIV_ZIP_DEBUG
1604  " with validation"
1605 #endif /* UNIV_ZIP_DEBUG */
1606  );
1607 #ifdef UNIV_ZIP_COPY
1608  ib_logf(IB_LOG_LEVEL_INFO, "and extra copying");
1609 #endif /* UNIV_ZIP_COPY */
1610 
1611 
1612  /* Since InnoDB does not currently clean up all its internal data
1613  structures in MySQL Embedded Server Library server_end(), we
1614  print an error message if someone tries to start up InnoDB a
1615  second time during the process lifetime. */
1616 
1617  if (srv_start_has_been_called) {
1618  ut_print_timestamp(stderr);
1619  fprintf(stderr, " InnoDB: Error: startup called second time "
1620  "during the process\n");
1621  ut_print_timestamp(stderr);
1622  fprintf(stderr, " InnoDB: lifetime. In the MySQL Embedded "
1623  "Server Library you\n");
1624  ut_print_timestamp(stderr);
1625  fprintf(stderr, " InnoDB: cannot call server_init() more "
1626  "than once during the\n");
1627  ut_print_timestamp(stderr);
1628  fprintf(stderr, " InnoDB: process lifetime.\n");
1629  }
1630 
1631  srv_start_has_been_called = TRUE;
1632 
1633 #ifdef UNIV_DEBUG
1634  log_do_write = TRUE;
1635 #endif /* UNIV_DEBUG */
1636  /* yydebug = TRUE; */
1637 
1638  srv_is_being_started = TRUE;
1640 
1641 #ifdef __WIN__
1642  switch (os_get_os_version()) {
1643  case OS_WIN95:
1644  case OS_WIN31:
1645  case OS_WINNT:
1646  /* On Win 95, 98, ME, Win32 subsystem for Windows 3.1,
1647  and NT use simulated aio. In NT Windows provides async i/o,
1648  but when run in conjunction with InnoDB Hot Backup, it seemed
1649  to corrupt the data files. */
1650 
1651  srv_use_native_aio = FALSE;
1652  break;
1653 
1654  case OS_WIN2000:
1655  case OS_WINXP:
1656  /* On 2000 and XP, async IO is available. */
1657  srv_use_native_aio = TRUE;
1658  break;
1659 
1660  default:
1661  /* Vista and later have both async IO and condition variables */
1662  srv_use_native_aio = TRUE;
1663  srv_use_native_conditions = TRUE;
1664  break;
1665  }
1666 
1667 #elif defined(LINUX_NATIVE_AIO)
1668 
1669  if (srv_use_native_aio) {
1670  ib_logf(IB_LOG_LEVEL_INFO, "Using Linux native AIO");
1671  }
1672 #else
1673  /* Currently native AIO is supported only on windows and linux
1674  and that also when the support is compiled in. In all other
1675  cases, we ignore the setting of innodb_use_native_aio. */
1676  srv_use_native_aio = FALSE;
1677 #endif /* __WIN__ */
1678 
1679  if (srv_file_flush_method_str == NULL) {
1680  /* These are the default options */
1681 
1682  srv_unix_file_flush_method = SRV_UNIX_FSYNC;
1683 
1684  srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
1685 #ifndef __WIN__
1686  } else if (0 == ut_strcmp(srv_file_flush_method_str, "fsync")) {
1687  srv_unix_file_flush_method = SRV_UNIX_FSYNC;
1688 
1689  } else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DSYNC")) {
1690  srv_unix_file_flush_method = SRV_UNIX_O_DSYNC;
1691 
1692  } else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DIRECT")) {
1693  srv_unix_file_flush_method = SRV_UNIX_O_DIRECT;
1694 
1695  } else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DIRECT_NO_FSYNC")) {
1696  srv_unix_file_flush_method = SRV_UNIX_O_DIRECT_NO_FSYNC;
1697 
1698  } else if (0 == ut_strcmp(srv_file_flush_method_str, "littlesync")) {
1699  srv_unix_file_flush_method = SRV_UNIX_LITTLESYNC;
1700 
1701  } else if (0 == ut_strcmp(srv_file_flush_method_str, "nosync")) {
1702  srv_unix_file_flush_method = SRV_UNIX_NOSYNC;
1703 #else
1704  } else if (0 == ut_strcmp(srv_file_flush_method_str, "normal")) {
1705  srv_win_file_flush_method = SRV_WIN_IO_NORMAL;
1706  srv_use_native_aio = FALSE;
1707 
1708  } else if (0 == ut_strcmp(srv_file_flush_method_str, "unbuffered")) {
1709  srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
1710  srv_use_native_aio = FALSE;
1711 
1712  } else if (0 == ut_strcmp(srv_file_flush_method_str,
1713  "async_unbuffered")) {
1714  srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
1715 #endif /* __WIN__ */
1716  } else {
1717  ib_logf(IB_LOG_LEVEL_ERROR,
1718  "Unrecognized value %s for innodb_flush_method",
1719  srv_file_flush_method_str);
1720  return(DB_ERROR);
1721  }
1722 
1723  /* Note that the call srv_boot() also changes the values of
1724  some variables to the units used by InnoDB internally */
1725 
1726  /* Set the maximum number of threads which can wait for a semaphore
1727  inside InnoDB: this is the 'sync wait array' size, as well as the
1728  maximum number of threads that can wait in the 'srv_conc array' for
1729  their time to enter InnoDB. */
1730 
1731  if (srv_buf_pool_size >= 1000 * 1024 * 1024) {
1732  /* If buffer pool is less than 1000 MB,
1733  assume fewer threads. Also use only one
1734  buffer pool instance */
1735  srv_max_n_threads = 50000;
1736 
1737  } else if (srv_buf_pool_size >= 8 * 1024 * 1024) {
1738 
1739  srv_buf_pool_instances = 1;
1740  srv_max_n_threads = 10000;
1741  } else {
1742  srv_buf_pool_instances = 1;
1743 
1744  /* Saves several MB of memory, especially in
1745  64-bit computers */
1746 
1747  srv_max_n_threads = 1000;
1748  }
1749 
1750  srv_boot();
1751 
1752  ib_logf(IB_LOG_LEVEL_INFO,
1753  "%s CPU crc32 instructions",
1754  ut_crc32_sse2_enabled ? "Using" : "Not using");
1755 
1756  if (!srv_read_only_mode) {
1757 
1758  mutex_create(srv_monitor_file_mutex_key,
1759  &srv_monitor_file_mutex, SYNC_NO_ORDER_CHECK);
1760 
1761  if (srv_innodb_status) {
1762 
1763  srv_monitor_file_name = static_cast<char*>(
1764  mem_alloc(
1766  + 20 + sizeof "/innodb_status."));
1767 
1768  sprintf(srv_monitor_file_name, "%s/innodb_status.%lu",
1770  os_proc_get_number());
1771 
1772  srv_monitor_file = fopen(srv_monitor_file_name, "w+");
1773 
1774  if (!srv_monitor_file) {
1775 
1776  ib_logf(IB_LOG_LEVEL_ERROR,
1777  "Unable to create %s: %s",
1778  srv_monitor_file_name,
1779  strerror(errno));
1780 
1781  return(DB_ERROR);
1782  }
1783  } else {
1784  srv_monitor_file_name = NULL;
1786 
1787  if (!srv_monitor_file) {
1788  return(DB_ERROR);
1789  }
1790  }
1791 
1792  mutex_create(srv_dict_tmpfile_mutex_key,
1793  &srv_dict_tmpfile_mutex, SYNC_DICT_OPERATION);
1794 
1796 
1797  if (!srv_dict_tmpfile) {
1798  return(DB_ERROR);
1799  }
1800 
1801  mutex_create(srv_misc_tmpfile_mutex_key,
1802  &srv_misc_tmpfile_mutex, SYNC_ANY_LATCH);
1803 
1805 
1806  if (!srv_misc_tmpfile) {
1807  return(DB_ERROR);
1808  }
1809  }
1810 
1811  /* If user has set the value of innodb_file_io_threads then
1812  we'll emit a message telling the user that this parameter
1813  is now deprecated. */
1814  if (srv_n_file_io_threads != 4) {
1815  ib_logf(IB_LOG_LEVEL_WARN,
1816  "innodb_file_io_threads is deprecated. Please use "
1817  "innodb_read_io_threads and innodb_write_io_threads "
1818  "instead");
1819  }
1820 
1821  /* Now overwrite the value on srv_n_file_io_threads */
1822  srv_n_file_io_threads = srv_n_read_io_threads;
1823 
1824  if (!srv_read_only_mode) {
1825  /* Add the log and ibuf IO threads. */
1826  srv_n_file_io_threads += 2;
1827  srv_n_file_io_threads += srv_n_write_io_threads;
1828  } else {
1829  ib_logf(IB_LOG_LEVEL_INFO,
1830  "Disabling background IO write threads.");
1831 
1832  srv_n_write_io_threads = 0;
1833  }
1834 
1835  ut_a(srv_n_file_io_threads <= SRV_MAX_N_IO_THREADS);
1836 
1837  io_limit = 8 * SRV_N_PENDING_IOS_PER_THREAD;
1838 
1839  /* On Windows when using native aio the number of aio requests
1840  that a thread can handle at a given time is limited to 32
1841  i.e.: SRV_N_PENDING_IOS_PER_THREAD */
1842 # ifdef __WIN__
1843  if (srv_use_native_aio) {
1844  io_limit = SRV_N_PENDING_IOS_PER_THREAD;
1845  }
1846 # endif /* __WIN__ */
1847 
1848  if (!os_aio_init(io_limit,
1849  srv_n_read_io_threads,
1850  srv_n_write_io_threads,
1851  SRV_MAX_N_PENDING_SYNC_IOS)) {
1852 
1853  ib_logf(IB_LOG_LEVEL_ERROR,
1854  "Fatal : Cannot initialize AIO sub-system");
1855 
1856  return(DB_ERROR);
1857  }
1858 
1859  fil_init(srv_file_per_table ? 50000 : 5000, srv_max_n_open_files);
1860 
1861  double size;
1862  char unit;
1863 
1864  if (srv_buf_pool_size >= 1024 * 1024 * 1024) {
1865  size = ((double) srv_buf_pool_size) / (1024 * 1024 * 1024);
1866  unit = 'G';
1867  } else {
1868  size = ((double) srv_buf_pool_size) / (1024 * 1024);
1869  unit = 'M';
1870  }
1871 
1872  /* Print time to initialize the buffer pool */
1873  ib_logf(IB_LOG_LEVEL_INFO,
1874  "Initializing buffer pool, size = %.1f%c", size, unit);
1875 
1876  err = buf_pool_init(srv_buf_pool_size, srv_buf_pool_instances);
1877 
1878  if (err != DB_SUCCESS) {
1879  ib_logf(IB_LOG_LEVEL_ERROR,
1880  "Cannot allocate memory for the buffer pool");
1881 
1882  return(DB_ERROR);
1883  }
1884 
1885  ib_logf(IB_LOG_LEVEL_INFO,
1886  "Completed initialization of buffer pool");
1887 
1888 #ifdef UNIV_DEBUG
1889  /* We have observed deadlocks with a 5MB buffer pool but
1890  the actual lower limit could very well be a little higher. */
1891 
1892  if (srv_buf_pool_size <= 5 * 1024 * 1024) {
1893 
1894  ib_logf(IB_LOG_LEVEL_INFO,
1895  "Small buffer pool size (%luM), the flst_validate() "
1896  "debug function can cause a deadlock if the "
1897  "buffer pool fills up.",
1898  srv_buf_pool_size / 1024 / 1024);
1899  }
1900 #endif /* UNIV_DEBUG */
1901 
1902  fsp_init();
1903  log_init();
1904 
1905  lock_sys_create(srv_lock_table_size);
1906 
1907  /* Create i/o-handler threads: */
1908 
1909  for (i = 0; i < srv_n_file_io_threads; ++i) {
1910 
1911  n[i] = i;
1912 
1913  os_thread_create(io_handler_thread, n + i, thread_ids + i);
1914  }
1915 
1916 #ifdef UNIV_LOG_ARCHIVE
1917  if (0 != ut_strcmp(srv_log_group_home_dir, srv_arch_dir)) {
1918  ut_print_timestamp(stderr);
1919  fprintf(stderr, " InnoDB: Error: you must set the log group home dir in my.cnf\n");
1920  ut_print_timestamp(stderr);
1921  fprintf(stderr, " InnoDB: the same as log arch dir.\n");
1922 
1923  return(DB_ERROR);
1924  }
1925 #endif /* UNIV_LOG_ARCHIVE */
1926 
1927  if (srv_n_log_files * srv_log_file_size * UNIV_PAGE_SIZE
1928  >= 512ULL * 1024ULL * 1024ULL * 1024ULL) {
1929  /* log_block_convert_lsn_to_no() limits the returned block
1930  number to 1G and given that OS_FILE_LOG_BLOCK_SIZE is 512
1931  bytes, then we have a limit of 512 GB. If that limit is to
1932  be raised, then log_block_convert_lsn_to_no() must be
1933  modified. */
1934  ib_logf(IB_LOG_LEVEL_ERROR,
1935  "Combined size of log files must be < 512 GB");
1936 
1937  return(DB_ERROR);
1938  }
1939 
1940  if (srv_n_log_files * srv_log_file_size >= ULINT_MAX) {
1941  /* fil_io() takes ulint as an argument and we are passing
1942  (next_offset / UNIV_PAGE_SIZE) to it in log_group_write_buf().
1943  So (next_offset / UNIV_PAGE_SIZE) must be less than ULINT_MAX.
1944  So next_offset must be < ULINT_MAX * UNIV_PAGE_SIZE. This
1945  means that we are limited to ULINT_MAX * UNIV_PAGE_SIZE which
1946  is 64 TB on 32 bit systems. */
1947  fprintf(stderr,
1948  " InnoDB: Error: combined size of log files"
1949  " must be < %lu GB\n",
1950  ULINT_MAX / 1073741824 * UNIV_PAGE_SIZE);
1951 
1952  return(DB_ERROR);
1953  }
1954 
1955  sum_of_new_sizes = 0;
1956 
1957  for (i = 0; i < srv_n_data_files; i++) {
1958 #ifndef __WIN__
1959  if (sizeof(off_t) < 5
1960  && srv_data_file_sizes[i]
1961  >= (ulint) (1 << (32 - UNIV_PAGE_SIZE_SHIFT))) {
1962  ut_print_timestamp(stderr);
1963  fprintf(stderr,
1964  " InnoDB: Error: file size must be < 4 GB"
1965  " with this MySQL binary\n");
1966  ut_print_timestamp(stderr);
1967  fprintf(stderr,
1968  " InnoDB: and operating system combination,"
1969  " in some OS's < 2 GB\n");
1970 
1971  return(DB_ERROR);
1972  }
1973 #endif
1974  sum_of_new_sizes += srv_data_file_sizes[i];
1975  }
1976 
1977  if (sum_of_new_sizes < 10485760 / UNIV_PAGE_SIZE) {
1978  ib_logf(IB_LOG_LEVEL_ERROR,
1979  "Tablespace size must be at least 10 MB");
1980 
1981  return(DB_ERROR);
1982  }
1983 
1984  err = open_or_create_data_files(&create_new_db,
1985 #ifdef UNIV_LOG_ARCHIVE
1986  &min_arch_log_no, &max_arch_log_no,
1987 #endif /* UNIV_LOG_ARCHIVE */
1988  &min_flushed_lsn, &max_flushed_lsn,
1989  &sum_of_new_sizes);
1990  if (err == DB_FAIL) {
1991 
1992  ib_logf(IB_LOG_LEVEL_ERROR,
1993  "The system tablespace must be writable!");
1994 
1995  return(DB_ERROR);
1996 
1997  } else if (err != DB_SUCCESS) {
1998 
1999  ib_logf(IB_LOG_LEVEL_ERROR,
2000  "Could not open or create the system tablespace. If "
2001  "you tried to add new data files to the system "
2002  "tablespace, and it failed here, you should now "
2003  "edit innodb_data_file_path in my.cnf back to what "
2004  "it was, and remove the new ibdata files InnoDB "
2005  "created in this failed attempt. InnoDB only wrote "
2006  "those files full of zeros, but did not yet use "
2007  "them in any way. But be careful: do not remove "
2008  "old data files which contain your precious data!");
2009 
2010  return(err);
2011  }
2012 
2013 #ifdef UNIV_LOG_ARCHIVE
2014  srv_normalize_path_for_win(srv_arch_dir);
2015  srv_arch_dir = srv_add_path_separator_if_needed(srv_arch_dir);
2016 #endif /* UNIV_LOG_ARCHIVE */
2017 
2018  dirnamelen = strlen(srv_log_group_home_dir);
2019  ut_a(dirnamelen < (sizeof logfilename) - 10 - sizeof "ib_logfile");
2020  memcpy(logfilename, srv_log_group_home_dir, dirnamelen);
2021 
2022  /* Add a path separator if needed. */
2023  if (dirnamelen && logfilename[dirnamelen - 1] != SRV_PATH_SEPARATOR) {
2024  logfilename[dirnamelen++] = SRV_PATH_SEPARATOR;
2025  }
2026 
2027  srv_log_file_size_requested = srv_log_file_size;
2028 
2029  if (create_new_db) {
2030  bool success = buf_flush_list(ULINT_MAX, LSN_MAX, NULL);
2031  ut_a(success);
2032 
2033  min_flushed_lsn = max_flushed_lsn = log_get_lsn();
2034 
2036 
2037  err = create_log_files(create_new_db, logfilename, dirnamelen,
2038  max_flushed_lsn, logfile0);
2039 
2040  if (err != DB_SUCCESS) {
2041  return(err);
2042  }
2043  } else {
2044  for (i = 0; i < SRV_N_LOG_FILES_MAX; i++) {
2045  os_offset_t size;
2046  os_file_stat_t stat_info;
2047 
2048  sprintf(logfilename + dirnamelen,
2049  "ib_logfile%u", i);
2050 
2051  err = os_file_get_status(
2052  logfilename, &stat_info, false);
2053 
2054  if (err == DB_NOT_FOUND) {
2055  if (i == 0) {
2056  if (max_flushed_lsn
2057  != min_flushed_lsn) {
2058  ib_logf(IB_LOG_LEVEL_ERROR,
2059  "Cannot create"
2060  " log files because"
2061  " data files are"
2062  " corrupt or"
2063  " not in sync"
2064  " with each other");
2065  return(DB_ERROR);
2066  }
2067 
2068  if (max_flushed_lsn < (lsn_t) 1000) {
2069  ib_logf(IB_LOG_LEVEL_ERROR,
2070  "Cannot create"
2071  " log files because"
2072  " data files are"
2073  " corrupt or the"
2074  " database was not"
2075  " shut down cleanly"
2076  " after creating"
2077  " the data files.");
2078  return(DB_ERROR);
2079  }
2080 
2081  err = create_log_files(
2082  create_new_db, logfilename,
2083  dirnamelen, max_flushed_lsn,
2084  logfile0);
2085 
2086  if (err != DB_SUCCESS) {
2087  return(err);
2088  }
2089 
2090  create_log_files_rename(
2091  logfilename, dirnamelen,
2092  max_flushed_lsn, logfile0);
2093 
2094  /* Suppress the message about
2095  crash recovery. */
2096  max_flushed_lsn = min_flushed_lsn
2097  = log_get_lsn();
2098  goto files_checked;
2099  } else if (i < 2) {
2100  /* must have at least 2 log files */
2101  ib_logf(IB_LOG_LEVEL_ERROR,
2102  "Only one log file found.");
2103  return(err);
2104  }
2105 
2106  /* opened all files */
2107  break;
2108  }
2109 
2110  if (!srv_file_check_mode(logfilename)) {
2111  return(DB_ERROR);
2112  }
2113 
2114  err = open_log_file(&files[i], logfilename, &size);
2115 
2116  if (err != DB_SUCCESS) {
2117  return(err);
2118  }
2119 
2120  ut_a(size != (os_offset_t) -1);
2121 
2122  if (size & ((1 << UNIV_PAGE_SIZE_SHIFT) - 1)) {
2123  ib_logf(IB_LOG_LEVEL_ERROR,
2124  "Log file %s size "
2125  UINT64PF " is not a multiple of"
2126  " innodb_page_size",
2127  logfilename, size);
2128  return(DB_ERROR);
2129  }
2130 
2131  size >>= UNIV_PAGE_SIZE_SHIFT;
2132 
2133  if (i == 0) {
2134  srv_log_file_size = size;
2135  } else if (size != srv_log_file_size) {
2136  ib_logf(IB_LOG_LEVEL_ERROR,
2137  "Log file %s is"
2138  " of different size "UINT64PF" bytes"
2139  " than other log"
2140  " files "UINT64PF" bytes!",
2141  logfilename,
2142  size << UNIV_PAGE_SIZE_SHIFT,
2143  (os_offset_t) srv_log_file_size
2144  << UNIV_PAGE_SIZE_SHIFT);
2145  return(DB_ERROR);
2146  }
2147  }
2148 
2149  srv_n_log_files_found = i;
2150 
2151  /* Create the in-memory file space objects. */
2152 
2153  sprintf(logfilename + dirnamelen, "ib_logfile%u", 0);
2154 
2155  fil_space_create(logfilename,
2157  fsp_flags_set_page_size(0, UNIV_PAGE_SIZE),
2158  FIL_LOG);
2159 
2160  ut_a(fil_validate());
2161 
2162  /* srv_log_file_size is measured in pages; if page size is 16KB,
2163  then we have a limit of 64TB on 32 bit systems */
2164  ut_a(srv_log_file_size <= ULINT_MAX);
2165 
2166  for (unsigned j = 0; j < i; j++) {
2167  sprintf(logfilename + dirnamelen, "ib_logfile%u", j);
2168 
2169  if (!fil_node_create(logfilename,
2170  (ulint) srv_log_file_size,
2171  SRV_LOG_SPACE_FIRST_ID, FALSE)) {
2172  return(DB_ERROR);
2173  }
2174  }
2175 
2176 #ifdef UNIV_LOG_ARCHIVE
2177  /* Create the file space object for archived logs. Under
2178  MySQL, no archiving ever done. */
2179  fil_space_create("arch_log_space", SRV_LOG_SPACE_FIRST_ID + 1,
2180  0, FIL_LOG);
2181 #endif /* UNIV_LOG_ARCHIVE */
2182  log_group_init(0, i, srv_log_file_size * UNIV_PAGE_SIZE,
2185  }
2186 
2187 files_checked:
2188  /* Open all log files and data files in the system
2189  tablespace: we keep them open until database
2190  shutdown */
2191 
2193 
2194  err = srv_undo_tablespaces_init(
2195  create_new_db,
2198 
2199  /* If the force recovery is set very high then we carry on regardless
2200  of all errors. Basically this is fingers crossed mode. */
2201 
2202  if (err != DB_SUCCESS
2204 
2205  return(err);
2206  }
2207 
2208  /* Initialize objects used by dict stats gathering thread, which
2209  can also be used by recovery if it tries to drop some table */
2210  if (!srv_read_only_mode) {
2212  }
2213 
2215 
2216  trx_sys_create();
2217 
2218  if (create_new_db) {
2219 
2221 
2222  mtr_start(&mtr);
2223 
2224  fsp_header_init(0, sum_of_new_sizes, &mtr);
2225 
2226  mtr_commit(&mtr);
2227 
2228  /* To maintain backward compatibility we create only
2229  the first rollback segment before the double write buffer.
2230  All the remaining rollback segments will be created later,
2231  after the double write buffer has been created. */
2233 
2234  ib_bh = trx_sys_init_at_db_start();
2235  n_recovered_trx = UT_LIST_GET_LEN(trx_sys->rw_trx_list);
2236 
2237  /* The purge system needs to create the purge view and
2238  therefore requires that the trx_sys is inited. */
2239 
2240  trx_purge_sys_create(srv_n_purge_threads, ib_bh);
2241 
2242  err = dict_create();
2243 
2244  if (err != DB_SUCCESS) {
2245  return(err);
2246  }
2247 
2249 
2250  bool success = buf_flush_list(ULINT_MAX, LSN_MAX, NULL);
2251  ut_a(success);
2252 
2253  min_flushed_lsn = max_flushed_lsn = log_get_lsn();
2254 
2256 
2257  /* Stamp the LSN to the data files. */
2258  fil_write_flushed_lsn_to_data_files(max_flushed_lsn, 0);
2259 
2261 
2262  create_log_files_rename(logfilename, dirnamelen,
2263  max_flushed_lsn, logfile0);
2264 #ifdef UNIV_LOG_ARCHIVE
2265  } else if (srv_archive_recovery) {
2266 
2267  ib_logf(IB_LOG_LEVEL_INFO,
2268  " Starting archive recovery from a backup...");
2269 
2270  err = recv_recovery_from_archive_start(
2271  min_flushed_lsn, srv_archive_recovery_limit_lsn,
2272  min_arch_log_no);
2273  if (err != DB_SUCCESS) {
2274 
2275  return(DB_ERROR);
2276  }
2277  /* Since ibuf init is in dict_boot, and ibuf is needed
2278  in any disk i/o, first call dict_boot */
2279 
2280  err = dict_boot();
2281 
2282  if (err != DB_SUCCESS) {
2283  return(err);
2284  }
2285 
2286  ib_bh = trx_sys_init_at_db_start();
2287  n_recovered_trx = UT_LIST_GET_LEN(trx_sys->rw_trx_list);
2288 
2289  /* The purge system needs to create the purge view and
2290  therefore requires that the trx_sys is inited. */
2291 
2292  trx_purge_sys_create(srv_n_purge_threads, ib_bh);
2293 
2295 
2296  recv_recovery_from_archive_finish();
2297 #endif /* UNIV_LOG_ARCHIVE */
2298  } else {
2299 
2300  /* Check if we support the max format that is stamped
2301  on the system tablespace.
2302  Note: We are NOT allowed to make any modifications to
2303  the TRX_SYS_PAGE_NO page before recovery because this
2304  page also contains the max_trx_id etc. important system
2305  variables that are required for recovery. We need to
2306  ensure that we return the system to a state where normal
2307  recovery is guaranteed to work. We do this by
2308  invalidating the buffer cache, this will force the
2309  reread of the page and restoration to its last known
2310  consistent state, this is REQUIRED for the recovery
2311  process to work. */
2314 
2315  if (err != DB_SUCCESS) {
2316  return(err);
2317  }
2318 
2319  /* Invalidate the buffer pool to ensure that we reread
2320  the page that we read above, during recovery.
2321  Note that this is not as heavy weight as it seems. At
2322  this point there will be only ONE page in the buf_LRU
2323  and there must be no page in the buf_flush list. */
2325 
2326  /* We always try to do a recovery, even if the database had
2327  been shut down normally: this is the normal startup path */
2328 
2330  LOG_CHECKPOINT, LSN_MAX,
2331  min_flushed_lsn, max_flushed_lsn);
2332 
2333  if (err != DB_SUCCESS) {
2334 
2335  return(DB_ERROR);
2336  }
2337 
2338  /* Since the insert buffer init is in dict_boot, and the
2339  insert buffer is needed in any disk i/o, first we call
2340  dict_boot(). Note that trx_sys_init_at_db_start() only needs
2341  to access space 0, and the insert buffer at this stage already
2342  works for space 0. */
2343 
2344  err = dict_boot();
2345 
2346  if (err != DB_SUCCESS) {
2347  return(err);
2348  }
2349 
2350  ib_bh = trx_sys_init_at_db_start();
2351  n_recovered_trx = UT_LIST_GET_LEN(trx_sys->rw_trx_list);
2352 
2353  /* The purge system needs to create the purge view and
2354  therefore requires that the trx_sys is inited. */
2355 
2356  trx_purge_sys_create(srv_n_purge_threads, ib_bh);
2357 
2358  /* recv_recovery_from_checkpoint_finish needs trx lists which
2359  are initialized in trx_sys_init_at_db_start(). */
2360 
2362 
2364  /* The following call is necessary for the insert
2365  buffer to work with multiple tablespaces. We must
2366  know the mapping between space id's and .ibd file
2367  names.
2368 
2369  In a crash recovery, we check that the info in data
2370  dictionary is consistent with what we already know
2371  about space id's from the call of
2372  fil_load_single_table_tablespaces().
2373 
2374  In a normal startup, we create the space objects for
2375  every table in the InnoDB data dictionary that has
2376  an .ibd file.
2377 
2378  We also determine the maximum tablespace id used. */
2379  dict_check_t dict_check;
2380 
2381  if (recv_needed_recovery) {
2382  dict_check = DICT_CHECK_ALL_LOADED;
2383  } else if (n_recovered_trx) {
2384  dict_check = DICT_CHECK_SOME_LOADED;
2385  } else {
2386  dict_check = DICT_CHECK_NONE_LOADED;
2387  }
2388 
2390  }
2391 
2392  if (!srv_force_recovery
2394  && (srv_log_file_size_requested != srv_log_file_size
2395  || srv_n_log_files_found != srv_n_log_files)) {
2396  /* Prepare to replace the redo log files. */
2397 
2398  if (srv_read_only_mode) {
2399  ib_logf(IB_LOG_LEVEL_ERROR,
2400  "Cannot resize log files "
2401  "in read-only mode.");
2402  return(DB_READ_ONLY);
2403  }
2404 
2405  /* Clean the buffer pool. */
2406  bool success = buf_flush_list(
2407  ULINT_MAX, LSN_MAX, NULL);
2408  ut_a(success);
2409 
2410  RECOVERY_CRASH(1);
2411 
2412  min_flushed_lsn = max_flushed_lsn = log_get_lsn();
2413 
2414  ib_logf(IB_LOG_LEVEL_WARN,
2415  "Resizing redo log from %u*%u to %u*%u pages"
2416  ", LSN=" LSN_PF,
2417  (unsigned) i,
2418  (unsigned) srv_log_file_size,
2419  (unsigned) srv_n_log_files,
2420  (unsigned) srv_log_file_size_requested,
2421  max_flushed_lsn);
2422 
2424 
2425  RECOVERY_CRASH(2);
2426 
2427  /* Flush the old log files. */
2429  /* If innodb_flush_method=O_DSYNC,
2430  we need to explicitly flush the log buffers. */
2432 
2433  ut_ad(max_flushed_lsn == log_get_lsn());
2434 
2435  /* Prohibit redo log writes from any other
2436  threads until creating a log checkpoint at the
2437  end of create_log_files(). */
2438  ut_d(recv_no_log_write = TRUE);
2440 
2441  RECOVERY_CRASH(3);
2442 
2443  /* Stamp the LSN to the data files. */
2445  max_flushed_lsn, 0);
2446 
2448 
2449  RECOVERY_CRASH(4);
2450 
2451  /* Close and free the redo log files, so that
2452  we can replace them. */
2453  fil_close_log_files(true);
2454 
2455  RECOVERY_CRASH(5);
2456 
2457  /* Free the old log file space. */
2459 
2460  ib_logf(IB_LOG_LEVEL_WARN,
2461  "Starting to delete and rewrite log files.");
2462 
2463  srv_log_file_size = srv_log_file_size_requested;
2464 
2465  err = create_log_files(create_new_db, logfilename,
2466  dirnamelen, max_flushed_lsn,
2467  logfile0);
2468 
2469  if (err != DB_SUCCESS) {
2470  return(err);
2471  }
2472 
2473  create_log_files_rename(logfilename, dirnamelen,
2474  max_flushed_lsn, logfile0);
2475  }
2476 
2479 
2480  /* It is possible that file_format tag has never
2481  been set. In this case we initialize it to minimum
2482  value. Important to note that we can do it ONLY after
2483  we have finished the recovery process so that the
2484  image of TRX_SYS_PAGE_NO is not stale. */
2486  }
2487 
2488  if (!create_new_db && sum_of_new_sizes > 0) {
2489  /* New data file(s) were added */
2490  mtr_start(&mtr);
2491 
2492  fsp_header_inc_size(0, sum_of_new_sizes, &mtr);
2493 
2494  mtr_commit(&mtr);
2495 
2496  /* Immediately write the log record about increased tablespace
2497  size to disk, so that it is durable even if mysqld would crash
2498  quickly */
2499 
2501  }
2502 
2503 #ifdef UNIV_LOG_ARCHIVE
2504  /* Archiving is always off under MySQL */
2505  if (!srv_log_archive_on) {
2506  ut_a(DB_SUCCESS == log_archive_noarchivelog());
2507  } else {
2508  mutex_enter(&(log_sys->mutex));
2509 
2510  start_archive = FALSE;
2511 
2512  if (log_sys->archiving_state == LOG_ARCH_OFF) {
2513  start_archive = TRUE;
2514  }
2515 
2516  mutex_exit(&(log_sys->mutex));
2517 
2518  if (start_archive) {
2519  ut_a(DB_SUCCESS == log_archive_archivelog());
2520  }
2521  }
2522 #endif /* UNIV_LOG_ARCHIVE */
2523 
2524  /* fprintf(stderr, "Max allowed record size %lu\n",
2525  page_get_free_space_of_empty() / 2); */
2526 
2527  if (buf_dblwr == NULL) {
2528  /* Create the doublewrite buffer to a new tablespace */
2529 
2530  buf_dblwr_create();
2531  }
2532 
2533  /* Here the double write buffer has already been created and so
2534  any new rollback segments will be allocated after the double
2535  write buffer. The default segment should already exist.
2536  We create the new segments only if it's a new database or
2537  the database was shutdown cleanly. */
2538 
2539  /* Note: When creating the extra rollback segments during an upgrade
2540  we violate the latching order, even if the change buffer is empty.
2541  We make an exception in sync0sync.cc and check srv_is_being_started
2542  for that violation. It cannot create a deadlock because we are still
2543  running in single threaded mode essentially. Only the IO threads
2544  should be running at this stage. */
2545 
2546  ut_a(srv_undo_logs > 0);
2547  ut_a(srv_undo_logs <= TRX_SYS_N_RSEGS);
2548 
2549  /* The number of rsegs that exist in InnoDB is given by status
2550  variable srv_available_undo_logs. The number of rsegs to use can
2551  be set using the dynamic global variable srv_undo_logs. */
2552 
2553  srv_available_undo_logs = trx_sys_create_rsegs(
2554  srv_undo_tablespaces, srv_undo_logs);
2555 
2556  if (srv_available_undo_logs == ULINT_UNDEFINED) {
2557  /* Can only happen if force recovery is set. */
2559  || srv_read_only_mode);
2560  srv_undo_logs = ULONG_UNDEFINED;
2561  }
2562 
2563  /* Flush the changes made to TRX_SYS_PAGE by trx_sys_create_rsegs()*/
2565  bool success = buf_flush_list(ULINT_MAX, LSN_MAX, NULL);
2566  ut_a(success);
2568  }
2569 
2570  if (!srv_read_only_mode) {
2571  /* Create the thread which watches the timeouts
2572  for lock waits */
2573  os_thread_create(
2575  NULL, thread_ids + 2 + SRV_MAX_N_IO_THREADS);
2576 
2577  /* Create the thread which warns of long semaphore waits */
2578  os_thread_create(
2580  NULL, thread_ids + 3 + SRV_MAX_N_IO_THREADS);
2581 
2582  /* Create the thread which prints InnoDB monitor info */
2583  os_thread_create(
2585  NULL, thread_ids + 4 + SRV_MAX_N_IO_THREADS);
2586  }
2587 
2588  /* Create the SYS_FOREIGN and SYS_FOREIGN_COLS system tables */
2590  if (err != DB_SUCCESS) {
2591  return(err);
2592  }
2593 
2594  /* Create the SYS_TABLESPACES system table */
2596  if (err != DB_SUCCESS) {
2597  return(err);
2598  }
2599 
2600  srv_is_being_started = FALSE;
2601 
2603 
2604  /* Create the master thread which does purge and other utility
2605  operations */
2606 
2607  if (!srv_read_only_mode) {
2608 
2609  os_thread_create(
2611  NULL, thread_ids + (1 + SRV_MAX_N_IO_THREADS));
2612  }
2613 
2614  if (!srv_read_only_mode
2616 
2617  os_thread_create(
2619  NULL, thread_ids + 5 + SRV_MAX_N_IO_THREADS);
2620 
2621  ut_a(UT_ARR_SIZE(thread_ids)
2622  > 5 + srv_n_purge_threads + SRV_MAX_N_IO_THREADS);
2623 
2624  /* We've already created the purge coordinator thread above. */
2625  for (i = 1; i < srv_n_purge_threads; ++i) {
2626  os_thread_create(
2627  srv_worker_thread, NULL,
2628  thread_ids + 5 + i + SRV_MAX_N_IO_THREADS);
2629  }
2630 
2631  srv_start_wait_for_purge_to_start();
2632 
2633  } else {
2635  }
2636 
2637  if (!srv_read_only_mode) {
2638  os_thread_create(buf_flush_page_cleaner_thread, NULL, NULL);
2639  }
2640 
2641 #ifdef UNIV_DEBUG
2642  /* buf_debug_prints = TRUE; */
2643 #endif /* UNIV_DEBUG */
2644  sum_of_data_file_sizes = 0;
2645 
2646  for (i = 0; i < srv_n_data_files; i++) {
2647  sum_of_data_file_sizes += srv_data_file_sizes[i];
2648  }
2649 
2650  tablespace_size_in_header = fsp_header_get_tablespace_size();
2651 
2652  if (!srv_read_only_mode
2653  && !srv_auto_extend_last_data_file
2654  && sum_of_data_file_sizes != tablespace_size_in_header) {
2655 
2656  ut_print_timestamp(stderr);
2657  fprintf(stderr,
2658  " InnoDB: Error: tablespace size"
2659  " stored in header is %lu pages, but\n",
2660  (ulong) tablespace_size_in_header);
2661  ut_print_timestamp(stderr);
2662  fprintf(stderr,
2663  "InnoDB: the sum of data file sizes is %lu pages\n",
2664  (ulong) sum_of_data_file_sizes);
2665 
2666  if (srv_force_recovery == 0
2667  && sum_of_data_file_sizes < tablespace_size_in_header) {
2668  /* This is a fatal error, the tail of a tablespace is
2669  missing */
2670 
2671  ut_print_timestamp(stderr);
2672  fprintf(stderr,
2673  " InnoDB: Cannot start InnoDB."
2674  " The tail of the system tablespace is\n");
2675  ut_print_timestamp(stderr);
2676  fprintf(stderr,
2677  " InnoDB: missing. Have you edited"
2678  " innodb_data_file_path in my.cnf in an\n");
2679  ut_print_timestamp(stderr);
2680  fprintf(stderr,
2681  " InnoDB: inappropriate way, removing"
2682  " ibdata files from there?\n");
2683  ut_print_timestamp(stderr);
2684  fprintf(stderr,
2685  " InnoDB: You can set innodb_force_recovery=1"
2686  " in my.cnf to force\n");
2687  ut_print_timestamp(stderr);
2688  fprintf(stderr,
2689  " InnoDB: a startup if you are trying"
2690  " to recover a badly corrupt database.\n");
2691 
2692  return(DB_ERROR);
2693  }
2694  }
2695 
2696  if (!srv_read_only_mode
2697  && srv_auto_extend_last_data_file
2698  && sum_of_data_file_sizes < tablespace_size_in_header) {
2699 
2700  ut_print_timestamp(stderr);
2701  fprintf(stderr,
2702  " InnoDB: Error: tablespace size stored in header"
2703  " is %lu pages, but\n",
2704  (ulong) tablespace_size_in_header);
2705  ut_print_timestamp(stderr);
2706  fprintf(stderr,
2707  " InnoDB: the sum of data file sizes"
2708  " is only %lu pages\n",
2709  (ulong) sum_of_data_file_sizes);
2710 
2711  if (srv_force_recovery == 0) {
2712 
2713  ut_print_timestamp(stderr);
2714  fprintf(stderr,
2715  " InnoDB: Cannot start InnoDB. The tail of"
2716  " the system tablespace is\n");
2717  ut_print_timestamp(stderr);
2718  fprintf(stderr,
2719  " InnoDB: missing. Have you edited"
2720  " innodb_data_file_path in my.cnf in an\n");
2721  ut_print_timestamp(stderr);
2722  fprintf(stderr,
2723  " InnoDB: inappropriate way, removing"
2724  " ibdata files from there?\n");
2725  ut_print_timestamp(stderr);
2726  fprintf(stderr,
2727  " InnoDB: You can set innodb_force_recovery=1"
2728  " in my.cnf to force\n");
2729  ut_print_timestamp(stderr);
2730  fprintf(stderr,
2731  " InnoDB: a startup if you are trying to"
2732  " recover a badly corrupt database.\n");
2733 
2734  return(DB_ERROR);
2735  }
2736  }
2737 
2738  /* Check that os_fast_mutexes work as expected */
2739  os_fast_mutex_init(PFS_NOT_INSTRUMENTED, &srv_os_test_mutex);
2740 
2741  if (0 != os_fast_mutex_trylock(&srv_os_test_mutex)) {
2742  ut_print_timestamp(stderr);
2743  fprintf(stderr,
2744  " InnoDB: Error: pthread_mutex_trylock returns"
2745  " an unexpected value on\n");
2746  ut_print_timestamp(stderr);
2747  fprintf(stderr,
2748  " InnoDB: success! Cannot continue.\n");
2749  exit(1);
2750  }
2751 
2752  os_fast_mutex_unlock(&srv_os_test_mutex);
2753 
2754  os_fast_mutex_lock(&srv_os_test_mutex);
2755 
2756  os_fast_mutex_unlock(&srv_os_test_mutex);
2757 
2758  os_fast_mutex_free(&srv_os_test_mutex);
2759 
2760  if (srv_print_verbose_log) {
2761  ib_logf(IB_LOG_LEVEL_INFO,
2762  "%s started; log sequence number " LSN_PF "",
2763  INNODB_VERSION_STR, srv_start_lsn);
2764  }
2765 
2766  if (srv_force_recovery > 0) {
2767  ib_logf(IB_LOG_LEVEL_INFO,
2768  "!!! innodb_force_recovery is set to %lu !!!",
2769  (ulong) srv_force_recovery);
2770  }
2771 
2772  if (srv_force_recovery == 0) {
2773  /* In the insert buffer we may have even bigger tablespace
2774  id's, because we may have dropped those tablespaces, but
2775  insert buffer merge has not had time to clean the records from
2776  the ibuf tree. */
2777 
2779  }
2780 
2781  if (!srv_read_only_mode) {
2782  /* Create the buffer pool dump/load thread */
2783  os_thread_create(buf_dump_thread, NULL, NULL);
2784 
2785  /* Create the dict stats gathering thread */
2786  os_thread_create(dict_stats_thread, NULL, NULL);
2787 
2788  /* Create the thread that will optimize the FTS sub-system. */
2790  }
2791 
2792  srv_was_started = TRUE;
2793 
2794  return(DB_SUCCESS);
2795 }
2796 
2797 #if 0
2798 /********************************************************************
2799 Sync all FTS cache before shutdown */
2800 static
2801 void
2802 srv_fts_close(void)
2803 /*===============*/
2804 {
2806 
2807  for (table = UT_LIST_GET_FIRST(dict_sys->table_LRU);
2808  table; table = UT_LIST_GET_NEXT(table_LRU, table)) {
2809  fts_t* fts = table->fts;
2810 
2811  if (fts != NULL) {
2812  fts_sync_table(table);
2813  }
2814  }
2815 
2816  for (table = UT_LIST_GET_FIRST(dict_sys->table_non_LRU);
2817  table; table = UT_LIST_GET_NEXT(table_LRU, table)) {
2818  fts_t* fts = table->fts;
2819 
2820  if (fts != NULL) {
2821  fts_sync_table(table);
2822  }
2823  }
2824 }
2825 #endif
2826 
2827 /****************************************************************/
2830 UNIV_INTERN
2831 dberr_t
2833 /*=============================*/
2834 {
2835  ulint i;
2836 
2837  if (!srv_was_started) {
2838  if (srv_is_being_started) {
2839  ib_logf(IB_LOG_LEVEL_WARN,
2840  "Shutting down an improperly started, "
2841  "or created database!");
2842  }
2843 
2844  return(DB_SUCCESS);
2845  }
2846 
2847  if (!srv_read_only_mode) {
2848  /* Shutdown the FTS optimize sub system. */
2850 
2851  fts_optimize_end();
2852  }
2853 
2854  /* 1. Flush the buffer pool to disk, write the current lsn to
2855  the tablespace header(s), and copy all log data to archive.
2856  The step 1 is the real InnoDB shutdown. The remaining steps 2 - ...
2857  just free data structures after the shutdown. */
2858 
2860 
2861  if (srv_conc_get_active_threads() != 0) {
2862  ib_logf(IB_LOG_LEVEL_WARN,
2863  "Query counter shows %ld queries still "
2864  "inside InnoDB at shutdown",
2866  }
2867 
2868  /* 2. Make all threads created by InnoDB to exit */
2869 
2871 
2872  /* All threads end up waiting for certain events. Put those events
2873  to the signaled state. Then the threads will exit themselves after
2874  os_event_wait(). */
2875 
2876  for (i = 0; i < 1000; i++) {
2877  /* NOTE: IF YOU CREATE THREADS IN INNODB, YOU MUST EXIT THEM
2878  HERE OR EARLIER */
2879 
2880  if (!srv_read_only_mode) {
2881  /* a. Let the lock timeout thread exit */
2883 
2884  /* b. srv error monitor thread exits automatically,
2885  no need to do anything here */
2886 
2887  /* c. We wake the master thread so that it exits */
2889 
2890  /* d. Wakeup purge threads. */
2891  srv_purge_wakeup();
2892  }
2893 
2894  /* e. Exit the i/o threads */
2895 
2897 
2898  /* f. dict_stats_thread is signaled from
2899  logs_empty_and_mark_files_at_shutdown() and should have
2900  already quit or is quitting right now. */
2901 
2903 
2904  if (os_thread_count == 0) {
2905  /* All the threads have exited or are just exiting;
2906  NOTE that the threads may not have completed their
2907  exit yet. Should we use pthread_join() to make sure
2908  they have exited? If we did, we would have to
2909  remove the pthread_detach() from
2910  os_thread_exit(). Now we just sleep 0.1
2911  seconds and hope that is enough! */
2912 
2914 
2915  os_thread_sleep(100000);
2916 
2917  break;
2918  }
2919 
2921 
2922  os_thread_sleep(100000);
2923  }
2924 
2925  if (i == 1000) {
2926  ib_logf(IB_LOG_LEVEL_WARN,
2927  "%lu threads created by InnoDB"
2928  " had not exited at shutdown!",
2929  (ulong) os_thread_count);
2930  }
2931 
2932  if (srv_monitor_file) {
2933  fclose(srv_monitor_file);
2934  srv_monitor_file = 0;
2935  if (srv_monitor_file_name) {
2936  unlink(srv_monitor_file_name);
2937  mem_free(srv_monitor_file_name);
2938  }
2939  }
2940 
2941  if (srv_dict_tmpfile) {
2942  fclose(srv_dict_tmpfile);
2943  srv_dict_tmpfile = 0;
2944  }
2945 
2946  if (srv_misc_tmpfile) {
2947  fclose(srv_misc_tmpfile);
2948  srv_misc_tmpfile = 0;
2949  }
2950 
2951  if (!srv_read_only_mode) {
2953  }
2954 
2955  /* This must be disabled before closing the buffer pool
2956  and closing the data dictionary. */
2958 
2959  ibuf_close();
2960  log_shutdown();
2961  lock_sys_close();
2963  trx_sys_close();
2964 
2965  /* We don't create these mutexes in RO mode because we don't create
2966  the temp files that the cover. */
2967  if (!srv_read_only_mode) {
2968  mutex_free(&srv_monitor_file_mutex);
2969  mutex_free(&srv_dict_tmpfile_mutex);
2970  mutex_free(&srv_misc_tmpfile_mutex);
2971  }
2972 
2973  dict_close();
2975 
2976  /* 3. Free all InnoDB's own mutexes and the os_fast_mutexes inside
2977  them */
2978  os_aio_free();
2979  que_close();
2980  row_mysql_close();
2981  srv_mon_free();
2982  sync_close();
2983  srv_free();
2984  fil_close();
2985 
2986  /* 4. Free the os_conc_mutex and all os_events and os_mutexes */
2987 
2988  os_sync_free();
2989 
2990  /* 5. Free all allocated memory */
2991 
2992  pars_lexer_close();
2993  log_mem_free();
2994  buf_pool_free(srv_buf_pool_instances);
2995  mem_close();
2996 
2997  /* ut_free_all_mem() frees all allocated memory not freed yet
2998  in shutdown, and it will also free the ut_list_mutex, so it
2999  should be the last one for all operation */
3000  ut_free_all_mem();
3001 
3002  if (os_thread_count != 0
3003  || os_event_count != 0
3004  || os_mutex_count != 0
3005  || os_fast_mutex_count != 0) {
3006  ib_logf(IB_LOG_LEVEL_WARN,
3007  "Some resources were not cleaned up in shutdown: "
3008  "threads %lu, events %lu, os_mutexes %lu, "
3009  "os_fast_mutexes %lu",
3010  (ulong) os_thread_count, (ulong) os_event_count,
3011  (ulong) os_mutex_count, (ulong) os_fast_mutex_count);
3012  }
3013 
3014  if (dict_foreign_err_file) {
3015  fclose(dict_foreign_err_file);
3016  }
3017 
3018  if (srv_print_verbose_log) {
3019  ib_logf(IB_LOG_LEVEL_INFO,
3020  "Shutdown completed; log sequence number " LSN_PF "",
3022  }
3023 
3024  srv_was_started = FALSE;
3025  srv_start_has_been_called = FALSE;
3026 
3027  return(DB_SUCCESS);
3028 }
3029 #endif /* !UNIV_HOTBACKUP */
3030 
3031 
3032 /********************************************************************
3033 Signal all per-table background threads to shutdown, and wait for them to do
3034 so. */
3035 UNIV_INTERN
3036 void
3037 srv_shutdown_table_bg_threads(void)
3038 /*===============================*/
3039 {
3041  dict_table_t* first;
3042  dict_table_t* last = NULL;
3043 
3044  mutex_enter(&dict_sys->mutex);
3045 
3046  /* Signal all threads that they should stop. */
3048  first = table;
3049  while (table) {
3050  dict_table_t* next;
3051  fts_t* fts = table->fts;
3052 
3053  if (fts != NULL) {
3054  fts_start_shutdown(table, fts);
3055  }
3056 
3057  next = UT_LIST_GET_NEXT(table_LRU, table);
3058 
3059  if (!next) {
3060  last = table;
3061  }
3062 
3063  table = next;
3064  }
3065 
3066  /* We must release dict_sys->mutex here; if we hold on to it in the
3067  loop below, we will deadlock if any of the background threads try to
3068  acquire it (for example, the FTS thread by calling que_eval_sql).
3069 
3070  Releasing it here and going through dict_sys->table_LRU without
3071  holding it is safe because:
3072 
3073  a) MySQL only starts the shutdown procedure after all client
3074  threads have been disconnected and no new ones are accepted, so no
3075  new tables are added or old ones dropped.
3076 
3077  b) Despite its name, the list is not LRU, and the order stays
3078  fixed.
3079 
3080  To safeguard against the above assumptions ever changing, we store
3081  the first and last items in the list above, and then check that
3082  they've stayed the same below. */
3083 
3084  mutex_exit(&dict_sys->mutex);
3085 
3086  /* Wait for the threads of each table to stop. This is not inside
3087  the above loop, because by signaling all the threads first we can
3088  overlap their shutting down delays. */
3090  ut_a(first == table);
3091  while (table) {
3092  dict_table_t* next;
3093  fts_t* fts = table->fts;
3094 
3095  if (fts != NULL) {
3096  fts_shutdown(table, fts);
3097  }
3098 
3099  next = UT_LIST_GET_NEXT(table_LRU, table);
3100 
3101  if (table == last) {
3102  ut_a(!next);
3103  }
3104 
3105  table = next;
3106  }
3107 }
3108 
3109 /*****************************************************************/
3111 UNIV_INTERN
3112 void
3114 /*=======================*/
3115  dict_table_t* table,
3116  char* filename,
3117  ulint max_len)
3118 {
3119  ulint len;
3120  char* path;
3121  char* suffix;
3122  static const ulint suffix_len = strlen(".cfg");
3123 
3124  if (DICT_TF_HAS_DATA_DIR(table->flags)) {
3125  dict_get_and_save_data_dir_path(table, false);
3126  ut_a(table->data_dir_path);
3127 
3129  table->data_dir_path, table->name, "cfg");
3130  } else {
3131  path = fil_make_ibd_name(table->name, false);
3132  }
3133 
3134  ut_a(path);
3135  len = ut_strlen(path);
3136  ut_a(max_len >= len);
3137 
3138  suffix = path + (len - suffix_len);
3139  if (strncmp(suffix, ".cfg", suffix_len) == 0) {
3140  strcpy(filename, path);
3141  } else {
3142  ut_ad(strncmp(suffix, ".ibd", suffix_len) == 0);
3143 
3144  strncpy(filename, path, len - suffix_len);
3145  suffix = filename + (len - suffix_len);
3146  strcpy(suffix, ".cfg");
3147  }
3148 
3149  mem_free(path);
3150 
3151  srv_normalize_path_for_win(filename);
3152 }