srv0start.c 22.1 KB
Newer Older
1
/************************************************************************
2
Starts the InnoDB database server
3

4
(c) 1996-2000 Innobase Oy
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

Created 2/16/1996 Heikki Tuuri
*************************************************************************/

#include "os0proc.h"
#include "sync0sync.h"
#include "ut0mem.h"
#include "mem0mem.h"
#include "mem0pool.h"
#include "data0data.h"
#include "data0type.h"
#include "dict0dict.h"
#include "buf0buf.h"
#include "buf0flu.h"
#include "buf0rea.h"
#include "os0file.h"
#include "os0thread.h"
#include "fil0fil.h"
#include "fsp0fsp.h"
#include "rem0rec.h"
#include "rem0cmp.h"
#include "mtr0mtr.h"
#include "log0log.h"
#include "log0recv.h"
#include "page0page.h"
#include "page0cur.h"
#include "trx0trx.h"
#include "dict0boot.h"
#include "trx0sys.h"
#include "dict0crea.h"
#include "btr0btr.h"
#include "btr0pcur.h"
#include "btr0cur.h"
#include "btr0sea.h"
#include "rem0rec.h"
#include "srv0srv.h"
#include "que0que.h"
#include "com0com.h"
#include "usr0sess.h"
#include "lock0lock.h"
#include "trx0roll.h"
#include "trx0purge.h"
#include "row0ins.h"
#include "row0sel.h"
#include "row0upd.h"
#include "row0row.h"
#include "row0mysql.h"
#include "lock0lock.h"
#include "ibuf0ibuf.h"
#include "pars0pars.h"
#include "btr0sea.h"
#include "srv0start.h"
#include "que0que.h"

59
ibool           srv_startup_is_before_trx_rollback_phase = FALSE;
unknown's avatar
unknown committed
60 61 62
ibool           srv_is_being_started = FALSE;
ibool           srv_was_started      = FALSE;

63 64 65 66 67 68 69 70 71 72 73 74 75
ibool		measure_cont	= FALSE;

os_file_t	files[1000];

mutex_t		ios_mutex;
ulint		ios;

ulint		n[SRV_MAX_N_IO_THREADS + 5];
os_thread_id_t	thread_ids[SRV_MAX_N_IO_THREADS + 5];

#define SRV_N_PENDING_IOS_PER_THREAD 	OS_AIO_N_PENDING_IOS_PER_THREAD
#define SRV_MAX_N_PENDING_SYNC_IOS	100

76 77 78 79
/* The following limit may be too big in some old operating systems:
we may get an assertion failure in os0file.c */

#define SRV_MAX_N_OPEN_FILES		500
80 81 82 83 84 85

#define SRV_LOG_SPACE_FIRST_ID		1000000000

/************************************************************************
I/o-handler thread function. */
static
86 87 88 89

#ifndef __WIN__
void*
#else
90
ulint
91
#endif
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
io_handler_thread(
/*==============*/
	void*	arg)
{
	ulint	segment;
	ulint	i;
	
	segment = *((ulint*)arg);

/*	printf("Io handler thread %lu starts\n", segment); */

	for (i = 0;; i++) {
		fil_aio_wait(segment);

		mutex_enter(&ios_mutex);
		ios++;
		mutex_exit(&ios_mutex);
	}

111 112 113
#ifndef __WIN__
	return(NULL);
#else
114
	return(0);
115
#endif
116 117
}

unknown's avatar
unknown committed
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
#ifdef __WIN__
#define SRV_PATH_SEPARATOR	"\\"
#else
#define SRV_PATH_SEPARATOR	"/"
#endif

/*************************************************************************
Normalizes a directory path for Windows: converts slashes to backslashes. */
static
void
srv_normalize_path_for_win(
/*=======================*/
	char*	str)	/* in/out: null-terminated character string */
{
#ifdef __WIN__
	ulint	i;

	for (i = 0; i < ut_strlen(str); i++) {

		if (str[i] == '/') {
			str[i] = '\\';
		}
	}
#endif
}
	
