srv0start.c 44.9 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"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi 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"

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
59 60 61 62 63 64 65
/* Log sequence number immediately after startup */
dulint		srv_start_lsn;
/* Log sequence number at shutdown */
dulint		srv_shutdown_lsn;

ibool		srv_start_raw_disk_in_use  = FALSE;

66
static ibool	srv_start_has_been_called  = FALSE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
67

68 69
ulint           srv_sizeof_trx_t_in_ha_innodb_cc;

70
ibool           srv_startup_is_before_trx_rollback_phase = FALSE;
71
ibool           srv_is_being_started = FALSE;
72
static ibool	srv_was_started      = FALSE;
73

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
74 75 76 77
/* At a shutdown the value first climbs to SRV_SHUTDOWN_CLEANUP
and then to SRV_SHUTDOWN_LAST_PHASE */
ulint		srv_shutdown_state = 0;

78 79
ibool		measure_cont	= FALSE;

80
static os_file_t	files[1000];
81

82 83
static mutex_t		ios_mutex;
static ulint		ios;
84

85 86
static ulint		n[SRV_MAX_N_IO_THREADS + 5];
static os_thread_id_t	thread_ids[SRV_MAX_N_IO_THREADS + 5];
87

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
88 89
/* 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. */
90
static os_fast_mutex_t	srv_os_test_mutex;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
91

92 93
/* Name of srv_monitor_file */
static char*	srv_monitor_file_name;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
94

95 96 97
#define SRV_N_PENDING_IOS_PER_THREAD 	OS_AIO_N_PENDING_IOS_PER_THREAD
#define SRV_MAX_N_PENDING_SYNC_IOS	100

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/*************************************************************************
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 == ':'
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
141 142
			   && (*(str + 1) == '\\' || *(str + 1) == '/'
					     || *(str + 1) == ':'))) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
			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++;
		}

165
	        if (0 == memcmp(str, ":autoextend", (sizeof ":autoextend") - 1)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
166

167
			str += (sizeof ":autoextend") - 1;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
168

169
	        	if (0 == memcmp(str, ":max:", (sizeof ":max:") - 1)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
170

171
				str += (sizeof ":max:") - 1;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

				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;
		}

200
	        if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
		  	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;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
232 233 234 235
		/* 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 */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
236 237 238

		while ((*str != ':' && *str != '\0')
		       || (*str == ':'
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
239 240
			   && (*(str + 1) == '\\' || *(str + 1) == '/'
			        || *(str + 1) == ':'))) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
			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;

266
	        if (0 == memcmp(str, ":autoextend", (sizeof ":autoextend") - 1)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
267 268 269

			*is_auto_extending = TRUE;

270
			str += (sizeof ":autoextend") - 1;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
271

272
	        	if (0 == memcmp(str, ":max:", (sizeof ":max:") - 1)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
273

274
				str += (sizeof ":max:") - 1;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
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

				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;
		}

308
		if (*str == 'r' && *(str + 1) == 'a' && *(str + 2) == 'w') {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
		 	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);
}

391 392 393
/************************************************************************
I/o-handler thread function. */
static
394 395 396 397

#ifndef __WIN__
void*
#else
398
ulint
399
#endif
400 401 402 403 404 405 406 407 408
io_handler_thread(
/*==============*/
	void*	arg)
{
	ulint	segment;
	ulint	i;
	
	segment = *((ulint*)arg);

409
#ifdef UNIV_DEBUG_THREAD_CREATION
410 411
	fprintf(stderr, "Io handler thread %lu starts, id %lu\n", segment,
			os_thread_pf(os_thread_get_curr_id()));
412
#endif
413 414 415 416 417 418 419 420
	for (i = 0;; i++) {
		fil_aio_wait(segment);

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

421 422 423 424 425 426 427
	/* 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);

428
#ifndef __WIN__
429
	return(NULL);				/* Not reached */
430
#else
431
	return(0);
432
#endif
433 434
}

435
#ifdef __WIN__
436
#define SRV_PATH_SEPARATOR	'\\'
437
#else
438
#define SRV_PATH_SEPARATOR	'/'
439 440 441 442
#endif

/*************************************************************************
Normalizes a directory path for Windows: converts slashes to backslashes. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
443

444 445 446
void
srv_normalize_path_for_win(
/*=======================*/
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
447 448
	char*	str __attribute__((unused)))	/* in/out: null-terminated
							   character string */
449 450
{
#ifdef __WIN__
451
	for (; *str; str++) {
452

453 454
		if (*str == '/') {
			*str = '\\';
455 456 457 458 459 460
		}
	}
#endif
}
	
/*************************************************************************
461 462
Adds a slash or a backslash to the end of a string if it is missing
and the string is not empty. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
463

464
static
465 466 467
char*
srv_add_path_separator_if_needed(
/*=============================*/
468
			/* out: string which has the separator if the
469
			string is not empty */
