This repository has been archived by the owner on Oct 11, 2018. It is now read-only.
/
srv0start.cc
3182 lines (2518 loc) · 81 KB
/
srv0start.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2008, Google Inc.
Copyright (c) 2009, Percona Inc.
Portions of this file contain modifications contributed and copyrighted by
Google, Inc. Those modifications are gratefully acknowledged and are described
briefly in the InnoDB documentation. The contributions by Google are
incorporated with their permission, and subject to the conditions contained in
the file COPYING.Google.
Portions of this file contain modifications contributed and copyrighted
by Percona Inc.. Those modifications are
gratefully acknowledged and are described briefly in the InnoDB
documentation. The contributions by Percona Inc. are incorporated with
their permission, and subject to the conditions contained in the file
COPYING.Percona.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/********************************************************************//**
@file srv/srv0start.cc
Starts the InnoDB database server
Created 2/16/1996 Heikki Tuuri
*************************************************************************/
#include "mysqld.h"
#include "pars0pars.h"
#include "row0ftsort.h"
#include "ut0mem.h"
#include "mem0mem.h"
#include "data0data.h"
#include "data0type.h"
#include "dict0dict.h"
#include "buf0buf.h"
#include "buf0dump.h"
#include "os0file.h"
#include "os0thread.h"
#include "fil0fil.h"
#include "fsp0fsp.h"
#include "rem0rec.h"
#include "mtr0mtr.h"
#include "log0log.h"
#include "log0recv.h"
#include "page0page.h"
#include "page0cur.h"
#include "trx0trx.h"
#include "trx0sys.h"
#include "btr0btr.h"
#include "btr0cur.h"
#include "rem0rec.h"
#include "ibuf0ibuf.h"
#include "srv0start.h"
#include "srv0srv.h"
#ifndef UNIV_HOTBACKUP
# include "trx0rseg.h"
# include "os0proc.h"
# include "sync0sync.h"
# include "buf0flu.h"
# include "buf0rea.h"
# include "dict0boot.h"
# include "dict0load.h"
# include "dict0stats_bg.h"
# include "que0que.h"
# include "usr0sess.h"
# include "lock0lock.h"
# include "trx0roll.h"
# include "trx0purge.h"
# include "lock0lock.h"
# include "pars0pars.h"
# include "btr0sea.h"
# include "rem0cmp.h"
# include "dict0crea.h"
# include "row0ins.h"
# include "row0sel.h"
# include "row0upd.h"
# include "row0row.h"
# include "row0mysql.h"
# include "btr0pcur.h"
# include "os0sync.h"
# include "zlib.h"
# include "ut0crc32.h"
/** Log sequence number immediately after startup */
UNIV_INTERN lsn_t srv_start_lsn;
/** Log sequence number at shutdown */
UNIV_INTERN lsn_t srv_shutdown_lsn;
#ifdef HAVE_DARWIN_THREADS
# include <sys/utsname.h>
/** TRUE if the F_FULLFSYNC option is available */
UNIV_INTERN ibool srv_have_fullfsync = FALSE;
#endif
/** TRUE if a raw partition is in use */
UNIV_INTERN ibool srv_start_raw_disk_in_use = FALSE;
/** TRUE if the server is being started, before rolling back any
incomplete transactions */
UNIV_INTERN ibool srv_startup_is_before_trx_rollback_phase = FALSE;
/** TRUE if the server is being started */
UNIV_INTERN ibool srv_is_being_started = FALSE;
/** TRUE if the server was successfully started */
UNIV_INTERN ibool srv_was_started = FALSE;
/** TRUE if innobase_start_or_create_for_mysql() has been called */
static ibool srv_start_has_been_called = FALSE;
/** At a shutdown this value climbs from SRV_SHUTDOWN_NONE to
SRV_SHUTDOWN_CLEANUP and then to SRV_SHUTDOWN_LAST_PHASE, and so on */
UNIV_INTERN enum srv_shutdown_state srv_shutdown_state = SRV_SHUTDOWN_NONE;
/** Files comprising the system tablespace */
static os_file_t files[1000];
/** io_handler_thread parameters for thread identification */
static ulint n[SRV_MAX_N_IO_THREADS + 6];
/** io_handler_thread identifiers, 32 is the maximum number of purge threads */
static os_thread_id_t thread_ids[SRV_MAX_N_IO_THREADS + 6 + 32];
/** We use this mutex to test the return value of pthread_mutex_trylock
on successful locking. HP-UX does NOT return 0, though Linux et al do. */
static os_fast_mutex_t srv_os_test_mutex;
/** Name of srv_monitor_file */
static char* srv_monitor_file_name;
#endif /* !UNIV_HOTBACKUP */
/** Default undo tablespace size in UNIV_PAGEs count (10MB). */
static const ulint SRV_UNDO_TABLESPACE_SIZE_IN_PAGES =
((1024 * 1024) * 10) / UNIV_PAGE_SIZE_DEF;
/** */
#define SRV_N_PENDING_IOS_PER_THREAD OS_AIO_N_PENDING_IOS_PER_THREAD
#define SRV_MAX_N_PENDING_SYNC_IOS 100
#ifdef UNIV_PFS_THREAD
/* Keys to register InnoDB threads with performance schema */
UNIV_INTERN mysql_pfs_key_t io_handler_thread_key;
UNIV_INTERN mysql_pfs_key_t srv_lock_timeout_thread_key;
UNIV_INTERN mysql_pfs_key_t srv_error_monitor_thread_key;
UNIV_INTERN mysql_pfs_key_t srv_monitor_thread_key;
UNIV_INTERN mysql_pfs_key_t srv_master_thread_key;
UNIV_INTERN mysql_pfs_key_t srv_purge_thread_key;
#endif /* UNIV_PFS_THREAD */
/*********************************************************************//**
Convert a numeric string that optionally ends in G or M, to a number
containing megabytes.
@return next character in string */
static
char*
srv_parse_megabytes(
/*================*/
char* str, /*!< in: string containing a quantity in bytes */
ulint* megs) /*!< out: the number in megabytes */
{
char* endp;
ulint size;
size = strtoul(str, &endp, 10);
str = endp;
switch (*str) {
case 'G': case 'g':
size *= 1024;
/* fall through */
case 'M': case 'm':
str++;
break;
default:
size /= 1024 * 1024;
break;
}
*megs = size;
return(str);
}
/*********************************************************************//**
Check if a file can be opened in read-write mode.
@return true if it doesn't exist or can be opened in rw mode. */
static
bool
srv_file_check_mode(
/*================*/
const char* name) /*!< in: filename to check */
{
os_file_stat_t stat;
memset(&stat, 0x0, sizeof(stat));
dberr_t err = os_file_get_status(name, &stat, true);
if (err == DB_FAIL) {
ib_logf(IB_LOG_LEVEL_ERROR,
"os_file_get_status() failed on '%s'. Can't determine "
"file permissions", name);
return(false);
} else if (err == DB_SUCCESS) {
/* Note: stat.rw_perm is only valid of files */
if (stat.type == OS_FILE_TYPE_FILE
|| stat.type == OS_FILE_TYPE_BLOCK) {
if (!stat.rw_perm) {
ib_logf(IB_LOG_LEVEL_ERROR,
"%s can't be opened in %s mode",
name,
srv_read_only_mode
? "read" : "read-write");
return(false);
}
} else {
/* Not a regular file, bail out. */
ib_logf(IB_LOG_LEVEL_ERROR,
"'%s' not a regular file.", name);
return(false);
}
} else {
/* This is OK. If the file create fails on RO media, there
is nothing we can do. */
ut_a(err == DB_NOT_FOUND);
}
return(true);
}
/*********************************************************************//**
Reads the data files and their sizes from a character string given in
the .cnf file.
@return TRUE if ok, FALSE on parse error */
UNIV_INTERN
ibool
srv_parse_data_file_paths_and_sizes(
/*================================*/
char* str) /*!< in/out: the data file path string */
{
char* input_str;
char* path;
ulint size;
ulint i = 0;
srv_auto_extend_last_data_file = FALSE;
srv_last_file_size_max = 0;
srv_data_file_names = NULL;
srv_data_file_sizes = NULL;
srv_data_file_is_raw_partition = NULL;
input_str = str;
/* First calculate the number of data files and check syntax:
path:size[M | G];path:size[M | G]... . Note that a Windows path may
contain a drive name and a ':'. */
while (*str != '\0') {
path = str;
while ((*str != ':' && *str != '\0')
|| (*str == ':'
&& (*(str + 1) == '\\' || *(str + 1) == '/'
|| *(str + 1) == ':'))) {
str++;
}
if (*str == '\0') {
return(FALSE);
}
str++;
str = srv_parse_megabytes(str, &size);
if (0 == strncmp(str, ":autoextend",
(sizeof ":autoextend") - 1)) {
str += (sizeof ":autoextend") - 1;
if (0 == strncmp(str, ":max:",
(sizeof ":max:") - 1)) {
str += (sizeof ":max:") - 1;
str = srv_parse_megabytes(str, &size);
}
if (*str != '\0') {
return(FALSE);
}
}
if (strlen(str) >= 6
&& *str == 'n'
&& *(str + 1) == 'e'
&& *(str + 2) == 'w') {
str += 3;
}
if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
str += 3;
}
if (size == 0) {
return(FALSE);
}
i++;
if (*str == ';') {
str++;
} else if (*str != '\0') {
return(FALSE);
}
}
if (i == 0) {
/* If innodb_data_file_path was defined it must contain
at least one data file definition */
return(FALSE);
}
srv_data_file_names = static_cast<char**>(
malloc(i * sizeof *srv_data_file_names));
srv_data_file_sizes = static_cast<ulint*>(
malloc(i * sizeof *srv_data_file_sizes));
srv_data_file_is_raw_partition = static_cast<ulint*>(
malloc(i * sizeof *srv_data_file_is_raw_partition));
srv_n_data_files = i;
/* Then store the actual values to our arrays */
str = input_str;
i = 0;
while (*str != '\0') {
path = str;
/* Note that we must step over the ':' in a Windows path;
a Windows path normally looks like C:\ibdata\ibdata1:1G, but
a Windows raw partition may have a specification like
\\.\C::1Gnewraw or \\.\PHYSICALDRIVE2:1Gnewraw */
while ((*str != ':' && *str != '\0')
|| (*str == ':'
&& (*(str + 1) == '\\' || *(str + 1) == '/'
|| *(str + 1) == ':'))) {
str++;
}
if (*str == ':') {
/* Make path a null-terminated string */
*str = '\0';
str++;
}
str = srv_parse_megabytes(str, &size);
srv_data_file_names[i] = path;
srv_data_file_sizes[i] = size;
if (0 == strncmp(str, ":autoextend",
(sizeof ":autoextend") - 1)) {
srv_auto_extend_last_data_file = TRUE;
str += (sizeof ":autoextend") - 1;
if (0 == strncmp(str, ":max:",
(sizeof ":max:") - 1)) {
str += (sizeof ":max:") - 1;
str = srv_parse_megabytes(
str, &srv_last_file_size_max);
}
if (*str != '\0') {
return(FALSE);
}
}
(srv_data_file_is_raw_partition)[i] = 0;
if (strlen(str) >= 6
&& *str == 'n'
&& *(str + 1) == 'e'
&& *(str + 2) == 'w') {
str += 3;
(srv_data_file_is_raw_partition)[i] = SRV_NEW_RAW;
}
if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
str += 3;
if ((srv_data_file_is_raw_partition)[i] == 0) {
(srv_data_file_is_raw_partition)[i] = SRV_OLD_RAW;
}
}
i++;
if (*str == ';') {
str++;
}
}
return(TRUE);
}
/*********************************************************************//**
Frees the memory allocated by srv_parse_data_file_paths_and_sizes()
and srv_parse_log_group_home_dirs(). */
UNIV_INTERN
void
srv_free_paths_and_sizes(void)
/*==========================*/
{
free(srv_data_file_names);
srv_data_file_names = NULL;
free(srv_data_file_sizes);
srv_data_file_sizes = NULL;
free(srv_data_file_is_raw_partition);
srv_data_file_is_raw_partition = NULL;
}
#ifndef UNIV_HOTBACKUP
/********************************************************************//**
I/o-handler thread function.
@return OS_THREAD_DUMMY_RETURN */
extern "C" UNIV_INTERN
os_thread_ret_t
DECLARE_THREAD(io_handler_thread)(
/*==============================*/
void* arg) /*!< in: pointer to the number of the segment in
the aio array */
{
ulint segment;
segment = *((ulint*) arg);
#ifdef UNIV_DEBUG_THREAD_CREATION
fprintf(stderr, "Io handler thread %lu starts, id %lu\n", segment,
os_thread_pf(os_thread_get_curr_id()));
#endif
#ifdef UNIV_PFS_THREAD
pfs_register_thread(io_handler_thread_key);
#endif /* UNIV_PFS_THREAD */
while (srv_shutdown_state != SRV_SHUTDOWN_EXIT_THREADS) {
fil_aio_wait(segment);
}
/* We count the number of threads in os_thread_exit(). A created
thread should always use that to exit and not use return() to exit.
The thread actually never comes here because it is exited in an
os_event_wait(). */
os_thread_exit(NULL);
OS_THREAD_DUMMY_RETURN;
}
#endif /* !UNIV_HOTBACKUP */
/*********************************************************************//**
Normalizes a directory path for Windows: converts slashes to backslashes. */
UNIV_INTERN
void
srv_normalize_path_for_win(
/*=======================*/
char* str __attribute__((unused))) /*!< in/out: null-terminated
character string */
{
#ifdef __WIN__
for (; *str; str++) {
if (*str == '/') {
*str = '\\';
}
}
#endif
}
#ifndef UNIV_HOTBACKUP
/*********************************************************************//**
Creates a log file.
@return DB_SUCCESS or error code */
static __attribute__((nonnull, warn_unused_result))
dberr_t
create_log_file(
/*============*/
os_file_t* file, /*!< out: file handle */
const char* name) /*!< in: log file name */
{
ibool ret;
*file = os_file_create(
innodb_file_log_key, name,
OS_FILE_CREATE|OS_FILE_ON_ERROR_NO_EXIT, OS_FILE_NORMAL,
OS_LOG_FILE, &ret);
if (!ret) {
ib_logf(IB_LOG_LEVEL_ERROR, "Cannot create %s", name);
return(DB_ERROR);
}
ib_logf(IB_LOG_LEVEL_INFO,
"Setting log file %s size to %lu MB",
name, (ulong) srv_log_file_size
>> (20 - UNIV_PAGE_SIZE_SHIFT));
ret = os_file_set_size(name, *file,
(os_offset_t) srv_log_file_size
<< UNIV_PAGE_SIZE_SHIFT);
if (!ret) {
ib_logf(IB_LOG_LEVEL_ERROR, "Cannot set log file"
" %s to size %lu MB", name, (ulong) srv_log_file_size
>> (20 - UNIV_PAGE_SIZE_SHIFT));
return(DB_ERROR);
}
ret = os_file_close(*file);
ut_a(ret);
return(DB_SUCCESS);
}
/** Initial number of the first redo log file */
#define INIT_LOG_FILE0 (SRV_N_LOG_FILES_MAX + 1)
#ifdef DBUG_OFF
# define RECOVERY_CRASH(x) do {} while(0)
#else
# define RECOVERY_CRASH(x) do { \
if (srv_force_recovery_crash == x) { \
fprintf(stderr, "innodb_force_recovery_crash=%lu\n", \
srv_force_recovery_crash); \
fflush(stderr); \
exit(3); \
} \
} while (0)
#endif
/*********************************************************************//**
Creates all log files.
@return DB_SUCCESS or error code */
static
dberr_t
create_log_files(
/*=============*/
bool create_new_db, /*!< in: TRUE if new database is being
created */
char* logfilename, /*!< in/out: buffer for log file name */
size_t dirnamelen, /*!< in: length of the directory path */
lsn_t lsn, /*!< in: FIL_PAGE_FILE_FLUSH_LSN value */
char*& logfile0) /*!< out: name of the first log file */
{
if (srv_read_only_mode) {
ib_logf(IB_LOG_LEVEL_ERROR,
"Cannot create log files in read-only mode");
return(DB_READ_ONLY);
}
/* We prevent system tablespace creation with existing files in
data directory. So we do not delete log files when creating new system
tablespace */
if (!create_new_db) {
/* Remove any old log files. */
for (unsigned i = 0; i <= INIT_LOG_FILE0; i++) {
sprintf(logfilename + dirnamelen, "ib_logfile%u", i);
/* Ignore errors about non-existent files or files
that cannot be removed. The create_log_file() will
return an error when the file exists. */
#ifdef __WIN__
DeleteFile((LPCTSTR) logfilename);
#else
unlink(logfilename);
#endif
/* Crashing after deleting the first
file should be recoverable. The buffer
pool was clean, and we can simply create
all log files from the scratch. */
RECOVERY_CRASH(6);
}
}
ut_ad(!buf_pool_check_no_pending_io());
RECOVERY_CRASH(7);
for (unsigned i = 0; i < srv_n_log_files; i++) {
sprintf(logfilename + dirnamelen,
"ib_logfile%u", i ? i : INIT_LOG_FILE0);
dberr_t err = create_log_file(&files[i], logfilename);
if (err != DB_SUCCESS) {
return(err);
}
}
RECOVERY_CRASH(8);
/* We did not create the first log file initially as
ib_logfile0, so that crash recovery cannot find it until it
has been completed and renamed. */
sprintf(logfilename + dirnamelen, "ib_logfile%u", INIT_LOG_FILE0);
fil_space_create(
logfilename, SRV_LOG_SPACE_FIRST_ID,
fsp_flags_set_page_size(0, UNIV_PAGE_SIZE),
FIL_LOG);
ut_a(fil_validate());
logfile0 = fil_node_create(
logfilename, (ulint) srv_log_file_size,
SRV_LOG_SPACE_FIRST_ID, FALSE);
ut_a(logfile0);
for (unsigned i = 1; i < srv_n_log_files; i++) {
sprintf(logfilename + dirnamelen, "ib_logfile%u", i);
if (!fil_node_create(
logfilename,
(ulint) srv_log_file_size,
SRV_LOG_SPACE_FIRST_ID, FALSE)) {
ut_error;
}
}
log_group_init(0, srv_n_log_files,
srv_log_file_size * UNIV_PAGE_SIZE,
SRV_LOG_SPACE_FIRST_ID,
SRV_LOG_SPACE_FIRST_ID + 1);
fil_open_log_and_system_tablespace_files();
/* Create a log checkpoint. */
mutex_enter(&log_sys->mutex);
ut_d(recv_no_log_write = FALSE);
recv_reset_logs(lsn);
mutex_exit(&log_sys->mutex);
return(DB_SUCCESS);
}
/*********************************************************************//**
Renames the first log file. */
static
void
create_log_files_rename(
/*====================*/
char* logfilename, /*!< in/out: buffer for log file name */
size_t dirnamelen, /*!< in: length of the directory path */
lsn_t lsn, /*!< in: FIL_PAGE_FILE_FLUSH_LSN value */
char* logfile0) /*!< in/out: name of the first log file */
{
/* If innodb_flush_method=O_DSYNC,
we need to explicitly flush the log buffers. */
fil_flush(SRV_LOG_SPACE_FIRST_ID);
/* Close the log files, so that we can rename
the first one. */
fil_close_log_files(false);
/* Rename the first log file, now that a log
checkpoint has been created. */
sprintf(logfilename + dirnamelen, "ib_logfile%u", 0);
RECOVERY_CRASH(9);
ib_logf(IB_LOG_LEVEL_INFO,
"Renaming log file %s to %s", logfile0, logfilename);
mutex_enter(&log_sys->mutex);
ut_ad(strlen(logfile0) == 2 + strlen(logfilename));
ibool success = os_file_rename(
innodb_file_log_key, logfile0, logfilename);
ut_a(success);
RECOVERY_CRASH(10);
/* Replace the first file with ib_logfile0. */
strcpy(logfile0, logfilename);
mutex_exit(&log_sys->mutex);
fil_open_log_and_system_tablespace_files();
ib_logf(IB_LOG_LEVEL_WARN, "New log files created, LSN=" LSN_PF, lsn);
}
/*********************************************************************//**
Opens a log file.
@return DB_SUCCESS or error code */
static __attribute__((nonnull, warn_unused_result))
dberr_t
open_log_file(
/*==========*/
os_file_t* file, /*!< out: file handle */
const char* name, /*!< in: log file name */
os_offset_t* size) /*!< out: file size */
{
ibool ret;
*file = os_file_create(innodb_file_log_key, name,
OS_FILE_OPEN, OS_FILE_AIO,
OS_LOG_FILE, &ret);
if (!ret) {
ib_logf(IB_LOG_LEVEL_ERROR, "Unable to open '%s'", name);
return(DB_ERROR);
}
*size = os_file_get_size(*file);
ret = os_file_close(*file);
ut_a(ret);
return(DB_SUCCESS);
}
/*********************************************************************//**
Creates or opens database data files and closes them.
@return DB_SUCCESS or error code */
static __attribute__((nonnull, warn_unused_result))
dberr_t
open_or_create_data_files(
/*======================*/
ibool* create_new_db, /*!< out: TRUE if new database should be
created */
#ifdef UNIV_LOG_ARCHIVE
ulint* min_arch_log_no,/*!< out: min of archived log
numbers in data files */
ulint* max_arch_log_no,/*!< out: max of archived log
numbers in data files */
#endif /* UNIV_LOG_ARCHIVE */
lsn_t* min_flushed_lsn,/*!< out: min of flushed lsn
values in data files */
lsn_t* max_flushed_lsn,/*!< out: max of flushed lsn
values in data files */
ulint* sum_of_new_sizes)/*!< out: sum of sizes of the
new files added */
{
ibool ret;
ulint i;
ibool one_opened = FALSE;
ibool one_created = FALSE;
os_offset_t size;
ulint flags;
ulint space;
ulint rounded_size_pages;
char name[10000];
if (srv_n_data_files >= 1000) {
ib_logf(IB_LOG_LEVEL_ERROR,
"Can only have < 1000 data files, you have "
"defined %lu", (ulong) srv_n_data_files);
return(DB_ERROR);
}
*sum_of_new_sizes = 0;
*create_new_db = FALSE;
srv_normalize_path_for_win(srv_data_home);
for (i = 0; i < srv_n_data_files; i++) {
ulint dirnamelen;
srv_normalize_path_for_win(srv_data_file_names[i]);
dirnamelen = strlen(srv_data_home);
ut_a(dirnamelen + strlen(srv_data_file_names[i])
< (sizeof name) - 1);
memcpy(name, srv_data_home, dirnamelen);
/* Add a path separator if needed. */
if (dirnamelen && name[dirnamelen - 1] != SRV_PATH_SEPARATOR) {
name[dirnamelen++] = SRV_PATH_SEPARATOR;
}
strcpy(name + dirnamelen, srv_data_file_names[i]);
/* Note: It will return true if the file doesn' exist. */
if (!srv_file_check_mode(name)) {
return(DB_FAIL);
} else if (srv_data_file_is_raw_partition[i] == 0) {
/* First we try to create the file: if it already
exists, ret will get value FALSE */
files[i] = os_file_create(
innodb_file_data_key, name, OS_FILE_CREATE,
OS_FILE_NORMAL, OS_DATA_FILE, &ret);
if (srv_read_only_mode) {
if (ret) {
goto size_check;
}
ib_logf(IB_LOG_LEVEL_ERROR,
"Opening %s failed!", name);
return(DB_ERROR);
} else if (!ret
&& os_file_get_last_error(false)
!= OS_FILE_ALREADY_EXISTS
#ifdef UNIV_AIX
/* AIX 5.1 after security patch ML7 may have
errno set to 0 here, which causes our
function to return 100; work around that
AIX problem */
&& os_file_get_last_error(false) != 100
#endif /* UNIV_AIX */
) {
ib_logf(IB_LOG_LEVEL_ERROR,
"Creating or opening %s failed!",
name);
return(DB_ERROR);
}
} else if (srv_data_file_is_raw_partition[i] == SRV_NEW_RAW) {
ut_a(!srv_read_only_mode);
/* The partition is opened, not created; then it is
written over */
srv_start_raw_disk_in_use = TRUE;
srv_created_new_raw = TRUE;
files[i] = os_file_create(
innodb_file_data_key, name, OS_FILE_OPEN_RAW,
OS_FILE_NORMAL, OS_DATA_FILE, &ret);
if (!ret) {
ib_logf(IB_LOG_LEVEL_ERROR,
"Error in opening %s", name);
return(DB_ERROR);
}
} else if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
srv_start_raw_disk_in_use = TRUE;
ret = FALSE;
} else {
ut_a(0);
}
if (ret == FALSE) {
const char* check_msg;
/* We open the data file */
if (one_created) {
ib_logf(IB_LOG_LEVEL_ERROR,
"Data files can only be added at "
"the end of a tablespace, but "
"data file %s existed beforehand.",
name);
return(DB_ERROR);
}
if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
ut_a(!srv_read_only_mode);
files[i] = os_file_create(
innodb_file_data_key,
name, OS_FILE_OPEN_RAW,
OS_FILE_NORMAL, OS_DATA_FILE, &ret);
} else if (i == 0) {
files[i] = os_file_create(
innodb_file_data_key,
name, OS_FILE_OPEN_RETRY,
OS_FILE_NORMAL, OS_DATA_FILE, &ret);
} else {
files[i] = os_file_create(
innodb_file_data_key,
name, OS_FILE_OPEN, OS_FILE_NORMAL,
OS_DATA_FILE, &ret);
}
if (!ret) {
os_file_get_last_error(true);
ib_logf(IB_LOG_LEVEL_ERROR,
"Can't open '%s'", name);
return(DB_ERROR);
}
if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
goto skip_size_check;
}
size_check:
size = os_file_get_size(files[i]);
ut_a(size != (os_offset_t) -1);
/* Round size downward to megabytes */
rounded_size_pages = (ulint)
(size >> UNIV_PAGE_SIZE_SHIFT);
if (i == srv_n_data_files - 1
&& srv_auto_extend_last_data_file) {
if (srv_data_file_sizes[i] > rounded_size_pages
|| (srv_last_file_size_max > 0
&& srv_last_file_size_max
< rounded_size_pages)) {
ib_logf(IB_LOG_LEVEL_ERROR,
"auto-extending "
"data file %s is "
"of a different size "
"%lu pages (rounded "
"down to MB) than specified "
"in the .cnf file: "
"initial %lu pages, "
"max %lu (relevant if "
"non-zero) pages!",
name,
(ulong) rounded_size_pages,
(ulong) srv_data_file_sizes[i],
(ulong)
srv_last_file_size_max);
return(DB_ERROR);
}
srv_data_file_sizes[i] = rounded_size_pages;
}
if (rounded_size_pages != srv_data_file_sizes[i]) {
ib_logf(IB_LOG_LEVEL_ERROR,
"Data file %s is of a different "
"size %lu pages (rounded down to MB) "
"than specified in the .cnf file "
"%lu pages!",
name,
(ulong) rounded_size_pages,
(ulong) srv_data_file_sizes[i]);
return(DB_ERROR);
}
skip_size_check:
check_msg = fil_read_first_page(
files[i], one_opened, &flags, &space,
#ifdef UNIV_LOG_ARCHIVE
min_arch_log_no, max_arch_log_no,
#endif /* UNIV_LOG_ARCHIVE */
min_flushed_lsn, max_flushed_lsn);
if (check_msg) {
ib_logf(IB_LOG_LEVEL_ERROR,
"%s in data file %s",
check_msg, name);
return(DB_ERROR);
}
/* The first file of the system tablespace must
have space ID = TRX_SYS_SPACE. The FSP_SPACE_ID
field in files greater than ibdata1 are unreliable. */