srv0start.c 48.3 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

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"
unknown's avatar
unknown committed
33
#include "dict0load.h"
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
#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 "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"

unknown's avatar
unknown committed
59 60 61 62 63
/* Log sequence number immediately after startup */
dulint		srv_start_lsn;
/* Log sequence number at shutdown */
dulint		srv_shutdown_lsn;

64 65 66 67 68
#ifdef HAVE_DARWIN_THREADS
# include <sys/utsname.h>
ibool		srv_have_fullfsync = FALSE;
#endif

unknown's avatar
unknown committed
69 70
ibool		srv_start_raw_disk_in_use  = FALSE;

71
static ibool	srv_start_has_been_called  = FALSE;
unknown's avatar
unknown committed
72

73 74
ulint           srv_sizeof_trx_t_in_ha_innodb_cc;

75
ibool           srv_startup_is_before_trx_rollback_phase = FALSE;
unknown's avatar
unknown committed
76
ibool           srv_is_being_started = FALSE;
77
static ibool	srv_was_started      = FALSE;
unknown's avatar
unknown committed
78

unknown's avatar
Merge  
unknown committed
79 80 81 82
/* At a shutdown the value first climbs to SRV_SHUTDOWN_CLEANUP
and then to SRV_SHUTDOWN_LAST_PHASE */
ulint		srv_shutdown_state = 0;

83 84
ibool		measure_cont	= FALSE;

85
static os_file_t	files[1000];
86

87 88
static mutex_t		ios_mutex;
static ulint		ios;
89

90 91
static ulint		n[SRV_MAX_N_IO_THREADS + 5];
static os_thread_id_t	thread_ids[SRV_MAX_N_IO_THREADS + 5];
92

unknown's avatar
unknown committed
93 94
/* 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. */
95
static os_fast_mutex_t	srv_os_test_mutex;
unknown's avatar
unknown committed
96

97 98
/* Name of srv_monitor_file */
static char*	srv_monitor_file_name;
unknown's avatar
unknown committed
99

100 101 102
#define SRV_N_PENDING_IOS_PER_THREAD 	OS_AIO_N_PENDING_IOS_PER_THREAD
#define SRV_MAX_N_PENDING_SYNC_IOS	100

103 104 105 106 107 108 109 110 111 112 113 114 115

/* Avoid warnings when using purify */

#ifdef HAVE_purify
static int inno_bcmp(register const char *s1, register const char *s2,
                     register uint len)
{
  while (len-- != 0 && *s1++ == *s2++) ;
  return len+1;
}
#define memcmp(A,B,C) inno_bcmp((A),(B),(C))
#endif

unknown's avatar
unknown committed
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
/*************************************************************************
Reads the data files and their sizes from a character string given in
the .cnf file. */

ibool
srv_parse_data_file_paths_and_sizes(
/*================================*/
					/* out: TRUE if ok, FALSE if parsing
					error */
	char*	str,			/* in: the data file path string */
	char***	data_file_names,	/* out, own: array of data file
					names */
	ulint**	data_file_sizes,	/* out, own: array of data file sizes
					in megabytes */
	ulint**	data_file_is_raw_partition,/* out, own: array of flags
					showing which data files are raw
					partitions */
	ulint*	n_data_files,		/* out: number of data files */
	ibool*	is_auto_extending,	/* out: TRUE if the last data file is
					auto-extending */
	ulint*	max_auto_extend_size)	/* out: max auto extend size for the
					last file if specified, 0 if not */
{
	char*	input_str;
	char*	endp;
	char*	path;
	ulint	size;
	ulint	i	= 0;

	*is_auto_extending = FALSE;
	*max_auto_extend_size = 0;

	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 == ':'
unknown's avatar
unknown committed
159 160
			   && (*(str + 1) == '\\' || *(str + 1) == '/'
					     || *(str + 1) == ':'))) {
unknown's avatar
unknown committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
			str++;
		}

		if (*str == '\0') {
			return(FALSE);
		}

		str++;

		size = strtoul(str, &endp, 10);

		str = endp;

		if (*str != 'M' && *str != 'G') {
			size = size / (1024 * 1024);
		} else if (*str == 'G') {
		        size = size * 1024;
			str++;
		} else {
		        str++;
		}

183
	        if (0 == memcmp(str, ":autoextend", (sizeof ":autoextend") - 1)) {
unknown's avatar
unknown committed
184

185
			str += (sizeof ":autoextend") - 1;
unknown's avatar
unknown committed
186

187
	        	if (0 == memcmp(str, ":max:", (sizeof ":max:") - 1)) {
unknown's avatar
unknown committed
188

189
				str += (sizeof ":max:") - 1;
unknown's avatar
unknown committed
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

				size = strtoul(str, &endp, 10);

				str = endp;

				if (*str != 'M' && *str != 'G') {
					size = size / (1024 * 1024);
				} else if (*str == 'G') {
		        		size = size * 1024;
					str++;
				} else {
		        		str++;
				}
			}

			if (*str != '\0') {

				return(FALSE);
			}
		}

	        if (strlen(str) >= 6
			   && *str == 'n'
			   && *(str + 1) == 'e' 
		           && *(str + 2) == 'w') {
		  	str += 3;
		}

218
	        if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
unknown's avatar
unknown committed
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
		  	str += 3;
		}

		if (size == 0) {
			return(FALSE);
		}

		i++;

		if (*str == ';') {
			str++;
		} else if (*str != '\0') {

			return(FALSE);
		}
	}

	*data_file_names = (char**)ut_malloc(i * sizeof(void*));
	*data_file_sizes = (ulint*)ut_malloc(i * sizeof(ulint));
	*data_file_is_raw_partition = (ulint*)ut_malloc(i * sizeof(ulint));

	*n_data_files = i;

	/* Then store the actual values to our arrays */

	str = input_str;
	i = 0;

	while (*str != '\0') {
		path = str;

unknown's avatar
unknown committed
250 251 252 253
		/* 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 */
unknown's avatar
unknown committed
254 255 256

		while ((*str != ':' && *str != '\0')
		       || (*str == ':'
unknown's avatar
unknown committed
257 258
			   && (*(str + 1) == '\\' || *(str + 1) == '/'
			        || *(str + 1) == ':'))) {
unknown's avatar
unknown committed
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
			str++;
		}

		if (*str == ':') {
			/* Make path a null-terminated string */
			*str = '\0';
			str++;
		}

		size = strtoul(str, &endp, 10);

		str = endp;

		if ((*str != 'M') && (*str != 'G')) {
			size = size / (1024 * 1024);
		} else if (*str == 'G') {
		        size = size * 1024;
			str++;
		} else {
		        str++;
		}

		(*data_file_names)[i] = path;
		(*data_file_sizes)[i] = size;

284
	        if (0 == memcmp(str, ":autoextend", (sizeof ":autoextend") - 1)) {
unknown's avatar
unknown committed
285 286 287

			*is_auto_extending = TRUE;

288
			str += (sizeof ":autoextend") - 1;
unknown's avatar
unknown committed
289

290
	        	if (0 == memcmp(str, ":max:", (sizeof ":max:") - 1)) {
unknown's avatar
unknown committed
291

292
				str += (sizeof ":max:") - 1;
unknown's avatar
unknown committed
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

				size = strtoul(str, &endp, 10);

				str = endp;

				if (*str != 'M' && *str != 'G') {
					size = size / (1024 * 1024);
				} else if (*str == 'G') {
		        		size = size * 1024;
					str++;
				} else {
		        		str++;
				}

				*max_auto_extend_size = size;
			}

			if (*str != '\0') {

				return(FALSE);
			}
		}
		
		(*data_file_is_raw_partition)[i] = 0;

	        if (strlen(str) >= 6
			   && *str == 'n'
			   && *(str + 1) == 'e' 
		           && *(str + 2) == 'w') {
		  	str += 3;
		  	(*data_file_is_raw_partition)[i] = SRV_NEW_RAW;
		}

326
		if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
unknown's avatar
unknown committed
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
		 	str += 3;
		  
		  	if ((*data_file_is_raw_partition)[i] == 0) {
		    		(*data_file_is_raw_partition)[i] = SRV_OLD_RAW;
		  	}		  
		}

		i++;

		if (*str == ';') {
			str++;
		}
	}

	return(TRUE);
}

/*************************************************************************
Reads log group home directories from a character string given in
the .cnf file. */