470 471 472
	char*	str)	/* in: null-terminated character string */
{
	char*	out_str;
473
	ulint	len	= ut_strlen(str);
474

475
	if (len == 0 || str[len - 1] == SRV_PATH_SEPARATOR) {
476

477
		return(str);
478 479
	}

480 481 482 483
	out_str = ut_malloc(len + 2);
	memcpy(out_str, str, len);
	out_str[len] = SRV_PATH_SEPARATOR;
	out_str[len + 1] = 0;
484 485 486 487

	return(out_str);
}

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
488 489 490 491 492 493 494 495 496 497 498
/*************************************************************************
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 */
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
499
	return(0xFFFFFFFFUL & (file_size << UNIV_PAGE_SIZE_SHIFT));
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
}

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

516
/*************************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
517
Creates or opens the log files and closes them. */
518 519 520 521 522
static
ulint
open_or_create_log_file(
/*====================*/
					/* out: DB_SUCCESS or error code */
monty@mishka.local's avatar
monty@mishka.local committed
523 524
        ibool   create_new_db,          /* in: TRUE if we should create a
                                        new database */
525 526
	ibool*	log_file_created,	/* out: TRUE if new log file
					created */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
527 528 529
	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 */
530 531 532 533 534 535 536 537
	ulint	k,			/* in: log group number */
	ulint	i)			/* in: log file number in group */
{
	ibool	ret;
	ulint	size;
	ulint	size_high;
	char	name[10000];

monty@mishka.local's avatar
monty@mishka.local committed
538 539
	UT_NOT_USED(create_new_db);

540
	*log_file_created = FALSE;
541 542 543 544 545

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

546 547
	ut_a(strlen(srv_log_group_home_dirs[k]) <
		(sizeof name) - 10 - sizeof "ib_logfile");
548
	sprintf(name, "%s%s%lu", srv_log_group_home_dirs[k], "ib_logfile", (ulong) i);
549

550 551
	files[i] = os_file_create(name, OS_FILE_CREATE, OS_FILE_NORMAL,
						OS_LOG_FILE, &ret);
552
	if (ret == FALSE) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
553
		if (os_file_get_last_error(FALSE) != OS_FILE_ALREADY_EXISTS) {
554
			fprintf(stderr,
555
			"InnoDB: Error in creating or opening %s\n", name);
556 557 558 559
				
			return(DB_ERROR);
		}

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
560
		files[i] = os_file_create(name, OS_FILE_OPEN, OS_FILE_AIO,
561
							OS_LOG_FILE, &ret);
562 563
		if (!ret) {
			fprintf(stderr,
564
			"InnoDB: Error in opening %s\n", name);
565 566 567 568 569 570 571
				
			return(DB_ERROR);
		}

		ret = os_file_get_size(files[i], &size, &size_high);
		ut_a(ret);
		
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
572 573 574
		if (size != srv_calc_low32(srv_log_file_size)
		    || size_high != srv_calc_high32(srv_log_file_size)) {
		    	
575
			fprintf(stderr,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
576 577
"InnoDB: Error: log file %s is of different size %lu %lu bytes\n"
"InnoDB: than specified in the .cnf file %lu %lu bytes!\n",
578 579 580
				name, (ulong) size_high, (ulong) size,
				(ulong) srv_calc_high32(srv_log_file_size),
				(ulong) srv_calc_low32(srv_log_file_size));
581 582 583 584 585
				
			return(DB_ERROR);
		}					
	} else {
		*log_file_created = TRUE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
586

heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
587 588
	    	ut_print_timestamp(stderr);

589
		fprintf(stderr,
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
590
		"  InnoDB: Log file %s did not exist: new to be created\n",
591
									name);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
592 593 594 595 596
		if (log_file_has_been_opened) {

			return(DB_ERROR);
		}

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
597
		fprintf(stderr, "InnoDB: Setting log file %s size to %lu MB\n",
598
			             name, (ulong) srv_log_file_size
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
599
			>> (20 - UNIV_PAGE_SIZE_SHIFT));
600

heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
601 602 603
		fprintf(stderr,
	    "InnoDB: Database physically writes the file full: wait...\n");

604
		ret = os_file_set_size(name, files[i],
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
605 606
					srv_calc_low32(srv_log_file_size),
					srv_calc_high32(srv_log_file_size));
607 608
		if (!ret) {
			fprintf(stderr,
609
		"InnoDB: Error in creating %s: probably out of disk space\n",
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629
			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,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
630
				2 * k + SRV_LOG_SPACE_FIRST_ID, FALSE);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
631
#ifdef UNIV_LOG_ARCHIVE
632
	/* If this is the first log group, create the file space object
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
633 634
	for archived logs.
	Under MySQL, no archiving ever done. */
635 636 637 638

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

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
639
	    	fil_space_create("arch_log_space", arch_space_id, FIL_LOG);
640 641 642
	} else {
		arch_space_id = ULINT_UNDEFINED;
	}
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
643
#endif /* UNIV_LOG_ARCHIVE */
644 645 646 647
	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,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
648 649
				SRV_LOG_SPACE_FIRST_ID + 1); /* dummy arch
								space id */