/*************************************************************************
145 146
Adds a slash or a backslash to the end of a string if it is missing
and the string is not empty. */
unknown's avatar
unknown committed
147 148 149 150
static
char*
srv_add_path_separator_if_needed(
/*=============================*/
151 152
			/* out, own: string which has the separator if the
			string is not empty */
unknown's avatar
unknown committed
153 154 155 156 157 158
	char*	str)	/* in: null-terminated character string */
{
	char*	out_str;

	if (ut_strlen(str) == 0) {

159
		return(str);
unknown's avatar
unknown committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	}

	if (str[ut_strlen(str) - 1] == SRV_PATH_SEPARATOR[0]) {
		out_str = ut_malloc(ut_strlen(str) + 1);
		
		sprintf(out_str, "%s", str);

		return(out_str);
	}
		
	out_str = ut_malloc(ut_strlen(str) + 2);
		
	sprintf(out_str, "%s%s", str, SRV_PATH_SEPARATOR);

	return(out_str);
}

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/*************************************************************************
Creates or opens the log files. */
static
ulint
open_or_create_log_file(
/*====================*/
					/* out: DB_SUCCESS or error code */
	ibool	create_new_db,		/* in: TRUE if we should create a
					new database */
	ibool*	log_file_created,	/* out: TRUE if new log file
					created */
	ulint	k,			/* in: log group number */
	ulint	i)			/* in: log file number in group */
{
	ibool	ret;
	ulint	arch_space_id;
	ulint	size;
	ulint	size_high;
	char	name[10000];

197 198
	UT_NOT_USED(create_new_db);

199
	*log_file_created = FALSE;
unknown's avatar
unknown committed
200 201 202 203 204

	srv_normalize_path_for_win(srv_log_group_home_dirs[k]);
	srv_log_group_home_dirs[k] = srv_add_path_separator_if_needed(
						srv_log_group_home_dirs[k]);

205 206
	sprintf(name, "%s%s%lu", srv_log_group_home_dirs[k], "ib_logfile", i);

207 208
	files[i] = os_file_create(name, OS_FILE_CREATE, OS_FILE_NORMAL,
						OS_LOG_FILE, &ret);
209 210 211
	if (ret == FALSE) {
		if (os_file_get_last_error() != OS_FILE_ALREADY_EXISTS) {
			fprintf(stderr,
212
			"InnoDB: Error in creating or opening %s\n", name);
213 214 215 216 217
				
			return(DB_ERROR);
		}

		files[i] = os_file_create(
218 219
					name, OS_FILE_OPEN, OS_FILE_AIO,
							OS_LOG_FILE, &ret);
220 221
		if (!ret) {
			fprintf(stderr,
222
			"InnoDB: Error in opening %s\n", name);
223 224 225 226 227 228 229 230 231 232
				
			return(DB_ERROR);
		}

		ret = os_file_get_size(files[i], &size, &size_high);
		ut_a(ret);
		
		if (size != UNIV_PAGE_SIZE * srv_log_file_size
							|| size_high != 0) {
			fprintf(stderr,
233 234
			"InnoDB: Error: log file %s is of different size\n"
			"InnoDB: than specified in the .cnf file!\n", name);
235 236 237 238 239 240 241
				
			return(DB_ERROR);
		}					
	} else {
		*log_file_created = TRUE;
					
		fprintf(stderr,
242
		"InnoDB: Log file %s did not exist: new to be created\n",
243
									name);
244
		fprintf(stderr, "InnoDB: Setting log file %s size to %lu\n",
245 246
			             name, UNIV_PAGE_SIZE * srv_log_file_size);

247 248 249 250
		ret = os_file_set_size(name, files[i],
					UNIV_PAGE_SIZE * srv_log_file_size, 0);
		if (!ret) {
			fprintf(stderr,
251
		"InnoDB: Error in creating %s: probably out of disk space\n",
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
			name);

			return(DB_ERROR);
		}
	}

	ret = os_file_close(files[i]);
	ut_a(ret);

	if (i == 0) {
		/* Create in memory the file space object
		which is for this log group */
				
		fil_space_create(name,
		2 * k + SRV_LOG_SPACE_FIRST_ID, FIL_LOG);
	}

	ut_a(fil_validate());

	fil_node_create(name, srv_log_file_size,
					2 * k + SRV_LOG_SPACE_FIRST_ID);

	/* If this is the first log group, create the file space object
	for archived logs */

	if (k == 0 && i == 0) {
		arch_space_id = 2 * k + 1 + SRV_LOG_SPACE_FIRST_ID;

	    	fil_space_create("arch_log_space", arch_space_id,
								FIL_LOG);
	} else {
		arch_space_id = ULINT_UNDEFINED;
	}

	if (i == 0) {
		log_group_init(k, srv_n_log_files,
				srv_log_file_size * UNIV_PAGE_SIZE,
				2 * k + SRV_LOG_SPACE_FIRST_ID,
				arch_space_id);
	}

	return(DB_SUCCESS);
}