ibool
srv_parse_log_group_home_dirs(
/*==========================*/
					/* out: TRUE if ok, FALSE if parsing
					error */
	char*	str,			/* in: character string */
	char***	log_group_home_dirs)	/* out, own: log group home dirs */
{
	char*	input_str;
	char*	path;
	ulint	i	= 0;

	input_str = str;
	
	/* First calculate the number of directories and check syntax:
	path;path;... */

	while (*str != '\0') {
		path = str;

		while (*str != ';' && *str != '\0') {
			str++;
		}

		i++;

		if (*str == ';') {
			str++;
		} else if (*str != '\0') {

			return(FALSE);
		}
	}

	*log_group_home_dirs = (char**) ut_malloc(i * sizeof(void*));

	/* Then store the actual values to our array */

	str = input_str;
	i = 0;

	while (*str != '\0') {
		path = str;

		while (*str != ';' && *str != '\0') {
			str++;
		}

		if (*str == ';') {
			*str = '\0';
			str++;
		}

		(*log_group_home_dirs)[i] = path;

		i++;
	}

	return(TRUE);
}

409 410 411
/************************************************************************
I/o-handler thread function. */
static
412 413 414 415

#ifndef __WIN__
void*
#else
416
ulint
417
#endif
418 419 420 421 422 423 424 425 426
io_handler_thread(
/*==============*/
	void*	arg)
{
	ulint	segment;
	ulint	i;
	
	segment = *((ulint*)arg);

427
#ifdef UNIV_DEBUG_THREAD_CREATION
428 429
	fprintf(stderr, "Io handler thread %lu starts, id %lu\n", segment,
			os_thread_pf(os_thread_get_curr_id()));
430
#endif
431 432 433 434 435 436 437 438
	for (i = 0;; i++) {
		fil_aio_wait(segment);

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

439 440 441 442 443 444 445
	/* 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);

446
#ifndef __WIN__
447
	return(NULL);				/* Not reached */
448
#else
449
	return(0);
450
#endif
451 452
}

unknown's avatar
unknown committed
453
#ifdef __WIN__
454
#define SRV_PATH_SEPARATOR	'\\'
unknown's avatar
unknown committed
455
#else
456
#define SRV_PATH_SEPARATOR	'/'
unknown's avatar
unknown committed
457 458 459 460
#endif

/*************************************************************************
Normalizes a directory path for Windows: converts slashes to backslashes. */
unknown's avatar
unknown committed
461

unknown's avatar
unknown committed
462 463 464
void
srv_normalize_path_for_win(
/*=======================*/
unknown's avatar
unknown committed
465 466
	char*	str __attribute__((unused)))	/* in/out: null-terminated
							   character string */
unknown's avatar
unknown committed
467 468
{
#ifdef __WIN__
469
	for (; *str; str++) {
unknown's avatar
unknown committed
470

471 472
		if (*str == '/') {
			*str = '\\';
unknown's avatar
unknown committed
473 474 475 476 477 478
		}
	}
#endif
}
	
/*************************************************************************
479 480
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
481

unknown's avatar
unknown committed
482 483 484
char*
srv_add_path_separator_if_needed(
/*=============================*/
485
			/* out: string which has the separator if the
486
			string is not empty */
unknown's avatar
unknown committed
487 488 489
	char*	str)	/* in: null-terminated character string */
{
	char*	out_str;
490
	ulint	len	= ut_strlen(str);
unknown's avatar
unknown committed
491

492
	if (len == 0 || str[len - 1] == SRV_PATH_SEPARATOR) {
unknown's avatar
unknown committed
493

494
		return(str);
unknown's avatar
unknown committed
495 496
	}

497 498 499 500
	out_str = ut_malloc(len + 2);
	memcpy(out_str, str, len);
	out_str[len] = SRV_PATH_SEPARATOR;
	out_str[len + 1] = 0;
unknown's avatar
unknown committed
501 502 503 504

	return(out_str);
}

unknown's avatar
Merge  
unknown committed
505 506 507 508 509 510 511 512 513 514 515
/*************************************************************************
Calculates the low 32 bits when a file size which is given as a number
database pages is converted to the number of bytes. */
static
ulint
srv_calc_low32(
/*===========*/
				/* out: low 32 bytes of file size when
				expressed in bytes */
	ulint	file_size)	/* in: file size in database pages */
{
unknown's avatar
unknown committed
516
	return(0xFFFFFFFFUL & (file_size << UNIV_PAGE_SIZE_SHIFT));
unknown's avatar
Merge  
unknown committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
}

/*************************************************************************
Calculates the high 32 bits when a file size which is given as a number
database pages is converted to the number of bytes. */
static
ulint
srv_calc_high32(
/*============*/
				/* out: high 32 bytes of file size when
				expressed in bytes */
	ulint	file_size)	/* in: file size in database pages */
{
	return(file_size >> (32 - UNIV_PAGE_SIZE_SHIFT));
}

533
#ifndef UNIV_HOTBACKUP
534
/*************************************************************************
unknown's avatar
unknown committed
535
Creates or opens the log files and closes them. */
536 537 538 539 540
static
ulint
open_or_create_log_file(
/*====================*/
					/* out: DB_SUCCESS or error code */
unknown's avatar
unknown committed
541 542
        ibool   create_new_db,          /* in: TRUE if we should create a
                                        new database */
543 544
	ibool*	log_file_created,	/* out: TRUE if new log file
					created */
unknown's avatar
unknown committed
545 546 547
	ibool	log_file_has_been_opened,/* in: TRUE if a log file has been
					opened before: then it is an error
					to try to create another log file */
548 549 550 551 552 553 554 555
	ulint	k,			/* in: log group number */
	ulint	i)			/* in: log file number in group */
{
	ibool	ret;
	ulint	size;
	ulint	size_high;
	char	name[10000];

unknown's avatar
unknown committed
556 557
	UT_NOT_USED(create_new_db);

558
	*log_file_created = FALSE;
unknown's avatar
unknown committed
559 560 561 562 563

	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]);

564 565
	ut_a(strlen(srv_log_group_home_dirs[k]) <
		(sizeof name) - 10 - sizeof "ib_logfile");
566
	sprintf(name, "%s%s%lu", srv_log_group_home_dirs[k], "ib_logfile", (ulong) i);
567

568 569
	files[i] = os_file_create(name, OS_FILE_CREATE, OS_FILE_NORMAL,
						OS_LOG_FILE, &ret);
570
	if (ret == FALSE) {
unknown's avatar
unknown committed
571 572 573 574 575 576 577 578
		if (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
		) {
579
			fprintf(stderr,
580
			"InnoDB: Error in creating or opening %s\n", name);
581 582 583 584
				
			return(DB_ERROR);
		}

unknown's avatar
Merge  
unknown committed
585
		files[i] = os_file_create(name, OS_FILE_OPEN, OS_FILE_AIO,
586
							OS_LOG_FILE, &ret);
587 588
		if (!ret) {
			fprintf(stderr,
589
			"InnoDB: Error in opening %s\n", name);
590 591 592 593 594 595 596
				
			return(DB_ERROR);
		}

		ret = os_file_get_size(files[i], &size, &size_high);
		ut_a(ret);
		
unknown's avatar
Merge  
unknown committed
597 598 599
		if (size != srv_calc_low32(srv_log_file_size)
		    || size_high != srv_calc_high32(srv_log_file_size)) {
		    	
600
			fprintf(stderr,
unknown's avatar
unknown committed
601 602
"InnoDB: Error: log file %s is of different size %lu %lu bytes\n"
"InnoDB: than specified in the .cnf file %lu %lu bytes!\n",
603 604 605
				name, (ulong) size_high, (ulong) size,
				(ulong) srv_calc_high32(srv_log_file_size),
				(ulong) srv_calc_low32(srv_log_file_size));
606 607 608 609 610
				
			return(DB_ERROR);
		}					
	} else {
		*log_file_created = TRUE;
unknown's avatar
unknown committed
611

unknown's avatar
unknown committed
612 613
	    	ut_print_timestamp(stderr);

614
		fprintf(stderr,
unknown's avatar
unknown committed
615
		"  InnoDB: Log file %s did not exist: new to be created\n",
616
									name);
unknown's avatar
unknown committed
617 618 619 620 621
		if (log_file_has_been_opened) {

			return(DB_ERROR);
		}

unknown's avatar
Merge  
unknown committed
622
		fprintf(stderr, "InnoDB: Setting log file %s size to %lu MB\n",
623
			             name, (ulong) srv_log_file_size
unknown's avatar
Merge  
unknown committed
624
			>> (20 - UNIV_PAGE_SIZE_SHIFT));
625

unknown's avatar
unknown committed
626 627 628
		fprintf(stderr,
	    "InnoDB: Database physically writes the file full: wait...\n");

629
		ret = os_file_set_size(name, files[i],
unknown's avatar
Merge  
unknown committed
630 631
					srv_calc_low32(srv_log_file_size),
					srv_calc_high32(srv_log_file_size));
632 633
		if (!ret) {
			fprintf(stderr,
634
		"InnoDB: Error in creating %s: probably out of disk space\n",
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
			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,
unknown's avatar
unknown committed
655
				2 * k + SRV_LOG_SPACE_FIRST_ID, FALSE);
unknown's avatar
unknown committed
656
#ifdef UNIV_LOG_ARCHIVE
657
	/* If this is the first log group, create the file space object
unknown's avatar
unknown committed
658 659
	for archived logs.
	Under MySQL, no archiving ever done. */
660 661 662 663

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

unknown's avatar
unknown committed
664
	    	fil_space_create("arch_log_space", arch_space_id, FIL_LOG);
665 666 667
	} else {
		arch_space_id = ULINT_UNDEFINED;
	}
unknown's avatar
unknown committed
668
#endif /* UNIV_LOG_ARCHIVE */
669 670 671 672
	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,
unknown's avatar
unknown committed
673 674
				SRV_LOG_SPACE_FIRST_ID + 1); /* dummy arch
								space id */