650 651 652 653 654 655
	}

	return(DB_SUCCESS);
}

/*************************************************************************
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
656
Creates or opens database data files and closes them. */
657 658 659 660 661 662 663
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 */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
664
#ifdef UNIV_LOG_ARCHIVE
665 666 667
	ulint*	min_arch_log_no,/* out: min of archived log numbers in data
				files */
	ulint*	max_arch_log_no,/* out: */
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
668 669 670 671
#endif /* UNIV_LOG_ARCHIVE */
	dulint*	min_flushed_lsn,/* out: min of flushed lsn values in data
				files */
	dulint*	max_flushed_lsn,/* out: */
672 673 674 675 676 677 678 679
	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;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
680
	ulint	rounded_size_pages;
681 682
	char	name[10000];

683 684 685
	if (srv_n_data_files >= 1000) {
		fprintf(stderr, "InnoDB: can only have < 1000 data files\n"
				"InnoDB: you have defined %lu\n",
686
				(ulong) srv_n_data_files);
687 688
		return(DB_ERROR);
	}
689 690 691 692 693

	*sum_of_new_sizes = 0;
	
	*create_new_db = FALSE;

694 695 696
	srv_normalize_path_for_win(srv_data_home);
	srv_data_home = srv_add_path_separator_if_needed(srv_data_home);

697
	for (i = 0; i < srv_n_data_files; i++) {
698
		srv_normalize_path_for_win(srv_data_file_names[i]);
699

700 701
		ut_a(strlen(srv_data_home) + strlen(srv_data_file_names[i])
			< (sizeof name) - 1);
702 703
		sprintf(name, "%s%s", srv_data_home, srv_data_file_names[i]);
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
704 705 706 707 708 709
		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,
710
					OS_FILE_NORMAL, OS_DATA_FILE, &ret);
711

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
712 713 714 715 716 717 718 719 720
			if (ret == FALSE && os_file_get_last_error(FALSE) !=
						OS_FILE_ALREADY_EXISTS) {
				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) {
721 722
			/* The partition is opened, not created; then it is
			written over */
723

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
724
			srv_start_raw_disk_in_use = TRUE;
725 726
			srv_created_new_raw = TRUE;

727
			files[i] = os_file_create(
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
728 729
				name, OS_FILE_OPEN_RAW, OS_FILE_NORMAL,
							OS_DATA_FILE, &ret);
730
			if (!ret) {
731 732 733 734
				fprintf(stderr,
				"InnoDB: Error in opening %s\n", name);

				return(DB_ERROR);
735 736
			}
		} else if (srv_data_file_is_raw_partition[i] == SRV_OLD_RAW) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
737 738
			srv_start_raw_disk_in_use = TRUE;

739
			ret = FALSE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
740 741
		} else {
			ut_a(0);
742 743
		}

744
		if (ret == FALSE) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
745
			/* We open the data file */
746 747 748

			if (one_created) {
				fprintf(stderr,
749
	"InnoDB: Error: data files can only be added at the end\n");
750
				fprintf(stderr,
751
	"InnoDB: of a tablespace, but data file %s existed beforehand.\n",
752 753 754 755
				name);
				return(DB_ERROR);
			}
				
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
756 757 758 759 760 761 762 763 764 765
			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);
			}

766 767
			if (!ret) {
				fprintf(stderr,
768
				"InnoDB: Error in opening %s\n", name);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
769
				os_file_get_last_error(TRUE);
770 771 772 773

				return(DB_ERROR);
			}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
774 775 776 777 778 779 780 781
			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 */
782
		
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
783
			rounded_size_pages = (size / (1024 * 1024)
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
784 785 786
							+ 4096 * size_high)
					     << (20 - UNIV_PAGE_SIZE_SHIFT);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
787
			if (i == srv_n_data_files - 1
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
788 789
				    && srv_auto_extend_last_data_file) {

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
790
				if (srv_data_file_sizes[i] >
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
791 792 793 794 795
				    		rounded_size_pages
				    	   || (srv_last_file_size_max > 0
				    	      && srv_last_file_size_max <
				    	       rounded_size_pages)) {
				    	       	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
796
					fprintf(stderr,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
797 798 799
"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",
800 801 802
		  name, (ulong) rounded_size_pages,
		  (ulong) srv_data_file_sizes[i],
		  (ulong) srv_last_file_size_max);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
803

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
804
					return(DB_ERROR);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
805
				}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
806 807 808
				    	     
				srv_data_file_sizes[i] = rounded_size_pages;
			}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