/*************************************************************************
Creates or opens database data files. */
static
ulint
open_or_create_data_files(
/*======================*/
				/* out: DB_SUCCESS or error code */
	ibool*	create_new_db,	/* out: TRUE if new database should be
								created */
	dulint*	min_flushed_lsn,/* out: min of flushed lsn values in data
				files */
	ulint*	min_arch_log_no,/* out: min of archived log numbers in data
				files */
	dulint*	max_flushed_lsn,/* out: */
	ulint*	max_arch_log_no,/* out: */
	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;
	ulint	size;
	ulint	size_high;
	char	name[10000];

321 322 323 324 325 326
	if (srv_n_data_files >= 1000) {
		fprintf(stderr, "InnoDB: can only have < 1000 data files\n"
				"InnoDB: you have defined %lu\n",
				srv_n_data_files);
		return(DB_ERROR);
	}
327 328 329 330 331

	*sum_of_new_sizes = 0;
	
	*create_new_db = FALSE;

unknown's avatar
unknown committed
332 333 334
	srv_normalize_path_for_win(srv_data_home);
	srv_data_home = srv_add_path_separator_if_needed(srv_data_home);

335
	for (i = 0; i < srv_n_data_files; i++) {
unknown's avatar
unknown committed
336
		srv_normalize_path_for_win(srv_data_file_names[i]);
337 338 339

		sprintf(name, "%s%s", srv_data_home, srv_data_file_names[i]);
	
340 341
		files[i] = os_file_create(name, OS_FILE_CREATE,
					OS_FILE_NORMAL, OS_DATA_FILE, &ret);
342

343 344 345
		if (srv_data_file_is_raw_partition[i] == SRV_NEW_RAW) {
			/* The partition is opened, not created; then it is
			written over */
346

347 348
			srv_created_new_raw = TRUE;

349 350 351 352
			files[i] = os_file_create(
				name, OS_FILE_OPEN, OS_FILE_NORMAL,
						OS_DATA_FILE, &ret);
			if (!ret) {
353 354 355 356
				fprintf(stderr,
				"InnoDB: Error in opening %s\n", name);

				return(DB_ERROR);
357 358 359
			}
		} else if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
			ret = FALSE;
360 361
		}

362
		if (ret == FALSE) {
363
			if (srv_data_file_is_raw_partition[i] != SRV_OLD_RAW
364
			    && os_file_get_last_error() !=
365 366
						OS_FILE_ALREADY_EXISTS) {
				fprintf(stderr,
367
				"InnoDB: Error in creating or opening %s\n",
368 369 370 371 372 373 374
				name);

				return(DB_ERROR);
			}

			if (one_created) {
				fprintf(stderr,
375
	"InnoDB: Error: data files can only be added at the end\n");
376
				fprintf(stderr,
377
	"InnoDB: of a tablespace, but data file %s existed beforehand.\n",
378 379 380 381 382
				name);
				return(DB_ERROR);
			}
				
			files[i] = os_file_create(
383 384
				name, OS_FILE_OPEN, OS_FILE_NORMAL,
						OS_DATA_FILE, &ret);
385 386
			if (!ret) {
				fprintf(stderr,
387
				"InnoDB: Error in opening %s\n", name);
388
				os_file_get_last_error();
389 390 391 392

				return(DB_ERROR);
			}

393 394 395 396 397
			if (srv_data_file_is_raw_partition[i] != SRV_OLD_RAW) {
			
				ret = os_file_get_size(files[i], &size,
								&size_high);
				ut_a(ret);
398
		
399 400 401 402
				if (size !=
					UNIV_PAGE_SIZE * srv_data_file_sizes[i]
		    					|| size_high != 0) {
					fprintf(stderr,
403 404
			"InnoDB: Error: data file %s is of different size\n"
			"InnoDB: than specified in the .cnf file!\n", name);
405
				
406 407
					return(DB_ERROR);
				}
408 409 410 411 412 413 414 415 416 417 418 419
			}

			fil_read_flushed_lsn_and_arch_log_no(files[i],
					one_opened,
					min_flushed_lsn, min_arch_log_no,
					max_flushed_lsn, max_arch_log_no);
			one_opened = TRUE;
		} else {
			one_created = TRUE;

			if (i > 0) {
				fprintf(stderr, 
420 421
		"InnoDB: Data file %s did not exist: new to be created\n",
									name);
422 423
			} else {
				fprintf(stderr, 
424 425
 		"InnoDB: The first specified data file %s did not exist:\n"
		"InnoDB: a new database to be created!\n", name);
426 427 428
				*create_new_db = TRUE;
			}
			
429
			fprintf(stderr, "InnoDB: Setting file %s size to %lu\n",
430 431
			       name, UNIV_PAGE_SIZE * srv_data_file_sizes[i]);

432
			fprintf(stderr,
433
	    "InnoDB: Database physically writes the file full: wait...\n");
434

435 436 437 438 439
			ret = os_file_set_size(name, files[i],
				UNIV_PAGE_SIZE * srv_data_file_sizes[i], 0);

			if (!ret) {
				fprintf(stderr, 
440
	"InnoDB: Error in creating %s: probably out of disk space\n", name);
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

				return(DB_ERROR);
			}

			*sum_of_new_sizes = *sum_of_new_sizes
						+ srv_data_file_sizes[i];
		}

		ret = os_file_close(files[i]);
		ut_a(ret);

		if (i == 0) {
			fil_space_create(name, 0, FIL_TABLESPACE);
		}

		ut_a(fil_validate());

		fil_node_create(name, srv_data_file_sizes[i], 0);
	}

	ios = 0;

	mutex_create(&ios_mutex);
	mutex_set_level(&ios_mutex, SYNC_NO_ORDER_CHECK);

	return(DB_SUCCESS);
}