675 676 677 678 679 680
	}

	return(DB_SUCCESS);
}

/*************************************************************************
unknown's avatar
unknown committed
681
Creates or opens database data files and closes them. */
682 683 684 685 686 687 688
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 */
unknown's avatar
unknown committed
689
#ifdef UNIV_LOG_ARCHIVE
690 691 692
	ulint*	min_arch_log_no,/* out: min of archived log numbers in data
				files */
	ulint*	max_arch_log_no,/* out: */
unknown's avatar
unknown committed
693 694 695 696
#endif /* UNIV_LOG_ARCHIVE */
	dulint*	min_flushed_lsn,/* out: min of flushed lsn values in data
				files */
	dulint*	max_flushed_lsn,/* out: */
697 698 699 700 701 702 703 704
	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;
unknown's avatar
unknown committed
705
	ulint	rounded_size_pages;
706 707
	char	name[10000];

708 709 710
	if (srv_n_data_files >= 1000) {
		fprintf(stderr, "InnoDB: can only have < 1000 data files\n"
				"InnoDB: you have defined %lu\n",
711
				(ulong) srv_n_data_files);
712 713
		return(DB_ERROR);
	}
714 715 716 717 718

	*sum_of_new_sizes = 0;
	
	*create_new_db = FALSE;

unknown's avatar
unknown committed
719 720 721
	srv_normalize_path_for_win(srv_data_home);
	srv_data_home = srv_add_path_separator_if_needed(srv_data_home);

722
	for (i = 0; i < srv_n_data_files; i++) {
unknown's avatar
unknown committed
723
		srv_normalize_path_for_win(srv_data_file_names[i]);
724

725 726
		ut_a(strlen(srv_data_home) + strlen(srv_data_file_names[i])
			< (sizeof name) - 1);
727 728
		sprintf(name, "%s%s", srv_data_home, srv_data_file_names[i]);
	
unknown's avatar
unknown committed
729 730 731 732 733 734
		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(name, OS_FILE_CREATE,
735
					OS_FILE_NORMAL, OS_DATA_FILE, &ret);
736

unknown's avatar
unknown committed
737
			if (ret == FALSE && os_file_get_last_error(FALSE) !=
unknown's avatar
unknown committed
738 739 740 741 742 743 744 745
						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
			) {
unknown's avatar
unknown committed
746 747 748 749 750 751 752
				fprintf(stderr,
				"InnoDB: Error in creating or opening %s\n",
				name);

				return(DB_ERROR);
			}
		} else if (srv_data_file_is_raw_partition[i] == SRV_NEW_RAW) {
753 754
			/* The partition is opened, not created; then it is
			written over */
755

unknown's avatar
unknown committed
756
			srv_start_raw_disk_in_use = TRUE;
757 758
			srv_created_new_raw = TRUE;

759
			files[i] = os_file_create(
unknown's avatar
unknown committed
760 761
				name, OS_FILE_OPEN_RAW, OS_FILE_NORMAL,
							OS_DATA_FILE, &ret);
762
			if (!ret) {
763 764 765 766
				fprintf(stderr,
				"InnoDB: Error in opening %s\n", name);

				return(DB_ERROR);
767 768
			}
		} else if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
unknown's avatar
unknown committed
769 770
			srv_start_raw_disk_in_use = TRUE;

771
			ret = FALSE;
unknown's avatar
unknown committed
772 773
		} else {
			ut_a(0);
774 775
		}

776
		if (ret == FALSE) {
unknown's avatar
unknown committed
777
			/* We open the data file */
778 779 780

			if (one_created) {
				fprintf(stderr,
781
	"InnoDB: Error: data files can only be added at the end\n");
782
				fprintf(stderr,
783
	"InnoDB: of a tablespace, but data file %s existed beforehand.\n",
784 785 786 787
				name);
				return(DB_ERROR);
			}
				
unknown's avatar
unknown committed
788 789 790 791 792 793 794 795 796 797
			if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
				files[i] = os_file_create(
					name, OS_FILE_OPEN_RAW, OS_FILE_NORMAL,
							 OS_DATA_FILE, &ret);
			} else {
				files[i] = os_file_create(
					name, OS_FILE_OPEN, OS_FILE_NORMAL,
							 OS_DATA_FILE, &ret);
			}

798 799
			if (!ret) {
				fprintf(stderr,
800
				"InnoDB: Error in opening %s\n", name);
unknown's avatar
unknown committed
801
				os_file_get_last_error(TRUE);
802 803 804 805

				return(DB_ERROR);
			}

unknown's avatar
unknown committed
806 807 808 809 810 811 812 813
			if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {

				goto skip_size_check;
			}

			ret = os_file_get_size(files[i], &size, &size_high);
			ut_a(ret);
			/* Round size downward to megabytes */
814
		
unknown's avatar
unknown committed
815
			rounded_size_pages = (size / (1024 * 1024)
unknown's avatar
unknown committed
816 817 818
							+ 4096 * size_high)
					     << (20 - UNIV_PAGE_SIZE_SHIFT);

unknown's avatar
unknown committed
819
			if (i == srv_n_data_files - 1
unknown's avatar
unknown committed
820 821
				    && srv_auto_extend_last_data_file) {

unknown's avatar
unknown committed
822
				if (srv_data_file_sizes[i] >
unknown's avatar
unknown committed
823 824 825 826 827
				    		rounded_size_pages
				    	   || (srv_last_file_size_max > 0
				    	      && srv_last_file_size_max <
				    	       rounded_size_pages)) {
				    	       	
unknown's avatar
unknown committed
828
					fprintf(stderr,
unknown's avatar
unknown committed
829 830 831
"InnoDB: Error: auto-extending data file %s is of a different size\n"
"InnoDB: %lu pages (rounded down to MB) than specified in the .cnf file:\n"
"InnoDB: initial %lu pages, max %lu (relevant if non-zero) pages!\n",
832 833 834
		  name, (ulong) rounded_size_pages,
		  (ulong) srv_data_file_sizes[i],
		  (ulong) srv_last_file_size_max);
unknown's avatar
unknown committed
835

unknown's avatar
unknown committed
836
					return(DB_ERROR);
unknown's avatar
unknown committed
837
				}
unknown's avatar
unknown committed
838 839 840
				    	     
				srv_data_file_sizes[i] = rounded_size_pages;
			}
unknown's avatar
unknown committed
841
				
unknown's avatar
unknown committed
842
			if (rounded_size_pages != srv_data_file_sizes[i]) {
unknown's avatar
Merge  
unknown committed
843

unknown's avatar
unknown committed
844
				fprintf(stderr,
unknown's avatar
unknown committed
845 846 847
"InnoDB: Error: data file %s is of a different size\n"
"InnoDB: %lu pages (rounded down to MB)\n"
"InnoDB: than specified in the .cnf file %lu pages!\n", name,
848 849
					       (ulong) rounded_size_pages,
					       (ulong) srv_data_file_sizes[i]);
850
				
unknown's avatar
unknown committed
851
				return(DB_ERROR);
852
			}