809
				
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
810
			if (rounded_size_pages != srv_data_file_sizes[i]) {
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
811

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
812
				fprintf(stderr,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
813 814 815
"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,
816 817
					       (ulong) rounded_size_pages,
					       (ulong) srv_data_file_sizes[i]);
818
				
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
819
				return(DB_ERROR);
820
			}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
821
skip_size_check:
822 823
			fil_read_flushed_lsn_and_arch_log_no(files[i],
					one_opened,
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
824 825 826 827
#ifdef UNIV_LOG_ARCHIVE
					min_arch_log_no, max_arch_log_no,
#endif /* UNIV_LOG_ARCHIVE */
					min_flushed_lsn, max_flushed_lsn);
828 829
			one_opened = TRUE;
		} else {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
830 831 832
		        /* We created the data file and now write it full of
			zeros */

833 834 835
			one_created = TRUE;

			if (i > 0) {
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
836
	    			ut_print_timestamp(stderr);
837
				fprintf(stderr, 
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
838
		"  InnoDB: Data file %s did not exist: new to be created\n",
839
									name);
840 841
			} else {
				fprintf(stderr, 
842 843
 		"InnoDB: The first specified data file %s did not exist:\n"
		"InnoDB: a new database to be created!\n", name);
844 845 846
				*create_new_db = TRUE;
			}
			
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
847
	    		ut_print_timestamp(stderr);
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
848
			fprintf(stderr, 
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
849
				"  InnoDB: Setting file %s size to %lu MB\n",
850
			       name, (ulong) (srv_data_file_sizes[i]
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
851
				      >> (20 - UNIV_PAGE_SIZE_SHIFT)));
852

853
			fprintf(stderr,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
854
	"InnoDB: Database physically writes the file full: wait...\n");
855

856
			ret = os_file_set_size(name, files[i],
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
857 858
				srv_calc_low32(srv_data_file_sizes[i]),
				srv_calc_high32(srv_data_file_sizes[i]));
859 860 861

			if (!ret) {
				fprintf(stderr, 
862
	"InnoDB: Error in creating %s: probably out of disk space\n", name);
863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

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

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
880 881 882 883 884 885 886
		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);
		}
887 888 889 890 891 892 893 894 895 896 897
	}

	ios = 0;

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

	return(DB_SUCCESS);
}

/********************************************************************
898
Starts InnoDB and creates a new database if database files
899 900 901 902 903 904 905 906
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 */
{
907
	buf_pool_t*	ret;
908 909 910 911 912 913
	ibool	create_new_db;
	ibool	log_file_created;
	ibool	log_created	= FALSE;
	ibool	log_opened	= FALSE;
	dulint	min_flushed_lsn;
	dulint	max_flushed_lsn;
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
914
#ifdef UNIV_LOG_ARCHIVE
915 916
	ulint	min_arch_log_no;
	ulint	max_arch_log_no;
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
917
#endif /* UNIV_LOG_ARCHIVE */
918
	ulint   sum_of_new_sizes;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
919 920
	ulint	sum_of_data_file_sizes;
	ulint	tablespace_size_in_header;
921 922
	ulint	err;
	ulint	i;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
923
	ibool	srv_file_per_table_original_value  = srv_file_per_table;
924 925
	mtr_t   mtr;

926 927 928 929 930 931 932 933
	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*));
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
934 935
	srv_file_per_table = FALSE; /* system tables are created in tablespace
									0 */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
#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"); 
954 955 956 957 958
#endif

#ifdef UNIV_SIMULATE_AWE
	fprintf(stderr,
"InnoDB: !!!!!!!!!!!!!! UNIV_SIMULATE_AWE switched on !!!!!!!!!!!!!!!!!\n");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
959
#endif
960 961 962 963 964
        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",
965 966
		 (ulong)  srv_sizeof_trx_t_in_ha_innodb_cc,
		 (ulong) sizeof(trx_t));
967 968 969
		return(DB_ERROR);
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
970 971 972 973 974 975 976 977 978 979 980 981 982 983
	/* 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;

984 985 986
	log_do_write = TRUE;
/*	yydebug = TRUE; */

987
	srv_is_being_started = TRUE;
988
        srv_startup_is_before_trx_rollback_phase = TRUE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
989 990
	os_aio_use_native_aio = FALSE;

991
#if !defined(__WIN2000__) && !defined(UNIV_SIMULATE_AWE)
992 993 994 995 996 997 998 999 1000 1001
	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

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
#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 */
1020

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1021 1022 1023 1024
		srv_unix_file_flush_method = SRV_UNIX_FDATASYNC;

		srv_win_file_flush_method = SRV_WIN_IO_UNBUFFERED;
#ifndef __WIN__        
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1025
	} else if (0 == ut_strcmp(srv_file_flush_method_str, "fdatasync")) {
1026 1027
	  	srv_unix_file_flush_method = SRV_UNIX_FDATASYNC;

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1028
	} else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DSYNC")) {