/*********************************************************************
This thread is used to measure contention of latches. */
static
ulint
test_measure_cont(
/*==============*/
	void*	arg)
{
	ulint	i, j;
	ulint	pcount, kcount, s_scount, s_xcount, s_mcount, lcount;

	UT_NOT_USED(arg);

	fprintf(stderr, "Starting contention measurement\n");
	
	for (i = 0; i < 1000; i++) {

		pcount = 0;
		kcount = 0;
		s_scount = 0;
		s_xcount = 0;
		s_mcount = 0;
		lcount = 0;

		for (j = 0; j < 100; j++) {

		    if (srv_measure_by_spin) {
		    	ut_delay(ut_rnd_interval(0, 20000));
		    } else {
		    	os_thread_sleep(20000);
		    }

		    if (kernel_mutex.lock_word) {
			kcount++;
		    }

		    if (buf_pool->mutex.lock_word) {
		    	pcount++;
		    }

		    if (log_sys->mutex.lock_word) {
		    	lcount++;
		    }

		    if (btr_search_latch.reader_count) {
		    	s_scount++;
		    }

		    if (btr_search_latch.writer != RW_LOCK_NOT_LOCKED) {
		    	s_xcount++;
		    }

		    if (btr_search_latch.mutex.lock_word) {
		    	s_mcount++;
		    }
		}

		fprintf(stderr, 
	"Mutex res. l %lu, p %lu, k %lu s x %lu s s %lu s mut %lu of %lu\n",
		lcount, pcount, kcount, s_xcount, s_scount, s_mcount, j);

		sync_print_wait_info();

		fprintf(stderr, 
    "log i/o %lu n non sea %lu n succ %lu n h fail %lu\n",
			log_sys->n_log_ios, btr_cur_n_non_sea,
			btr_search_n_succ, btr_search_n_hash_fail);
	}

	return(0);
}