unknown's avatar
unknown committed
853
skip_size_check:
854 855
			fil_read_flushed_lsn_and_arch_log_no(files[i],
					one_opened,
unknown's avatar
unknown committed
856 857 858 859
#ifdef UNIV_LOG_ARCHIVE
					min_arch_log_no, max_arch_log_no,
#endif /* UNIV_LOG_ARCHIVE */
					min_flushed_lsn, max_flushed_lsn);
860 861
			one_opened = TRUE;
		} else {
unknown's avatar
unknown committed
862 863 864
		        /* We created the data file and now write it full of
			zeros */

865 866 867
			one_created = TRUE;

			if (i > 0) {
unknown's avatar
unknown committed
868
	    			ut_print_timestamp(stderr);
869
				fprintf(stderr, 
unknown's avatar
unknown committed
870
		"  InnoDB: Data file %s did not exist: new to be created\n",
871
									name);
872 873
			} else {
				fprintf(stderr, 
874 875
 		"InnoDB: The first specified data file %s did not exist:\n"
		"InnoDB: a new database to be created!\n", name);
876 877 878
				*create_new_db = TRUE;
			}
			
unknown's avatar
unknown committed
879
	    		ut_print_timestamp(stderr);
unknown's avatar
Merge  
unknown committed
880
			fprintf(stderr, 
unknown's avatar
unknown committed
881
				"  InnoDB: Setting file %s size to %lu MB\n",
882
			       name, (ulong) (srv_data_file_sizes[i]
unknown's avatar
Merge  
unknown committed
883
				      >> (20 - UNIV_PAGE_SIZE_SHIFT)));
884

885
			fprintf(stderr,
unknown's avatar
unknown committed
886
	"InnoDB: Database physically writes the file full: wait...\n");
887

888
			ret = os_file_set_size(name, files[i],
unknown's avatar
Merge  
unknown committed
889 890
				srv_calc_low32(srv_data_file_sizes[i]),
				srv_calc_high32(srv_data_file_sizes[i]));
891 892 893

			if (!ret) {
				fprintf(stderr, 
894
	"InnoDB: Error in creating %s: probably out of disk space\n", name);
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911

				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());

unknown's avatar
unknown committed
912 913 914 915 916 917 918
		if (srv_data_file_is_raw_partition[i]) {

		        fil_node_create(name, srv_data_file_sizes[i], 0, TRUE);
		} else {
		        fil_node_create(name, srv_data_file_sizes[i], 0,
									FALSE);
		}
919 920 921 922 923 924 925 926 927 928 929
	}

	ios = 0;

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

	return(DB_SUCCESS);
}

/********************************************************************
930
Starts InnoDB and creates a new database if database files
931 932 933 934 935 936 937 938
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 */
{
unknown's avatar
unknown committed
939
	buf_pool_t*	ret;
940 941 942 943 944 945
	ibool	create_new_db;
	ibool	log_file_created;
	ibool	log_created	= FALSE;
	ibool	log_opened	= FALSE;
	dulint	min_flushed_lsn;
	dulint	max_flushed_lsn;
unknown's avatar
unknown committed
946
#ifdef UNIV_LOG_ARCHIVE
947 948
	ulint	min_arch_log_no;
	ulint	max_arch_log_no;
unknown's avatar
unknown committed
949
#endif /* UNIV_LOG_ARCHIVE */
950
	ulint   sum_of_new_sizes;
unknown's avatar
unknown committed
951 952
	ulint	sum_of_data_file_sizes;
	ulint	tablespace_size_in_header;
953 954
	ulint	err;
	ulint	i;
unknown's avatar
unknown committed
955
	ibool	srv_file_per_table_original_value  = srv_file_per_table;
956
	mtr_t   mtr;
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
#ifdef HAVE_DARWIN_THREADS
# ifdef F_FULLFSYNC
	/* This executable has been compiled on Mac OS X 10.3 or later.
	Assume that F_FULLFSYNC is available at run-time. */
	srv_have_fullfsync = TRUE;
# else /* F_FULLFSYNC */
	/* This executable has been compiled on Mac OS X 10.2
	or earlier.  Determine if the executable is running
	on Mac OS X 10.3 or later. */
	struct utsname utsname;
	if (uname(&utsname)) {
		fputs("InnoDB: cannot determine Mac OS X version!\n", stderr);
	} else {
		srv_have_fullfsync = strcmp(utsname.release, "7.") >= 0;
	}
	if (!srv_have_fullfsync) {
		fputs(
"InnoDB: On Mac OS X, fsync() may be broken on internal drives,\n"
"InnoDB: making transactions unsafe!\n", stderr);
	}
# endif /* F_FULLFSYNC */
#endif /* HAVE_DARWIN_THREADS */
979

unknown's avatar
unknown committed
980 981 982 983 984 985 986 987
	if (sizeof(ulint) != sizeof(void*)) {
		fprintf(stderr,
"InnoDB: Error: size of InnoDB's ulint is %lu, but size of void* is %lu.\n"
"InnoDB: The sizes should be the same so that on a 64-bit platform you can\n"
"InnoDB: allocate more than 4 GB of memory.",
			(ulong)sizeof(ulint), (ulong)sizeof(void*));
	}

unknown's avatar
unknown committed
988 989
	srv_file_per_table = FALSE; /* system tables are created in tablespace
									0 */
unknown's avatar
unknown committed
990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
#ifdef UNIV_DEBUG
	fprintf(stderr,
"InnoDB: !!!!!!!!!!!!!! UNIV_DEBUG switched on !!!!!!!!!!!!!!!\n"); 
#endif

#ifdef UNIV_SYNC_DEBUG
	fprintf(stderr,
"InnoDB: !!!!!!!!!!!!!! UNIV_SYNC_DEBUG switched on !!!!!!!!!!!!!!!\n"); 
#endif

#ifdef UNIV_SEARCH_DEBUG
	fprintf(stderr,
"InnoDB: !!!!!!!!!!!!!! UNIV_SEARCH_DEBUG switched on !!!!!!!!!!!!!!!\n"); 
#endif

#ifdef UNIV_MEM_DEBUG
	fprintf(stderr,
"InnoDB: !!!!!!!!!!!!!! UNIV_MEM_DEBUG switched on !!!!!!!!!!!!!!!\n"); 
unknown's avatar
unknown committed
1008 1009 1010 1011 1012
#endif

#ifdef UNIV_SIMULATE_AWE
	fprintf(stderr,
"InnoDB: !!!!!!!!!!!!!! UNIV_SIMULATE_AWE switched on !!!!!!!!!!!!!!!!!\n");
unknown's avatar
unknown committed
1013
#endif
1014 1015 1016 1017 1018
        if (srv_sizeof_trx_t_in_ha_innodb_cc != (ulint)sizeof(trx_t)) {
	        fprintf(stderr,
  "InnoDB: Error: trx_t size is %lu in ha_innodb.cc but %lu in srv0start.c\n"
  "InnoDB: Check that pthread_mutex_t is defined in the same way in these\n"
  "InnoDB: compilation modules. Cannot continue.\n",
1019 1020
		 (ulong)  srv_sizeof_trx_t_in_ha_innodb_cc,
		 (ulong) sizeof(trx_t));
1021 1022 1023
		return(DB_ERROR);
	}

unknown's avatar
unknown committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
	/* Since InnoDB does not currently clean up all its internal data
	   structures in MySQL Embedded Server Library server_end(), we
	   print an error message if someone tries to start up InnoDB a
	   second time during the process lifetime. */

	if (srv_start_has_been_called) {
	        fprintf(stderr,
"InnoDB: Error:startup called second time during the process lifetime.\n"
"InnoDB: In the MySQL Embedded Server Library you cannot call server_init()\n"
"InnoDB: more than once during the process lifetime.\n");
	}

	srv_start_has_been_called = TRUE;

1038 1039 1040
	log_do_write = TRUE;
/*	yydebug = TRUE; */

unknown's avatar
unknown committed
1041
	srv_is_being_started = TRUE;
1042
        srv_startup_is_before_trx_rollback_phase = TRUE;
unknown's avatar
unknown committed
1043 1044
	os_aio_use_native_aio = FALSE;

1045
#if !defined(__WIN2000__) && !defined(UNIV_SIMULATE_AWE)
unknown's avatar
unknown committed
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	if (srv_use_awe) {

	        fprintf(stderr,
"InnoDB: Error: You have specified innodb_buffer_pool_awe_mem_mb\n"
"InnoDB: in my.cnf, but AWE can only be used in Windows 2000 and later.\n");

	        return(DB_ERROR);
	}
#endif

unknown's avatar
unknown committed
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
#ifdef __WIN__
	if (os_get_os_version() == OS_WIN95
	    || os_get_os_version() == OS_WIN31
	    || os_get_os_version() == OS_WINNT) {

	  	/* On Win 95, 98, ME, Win32 subsystem for Windows 3.1,
		and NT use simulated aio. In NT Windows provides async i/o,
		but when run in conjunction with InnoDB Hot Backup, it seemed
		to corrupt the data files. */

	  	os_aio_use_native_aio = FALSE;
	} else {
	  	/* On Win 2000 and XP use async i/o */
	  	os_aio_use_native_aio = TRUE;
	}
#endif	
        if (srv_file_flush_method_str == NULL) {
        	/* These are the default options */
unknown's avatar
unknown committed
1074

unknown's avatar
unknown committed
1075 1076 1077 1078
		srv_unix_file_flush_method = SRV_UNIX_FDATASYNC;

		srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
#ifndef __WIN__        
unknown's avatar
unknown committed
1079
	} else if (0 == ut_strcmp(srv_file_flush_method_str, "fdatasync")) {
1080 1081
	  	srv_unix_file_flush_method = SRV_UNIX_FDATASYNC;

unknown's avatar
unknown committed
1082
	} else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DSYNC")) {
1083 1084
	  	srv_unix_file_flush_method = SRV_UNIX_O_DSYNC;

unknown's avatar
unknown committed
1085
	} else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DIRECT")) {