1029 1030
	  	srv_unix_file_flush_method = SRV_UNIX_O_DSYNC;

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1031
	} else if (0 == ut_strcmp(srv_file_flush_method_str, "O_DIRECT")) {
1032 1033
	  	srv_unix_file_flush_method = SRV_UNIX_O_DIRECT;

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1034
	} else if (0 == ut_strcmp(srv_file_flush_method_str, "littlesync")) {
1035 1036
	  	srv_unix_file_flush_method = SRV_UNIX_LITTLESYNC;

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1037
	} else if (0 == ut_strcmp(srv_file_flush_method_str, "nosync")) {
1038
	  	srv_unix_file_flush_method = SRV_UNIX_NOSYNC;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1039
#else
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1040
	} else if (0 == ut_strcmp(srv_file_flush_method_str, "normal")) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
	  	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
1052
	} else {
1053 1054
	  	fprintf(stderr, 
          	"InnoDB: Unrecognized value %s for innodb_flush_method\n",
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1055
          				srv_file_flush_method_str);
1056
	  	return(DB_ERROR);
1057
	}
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
	
	/* Set the maximum number of threads which can wait for a semaphore
	inside InnoDB */
#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
	if (srv_pool_size >= 8 * 1024) {
			          /* Here we still have srv_pool_size counted
				  in kilobytes, srv_boot converts the value to
				  pages; if buffer pool is less than 8 MB,
				  assume fewer threads. */
		srv_max_n_threads = 10000;
	} else {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1076
	        srv_max_n_threads = 1000;	/* saves several MB of memory,
1077 1078 1079 1080
						especially in 64-bit
						computers */
	}
#endif
1081 1082
	/* Note that the call srv_boot() also changes the values of
	srv_pool_size etc. to the units used by InnoDB internally */
1083

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
        /* Set the maximum number of threads which can wait for a semaphore
        inside InnoDB */
#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
        if (srv_pool_size >= 8 * 1024 * 1024) {
                                  /* Here we still have srv_pool_size counted
                                  in bytes, srv_boot converts the value to
                                  pages; if buffer pool is less than 8 MB,
                                  assume fewer threads. */
                srv_max_n_threads = 10000;
        } else {
		srv_max_n_threads = 1000;       /* saves several MB of memory,
                                                especially in 64-bit
                                                computers */
        }
#endif
1106 1107 1108 1109 1110 1111 1112
	err = srv_boot();

	if (err != DB_SUCCESS) {

		return((int) err);
	}

1113
	mutex_create(&srv_monitor_file_mutex);
1114
	mutex_set_level(&srv_monitor_file_mutex, SYNC_NO_ORDER_CHECK);
1115 1116 1117
	srv_monitor_file_name = mem_alloc(
			strlen(fil_path_to_mysql_datadir) +
			20 + sizeof "/innodb_status.");
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1118
	sprintf(srv_monitor_file_name, "%s/innodb_status.%lu",
1119 1120 1121 1122 1123 1124 1125 1126
		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);
	}

1127 1128
	/* Restrict the maximum number of file i/o threads */
	if (srv_n_file_io_threads > SRV_MAX_N_IO_THREADS) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1129

1130 1131 1132
		srv_n_file_io_threads = SRV_MAX_N_IO_THREADS;
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1133 1134 1135
	if (!os_aio_use_native_aio) {
 		/* In simulated aio we currently have use only for 4 threads */
		srv_n_file_io_threads = 4;
1136

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1137
		os_aio_init(8 * SRV_N_PENDING_IOS_PER_THREAD
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
						* 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);
	}
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1148
	fil_init(srv_max_n_open_files);
1149

1150 1151 1152
	if (srv_use_awe) {
		fprintf(stderr,
"InnoDB: Using AWE: Memory window is %lu MB and AWE memory is %lu MB\n",
1153 1154
		(ulong) (srv_awe_window_size / ((1024 * 1024) / UNIV_PAGE_SIZE)),
		(ulong) (srv_pool_size / ((1024 * 1024) / UNIV_PAGE_SIZE)));
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169

		/* 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) {
		return(DB_ERROR);
	}
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180

	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);
1181
    	}
1182

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1183
#ifdef UNIV_LOG_ARCHIVE
1184 1185 1186 1187 1188 1189 1190
	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);
	}
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1191
#endif /* UNIV_LOG_ARCHIVE */
1192

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1193
	if (srv_n_log_files * srv_log_file_size >= 262144) {
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1194
		fprintf(stderr,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1195
		"InnoDB: Error: combined size of log files must be < 4 GB\n");
1196

monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1197 1198 1199 1200 1201
		return(DB_ERROR);
	}

	sum_of_new_sizes = 0;
	
1202
	for (i = 0; i < srv_n_data_files; i++) {
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1203 1204
#ifndef __WIN__
		if (sizeof(off_t) < 5 && srv_data_file_sizes[i] >= 262144) {
1205
		 	fprintf(stderr,
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1206 1207
	"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");
1208 1209 1210

		  	return(DB_ERROR);
		}
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1211
#endif
1212
		sum_of_new_sizes += srv_data_file_sizes[i];
1213 1214 1215
	}

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

1219
		  return(DB_ERROR);
1220 1221
	}