/********************************************************************
542
Starts InnoDB and creates a new database if database files
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
are not found and the user wants. Server parameters are
read from a file of name "srv_init" in the ib_home directory. */

int
innobase_start_or_create_for_mysql(void)
/*====================================*/
				/* out: DB_SUCCESS or error code */
{
	ibool	create_new_db;
	ibool	log_file_created;
	ibool	log_created	= FALSE;
	ibool	log_opened	= FALSE;
	dulint	min_flushed_lsn;
	dulint	max_flushed_lsn;
	ulint	min_arch_log_no;
	ulint	max_arch_log_no;
	ibool	start_archive;
	ulint   sum_of_new_sizes;
561 562 563
	ulint	err;
	ulint	i;
	ulint	k;
564 565 566 567 568
	mtr_t   mtr;

	log_do_write = TRUE;
/*	yydebug = TRUE; */

unknown's avatar
unknown committed
569
	srv_is_being_started = TRUE;
570
        srv_startup_is_before_trx_rollback_phase = TRUE;
unknown's avatar
unknown committed
571

572
	if (0 == ut_strcmp(srv_unix_file_flush_method_str, "fdatasync")) {
573 574
	  	srv_unix_file_flush_method = SRV_UNIX_FDATASYNC;

575
	} else if (0 == ut_strcmp(srv_unix_file_flush_method_str, "O_DSYNC")) {
576 577
	  	srv_unix_file_flush_method = SRV_UNIX_O_DSYNC;

578 579
	} else if (0 == ut_strcmp(srv_unix_file_flush_method_str,
				  "littlesync")) {
580 581
	  	srv_unix_file_flush_method = SRV_UNIX_LITTLESYNC;

582
	} else if (0 == ut_strcmp(srv_unix_file_flush_method_str, "nosync")) {
583
	  	srv_unix_file_flush_method = SRV_UNIX_NOSYNC;
584
	} else {
585 586 587 588
	  	fprintf(stderr, 
          	"InnoDB: Unrecognized value %s for innodb_flush_method\n",
          				srv_unix_file_flush_method_str);
	  	return(DB_ERROR);
589 590
	}

591
	/*
592
	printf("srv_unix set to %lu\n", srv_unix_file_flush_method);
593
	*/
594 595 596 597 598 599 600 601 602
	os_aio_use_native_aio = srv_use_native_aio;

	err = srv_boot();

	if (err != DB_SUCCESS) {

		return((int) err);
	}

603 604 605 606 607
	/* Restrict the maximum number of file i/o threads */
	if (srv_n_file_io_threads > SRV_MAX_N_IO_THREADS) {
		srv_n_file_io_threads = SRV_MAX_N_IO_THREADS;
	}

608 609 610 611 612 613 614 615
#if !(defined(WIN_ASYNC_IO) || defined(POSIX_ASYNC_IO))
	/* In simulated aio we currently have use only for 4 threads */

	os_aio_use_native_aio = FALSE;

	srv_n_file_io_threads = 4;
#endif

616 617 618
#ifdef __WIN__
	if (os_get_os_version() == OS_WIN95
	    || os_get_os_version() == OS_WIN31) {
619

620 621 622 623 624
	  	/* On Win 95, 98, ME, and Win32 subsystem for Windows 3.1 use
	     	simulated aio */

	  	os_aio_use_native_aio = FALSE;
	  	srv_n_file_io_threads = 4;
625
	} else {
626 627
	  	/* On NT and Win 2000 always use aio */
	  	os_aio_use_native_aio = TRUE;
628 629
	}
#endif
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
	if (!os_aio_use_native_aio) {
		os_aio_init(4 * SRV_N_PENDING_IOS_PER_THREAD
						* srv_n_file_io_threads,
					srv_n_file_io_threads,
					SRV_MAX_N_PENDING_SYNC_IOS);
	} else {
		os_aio_init(SRV_N_PENDING_IOS_PER_THREAD
						* srv_n_file_io_threads,
					srv_n_file_io_threads,
					SRV_MAX_N_PENDING_SYNC_IOS);
	}
	
	fil_init(SRV_MAX_N_OPEN_FILES);

	buf_pool_init(srv_pool_size, srv_pool_size);

	fsp_init();
	log_init();
	
	lock_sys_create(srv_lock_table_size);

#ifdef POSIX_ASYNC_IO
	if (os_aio_use_native_aio) {
		/* There is only one thread per async io array:
		one for ibuf i/o, one for log i/o, one for ordinary reads,
		one for ordinary writes; we need only 4 i/o threads */

		srv_n_file_io_threads = 4;
	}
#endif
	/* Create i/o-handler threads: */

	for (i = 0; i < srv_n_file_io_threads; i++) {
		n[i] = i;

		os_thread_create(io_handler_thread, n + i, thread_ids + i);
666
    	}
667

unknown's avatar
unknown committed
668 669 670 671 672 673 674 675
	if (0 != ut_strcmp(srv_log_group_home_dirs[0], srv_arch_dir)) {
		fprintf(stderr,
	"InnoDB: Error: you must set the log group home dir in my.cnf the\n"
	"InnoDB: same as log arch dir.\n");

		return(DB_ERROR);
	}

676 677 678
	sum_of_new_sizes = 0;

	for (i = 0; i < srv_n_data_files; i++) {
679 680 681 682 683 684 685 686
		if (srv_data_file_sizes[i] >= 262144) {
		 	fprintf(stderr,
	"InnoDB: Error: file size must be < 4 GB, or on some OS's < 2 GB\n");

		  	return(DB_ERROR);
		}

		sum_of_new_sizes += srv_data_file_sizes[i];
687 688 689
	}

	if (sum_of_new_sizes < 640) {
690
		  fprintf(stderr,
691 692
		  "InnoDB: Error: tablespace size must be at least 10 MB\n");

693
		  return(DB_ERROR);
694 695
	}

696 697 698 699 700 701
	err = open_or_create_data_files(&create_new_db,
					&min_flushed_lsn, &min_arch_log_no,
					&max_flushed_lsn, &max_arch_log_no,
					&sum_of_new_sizes);
	if (err != DB_SUCCESS) {

702
	        fprintf(stderr, "InnoDB: Could not open data files\n");
703

704 705 706
		return((int) err);
	}

707 708 709 710 711 712 713 714 715
	if (!create_new_db) {
		/* If we are using the doublewrite method, we will
		check if there are half-written pages in data files,
		and restore them from the doublewrite buffer if
		possible */
		
		trx_sys_doublewrite_restore_corrupt_pages();
	}

unknown's avatar
unknown committed
716 717 718
	srv_normalize_path_for_win(srv_arch_dir);
	srv_arch_dir = srv_add_path_separator_if_needed(srv_arch_dir);

719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
	for (k = 0; k < srv_n_log_groups; k++) {

		for (i = 0; i < srv_n_log_files; i++) {

			err = open_or_create_log_file(create_new_db,
						&log_file_created, k, i);
			if (err != DB_SUCCESS) {

				return((int) err);
			}

			if (log_file_created) {
				log_created = TRUE;
			} else {
				log_opened = TRUE;
			}

			if ((log_opened && create_new_db)
			    		|| (log_opened && log_created)) {
				fprintf(stderr, 
739 740 741 742 743 744
	"InnoDB: Error: all log files must be created at the same time.\n"
	"InnoDB: If you want bigger or smaller log files,\n"
	"InnoDB: shut down the database and make sure there\n"
	"InnoDB: were no errors in shutdown.\n"
	"InnoDB: Then delete the existing log files. Edit the .cnf file\n"
	"InnoDB: and start the database again.\n");
745 746 747 748 749 750 751 752 753 754 755 756

				return(DB_ERROR);
			}
			
		}
	}

	if (log_created && !create_new_db && !srv_archive_recovery) {

		if (ut_dulint_cmp(max_flushed_lsn, min_flushed_lsn) != 0
				|| max_arch_log_no != min_arch_log_no) {
			fprintf(stderr, 
757 758 759
		"InnoDB: Cannot initialize created log files because\n"
		"InnoDB: data files were not in sync with each other\n"
		"InnoDB: or the data files are corrupt./n");
760 761 762 763 764 765 766

			return(DB_ERROR);
		}

		if (ut_dulint_cmp(max_flushed_lsn, ut_dulint_create(0, 1000))
		    < 0) {
		    	fprintf(stderr,
767 768 769 770 771
		"InnoDB: Cannot initialize created log files because\n"
		"InnoDB: data files are corrupt, or new data files were\n"
		"InnoDB: created when the database was started previous\n"
		"InnoDB: time but the database was not shut down\n"
		"InnoDB: normally after that.\n");
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795

			return(DB_ERROR);
		}

		mutex_enter(&(log_sys->mutex));

		recv_reset_logs(ut_dulint_align_down(max_flushed_lsn,
					OS_FILE_LOG_BLOCK_SIZE),
					max_arch_log_no + 1, TRUE);
		
		mutex_exit(&(log_sys->mutex));
	}

	sess_sys_init_at_db_start();

	if (create_new_db) {
		mtr_start(&mtr);

		fsp_header_init(0, sum_of_new_sizes, &mtr);		

		mtr_commit(&mtr);

		trx_sys_create();
		dict_create();
796
                srv_startup_is_before_trx_rollback_phase = FALSE;
797 798 799

	} else if (srv_archive_recovery) {
		fprintf(stderr,
800
	"InnoDB: Starting archive recovery from a backup...\n");
801 802 803 804 805 806 807 808 809 810
	
		err = recv_recovery_from_archive_start(
					min_flushed_lsn,
					srv_archive_recovery_limit_lsn,
					min_arch_log_no);
		if (err != DB_SUCCESS) {

			return(DB_ERROR);
		}

811 812 813
		/* Since ibuf init is in dict_boot, and ibuf is needed
		in any disk i/o, first call dict_boot */

814
		dict_boot();
815 816

		trx_sys_init_at_db_start();
817
		
818 819
                srv_startup_is_before_trx_rollback_phase = FALSE;

820 821 822 823 824 825 826 827 828 829 830 831 832 833
		recv_recovery_from_archive_finish();
	} else {
		/* We always try to do a recovery, even if the database had
		been shut down normally */
		
		err = recv_recovery_from_checkpoint_start(LOG_CHECKPOINT,
							ut_dulint_max,
							min_flushed_lsn,
							max_flushed_lsn);
		if (err != DB_SUCCESS) {

			return(DB_ERROR);
		}

834 835
		/* Since ibuf init is in dict_boot, and ibuf is needed
		in any disk i/o, first call dict_boot */
836
		dict_boot();
837
		trx_sys_init_at_db_start();
838 839 840

		/* The following needs trx lists which are initialized in
		trx_sys_init_at_db_start */
841 842

                srv_startup_is_before_trx_rollback_phase = FALSE;
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
		recv_recovery_from_checkpoint_finish();
	}
	
	if (!create_new_db && sum_of_new_sizes > 0) {
		/* New data file(s) were added */
		mtr_start(&mtr);

		fsp_header_inc_size(0, sum_of_new_sizes, &mtr);		

		mtr_commit(&mtr);
	}

	log_make_checkpoint_at(ut_dulint_max, TRUE);

	if (!srv_log_archive_on) {
		ut_a(DB_SUCCESS == log_archive_noarchivelog());
	} else {
		mutex_enter(&(log_sys->mutex));

		start_archive = FALSE;

		if (log_sys->archiving_state == LOG_ARCH_OFF) {
			start_archive = TRUE;
		}

		mutex_exit(&(log_sys->mutex));

		if (start_archive) {
			ut_a(DB_SUCCESS == log_archive_archivelog());
		}
	}

	if (srv_measure_contention) {
876
	  	/* os_thread_create(&test_measure_cont, NULL, thread_ids +
877
                             	     SRV_MAX_N_IO_THREADS); */
878 879 880 881 882
	}

	/* fprintf(stderr, "Max allowed record size %lu\n",
				page_get_free_space_of_empty() / 2); */

883 884 885 886
	/* Create the thread which watches the timeouts for lock waits
	and prints InnoDB monitor info */
	
	os_thread_create(&srv_lock_timeout_and_monitor_thread, NULL,
887
					thread_ids + 2 + SRV_MAX_N_IO_THREADS);	
888 889 890 891 892

	/* Create the thread which warns of long semaphore waits */
	os_thread_create(&srv_error_monitor_thread, NULL,
					thread_ids + 3 + SRV_MAX_N_IO_THREADS);	

unknown's avatar
unknown committed
893 894 895
	srv_was_started = TRUE;
	srv_is_being_started = FALSE;

896 897
	sync_order_checks_on = TRUE;

898 899 900 901
	if (srv_use_doublewrite_buf && trx_doublewrite == NULL) {
		trx_sys_create_doublewrite_buf();
	}

902 903 904 905 906 907 908 909 910 911 912
	err = dict_create_or_check_foreign_constraint_tables();

	if (err != DB_SUCCESS) {
		return((int)DB_ERROR);
	}

	/* Create the master thread which monitors the database
	server, and does purge and other utility operations */

	os_thread_create(&srv_master_thread, NULL, thread_ids + 1 +
							SRV_MAX_N_IO_THREADS);
913 914
	/* buf_debug_prints = TRUE; */
	
915 916 917 918 919
	if (srv_print_verbose_log)
	{
	  ut_print_timestamp(stderr);
	  fprintf(stderr, "  InnoDB: Started\n");
	}
920 921 922 923
	return((int) DB_SUCCESS);
}

/********************************************************************
924
Shuts down the InnoDB database. */
925 926 927 928 929 930

int
innobase_shutdown_for_mysql(void) 
/*=============================*/
				/* out: DB_SUCCESS or error code */
{
unknown's avatar
unknown committed
931
        if (!srv_was_started) {
932 933 934 935 936 937 938 939 940 941
	  	if (srv_is_being_started) {
	    		ut_print_timestamp(stderr);
            		fprintf(stderr, 
	"  InnoDB: Warning: shutting down a not properly started\n");
	    		ut_print_timestamp(stderr);
            		fprintf(stderr, 
	"  InnoDB: or created database!\n");
	  	}

	  	return(DB_SUCCESS);
unknown's avatar
unknown committed
942 943
	}

944 945 946 947 948
	/* Flush buffer pool to disk, write the current lsn to
	the tablespace header(s), and copy all log data to archive */

	logs_empty_and_mark_files_at_shutdown();

949
	ut_free_all_mem();
950
	
951 952
	return((int) DB_SUCCESS);
}