1086 1087
	  	srv_unix_file_flush_method = SRV_UNIX_O_DIRECT;

unknown's avatar
unknown committed
1088
	} else if (0 == ut_strcmp(srv_file_flush_method_str, "littlesync")) {
1089 1090
	  	srv_unix_file_flush_method = SRV_UNIX_LITTLESYNC;

unknown's avatar
unknown committed
1091
	} else if (0 == ut_strcmp(srv_file_flush_method_str, "nosync")) {
1092
	  	srv_unix_file_flush_method = SRV_UNIX_NOSYNC;
unknown's avatar
unknown committed
1093
#else
unknown's avatar
unknown committed
1094
	} else if (0 == ut_strcmp(srv_file_flush_method_str, "normal")) {
unknown's avatar
unknown committed
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
	  	srv_win_file_flush_method = SRV_WIN_IO_NORMAL;
	  	os_aio_use_native_aio = FALSE;

	} else if (0 == ut_strcmp(srv_file_flush_method_str, "unbuffered")) {
	  	srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
	  	os_aio_use_native_aio = FALSE;

	} else if (0 == ut_strcmp(srv_file_flush_method_str,
							"async_unbuffered")) {
	  	srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;	
#endif
1106
	} else {
1107 1108
	  	fprintf(stderr, 
          	"InnoDB: Unrecognized value %s for innodb_flush_method\n",
unknown's avatar
unknown committed
1109
          				srv_file_flush_method_str);
1110
	  	return(DB_ERROR);
1111
	}
1112

unknown's avatar
unknown committed
1113 1114
	/* Note that the call srv_boot() also changes the values of
	srv_pool_size etc. to the units used by InnoDB internally */
1115

1116
        /* Set the maximum number of threads which can wait for a semaphore
unknown's avatar
unknown committed
1117 1118 1119 1120
        inside InnoDB: this is the 'sync wait array' size, as well as the
	maximum number of threads that can wait in the 'srv_conc array' for
	their time to enter InnoDB. */

1121 1122 1123 1124 1125 1126 1127 1128
#if defined(__WIN__) || defined(__NETWARE__)

/* Create less event semaphores because Win 98/ME had difficulty creating
40000 event semaphores.
Comment from Novell, Inc.: also, these just take a lot of memory on
NetWare. */
        srv_max_n_threads = 1000;
#else
unknown's avatar
unknown committed
1129
        if (srv_pool_size >= 1000 * 1024) {
1130
                                  /* Here we still have srv_pool_size counted
unknown's avatar
unknown committed
1131 1132 1133
                                  in kilobytes (in 4.0 this was in bytes)
				  srv_boot() converts the value to
                                  pages; if buffer pool is less than 1000 MB,
1134
                                  assume fewer threads. */
unknown's avatar
unknown committed
1135 1136 1137 1138
                srv_max_n_threads = 50000;

        } else if (srv_pool_size >= 8 * 1024) {

1139 1140 1141 1142 1143 1144 1145
                srv_max_n_threads = 10000;
        } else {
		srv_max_n_threads = 1000;       /* saves several MB of memory,
                                                especially in 64-bit
                                                computers */
        }
#endif
unknown's avatar
unknown committed
1146
	err = srv_boot(); /* This changes srv_pool_size to units of a page */
1147 1148 1149 1150 1151 1152

	if (err != DB_SUCCESS) {

		return((int) err);
	}

1153
	mutex_create(&srv_monitor_file_mutex);
1154
	mutex_set_level(&srv_monitor_file_mutex, SYNC_NO_ORDER_CHECK);
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172
	if (srv_innodb_status) {
		srv_monitor_file_name = mem_alloc(
				strlen(fil_path_to_mysql_datadir) +
				20 + sizeof "/innodb_status.");
		sprintf(srv_monitor_file_name, "%s/innodb_status.%lu",
			fil_path_to_mysql_datadir, os_proc_get_number());
		srv_monitor_file = fopen(srv_monitor_file_name, "w+");
		if (!srv_monitor_file) {
			fprintf(stderr, "InnoDB: unable to create %s: %s\n",
				srv_monitor_file_name, strerror(errno));
			return(DB_ERROR);
		}
	} else {
		srv_monitor_file_name = NULL;
		srv_monitor_file = os_file_create_tmpfile();
		if (!srv_monitor_file) {
			return(DB_ERROR);
		}
1173 1174
	}

1175 1176
	/* Restrict the maximum number of file i/o threads */
	if (srv_n_file_io_threads > SRV_MAX_N_IO_THREADS) {
unknown's avatar
unknown committed
1177

1178 1179 1180
		srv_n_file_io_threads = SRV_MAX_N_IO_THREADS;
	}

unknown's avatar
unknown committed
1181 1182 1183
	if (!os_aio_use_native_aio) {
 		/* In simulated aio we currently have use only for 4 threads */
		srv_n_file_io_threads = 4;
1184

unknown's avatar
unknown committed
1185
		os_aio_init(8 * SRV_N_PENDING_IOS_PER_THREAD
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
						* 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);
	}
	
unknown's avatar
unknown committed
1196
	fil_init(srv_max_n_open_files);
1197

unknown's avatar
unknown committed
1198 1199 1200
	if (srv_use_awe) {
		fprintf(stderr,
"InnoDB: Using AWE: Memory window is %lu MB and AWE memory is %lu MB\n",
1201 1202
		(ulong) (srv_awe_window_size / ((1024 * 1024) / UNIV_PAGE_SIZE)),
		(ulong) (srv_pool_size / ((1024 * 1024) / UNIV_PAGE_SIZE)));
unknown's avatar
unknown committed
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215

		/* We must disable adaptive hash indexes because they do not
		tolerate remapping of pages in AWE */
		
		srv_use_adaptive_hash_indexes = FALSE;
		ret = buf_pool_init(srv_pool_size, srv_pool_size,
							srv_awe_window_size);
	} else {
		ret = buf_pool_init(srv_pool_size, srv_pool_size,
							srv_pool_size);
	}

	if (ret == NULL) {
unknown's avatar
unknown committed
1216 1217 1218
		fprintf(stderr,
"InnoDB: Fatal error: cannot allocate the memory for the buffer pool\n");

unknown's avatar
unknown committed
1219 1220
		return(DB_ERROR);
	}
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232

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

	/* 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);
1233
    	}
1234

unknown's avatar
unknown committed
1235
#ifdef UNIV_LOG_ARCHIVE
unknown's avatar
unknown committed
1236 1237 1238 1239 1240 1241 1242
	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);
	}
unknown's avatar
unknown committed
1243
#endif /* UNIV_LOG_ARCHIVE */
unknown's avatar
unknown committed
1244

unknown's avatar
unknown committed
1245
	if (srv_n_log_files * srv_log_file_size >= 262144) {
unknown's avatar
Merge  
unknown committed
1246
		fprintf(stderr,
unknown's avatar
unknown committed
1247
		"InnoDB: Error: combined size of log files must be < 4 GB\n");
1248

unknown's avatar
Merge  
unknown committed
1249 1250 1251 1252 1253
		return(DB_ERROR);
	}

	sum_of_new_sizes = 0;
	