1222
	err = open_or_create_data_files(&create_new_db,
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1223 1224 1225 1226
#ifdef UNIV_LOG_ARCHIVE
					&min_arch_log_no, &max_arch_log_no,
#endif /* UNIV_LOG_ARCHIVE */
					&min_flushed_lsn, &max_flushed_lsn,
1227 1228
					&sum_of_new_sizes);
	if (err != DB_SUCCESS) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1229 1230 1231 1232 1233 1234 1235 1236
	        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");
1237

1238 1239 1240
		return((int) err);
	}

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1241
#ifdef UNIV_LOG_ARCHIVE
1242 1243
	srv_normalize_path_for_win(srv_arch_dir);
	srv_arch_dir = srv_add_path_separator_if_needed(srv_arch_dir);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1244
#endif /* UNIV_LOG_ARCHIVE */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1245 1246 1247 1248 1249
		
	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) {
1250

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1251 1252
			return((int) err);
		}
1253

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1254 1255 1256 1257 1258 1259
		if (log_file_created) {
			log_created = TRUE;
		} else {
			log_opened = TRUE;
		}
		if ((log_opened && create_new_db)
1260
			    		|| (log_opened && log_created)) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1261
			fprintf(stderr, 
1262
	"InnoDB: Error: all log files must be created at the same time.\n"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1263 1264 1265
	"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"
1266 1267
	"InnoDB: Then delete the existing log files. Edit the .cnf file\n"
	"InnoDB: and start the database again.\n");
1268

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1269
			return(DB_ERROR);
1270 1271 1272
		}
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1273 1274
	/* Open all log files and data files in the system tablespace: we
	keep them open until database shutdown */
1275

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1276 1277
	fil_open_log_and_system_tablespace_files();

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1278 1279 1280 1281 1282
	if (log_created && !create_new_db
#ifdef UNIV_LOG_ARCHIVE
		&& !srv_archive_recovery
#endif /* UNIV_LOG_ARCHIVE */
	) {
1283
		if (ut_dulint_cmp(max_flushed_lsn, min_flushed_lsn) != 0
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1284 1285 1286 1287
#ifdef UNIV_LOG_ARCHIVE
				|| max_arch_log_no != min_arch_log_no
#endif /* UNIV_LOG_ARCHIVE */
		) {
1288
			fprintf(stderr, 
1289 1290
		"InnoDB: Cannot initialize created log files because\n"
		"InnoDB: data files were not in sync with each other\n"
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
1291
		"InnoDB: or the data files are corrupt.\n");
1292 1293 1294 1295 1296 1297 1298

			return(DB_ERROR);
		}

		if (ut_dulint_cmp(max_flushed_lsn, ut_dulint_create(0, 1000))
		    < 0) {
		    	fprintf(stderr,
1299 1300 1301 1302 1303
		"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");
1304 1305 1306 1307 1308 1309

			return(DB_ERROR);
		}

		mutex_enter(&(log_sys->mutex));

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1310
#ifdef UNIV_LOG_ARCHIVE
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1311 1312 1313
		/* Do not + 1 arch_log_no because we do not use log
		archiving */
		recv_reset_logs(max_flushed_lsn, max_arch_log_no, TRUE);
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1314 1315 1316 1317
#else
		recv_reset_logs(max_flushed_lsn, TRUE);
#endif /* UNIV_LOG_ARCHIVE */

1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329
		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();
1330
                srv_startup_is_before_trx_rollback_phase = FALSE;
1331

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1332
#ifdef UNIV_LOG_ARCHIVE
1333 1334
	} else if (srv_archive_recovery) {
		fprintf(stderr,
1335
	"InnoDB: Starting archive recovery from a backup...\n");
1336 1337 1338 1339 1340 1341 1342 1343
		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);
		}
1344 1345 1346
		/* Since ibuf init is in dict_boot, and ibuf is needed
		in any disk i/o, first call dict_boot */

1347
		dict_boot();
1348 1349 1350
		trx_sys_init_at_db_start();
                srv_startup_is_before_trx_rollback_phase = FALSE;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1351 1352 1353 1354
		/* Initialize the fsp free limit global variable in the log
		system */
		fsp_header_get_free_limit(0);