1254
	for (i = 0; i < srv_n_data_files; i++) {
unknown's avatar
Merge  
unknown committed
1255 1256
#ifndef __WIN__
		if (sizeof(off_t) < 5 && srv_data_file_sizes[i] >= 262144) {
1257
		 	fprintf(stderr,
unknown's avatar
Merge  
unknown committed
1258 1259
	"InnoDB: Error: file size must be < 4 GB with this MySQL binary\n"
	"InnoDB: and operating system combination, in some OS's < 2 GB\n");
1260 1261 1262

		  	return(DB_ERROR);
		}
unknown's avatar
Merge  
unknown committed
1263
#endif
1264
		sum_of_new_sizes += srv_data_file_sizes[i];
1265 1266 1267
	}

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

1271
		  return(DB_ERROR);
1272 1273
	}

1274
	err = open_or_create_data_files(&create_new_db,
unknown's avatar
unknown committed
1275 1276 1277 1278
#ifdef UNIV_LOG_ARCHIVE
					&min_arch_log_no, &max_arch_log_no,
#endif /* UNIV_LOG_ARCHIVE */
					&min_flushed_lsn, &max_flushed_lsn,
1279 1280
					&sum_of_new_sizes);
	if (err != DB_SUCCESS) {
unknown's avatar
unknown committed
1281 1282 1283 1284 1285 1286 1287 1288
	        fprintf(stderr,
"InnoDB: Could not open or create data files.\n"
"InnoDB: If you tried to add new data files, and it failed here,\n"
"InnoDB: you should now edit innodb_data_file_path in my.cnf back\n"
"InnoDB: to what it was, and remove the new ibdata files InnoDB created\n"
"InnoDB: in this failed attempt. InnoDB only wrote those files full of\n"
"InnoDB: zeros, but did not yet use them in any way. But be careful: do not\n"
"InnoDB: remove old data files which contain your precious data!\n");
1289

1290 1291 1292
		return((int) err);
	}

unknown's avatar
unknown committed
1293
#ifdef UNIV_LOG_ARCHIVE
unknown's avatar
unknown committed
1294 1295
	srv_normalize_path_for_win(srv_arch_dir);
	srv_arch_dir = srv_add_path_separator_if_needed(srv_arch_dir);
unknown's avatar
unknown committed
1296
#endif /* UNIV_LOG_ARCHIVE */
unknown's avatar
unknown committed
1297 1298 1299 1300 1301
		
	for (i = 0; i < srv_n_log_files; i++) {
		err = open_or_create_log_file(create_new_db, &log_file_created,
							     log_opened, 0, i);
		if (err != DB_SUCCESS) {
unknown's avatar
unknown committed
1302

unknown's avatar
unknown committed
1303 1304
			return((int) err);
		}
1305

unknown's avatar
unknown committed
1306 1307 1308 1309 1310 1311
		if (log_file_created) {
			log_created = TRUE;
		} else {
			log_opened = TRUE;
		}
		if ((log_opened && create_new_db)
1312
			    		|| (log_opened && log_created)) {
unknown's avatar
unknown committed
1313
			fprintf(stderr, 
1314
	"InnoDB: Error: all log files must be created at the same time.\n"
unknown's avatar
unknown committed
1315 1316 1317
	"InnoDB: All log files must be created also in database creation.\n"
	"InnoDB: If you want bigger or smaller log files, shut down the\n"
	"InnoDB: database and make sure there were no errors in shutdown.\n"
1318 1319
	"InnoDB: Then delete the existing log files. Edit the .cnf file\n"
	"InnoDB: and start the database again.\n");
1320

unknown's avatar
unknown committed
1321
			return(DB_ERROR);
1322 1323 1324
		}
	}

unknown's avatar
unknown committed
1325 1326
	/* Open all log files and data files in the system tablespace: we
	keep them open until database shutdown */
1327

unknown's avatar
unknown committed
1328 1329
	fil_open_log_and_system_tablespace_files();

unknown's avatar
unknown committed
1330 1331 1332 1333 1334
	if (log_created && !create_new_db
#ifdef UNIV_LOG_ARCHIVE
		&& !srv_archive_recovery
#endif /* UNIV_LOG_ARCHIVE */
	) {
1335
		if (ut_dulint_cmp(max_flushed_lsn, min_flushed_lsn) != 0
unknown's avatar
unknown committed
1336 1337 1338 1339
#ifdef UNIV_LOG_ARCHIVE
				|| max_arch_log_no != min_arch_log_no
#endif /* UNIV_LOG_ARCHIVE */
		) {
1340
			fprintf(stderr, 
1341 1342
		"InnoDB: Cannot initialize created log files because\n"
		"InnoDB: data files were not in sync with each other\n"
unknown's avatar
unknown committed
1343
		"InnoDB: or the data files are corrupt.\n");
1344 1345 1346 1347 1348 1349 1350

			return(DB_ERROR);
		}

		if (ut_dulint_cmp(max_flushed_lsn, ut_dulint_create(0, 1000))
		    < 0) {
		    	fprintf(stderr,
1351 1352 1353 1354 1355
		"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");
1356 1357 1358 1359 1360 1361

			return(DB_ERROR);
		}

		mutex_enter(&(log_sys->mutex));

unknown's avatar
unknown committed
1362
#ifdef UNIV_LOG_ARCHIVE
unknown's avatar
unknown committed
1363 1364 1365
		/* Do not + 1 arch_log_no because we do not use log
		archiving */
		recv_reset_logs(max_flushed_lsn, max_arch_log_no, TRUE);
unknown's avatar
unknown committed
1366 1367 1368 1369
#else
		recv_reset_logs(max_flushed_lsn, TRUE);
#endif /* UNIV_LOG_ARCHIVE */

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
		mutex_exit(&(log_sys->mutex));
	}

	if (create_new_db) {
		mtr_start(&mtr);

		fsp_header_init(0, sum_of_new_sizes, &mtr);		

		mtr_commit(&mtr);

		trx_sys_create();
		dict_create();
1382
                srv_startup_is_before_trx_rollback_phase = FALSE;
1383

unknown's avatar
unknown committed
1384
#ifdef UNIV_LOG_ARCHIVE
1385 1386
	} else if (srv_archive_recovery) {
		fprintf(stderr,
1387
	"InnoDB: Starting archive recovery from a backup...\n");
1388 1389 1390 1391 1392 1393 1394 1395
		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);
		}
1396 1397 1398
		/* Since ibuf init is in dict_boot, and ibuf is needed
		in any disk i/o, first call dict_boot */

1399
		dict_boot();
1400 1401 1402
		trx_sys_init_at_db_start();
                srv_startup_is_before_trx_rollback_phase = FALSE;

unknown's avatar
unknown committed
1403 1404 1405 1406
		/* Initialize the fsp free limit global variable in the log
		system */
		fsp_header_get_free_limit(0);

1407
		recv_recovery_from_archive_finish();
unknown's avatar
unknown committed
1408
#endif /* UNIV_LOG_ARCHIVE */
1409 1410
	} else {
		/* We always try to do a recovery, even if the database had
unknown's avatar
unknown committed
1411
		been shut down normally: this is the normal startup path */
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421
		
		err = recv_recovery_from_checkpoint_start(LOG_CHECKPOINT,
							ut_dulint_max,
							min_flushed_lsn,
							max_flushed_lsn);
		if (err != DB_SUCCESS) {

			return(DB_ERROR);
		}

unknown's avatar
unknown committed
1422 1423 1424 1425 1426
		/* Since the insert buffer init is in dict_boot, and the
		insert buffer is needed in any disk i/o, first we call
		dict_boot(). Note that trx_sys_init_at_db_start() only needs
		to access space 0, and the insert buffer at this stage already
		works for space 0. */
unknown's avatar
unknown committed
1427

1428
		dict_boot();
1429
		trx_sys_init_at_db_start();
1430

unknown's avatar
unknown committed
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
		if (srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE) {
			/* The following call is necessary for the insert
			buffer to work with multiple tablespaces. We must
			know the mapping between space id's and .ibd file
			names.

			In a crash recovery, we check that the info in data
			dictionary is consistent with what we already know
			about space id's from the call of
			fil_load_single_table_tablespaces().

			In a normal startup, we create the space objects for
			every table in the InnoDB data dictionary that has
			an .ibd file.
	
			We also determine the maximum tablespace id used.

			TODO: We may have incomplete transactions in the
			data dictionary tables. Does that harm the scanning of
			the data dictionary below? */

			dict_check_tablespaces_and_store_max_id(
							recv_needed_recovery);
		}
1455 1456

                srv_startup_is_before_trx_rollback_phase = FALSE;
unknown's avatar
unknown committed
1457 1458 1459 1460 1461

		/* Initialize the fsp free limit global variable in the log
		system */
		fsp_header_get_free_limit(0);

unknown's avatar
unknown committed
1462 1463 1464
		/* recv_recovery_from_checkpoint_finish needs trx lists which
		are initialized in trx_sys_init_at_db_start(). */

1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
		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);

unknown's avatar
unknown committed
1476 1477 1478
		/* Immediately write the log record about increased tablespace
		size to disk, so that it is durable even if mysqld would crash
		quickly */
unknown's avatar
unknown committed
1479

unknown's avatar
unknown committed
1480 1481
		log_buffer_flush_to_disk();
	}
1482

unknown's avatar
unknown committed
1483
#ifdef UNIV_LOG_ARCHIVE
unknown's avatar
unknown committed
1484
	/* Archiving is always off under MySQL */
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
	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());
		}
	}
unknown's avatar
unknown committed
1502
#endif /* UNIV_LOG_ARCHIVE */
unknown's avatar
unknown committed
1503

1504
	if (srv_measure_contention) {
1505
	  	/* os_thread_create(&test_measure_cont, NULL, thread_ids +
1506
                             	     SRV_MAX_N_IO_THREADS); */
1507 1508 1509 1510 1511
	}

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

1512 1513 1514 1515
	/* 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,
unknown's avatar
unknown committed
1516
				thread_ids + 2 + SRV_MAX_N_IO_THREADS);	
1517 1518 1519

	/* Create the thread which warns of long semaphore waits */
	os_thread_create(&srv_error_monitor_thread, NULL,
unknown's avatar
unknown committed
1520
				thread_ids + 3 + SRV_MAX_N_IO_THREADS);	
unknown's avatar
unknown committed
1521 1522 1523
	srv_was_started = TRUE;
	srv_is_being_started = FALSE;

unknown's avatar
unknown committed
1524
#ifdef UNIV_DEBUG
unknown's avatar
unknown committed
1525 1526 1527 1528 1529
        /* Wait a while so that the created threads have time to suspend
	themselves before we switch sync debugging on; otherwise a thread may
	execute mutex_enter() before the checks are on, and mutex_exit() after
	the checks are on, which will cause an assertion failure in sync
	debug. */
unknown's avatar
unknown committed
1530

unknown's avatar
unknown committed
1531
        os_thread_sleep(3000000);
unknown's avatar
unknown committed
1532
#endif
1533 1534
	sync_order_checks_on = TRUE;

unknown's avatar
unknown committed
1535 1536 1537
        if (srv_use_doublewrite_buf && trx_doublewrite == NULL) {
		/* Create the doublewrite buffer to a new tablespace */

1538 1539 1540
		trx_sys_create_doublewrite_buf();
	}

1541 1542 1543 1544 1545
	err = dict_create_or_check_foreign_constraint_tables();

	if (err != DB_SUCCESS) {
		return((int)DB_ERROR);
	}
unknown's avatar
unknown committed
1546
	
unknown's avatar
unknown committed
1547 1548
	/* Create the master thread which does purge and other utility
	operations */
1549 1550 1551

	os_thread_create(&srv_master_thread, NULL, thread_ids + 1 +
							SRV_MAX_N_IO_THREADS);
1552
	/* buf_debug_prints = TRUE; */
unknown's avatar
unknown committed
1553 1554

	sum_of_data_file_sizes = 0;
1555
	
unknown's avatar
unknown committed
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567
	for (i = 0; i < srv_n_data_files; i++) {
		sum_of_data_file_sizes += srv_data_file_sizes[i];
	}

	tablespace_size_in_header = fsp_header_get_tablespace_size(0);

	if (!srv_auto_extend_last_data_file
		&& sum_of_data_file_sizes != tablespace_size_in_header) {

		fprintf(stderr,
"InnoDB: Error: tablespace size stored in header is %lu pages, but\n"
"InnoDB: the sum of data file sizes is %lu pages\n",
1568 1569
 			(ulong) tablespace_size_in_header,
			(ulong) sum_of_data_file_sizes);
unknown's avatar
unknown committed
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584

		if (srv_force_recovery == 0
		    && sum_of_data_file_sizes < tablespace_size_in_header) {
			/* This is a fatal error, the tail of a tablespace is
			missing */

			fprintf(stderr,
"InnoDB: Cannot start InnoDB. The tail of the system tablespace is\n"
"InnoDB: missing. Have you edited innodb_data_file_path in my.cnf in an\n"
"InnoDB: inappropriate way, removing ibdata files from there?\n"
"InnoDB: You can set innodb_force_recovery=1 in my.cnf to force\n"
"InnoDB: a startup if you are trying to recover a badly corrupt database.\n");

			return(DB_ERROR);
		}
unknown's avatar
unknown committed
1585 1586 1587 1588 1589 1590 1591 1592
	}

	if (srv_auto_extend_last_data_file
		&& sum_of_data_file_sizes < tablespace_size_in_header) {

		fprintf(stderr,
"InnoDB: Error: tablespace size stored in header is %lu pages, but\n"
"InnoDB: the sum of data file sizes is only %lu pages\n",
1593 1594
 			(ulong) tablespace_size_in_header,
			(ulong) sum_of_data_file_sizes);
unknown's avatar
unknown committed
1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606

		if (srv_force_recovery == 0) {

			fprintf(stderr,
"InnoDB: Cannot start InnoDB. The tail of the system tablespace is\n"
"InnoDB: missing. Have you edited innodb_data_file_path in my.cnf in an\n"
"InnoDB: inappropriate way, removing ibdata files from there?\n"
"InnoDB: You can set innodb_force_recovery=1 in my.cnf to force\n"
"InnoDB: a startup if you are trying to recover a badly corrupt database.\n");

			return(DB_ERROR);
		}
unknown's avatar
unknown committed
1607 1608
	}

unknown's avatar
unknown committed
1609
	/* Check that os_fast_mutexes work as expected */
unknown's avatar
unknown committed
1610 1611 1612 1613 1614
	os_fast_mutex_init(&srv_os_test_mutex);

	if (0 != os_fast_mutex_trylock(&srv_os_test_mutex)) {
	        fprintf(stderr,
"InnoDB: Error: pthread_mutex_trylock returns an unexpected value on\n"
unknown's avatar
unknown committed
1615
"InnoDB: success! Cannot continue.\n");
unknown's avatar
unknown committed
1616 1617 1618 1619 1620 1621 1622 1623 1624
	        exit(1);
	}

	os_fast_mutex_unlock(&srv_os_test_mutex);

        os_fast_mutex_lock(&srv_os_test_mutex);

	os_fast_mutex_unlock(&srv_os_test_mutex);

unknown's avatar
unknown committed
1625
	os_fast_mutex_free(&srv_os_test_mutex);
unknown's avatar
unknown committed
1626

unknown's avatar
unknown committed
1627 1628
	if (srv_print_verbose_log) {
	  	ut_print_timestamp(stderr);
unknown's avatar
unknown committed
1629 1630
	  	fprintf(stderr,
"  InnoDB: Started; log sequence number %lu %lu\n",
1631 1632
			(ulong) ut_dulint_get_high(srv_start_lsn),
			(ulong) ut_dulint_get_low(srv_start_lsn));
1633
	}
unknown's avatar
unknown committed
1634 1635 1636 1637

	if (srv_force_recovery > 0) {
		fprintf(stderr,
		"InnoDB: !!! innodb_force_recovery is set to %lu !!!\n",
1638
			(ulong) srv_force_recovery);
unknown's avatar
unknown committed
1639 1640 1641
	}

	fflush(stderr);
unknown's avatar
unknown committed
1642