1355
		recv_recovery_from_archive_finish();
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1356
#endif /* UNIV_LOG_ARCHIVE */
1357 1358
	} else {
		/* We always try to do a recovery, even if the database had
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1359
		been shut down normally: this is the normal startup path */
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
		
		err = recv_recovery_from_checkpoint_start(LOG_CHECKPOINT,
							ut_dulint_max,
							min_flushed_lsn,
							max_flushed_lsn);
		if (err != DB_SUCCESS) {

			return(DB_ERROR);
		}

1370 1371
		/* Since ibuf init is in dict_boot, and ibuf is needed
		in any disk i/o, first call dict_boot */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1372

1373
		dict_boot();
1374
		trx_sys_init_at_db_start();
1375 1376 1377

		/* The following needs trx lists which are initialized in
		trx_sys_init_at_db_start */
1378 1379

                srv_startup_is_before_trx_rollback_phase = FALSE;
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1380 1381 1382 1383 1384

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

1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396
		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);
	}

heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
1397 1398 1399 1400 1401 1402
	if (recv_needed_recovery) {
	    	ut_print_timestamp(stderr);
		fprintf(stderr,
	        "  InnoDB: Flushing modified pages from the buffer pool...\n");
	}

1403 1404
	log_make_checkpoint_at(ut_dulint_max, TRUE);

marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1405
#ifdef UNIV_LOG_ARCHIVE
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1406
	/* Archiving is always off under MySQL */
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423
	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());
		}
	}
marko@hundin.mysql.fi's avatar
marko@hundin.mysql.fi committed
1424
#endif /* UNIV_LOG_ARCHIVE */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1425 1426 1427 1428 1429 1430 1431 1432
	if (!create_new_db && srv_force_recovery == 0) {
		/* After a crash recovery we only 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(). */

		dict_check_tablespaces_or_store_max_id(recv_needed_recovery);
	}

1433
	if (srv_measure_contention) {
1434
	  	/* os_thread_create(&test_measure_cont, NULL, thread_ids +
1435
                             	     SRV_MAX_N_IO_THREADS); */
1436 1437 1438 1439 1440
	}

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

1441 1442 1443 1444
	/* 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,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1445
				thread_ids + 2 + SRV_MAX_N_IO_THREADS);	
1446 1447 1448

	/* Create the thread which warns of long semaphore waits */
	os_thread_create(&srv_error_monitor_thread, NULL,
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1449
				thread_ids + 3 + SRV_MAX_N_IO_THREADS);	
1450 1451 1452
	srv_was_started = TRUE;
	srv_is_being_started = FALSE;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1453
#ifdef UNIV_DEBUG
1454 1455 1456 1457 1458
        /* 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. */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1459

1460
        os_thread_sleep(3000000);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1461
#endif
1462 1463
	sync_order_checks_on = TRUE;

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1464 1465 1466
        if (srv_use_doublewrite_buf && trx_doublewrite == NULL) {
		/* Create the doublewrite buffer to a new tablespace */

1467 1468 1469
		trx_sys_create_doublewrite_buf();
	}

1470 1471 1472 1473 1474
	err = dict_create_or_check_foreign_constraint_tables();

	if (err != DB_SUCCESS) {
		return((int)DB_ERROR);
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1475
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1476 1477
	/* Create the master thread which does purge and other utility
	operations */
1478 1479 1480

	os_thread_create(&srv_master_thread, NULL, thread_ids + 1 +
							SRV_MAX_N_IO_THREADS);
1481
	/* buf_debug_prints = TRUE; */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1482 1483

	sum_of_data_file_sizes = 0;
1484
	
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
	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",
1497 1498
 			(ulong) tablespace_size_in_header,
			(ulong) sum_of_data_file_sizes);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1499 1500 1501 1502 1503 1504 1505 1506
	}

	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",
1507 1508
 			(ulong) tablespace_size_in_header,
			(ulong) sum_of_data_file_sizes);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1509 1510
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1511
	/* Check that os_fast_mutexes work as expected */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1512 1513 1514 1515 1516
	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"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1517