unknown's avatar
unknown committed
1643
	if (trx_doublewrite_must_reset_space_ids) {
unknown's avatar
unknown committed
1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
		/* Actually, we did not change the undo log format between
		4.0 and 4.1.1, and we would not need to run purge to
		completion. Note also that the purge algorithm in 4.1.1
		can process the the history list again even after a full
		purge, because our algorithm does not cut the end of the
		history list in all cases so that it would become empty
		after a full purge. That mean that we may purge 4.0 type
		undo log even after this phase.
		
		The insert buffer record format changed between 4.0 and
		4.1.1. It is essential that the insert buffer is emptied
		here! */

unknown's avatar
unknown committed
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
		fprintf(stderr,
"InnoDB: You are upgrading to an InnoDB version which allows multiple\n"
"InnoDB: tablespaces. Wait that purge and insert buffer merge run to\n"
"InnoDB: completion...\n");
		for (;;) {
			os_thread_sleep(1000000);

			if (0 == strcmp(srv_main_thread_op_info,
					"waiting for server activity")) {

				ut_a(ibuf_is_empty());
				
				break;
			}
		}
		fprintf(stderr,
"InnoDB: Full purge and insert buffer merge completed.\n");

	        trx_sys_mark_upgraded_to_multiple_tablespaces();

		fprintf(stderr,
"InnoDB: You have now successfully upgraded to the multiple tablespaces\n"
unknown's avatar
unknown committed
1679 1680 1681 1682
"InnoDB: format. You should NOT DOWNGRADE to an earlier version of\n"
"InnoDB: InnoDB! But if you absolutely need to downgrade, see\n"
"InnoDB: http://dev.mysql.com/doc/mysql/en/Multiple_tablespaces.html\n"
"InnoDB: for instructions.\n");
unknown's avatar
unknown committed
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695
	}

	if (srv_force_recovery == 0) {
		/* In the insert buffer we may have even bigger tablespace
		id's, because we may have dropped those tablespaces, but
		insert buffer merge has not had time to clean the records from
		the ibuf tree. */

		ibuf_update_max_tablespace_id();
	}

	srv_file_per_table = srv_file_per_table_original_value;

1696 1697 1698 1699
	return((int) DB_SUCCESS);
}

/********************************************************************
1700
Shuts down the InnoDB database. */
1701 1702 1703 1704 1705 1706

int
innobase_shutdown_for_mysql(void) 
/*=============================*/
				/* out: DB_SUCCESS or error code */
{
unknown's avatar
unknown committed
1707
	ulint   i;
1708 1709 1710
#ifdef __NETWARE__
	extern ibool panic_shutdown;
#endif
unknown's avatar
unknown committed
1711
        if (!srv_was_started) {
1712 1713 1714
	  	if (srv_is_being_started) {
	    		ut_print_timestamp(stderr);
            		fprintf(stderr, 
unknown's avatar
unknown committed
1715 1716
"  InnoDB: Warning: shutting down a not properly started\n"
"                 InnoDB: or created database!\n");
1717 1718 1719
	  	}

	  	return(DB_SUCCESS);
unknown's avatar
unknown committed
1720 1721
	}

unknown's avatar
unknown committed
1722
	/* 1. Flush the buffer pool to disk, write the current lsn to
unknown's avatar
unknown committed
1723
	the tablespace header(s), and copy all log data to archive.
unknown's avatar
unknown committed
1724
	The step 1 is the real InnoDB shutdown. The remaining steps 2 - ...
unknown's avatar
unknown committed
1725
	just free data structures after the shutdown. */
1726

1727 1728 1729
#ifdef __NETWARE__
	if(!panic_shutdown)
#endif 
1730
	logs_empty_and_mark_files_at_shutdown();
1731

unknown's avatar
unknown committed
1732 1733 1734 1735 1736 1737
	if (srv_conc_n_threads != 0) {
		fprintf(stderr,
		"InnoDB: Warning: query counter shows %ld queries still\n"
		"InnoDB: inside InnoDB at shutdown\n",
		srv_conc_n_threads);
	}
unknown's avatar
unknown committed
1738

unknown's avatar
unknown committed
1739
	/* 2. Make all threads created by InnoDB to exit */
unknown's avatar
unknown committed
1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750

	srv_shutdown_state = SRV_SHUTDOWN_EXIT_THREADS;

	/* All threads end up waiting for certain events. Put those events
	to the signaled state. Then the threads will exit themselves in
	os_thread_event_wait(). */

	for (i = 0; i < 1000; i++) {
	        /* NOTE: IF YOU CREATE THREADS IN INNODB, YOU MUST EXIT THEM
	        HERE OR EARLIER */
		
unknown's avatar
unknown committed
1751
		/* a. Let the lock timeout thread exit */
unknown's avatar
unknown committed
1752 1753
		os_event_set(srv_lock_timeout_thread_event);		

unknown's avatar
unknown committed
1754
		/* b. srv error monitor thread exits automatically, no need
unknown's avatar
unknown committed
1755 1756
		to do anything here */

unknown's avatar
unknown committed
1757
		/* c. We wake the master thread so that it exits */
unknown's avatar
unknown committed
1758 1759
		srv_wake_master_thread();

unknown's avatar
unknown committed
1760
		/* d. Exit the i/o threads */
unknown's avatar
unknown committed
1761 1762 1763

		os_aio_wake_all_threads_at_shutdown();

unknown's avatar
unknown committed
1764
		os_mutex_enter(os_sync_mutex);
unknown's avatar
unknown committed
1765 1766 1767 1768 1769 1770 1771 1772

		if (os_thread_count == 0) {
		        /* All the threads have exited or are just exiting;
			NOTE that the threads may not have completed their
			exit yet. Should we use pthread_join() to make sure
			they have exited? Now we just sleep 0.1 seconds and
			hope that is enough! */

unknown's avatar
unknown committed
1773
			os_mutex_exit(os_sync_mutex);
unknown's avatar
unknown committed
1774 1775 1776 1777 1778 1779

			os_thread_sleep(100000);

			break;
		}

unknown's avatar
unknown committed
1780
		os_mutex_exit(os_sync_mutex);
unknown's avatar
unknown committed
1781 1782 1783 1784 1785 1786 1787

		os_thread_sleep(100000);
	}

	if (i == 1000) {
	        fprintf(stderr,
"InnoDB: Warning: %lu threads created by InnoDB had not exited at shutdown!\n",
1788
		      (ulong) os_thread_count);
unknown's avatar
unknown committed
1789 1790
	}

1791 1792 1793
	if (srv_monitor_file) {
		fclose(srv_monitor_file);
		srv_monitor_file = 0;
1794 1795 1796 1797
		if (srv_monitor_file_name) {
			unlink(srv_monitor_file_name);
			mem_free(srv_monitor_file_name);
		}
1798
	}
1799
	
1800 1801
	mutex_free(&srv_monitor_file_mutex);

1802 1803
	/* 3. Free all InnoDB's own mutexes and the os_fast_mutexes inside
	them */
unknown's avatar
unknown committed
1804 1805
	sync_close();

1806
	/* 4. Free the os_conc_mutex and all os_events and os_mutexes */
unknown's avatar
unknown committed
1807 1808

	srv_free();
unknown's avatar
unknown committed
1809
	os_sync_free();
unknown's avatar
unknown committed
1810

1811
	/* 5. Free all allocated memory and the os_fast_mutex created in
unknown's avatar
unknown committed
1812
	ut0mem.c */
unknown's avatar
unknown committed
1813

1814
	ut_free_all_mem();
unknown's avatar
unknown committed
1815

1816 1817 1818 1819 1820 1821 1822
	if (os_thread_count != 0
	    || os_event_count != 0
	    || os_mutex_count != 0
	    || os_fast_mutex_count != 0) {
	        fprintf(stderr,
"InnoDB: Warning: some resources were not cleaned up in shutdown:\n"
"InnoDB: threads %lu, events %lu, os_mutexes %lu, os_fast_mutexes %lu\n",
1823 1824
			(ulong) os_thread_count, (ulong) os_event_count,
			(ulong) os_mutex_count, (ulong) os_fast_mutex_count);
1825 1826
	}

1827 1828 1829 1830 1831 1832 1833
	if (dict_foreign_err_file) {
		fclose(dict_foreign_err_file);
	}
	if (lock_latest_err_file) {
		fclose(lock_latest_err_file);
	}

unknown's avatar
unknown committed
1834 1835
	if (srv_print_verbose_log) {
	        ut_print_timestamp(stderr);
unknown's avatar
unknown committed
1836 1837
	        fprintf(stderr,
"  InnoDB: Shutdown completed; log sequence number %lu %lu\n",
1838 1839
			       (ulong) ut_dulint_get_high(srv_shutdown_lsn),
			       (ulong) ut_dulint_get_low(srv_shutdown_lsn));
unknown's avatar
unknown committed
1840
	}
unknown's avatar
unknown committed
1841

1842 1843
	return((int) DB_SUCCESS);
}
1844 1845 1846 1847 1848 1849 1850

#ifdef __NETWARE__
void set_panic_flag_for_netware()
{
	extern ibool panic_shutdown;
	panic_shutdown = TRUE;
}
1851 1852
#endif /* __NETWARE__ */
#endif /* !UNIV_HOTBACKUP */