"InnoDB: success! Cannot continue.\n");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1518 1519 1520 1521 1522 1523 1524 1525 1526
	        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);

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1527
	os_fast_mutex_free(&srv_os_test_mutex);
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1528

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1529 1530
	if (srv_print_verbose_log) {
	  	ut_print_timestamp(stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1531 1532
	  	fprintf(stderr,
"  InnoDB: Started; log sequence number %lu %lu\n",
1533 1534
			(ulong) ut_dulint_get_high(srv_start_lsn),
			(ulong) ut_dulint_get_low(srv_start_lsn));
1535
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1536 1537 1538 1539

	if (srv_force_recovery > 0) {
		fprintf(stderr,
		"InnoDB: !!! innodb_force_recovery is set to %lu !!!\n",
1540
			(ulong) srv_force_recovery);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1541 1542 1543
	}

	fflush(stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1544

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1545
	if (trx_doublewrite_must_reset_space_ids) {
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
		/* 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! */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580
		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"
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1581 1582 1583
"InnoDB: format. You should NOT DOWNGRADE again to an earlier version of\n"
"InnoDB: InnoDB! But if you absolutely need to downgrade, see section 4.6 of\n"
"InnoDB: http://www.innodb.com/ibman.php for instructions.\n");
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
	}

	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;

1597 1598 1599 1600
	return((int) DB_SUCCESS);
}

/********************************************************************
1601
Shuts down the InnoDB database. */
1602 1603 1604 1605 1606 1607

int
innobase_shutdown_for_mysql(void) 
/*=============================*/
				/* out: DB_SUCCESS or error code */
{
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1608 1609
	ulint   i;

1610
        if (!srv_was_started) {
1611 1612 1613
	  	if (srv_is_being_started) {
	    		ut_print_timestamp(stderr);
            		fprintf(stderr, 
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1614 1615
"  InnoDB: Warning: shutting down a not properly started\n"
"                 InnoDB: or created database!\n");
1616 1617 1618
	  	}

	  	return(DB_SUCCESS);
1619 1620
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1621
	/* 1. Flush the buffer pool to disk, write the current lsn to
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1622
	the tablespace header(s), and copy all log data to archive.
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1623
	The step 1 is the real InnoDB shutdown. The remaining steps 2 - ...
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1624
	just free data structures after the shutdown. */
1625 1626

	logs_empty_and_mark_files_at_shutdown();
monty@donna.mysql.fi's avatar
Merge  
monty@donna.mysql.fi committed
1627
	
heikki@donna.mysql.fi's avatar
heikki@donna.mysql.fi committed
1628 1629 1630 1631 1632 1633
	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);
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1634

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1635
	/* 2. Make all threads created by InnoDB to exit */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646

	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 */
		
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1647
		/* a. Let the lock timeout thread exit */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1648 1649
		os_event_set(srv_lock_timeout_thread_event);		

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1650
		/* b. srv error monitor thread exits automatically, no need
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1651 1652
		to do anything here */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1653
		/* c. We wake the master thread so that it exits */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1654 1655
		srv_wake_master_thread();

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1656
		/* d. Exit the i/o threads */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1657 1658 1659

		os_aio_wake_all_threads_at_shutdown();

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1660
		os_mutex_enter(os_sync_mutex);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1661 1662 1663 1664 1665 1666 1667 1668

		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! */

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1669
			os_mutex_exit(os_sync_mutex);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1670 1671 1672 1673 1674 1675

			os_thread_sleep(100000);

			break;
		}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1676
		os_mutex_exit(os_sync_mutex);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1677 1678 1679 1680 1681 1682 1683

		os_thread_sleep(100000);
	}

	if (i == 1000) {
	        fprintf(stderr,
"InnoDB: Warning: %lu threads created by InnoDB had not exited at shutdown!\n",
1684
		      (ulong) os_thread_count);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1685 1686
	}

1687 1688 1689 1690 1691 1692 1693 1694 1695
	if (srv_monitor_file) {
		fclose(srv_monitor_file);
		srv_monitor_file = 0;
		unlink(srv_monitor_file_name);
		mem_free(srv_monitor_file_name);
	}

	mutex_free(&srv_monitor_file_mutex);

1696 1697
	/* 3. Free all InnoDB's own mutexes and the os_fast_mutexes inside
	them */
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1698 1699 1700

	sync_close();

1701
	/* 4. Free the os_conc_mutex and all os_events and os_mutexes */
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1702 1703

	srv_free();
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1704
	os_sync_free();
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1705

1706
	/* 5. Free all allocated memory and the os_fast_mutex created in
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1707
	ut0mem.c */
monty@mashka.mysql.fi's avatar
monty@mashka.mysql.fi committed
1708

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1709
        ut_free_all_mem();
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1710

1711 1712 1713 1714 1715 1716 1717
	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",
1718 1719
			(ulong) os_thread_count, (ulong) os_event_count,
			(ulong) os_mutex_count, (ulong) os_fast_mutex_count);
1720 1721
	}

1722 1723 1724 1725 1726 1727 1728
	if (dict_foreign_err_file) {
		fclose(dict_foreign_err_file);
	}
	if (lock_latest_err_file) {
		fclose(lock_latest_err_file);
	}

heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1729 1730
	if (srv_print_verbose_log) {
	        ut_print_timestamp(stderr);
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1731 1732
	        fprintf(stderr,
"  InnoDB: Shutdown completed; log sequence number %lu %lu\n",
1733 1734
			       (ulong) ut_dulint_get_high(srv_shutdown_lsn),
			       (ulong) ut_dulint_get_low(srv_shutdown_lsn));
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1735
	}
heikki@hundin.mysql.fi's avatar
heikki@hundin.mysql.fi committed
1736

1737 1738
	return((int) DB_SUCCESS);
}