log0recv.c 89.2 KB
Newer Older
1 2 3
/******************************************************
Recovery

unknown's avatar
unknown committed
4
(c) 1997 Innobase Oy
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Created 9/20/1997 Heikki Tuuri
*******************************************************/

#include "log0recv.h"

#ifdef UNIV_NONINL
#include "log0recv.ic"
#endif

#include "mem0mem.h"
#include "buf0buf.h"
#include "buf0flu.h"
#include "buf0rea.h"
#include "srv0srv.h"
unknown's avatar
unknown committed
20
#include "srv0start.h"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
#include "mtr0mtr.h"
#include "mtr0log.h"
#include "page0page.h"
#include "page0cur.h"
#include "btr0btr.h"
#include "btr0cur.h"
#include "ibuf0ibuf.h"
#include "trx0undo.h"
#include "trx0rec.h"
#include "trx0roll.h"
#include "btr0cur.h"
#include "btr0cur.h"
#include "btr0cur.h"
#include "dict0boot.h"
#include "fil0fil.h"

unknown's avatar
unknown committed
37
#ifdef UNIV_HOTBACKUP
unknown's avatar
unknown committed
38 39 40 41
/* This is set to FALSE if the backup was originally taken with the
ibbackup --include regexp option: then we do not want to create tables in
directories which were not included */
ibool	recv_replay_file_ops	= TRUE;
unknown's avatar
unknown committed
42
#endif /* UNIV_HOTBACKUP */
unknown's avatar
unknown committed
43

44 45 46 47 48 49 50 51 52 53 54
/* Log records are stored in the hash table in chunks at most of this size;
this must be less than UNIV_PAGE_SIZE as it is stored in the buffer pool */
#define RECV_DATA_BLOCK_SIZE	(MEM_MAX_ALLOC_IN_BUF - sizeof(recv_data_t))

/* Read-ahead area in applying log records to file pages */
#define RECV_READ_AHEAD_AREA	32

recv_sys_t*	recv_sys = NULL;
ibool		recv_recovery_on = FALSE;
ibool		recv_recovery_from_backup_on = FALSE;

unknown's avatar
unknown committed
55 56
ibool		recv_needed_recovery = FALSE;

57 58
ibool		recv_lsn_checks_on = FALSE;

59 60 61 62 63 64 65 66 67 68
/* If the following is TRUE, the buffer pool file pages must be invalidated
after recovery and no ibuf operations are allowed; this becomes TRUE if
the log record hash table becomes too full, and log records must be merged
to file pages already before the recovery is finished: in this case no
ibuf operations are allowed, as they could modify the pages read in the
buffer pool before the pages have been recovered to the up-to-date state */

/* Recovery is running and no operations on the log files are allowed
yet: the variable name is misleading */

unknown's avatar
Merge  
unknown committed
69 70
ibool	recv_no_ibuf_operations = FALSE;

unknown's avatar
unknown committed
71
/* The following counter is used to decide when to print info on
unknown's avatar
Merge  
unknown committed
72 73
log scan */
ulint	recv_scan_print_counter	= 0;
74

unknown's avatar
unknown committed
75
ibool	recv_is_from_backup	= FALSE;
unknown's avatar
unknown committed
76
#ifdef UNIV_HOTBACKUP
unknown's avatar
unknown committed
77
ibool	recv_is_making_a_backup = FALSE;
unknown's avatar
unknown committed
78 79 80
#else
# define recv_is_making_a_backup FALSE
#endif /* UNIV_HOTBACKUP */
unknown's avatar
unknown committed
81

unknown's avatar
unknown committed
82 83 84 85
ulint	recv_previous_parsed_rec_type	= 999999;
ulint	recv_previous_parsed_rec_offset	= 0;
ulint	recv_previous_parsed_rec_is_multi = 0;

unknown's avatar
unknown committed
86 87
ulint	recv_max_parsed_page_no		= 0;

unknown's avatar
unknown committed
88 89 90 91 92
/* This many frames must be left free in the buffer pool when we scan
the log and store the scanned log records in the buffer pool: we will
use these free frames to read in pages when we start applying the
log records to the database. */

unknown's avatar
unknown committed
93
ulint  recv_n_pool_free_frames         = 256;
unknown's avatar
unknown committed
94

unknown's avatar
unknown committed
95 96 97 98 99
/* The maximum lsn we see for a page during the recovery process. If this
is bigger than the lsn we are able to scan up to, that is an indication that
the recovery failed and the database may be corrupt. */

dulint	recv_max_page_lsn;
unknown's avatar
unknown committed
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
/************************************************************
Creates the recovery system. */

void
recv_sys_create(void)
/*=================*/
{
	if (recv_sys != NULL) {

		return;
	}

	recv_sys = mem_alloc(sizeof(recv_sys_t));

	mutex_create(&(recv_sys->mutex));
	mutex_set_level(&(recv_sys->mutex), SYNC_RECV);

	recv_sys->heap = NULL;
	recv_sys->addr_hash = NULL;
}

/************************************************************
Inits the recovery system for a recovery operation. */

void
unknown's avatar
unknown committed
126 127 128 129 130
recv_sys_init(
/*==========*/
	ibool	recover_from_backup,	/* in: TRUE if this is called
					to recover from a hot backup */
	ulint	available_memory)	/* in: available memory in bytes */
131 132 133 134 135 136 137 138
{
	if (recv_sys->heap != NULL) {

		return;
	}

	mutex_enter(&(recv_sys->mutex));

unknown's avatar
unknown committed
139 140 141 142 143 144
	if (!recover_from_backup) {
		recv_sys->heap = mem_heap_create_in_buffer(256);
	} else {
		recv_sys->heap = mem_heap_create(256);
		recv_is_from_backup = TRUE;
	}
145 146 147 148 149

	recv_sys->buf = ut_malloc(RECV_PARSING_BUF_SIZE);
	recv_sys->len = 0;
	recv_sys->recovered_offset = 0;

unknown's avatar
unknown committed
150
	recv_sys->addr_hash = hash_create(available_memory / 64);
151 152 153 154 155 156 157 158 159
	recv_sys->n_addrs = 0;
	
	recv_sys->apply_log_recs = FALSE;
	recv_sys->apply_batch_on = FALSE;

	recv_sys->last_block_buf_start = mem_alloc(2 * OS_FILE_LOG_BLOCK_SIZE);

	recv_sys->last_block = ut_align(recv_sys->last_block_buf_start,
						OS_FILE_LOG_BLOCK_SIZE);
unknown's avatar
unknown committed
160 161
	recv_sys->found_corrupt_log = FALSE;

162 163
	recv_max_page_lsn = ut_dulint_zero;

164 165 166 167 168 169 170 171 172 173
	mutex_exit(&(recv_sys->mutex));
}

/************************************************************
Empties the hash table when it has been fully processed. */
static
void
recv_sys_empty_hash(void)
/*=====================*/
{
174
#ifdef UNIV_SYNC_DEBUG
175
	ut_ad(mutex_own(&(recv_sys->mutex)));
176
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
177 178 179 180
	if (recv_sys->n_addrs != 0) {
		fprintf(stderr,
"InnoDB: Error: %lu pages with log records were left unprocessed!\n"
"InnoDB: Maximum page number with log records on it %lu\n",
181 182
			(ulong) recv_sys->n_addrs, 
			(ulong) recv_max_parsed_page_no);
183
		ut_error;
unknown's avatar
unknown committed
184
	}
185 186 187 188 189 190 191 192 193
	
	hash_table_free(recv_sys->addr_hash);
	mem_heap_empty(recv_sys->heap);

	recv_sys->addr_hash = hash_create(buf_pool_get_curr_size() / 256);
}

/************************************************************
Frees the recovery system. */
unknown's avatar
unknown committed
194
static
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
void
recv_sys_free(void)
/*===============*/
{
	mutex_enter(&(recv_sys->mutex));
	
	hash_table_free(recv_sys->addr_hash);
	mem_heap_free(recv_sys->heap);
	ut_free(recv_sys->buf);
	mem_free(recv_sys->last_block_buf_start);

	recv_sys->addr_hash = NULL;
	recv_sys->heap = NULL;

	mutex_exit(&(recv_sys->mutex));
}

/************************************************************
Truncates possible corrupted or extra records from a log group. */
static
void
recv_truncate_group(
/*================*/
	log_group_t*	group,		/* in: log group */
	dulint		recovered_lsn,	/* in: recovery succeeded up to this
					lsn */
	dulint		limit_lsn,	/* in: this was the limit for
					recovery */
	dulint		checkpoint_lsn,	/* in: recovery was started from this
					checkpoint */
	dulint		archived_lsn)	/* in: the log has been archived up to
					this lsn */
{
	dulint	start_lsn;
	dulint	end_lsn;
	dulint	finish_lsn1;
	dulint	finish_lsn2;
	dulint	finish_lsn;
	ulint	len;
	ulint	i;

	if (ut_dulint_cmp(archived_lsn, ut_dulint_max) == 0) {
		/* Checkpoint was taken in the NOARCHIVELOG mode */
		archived_lsn = checkpoint_lsn;
	}

	finish_lsn1 = ut_dulint_add(ut_dulint_align_down(archived_lsn,
						OS_FILE_LOG_BLOCK_SIZE),
					log_group_get_capacity(group));
					
	finish_lsn2 = ut_dulint_add(ut_dulint_align_up(recovered_lsn,
						OS_FILE_LOG_BLOCK_SIZE),
					recv_sys->last_log_buf_size);

	if (ut_dulint_cmp(limit_lsn, ut_dulint_max) != 0) {
		/* We do not know how far we should erase log records: erase
		as much as possible */

		finish_lsn = finish_lsn1;
	} else {
		/* It is enough to erase the length of the log buffer */
		finish_lsn = ut_dulint_get_min(finish_lsn1, finish_lsn2);
	}
				
	ut_a(RECV_SCAN_SIZE <= log_sys->buf_size);	

	/* Write the log buffer full of zeros */
	for (i = 0; i < RECV_SCAN_SIZE; i++) {

		*(log_sys->buf + i) = '\0';
	}

	start_lsn = ut_dulint_align_down(recovered_lsn,
						OS_FILE_LOG_BLOCK_SIZE);
	
	if (ut_dulint_cmp(start_lsn, recovered_lsn) != 0) {
		/* Copy the last incomplete log block to the log buffer and
		edit its data length: */

		ut_memcpy(log_sys->buf, recv_sys->last_block,
						OS_FILE_LOG_BLOCK_SIZE);
		log_block_set_data_len(log_sys->buf,
				ut_dulint_minus(recovered_lsn, start_lsn));
	}
				
	if (ut_dulint_cmp(start_lsn, finish_lsn) >= 0) {

		return;
	}

    	for (;;) {
		end_lsn = ut_dulint_add(start_lsn, RECV_SCAN_SIZE);
    	
		if (ut_dulint_cmp(end_lsn, finish_lsn) > 0) {

			end_lsn = finish_lsn;
		}

		len = ut_dulint_minus(end_lsn, start_lsn);
		
295
		log_group_write_buf(group, log_sys->buf, len, start_lsn, 0);
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
		if (ut_dulint_cmp(end_lsn, finish_lsn) >= 0) {

			return;
		}

		/* Write the log buffer full of zeros */
		for (i = 0; i < RECV_SCAN_SIZE; i++) {

			*(log_sys->buf + i) = '\0';
		}

		start_lsn = end_lsn;
	}
}

/************************************************************
Copies the log segment between group->recovered_lsn and recovered_lsn from the
most up-to-date log group to group, so that it contains the latest log data. */
static
void
recv_copy_group(
/*============*/
	log_group_t*	up_to_date_group,	/* in: the most up-to-date log
						group */
unknown's avatar
unknown committed
320 321
	log_group_t*	group,			/* in: copy to this log
						group */
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
	dulint		recovered_lsn)		/* in: recovery succeeded up
						to this lsn */
{
	dulint	start_lsn;
	dulint	end_lsn;
	ulint	len;

	if (ut_dulint_cmp(group->scanned_lsn, recovered_lsn) >= 0) {

		return;
	}
					
	ut_a(RECV_SCAN_SIZE <= log_sys->buf_size);

	start_lsn = ut_dulint_align_down(group->scanned_lsn,
						OS_FILE_LOG_BLOCK_SIZE);
    	for (;;) {
		end_lsn = ut_dulint_add(start_lsn, RECV_SCAN_SIZE);
    	
		if (ut_dulint_cmp(end_lsn, recovered_lsn) > 0) {
			end_lsn = ut_dulint_align_up(recovered_lsn,
						OS_FILE_LOG_BLOCK_SIZE);
		}

		log_group_read_log_seg(LOG_RECOVER, log_sys->buf,
					up_to_date_group, start_lsn, end_lsn);

		len = ut_dulint_minus(end_lsn, start_lsn);
		
351
		log_group_write_buf(group, log_sys->buf, len, start_lsn, 0);
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
		
		if (ut_dulint_cmp(end_lsn, recovered_lsn) >= 0) {

			return;
		}

		start_lsn = end_lsn;
	}
}

/************************************************************
Copies a log segment from the most up-to-date log group to the other log
groups, so that they all contain the latest log data. Also writes the info
about the latest checkpoint to the groups, and inits the fields in the group
memory structs to up-to-date values. */
unknown's avatar
unknown committed
367
static
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
void
recv_synchronize_groups(
/*====================*/
	log_group_t*	up_to_date_group)	/* in: the most up-to-date
						log group */
{
	log_group_t*	group;
	dulint		start_lsn;
	dulint		end_lsn;
	dulint		recovered_lsn;
	dulint		limit_lsn;

	recovered_lsn = recv_sys->recovered_lsn;
	limit_lsn = recv_sys->limit_lsn;

	/* Read the last recovered log block to the recovery system buffer:
	the block is always incomplete */

unknown's avatar
unknown committed
386 387
	start_lsn = ut_dulint_align_down(recovered_lsn,
						OS_FILE_LOG_BLOCK_SIZE);
388 389
	end_lsn = ut_dulint_align_up(recovered_lsn, OS_FILE_LOG_BLOCK_SIZE);

unknown's avatar
unknown committed
390
	ut_a(ut_dulint_cmp(start_lsn, end_lsn) != 0);
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

	log_group_read_log_seg(LOG_RECOVER, recv_sys->last_block,
					up_to_date_group, start_lsn, end_lsn);

	group = UT_LIST_GET_FIRST(log_sys->log_groups);

	while (group) {
		if (group != up_to_date_group) {

			/* Copy log data if needed */

			recv_copy_group(group, up_to_date_group,
								recovered_lsn);
		}

		/* Update the fields in the group struct to correspond to
		recovered_lsn */

		log_group_set_fields(group, recovered_lsn);

		group = UT_LIST_GET_NEXT(log_groups, group);
	}

	/* Copy the checkpoint info to the groups; remember that we have
	incremented checkpoint_no by one, and the info will not be written
	over the max checkpoint info, thus making the preservation of max
	checkpoint info on disk certain */

	log_groups_write_checkpoint_info();

	mutex_exit(&(log_sys->mutex));

	/* Wait for the checkpoint write to complete */
	rw_lock_s_lock(&(log_sys->checkpoint_lock));
	rw_lock_s_unlock(&(log_sys->checkpoint_lock));

	mutex_enter(&(log_sys->mutex));
}

unknown's avatar
unknown committed
430 431 432 433 434 435 436 437 438 439 440 441 442
/***************************************************************************
Checks the consistency of the checkpoint info */
static
ibool
recv_check_cp_is_consistent(
/*========================*/
			/* out: TRUE if ok */
	byte*	buf)	/* in: buffer containing checkpoint info */
{
	ulint	fold;

	fold = ut_fold_binary(buf, LOG_CHECKPOINT_CHECKSUM_1);

unknown's avatar
unknown committed
443
	if ((fold & 0xFFFFFFFFUL) != mach_read_from_4(buf
unknown's avatar
unknown committed
444 445 446 447 448 449 450
				+ LOG_CHECKPOINT_CHECKSUM_1)) {		
		return(FALSE);
	}

	fold = ut_fold_binary(buf + LOG_CHECKPOINT_LSN,
			LOG_CHECKPOINT_CHECKSUM_2 - LOG_CHECKPOINT_LSN);

unknown's avatar
unknown committed
451
	if ((fold & 0xFFFFFFFFUL) != mach_read_from_4(buf
unknown's avatar
unknown committed
452 453 454 455 456 457 458
					+ LOG_CHECKPOINT_CHECKSUM_2)) {
		return(FALSE);
	}

	return(TRUE);
}

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
/************************************************************
Looks for the maximum consistent checkpoint from the log groups. */
static
ulint
recv_find_max_checkpoint(
/*=====================*/
					/* out: error code or DB_SUCCESS */
	log_group_t**	max_group,	/* out: max group */
	ulint*		max_field)	/* out: LOG_CHECKPOINT_1 or
					LOG_CHECKPOINT_2 */
{
	log_group_t*	group;
	dulint		max_no;
	dulint		checkpoint_no;
	ulint		field;
	byte*		buf;
	
	group = UT_LIST_GET_FIRST(log_sys->log_groups);

	max_no = ut_dulint_zero;
	*max_group = NULL;
480
	*max_field = 0;
481 482 483 484 485 486 487 488 489 490 491
	
	buf = log_sys->checkpoint_buf;
	
	while (group) {
		group->state = LOG_GROUP_CORRUPTED;
	
		for (field = LOG_CHECKPOINT_1; field <= LOG_CHECKPOINT_2;
				field += LOG_CHECKPOINT_2 - LOG_CHECKPOINT_1) {
	
			log_group_read_checkpoint_info(group, field);

unknown's avatar
unknown committed
492
			if (!recv_check_cp_is_consistent(buf)) {
493
#ifdef UNIV_DEBUG
494 495
				if (log_debug_writes) {
					fprintf(stderr, 
unknown's avatar
unknown committed
496
	    "InnoDB: Checkpoint in group %lu at %lu invalid, %lu\n",
497 498 499
						(ulong) group->id,
						(ulong) field,
                                 (ulong) mach_read_from_4(buf
500 501
					      + LOG_CHECKPOINT_CHECKSUM_1));

502
				}
503
#endif /* UNIV_DEBUG */
504 505 506 507 508 509 510 511 512 513 514 515
				goto not_consistent;
			}

			group->state = LOG_GROUP_OK;

			group->lsn = mach_read_from_8(buf
						+ LOG_CHECKPOINT_LSN);
			group->lsn_offset = mach_read_from_4(buf
						+ LOG_CHECKPOINT_OFFSET);
			checkpoint_no =
				mach_read_from_8(buf + LOG_CHECKPOINT_NO);

516
#ifdef UNIV_DEBUG
517 518
			if (log_debug_writes) {
				fprintf(stderr, 
519
			"InnoDB: Checkpoint number %lu found in group %lu\n",
520 521
				(ulong) ut_dulint_get_low(checkpoint_no),
				(ulong) group->id);
522
			}
523 524
#endif /* UNIV_DEBUG */

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
			if (ut_dulint_cmp(checkpoint_no, max_no) >= 0) {
				*max_group = group;
				*max_field = field;
				max_no = checkpoint_no;
			}

		not_consistent:
			;
		}

		group = UT_LIST_GET_NEXT(log_groups, group);
	}

	if (*max_group == NULL) {

unknown's avatar
unknown committed
540 541 542 543 544
		fprintf(stderr,
"InnoDB: No valid checkpoint found.\n"
"InnoDB: If this error appears when you are creating an InnoDB database,\n"
"InnoDB: the problem may be that during an earlier attempt you managed\n"
"InnoDB: to create the InnoDB data files, but log file creation failed.\n"
545 546
"InnoDB: If that is the case, please refer to\n"
"InnoDB: http://dev.mysql.com/doc/mysql/en/Error_creating_InnoDB.html\n");
547 548 549 550 551 552
		return(DB_ERROR);
	}

	return(DB_SUCCESS);
}

unknown's avatar
unknown committed
553 554 555 556 557 558 559 560 561 562
/***********************************************************************
Reads the checkpoint info needed in hot backup. */

ibool
recv_read_cp_info_for_backup(
/*=========================*/
			/* out: TRUE if success */
	byte*	hdr,	/* in: buffer containing the log group header */
	dulint*	lsn,	/* out: checkpoint lsn */
	ulint*	offset,	/* out: checkpoint offset in the log group */
unknown's avatar
unknown committed
563 564
	ulint*	fsp_limit,/* out: fsp limit of space 0, 1000000000 if the
			database is running with < version 3.23.50 of InnoDB */
unknown's avatar
unknown committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
	dulint*	cp_no,	/* out: checkpoint number */
	dulint*	first_header_lsn)
			/* out: lsn of of the start of the first log file */
{
	ulint	max_cp		= 0;
	dulint	max_cp_no	= ut_dulint_zero;
	byte*	cp_buf;

	cp_buf = hdr + LOG_CHECKPOINT_1;

	if (recv_check_cp_is_consistent(cp_buf)) {
		max_cp_no = mach_read_from_8(cp_buf + LOG_CHECKPOINT_NO);
		max_cp = LOG_CHECKPOINT_1;
	}

	cp_buf = hdr + LOG_CHECKPOINT_2;

	if (recv_check_cp_is_consistent(cp_buf)) {
		if (ut_dulint_cmp(mach_read_from_8(cp_buf + LOG_CHECKPOINT_NO),
					max_cp_no) > 0) {
			max_cp = LOG_CHECKPOINT_2;
		}
	}

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

	cp_buf = hdr + max_cp;
	
	*lsn = mach_read_from_8(cp_buf + LOG_CHECKPOINT_LSN);
	*offset = mach_read_from_4(cp_buf + LOG_CHECKPOINT_OFFSET);

	/* If the user is running a pre-3.23.50 version of InnoDB, its
	checkpoint data does not contain the fsp limit info */
	if (mach_read_from_4(cp_buf + LOG_CHECKPOINT_FSP_MAGIC_N)
	    == LOG_CHECKPOINT_FSP_MAGIC_N_VAL) {
	
		*fsp_limit = mach_read_from_4(
				cp_buf + LOG_CHECKPOINT_FSP_FREE_LIMIT);

		if (*fsp_limit == 0) {
			*fsp_limit = 1000000000;
		}	
	} else {
		*fsp_limit = 1000000000;
	}

613
/*	fprintf(stderr, "fsp limit %lu MB\n", *fsp_limit); */
unknown's avatar
unknown committed
614 615 616 617 618 619 620 621

	*cp_no = mach_read_from_8(cp_buf + LOG_CHECKPOINT_NO);

	*first_header_lsn = mach_read_from_8(hdr + LOG_FILE_START_LSN);

	return(TRUE);
}

unknown's avatar
unknown committed
622
/**********************************************************
unknown's avatar
unknown committed
623 624 625
Checks the 4-byte checksum to the trailer checksum field of a log block.
We also accept a log block in the old format < InnoDB-3.23.52 where the
checksum field contains the log block number. */
unknown's avatar
unknown committed
626 627 628 629 630 631 632 633
static
ibool
log_block_checksum_is_ok_or_old_format(
/*===================================*/
			/* out: TRUE if ok, or if the log block may be in the
			format of InnoDB version < 3.23.52 */
	byte*	block)	/* in: pointer to a log block */
{
unknown's avatar
unknown committed
634 635 636
#ifdef UNIV_LOG_DEBUG
	return(TRUE);
#endif /* UNIV_LOG_DEBUG */
unknown's avatar
unknown committed
637
	if (log_block_calc_checksum(block) == log_block_get_checksum(block)) {
unknown's avatar
unknown committed
638 639 640 641

		return(TRUE);
	}

unknown's avatar
unknown committed
642
	if (log_block_get_hdr_no(block) == log_block_get_checksum(block)) {
unknown's avatar
unknown committed
643 644 645 646 647 648 649 650 651 652 653 654 655 656

		/* We assume the log block is in the format of
		InnoDB version < 3.23.52 and the block is ok */
/*
		fprintf(stderr,
"InnoDB: Scanned old format < InnoDB-3.23.52 log block number %lu\n",
			log_block_get_hdr_no(block));
*/
		return(TRUE);
	}

	return(FALSE);
}

unknown's avatar
unknown committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
/***********************************************************************
Scans the log segment and n_bytes_scanned is set to the length of valid
log scanned. */

void
recv_scan_log_seg_for_backup(
/*=========================*/
	byte*		buf,		/* in: buffer containing log data */
	ulint		buf_len,	/* in: data length in that buffer */
	dulint*		scanned_lsn,	/* in/out: lsn of buffer start,
					we return scanned lsn */
	ulint*		scanned_checkpoint_no,
					/* in/out: 4 lowest bytes of the
					highest scanned checkpoint number so
					far */
	ulint*		n_bytes_scanned)/* out: how much we were able to
					scan, smaller than buf_len if log
					data ended here */
{
	ulint	data_len;
	byte*	log_block;
	ulint	no;

	*n_bytes_scanned = 0;
	
	for (log_block = buf; log_block < buf + buf_len;
				log_block += OS_FILE_LOG_BLOCK_SIZE) {
	
		no = log_block_get_hdr_no(log_block);

unknown's avatar
unknown committed
687
/*		fprintf(stderr, "Log block header no %lu\n", no); */
unknown's avatar
unknown committed
688

unknown's avatar
unknown committed
689
		if (no != log_block_convert_lsn_to_no(*scanned_lsn)
unknown's avatar
unknown committed
690 691
		    || !log_block_checksum_is_ok_or_old_format(log_block)) {
/*
692
			fprintf(stderr,
unknown's avatar
unknown committed
693 694
"Log block n:o %lu, scanned lsn n:o %lu\n",
			no, log_block_convert_lsn_to_no(*scanned_lsn));
unknown's avatar
unknown committed
695 696 697 698
*/
			/* Garbage or an incompletely written log block */

			log_block += OS_FILE_LOG_BLOCK_SIZE;
unknown's avatar
unknown committed
699
/*
700
			fprintf(stderr,
unknown's avatar
unknown committed
701 702
"Next log block n:o %lu\n",
			log_block_get_hdr_no(log_block));
unknown's avatar
unknown committed
703 704 705 706 707 708 709 710 711
*/			
			break;
		}

		if (*scanned_checkpoint_no > 0
		    && log_block_get_checkpoint_no(log_block)
						< *scanned_checkpoint_no
		    && *scanned_checkpoint_no
			- log_block_get_checkpoint_no(log_block)
unknown's avatar
unknown committed
712
							> 0x80000000UL) {
unknown's avatar
unknown committed
713 714 715

			/* Garbage from a log buffer flush which was made
			before the most recent database recovery */
unknown's avatar
unknown committed
716
/*
717 718
			fprintf(stderr,
				"Scanned cp n:o %lu, block cp n:o %lu\n",
unknown's avatar
unknown committed
719 720
				*scanned_checkpoint_no,
				log_block_get_checkpoint_no(log_block));
unknown's avatar
unknown committed
721
*/
unknown's avatar
unknown committed
722 723 724 725 726 727 728 729 730 731 732 733 734 735
			break;
		}

		data_len = log_block_get_data_len(log_block);

		*scanned_checkpoint_no
				= log_block_get_checkpoint_no(log_block);
		*scanned_lsn = ut_dulint_add(*scanned_lsn, data_len);

		*n_bytes_scanned += data_len;
		
		if (data_len < OS_FILE_LOG_BLOCK_SIZE) {
			/* Log data ends here */

736 737
			/* fprintf(stderr, "Log block data len %lu\n",
				data_len); */
unknown's avatar
unknown committed
738 739 740 741 742 743

			break;
		}
	}
}

744 745
/***********************************************************************
Tries to parse a single log record body and also applies it to a page if
unknown's avatar
unknown committed
746
specified. File ops are parsed, but not applied in this function. */
747 748 749 750 751
static
byte*
recv_parse_or_apply_log_rec_body(
/*=============================*/
			/* out: log record end, NULL if not a complete
unknown's avatar
unknown committed
752
			record */
753 754 755 756 757 758 759 760 761
	byte	type,	/* in: type */
	byte*	ptr,	/* in: pointer to a buffer */
	byte*	end_ptr,/* in: pointer to the buffer end */
	page_t*	page,	/* in: buffer page or NULL; if not NULL, then the log
			record is applied to the page, and the log record
			should be complete then */
	mtr_t*	mtr)	/* in: mtr or NULL; should be non-NULL if and only if
			page is non-NULL */
{
unknown's avatar
unknown committed
762 763 764 765 766 767 768 769 770
	dict_index_t*	index = NULL;

	switch (type) {
	case MLOG_1BYTE: case MLOG_2BYTES: case MLOG_4BYTES: case MLOG_8BYTES:
		ptr = mlog_parse_nbytes(type, ptr, end_ptr, page);
		break;
	case MLOG_REC_INSERT: case MLOG_COMP_REC_INSERT:
		if (NULL != (ptr = mlog_parse_index(ptr, end_ptr,
				type == MLOG_COMP_REC_INSERT, &index))) {
unknown's avatar
unknown committed
771 772
			ut_a(!page
			  || (ibool)!!page_is_comp(page)==index->table->comp);
unknown's avatar
unknown committed
773 774 775 776 777 778 779
			ptr = page_cur_parse_insert_rec(FALSE, ptr, end_ptr,
							index, page, mtr);
		}
		break;
	case MLOG_REC_CLUST_DELETE_MARK: case MLOG_COMP_REC_CLUST_DELETE_MARK:
		if (NULL != (ptr = mlog_parse_index(ptr, end_ptr,
			type == MLOG_COMP_REC_CLUST_DELETE_MARK, &index))) {
unknown's avatar
unknown committed
780 781
			ut_a(!page
			  || (ibool)!!page_is_comp(page)==index->table->comp);
unknown's avatar
unknown committed
782 783 784 785
			ptr = btr_cur_parse_del_mark_set_clust_rec(ptr,
						end_ptr, index, page);
		}
		break;
786 787 788 789 790 791 792
	case MLOG_COMP_REC_SEC_DELETE_MARK:
		/* This log record type is obsolete, but we process it for
		backward compatibility with MySQL 5.0.3 and 5.0.4. */
		ut_a(!page || page_is_comp(page));
		ptr = mlog_parse_index(ptr, end_ptr, TRUE, &index);
		if (!ptr) {
			break;
unknown's avatar
unknown committed
793
		}
794 795 796
		/* Fall through */
	case MLOG_REC_SEC_DELETE_MARK:
		ptr = btr_cur_parse_del_mark_set_sec_rec(ptr, end_ptr, page);
unknown's avatar
unknown committed
797 798 799 800
		break;
	case MLOG_REC_UPDATE_IN_PLACE: case MLOG_COMP_REC_UPDATE_IN_PLACE:
		if (NULL != (ptr = mlog_parse_index(ptr, end_ptr,
			type == MLOG_COMP_REC_UPDATE_IN_PLACE, &index))) {
unknown's avatar
unknown committed
801 802
			ut_a(!page
			  || (ibool)!!page_is_comp(page)==index->table->comp);
unknown's avatar
unknown committed
803 804 805 806 807 808 809 810 811
			ptr = btr_cur_parse_update_in_place(ptr, end_ptr,
							page, index);
		}
		break;
	case MLOG_LIST_END_DELETE: case MLOG_COMP_LIST_END_DELETE:
	case MLOG_LIST_START_DELETE: case MLOG_COMP_LIST_START_DELETE:
		if (NULL != (ptr = mlog_parse_index(ptr, end_ptr,
			type == MLOG_COMP_LIST_END_DELETE
			|| type == MLOG_COMP_LIST_START_DELETE, &index))) {
unknown's avatar
unknown committed
812 813
			ut_a(!page
			  || (ibool)!!page_is_comp(page)==index->table->comp);
unknown's avatar
unknown committed
814 815 816 817 818 819 820
			ptr = page_parse_delete_rec_list(type, ptr, end_ptr,
							index, page, mtr);
		}
		break;
	case MLOG_LIST_END_COPY_CREATED: case MLOG_COMP_LIST_END_COPY_CREATED:
		if (NULL != (ptr = mlog_parse_index(ptr, end_ptr,
			type == MLOG_COMP_LIST_END_COPY_CREATED, &index))) {
unknown's avatar
unknown committed
821 822
			ut_a(!page
			  || (ibool)!!page_is_comp(page)==index->table->comp);
unknown's avatar
unknown committed
823 824 825 826 827 828 829
			ptr = page_parse_copy_rec_list_to_created_page(ptr,
						end_ptr, index, page, mtr);
		}
		break;
	case MLOG_PAGE_REORGANIZE: case MLOG_COMP_PAGE_REORGANIZE:
		if (NULL != (ptr = mlog_parse_index(ptr, end_ptr,
				type == MLOG_COMP_PAGE_REORGANIZE, &index))) {
unknown's avatar
unknown committed
830 831
			ut_a(!page
			  || (ibool)!!page_is_comp(page)==index->table->comp);
unknown's avatar
unknown committed
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
			ptr = btr_parse_page_reorganize(ptr, end_ptr, index,
								page, mtr);
		}
		break;
	case MLOG_PAGE_CREATE: case MLOG_COMP_PAGE_CREATE:
		ptr = page_parse_create(ptr, end_ptr,
				type == MLOG_COMP_PAGE_CREATE, page, mtr);
		break;
	case MLOG_UNDO_INSERT:
		ptr = trx_undo_parse_add_undo_rec(ptr, end_ptr, page);
		break;
	case MLOG_UNDO_ERASE_END:
		ptr = trx_undo_parse_erase_page_end(ptr, end_ptr, page, mtr);
		break;
	case MLOG_UNDO_INIT:
		ptr = trx_undo_parse_page_init(ptr, end_ptr, page, mtr);
		break;
	case MLOG_UNDO_HDR_DISCARD:
		ptr = trx_undo_parse_discard_latest(ptr, end_ptr, page, mtr);
		break;
	case MLOG_UNDO_HDR_CREATE:
	case MLOG_UNDO_HDR_REUSE:
		ptr = trx_undo_parse_page_header(type, ptr, end_ptr,
								page, mtr);
		break;
	case MLOG_REC_MIN_MARK: case MLOG_COMP_REC_MIN_MARK:
		ptr = btr_parse_set_min_rec_mark(ptr, end_ptr,
				type == MLOG_COMP_REC_MIN_MARK, page, mtr);
		break;
	case MLOG_REC_DELETE: case MLOG_COMP_REC_DELETE:
		if (NULL != (ptr = mlog_parse_index(ptr, end_ptr,
				type == MLOG_COMP_REC_DELETE, &index))) {
unknown's avatar
unknown committed
864 865
			ut_a(!page
			  || (ibool)!!page_is_comp(page)==index->table->comp);
unknown's avatar
unknown committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
			ptr = page_cur_parse_delete_rec(ptr, end_ptr,
							index, page, mtr);
		}
		break;
	case MLOG_IBUF_BITMAP_INIT:
		ptr = ibuf_parse_bitmap_init(ptr, end_ptr, page, mtr);
		break;
	case MLOG_INIT_FILE_PAGE:
		ptr = fsp_parse_init_file_page(ptr, end_ptr, page);
		break;
	case MLOG_WRITE_STRING:
		ptr = mlog_parse_string(ptr, end_ptr, page);
		break;
	case MLOG_FILE_CREATE:
	case MLOG_FILE_RENAME:
	case MLOG_FILE_DELETE:
		ptr = fil_op_log_parse_or_replay(ptr, end_ptr, type, FALSE,
unknown's avatar
unknown committed
883
							ULINT_UNDEFINED);
unknown's avatar
unknown committed
884 885 886
		break;
	default:
		ptr = NULL;
unknown's avatar
unknown committed
887
		recv_sys->found_corrupt_log = TRUE;
888 889
	}

unknown's avatar
unknown committed
890 891 892
	ut_ad(!page || ptr);
	if (index) {
		dict_table_t*	table = index->table;
unknown's avatar
unknown committed
893 894 895

		dict_mem_index_free(index);
		dict_mem_table_free(table);
unknown's avatar
unknown committed
896
	}
897
	
unknown's avatar
unknown committed
898
	return(ptr);
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
}

/*************************************************************************
Calculates the fold value of a page file address: used in inserting or
searching for a log record in the hash table. */
UNIV_INLINE
ulint
recv_fold(
/*======*/
			/* out: folded value */
	ulint	space,	/* in: space */
	ulint	page_no)/* in: page number */
{
	return(ut_fold_ulint_pair(space, page_no));
}

/*************************************************************************
Calculates the hash value of a page file address: used in inserting or
searching for a log record in the hash table. */
UNIV_INLINE
ulint
recv_hash(
/*======*/
			/* out: folded value */
	ulint	space,	/* in: space */
	ulint	page_no)/* in: page number */
{
	return(hash_calc_hash(recv_fold(space, page_no), recv_sys->addr_hash));
}

/*************************************************************************
Gets the hashed file address struct for a page. */
static
recv_addr_t*
recv_get_fil_addr_struct(
/*=====================*/
			/* out: file address struct, NULL if not found from
			the hash table */
	ulint	space,	/* in: space id */
	ulint	page_no)/* in: page number */
{
	recv_addr_t*	recv_addr;

	recv_addr = HASH_GET_FIRST(recv_sys->addr_hash,
						recv_hash(space, page_no));
	while (recv_addr) {
		if ((recv_addr->space == space)
				&& (recv_addr->page_no == page_no)) {

			break;
		}

		recv_addr = HASH_GET_NEXT(addr_hash, recv_addr);
	}

	return(recv_addr);
}

/***********************************************************************
Adds a new log record to the hash table of log records. */
static
void
recv_add_to_hash_table(
/*===================*/
	byte	type,		/* in: log record type */
	ulint	space,		/* in: space id */
	ulint	page_no,	/* in: page number */
	byte*	body,		/* in: log record body */
	byte*	rec_end,	/* in: log record end */
	dulint	start_lsn,	/* in: start lsn of the mtr */
	dulint	end_lsn)	/* in: end lsn of the mtr */
{
	recv_t*		recv;
	ulint		len;
	recv_data_t*	recv_data;
	recv_data_t**	prev_field;
	recv_addr_t*	recv_addr;
	
unknown's avatar
unknown committed
977 978 979 980 981 982 983
	if (fil_tablespace_deleted_or_being_deleted_in_mem(space, -1)) {
		/* The tablespace does not exist any more: do not store the
		log record */

		return;
	}

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
	len = rec_end - body;

	recv = mem_heap_alloc(recv_sys->heap, sizeof(recv_t));
	recv->type = type;
	recv->len = rec_end - body;
	recv->start_lsn = start_lsn;
	recv->end_lsn = end_lsn;

	recv_addr = recv_get_fil_addr_struct(space, page_no);
	
	if (recv_addr == NULL) {
		recv_addr = mem_heap_alloc(recv_sys->heap,
							sizeof(recv_addr_t));
		recv_addr->space = space;
		recv_addr->page_no = page_no;
		recv_addr->state = RECV_NOT_PROCESSED;

		UT_LIST_INIT(recv_addr->rec_list);

		HASH_INSERT(recv_addr_t, addr_hash, recv_sys->addr_hash,
					recv_fold(space, page_no), recv_addr);
		recv_sys->n_addrs++;
unknown's avatar
unknown committed
1006

unknown's avatar
unknown committed
1007
		/* fprintf(stderr, "Inserting log rec for space %lu, page %lu\n",
unknown's avatar
unknown committed
1008
					  space, page_no); */
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	}

	UT_LIST_ADD_LAST(rec_list, recv_addr->rec_list, recv);

	prev_field = &(recv->data);

	/* Store the log record body in chunks of less than UNIV_PAGE_SIZE:
	recv_sys->heap grows into the buffer pool, and bigger chunks could not
	be allocated */
	
	while (rec_end > body) {

		len = rec_end - body;
	
		if (len > RECV_DATA_BLOCK_SIZE) {
			len = RECV_DATA_BLOCK_SIZE;
		}
	
		recv_data = mem_heap_alloc(recv_sys->heap,
						sizeof(recv_data_t) + len);
		*prev_field = recv_data;

		ut_memcpy(((byte*)recv_data) + sizeof(recv_data_t), body, len);

		prev_field = &(recv_data->next);

		body += len;
	}

	*prev_field = NULL;
}

/*************************************************************************
Copies the log record body from recv to buf. */
static
void
recv_data_copy_to_buf(
/*==================*/
	byte*	buf,	/* in: buffer of length at least recv->len */
	recv_t*	recv)	/* in: log record */
{
	recv_data_t*	recv_data;
	ulint		part_len;
	ulint		len;

	len = recv->len;
	recv_data = recv->data;

	while (len > 0) {
		if (len > RECV_DATA_BLOCK_SIZE) {
			part_len = RECV_DATA_BLOCK_SIZE;
		} else {
			part_len = len;
		}

		ut_memcpy(buf, ((byte*)recv_data) + sizeof(recv_data_t),
								part_len);
		buf += part_len;
		len -= part_len;

		recv_data = recv_data->next;
	}
}

/****************************************************************************
Applies the hashed log records to the page, if the page lsn is less than the
lsn of a log record. This can be called when a buffer page has just been
read in, or also for a page already in the buffer pool. */

void
recv_recover_page(
/*==============*/
unknown's avatar
unknown committed
1081 1082 1083 1084
	ibool	recover_backup,	/* in: TRUE if we are recovering a backup
				page: then we do not acquire any latches
				since the page was read in outside the
				buffer pool */
1085 1086 1087 1088 1089 1090
	ibool	just_read_in,	/* in: TRUE if the i/o-handler calls this for
				a freshly read page */
	page_t*	page,		/* in: buffer page */
	ulint	space,		/* in: space id */
	ulint	page_no)	/* in: page number */
{
unknown's avatar
unknown committed
1091
	buf_block_t*	block		= NULL;
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	recv_addr_t*	recv_addr;
	recv_t*		recv;
	byte*		buf;
	dulint		start_lsn;
	dulint		end_lsn;
	dulint		page_lsn;
	dulint		page_newest_lsn;
	ibool		modification_to_page;
	ibool		success;
	mtr_t		mtr;

	mutex_enter(&(recv_sys->mutex));

	if (recv_sys->apply_log_recs == FALSE) {

		/* Log records should not be applied now */
	
		mutex_exit(&(recv_sys->mutex));

		return;
	}
	
	recv_addr = recv_get_fil_addr_struct(space, page_no);

	if ((recv_addr == NULL)
	    || (recv_addr->state == RECV_BEING_PROCESSED)
	    || (recv_addr->state == RECV_PROCESSED)) {

		mutex_exit(&(recv_sys->mutex));

		return;
	}

unknown's avatar
unknown committed
1125
	/* fprintf(stderr, "Recovering space %lu, page %lu\n", space, page_no); */
unknown's avatar
unknown committed
1126

1127 1128 1129 1130
	recv_addr->state = RECV_BEING_PROCESSED;
	
	mutex_exit(&(recv_sys->mutex));

unknown's avatar
unknown committed
1131 1132
	mtr_start(&mtr);
	mtr_set_log_mode(&mtr, MTR_LOG_NONE);
1133

unknown's avatar
unknown committed
1134 1135
	if (!recover_backup) {	
		block = buf_block_align(page);
1136

unknown's avatar
unknown committed
1137
		if (just_read_in) {
unknown's avatar
unknown committed
1138 1139 1140 1141
		  /* Move the ownership of the x-latch on the page to this OS
		  thread, so that we can acquire a second x-latch on it. This
		  is needed for the operations to the page to pass the debug
		  checks. */
1142

unknown's avatar
unknown committed
1143 1144
			rw_lock_x_lock_move_ownership(&(block->lock));
		}
1145

unknown's avatar
unknown committed
1146 1147
		success = buf_page_get_known_nowait(RW_X_LATCH, page,
					BUF_KEEP_OLD,
unknown's avatar
unknown committed
1148
					__FILE__, __LINE__,
1149
					&mtr);
unknown's avatar
unknown committed
1150
		ut_a(success);
1151

1152
#ifdef UNIV_SYNC_DEBUG
unknown's avatar
unknown committed
1153
		buf_page_dbg_add_level(page, SYNC_NO_ORDER_CHECK);
1154
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
1155
	}
1156 1157 1158 1159

	/* Read the newest modification lsn from the page */
	page_lsn = mach_read_from_8(page + FIL_PAGE_LSN);

unknown's avatar
unknown committed
1160 1161 1162
	if (!recover_backup) {
		/* It may be that the page has been modified in the buffer
		pool: read the newest modification lsn there */
1163
		
unknown's avatar
unknown committed
1164
		page_newest_lsn = buf_frame_get_newest_modification(page);
1165

unknown's avatar
unknown committed
1166
		if (!ut_dulint_is_zero(page_newest_lsn)) {
1167
		
unknown's avatar
unknown committed
1168 1169 1170
			page_lsn = page_newest_lsn;
		}
	} else {
unknown's avatar
unknown committed
1171
		/* In recovery from a backup we do not really use the buffer
unknown's avatar
unknown committed
1172 1173 1174
		pool */

		page_newest_lsn = ut_dulint_zero;
1175 1176 1177
	}

	modification_to_page = FALSE;
1178
	start_lsn = end_lsn = ut_dulint_zero;
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195

	recv = UT_LIST_GET_FIRST(recv_addr->rec_list);
	
	while (recv) {
		end_lsn = recv->end_lsn;
	
		if (recv->len > RECV_DATA_BLOCK_SIZE) {
			/* We have to copy the record body to a separate
			buffer */

			buf = mem_alloc(recv->len);

			recv_data_copy_to_buf(buf, recv);
		} else {
			buf = ((byte*)(recv->data)) + sizeof(recv_data_t);
		}

1196
		if (recv->type == MLOG_INIT_FILE_PAGE) {
1197 1198 1199
			page_lsn = page_newest_lsn;

			mach_write_to_8(page + UNIV_PAGE_SIZE
unknown's avatar
unknown committed
1200
				- FIL_PAGE_END_LSN_OLD_CHKSUM, ut_dulint_zero);
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
			mach_write_to_8(page + FIL_PAGE_LSN, ut_dulint_zero);
		}
		
		if (ut_dulint_cmp(recv->start_lsn, page_lsn) >= 0) {

			if (!modification_to_page) {
		
				modification_to_page = TRUE;
				start_lsn = recv->start_lsn;
			}

1212
#ifdef UNIV_DEBUG
1213 1214
			if (log_debug_writes) {
				fprintf(stderr, 
1215
     "InnoDB: Applying log rec type %lu len %lu to space %lu page no %lu\n",
1216 1217 1218
					(ulong) recv->type, (ulong) recv->len,
					(ulong) recv_addr->space,
					(ulong) recv_addr->page_no);
1219
			}
1220
#endif /* UNIV_DEBUG */
1221

1222 1223
			recv_parse_or_apply_log_rec_body(recv->type, buf,
						buf + recv->len, page, &mtr);
unknown's avatar
unknown committed
1224
			mach_write_to_8(page + UNIV_PAGE_SIZE
unknown's avatar
unknown committed
1225
					- FIL_PAGE_END_LSN_OLD_CHKSUM,
unknown's avatar
unknown committed
1226 1227 1228 1229 1230
					ut_dulint_add(recv->start_lsn,
							recv->len));
			mach_write_to_8(page + FIL_PAGE_LSN,
					ut_dulint_add(recv->start_lsn,
							recv->len));
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
		}
						
		if (recv->len > RECV_DATA_BLOCK_SIZE) {
			mem_free(buf);
		}

		recv = UT_LIST_GET_NEXT(rec_list, recv);
	}

	mutex_enter(&(recv_sys->mutex));
	
1242 1243 1244 1245
	if (ut_dulint_cmp(recv_max_page_lsn, page_lsn) < 0) {
		recv_max_page_lsn = page_lsn;
	}

1246 1247 1248 1249 1250 1251 1252
	recv_addr->state = RECV_PROCESSED;

	ut_a(recv_sys->n_addrs);
	recv_sys->n_addrs--;

	mutex_exit(&(recv_sys->mutex));
	
unknown's avatar
unknown committed
1253
	if (!recover_backup && modification_to_page) {
unknown's avatar
unknown committed
1254 1255
		ut_a(block);

1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
		buf_flush_recv_note_modification(block, start_lsn, end_lsn);
	}
	
	/* Make sure that committing mtr does not change the modification
	lsn values of page */
	
	mtr.modifications = FALSE;
	
	mtr_commit(&mtr);	
}

/***********************************************************************
Reads in pages which have hashed log records, from an area around a given
page number. */
static
ulint
recv_read_in_area(
/*==============*/
			/* out: number of pages found */
	ulint	space,	/* in: space */
	ulint	page_no)/* in: page number */
{
	recv_addr_t* recv_addr;
	ulint	page_nos[RECV_READ_AHEAD_AREA];
	ulint	low_limit;
	ulint	n;

	low_limit = page_no - (page_no % RECV_READ_AHEAD_AREA);

	n = 0;

	for (page_no = low_limit; page_no < low_limit + RECV_READ_AHEAD_AREA;
								page_no++) {
		recv_addr = recv_get_fil_addr_struct(space, page_no);

		if (recv_addr && !buf_page_peek(space, page_no)) {

			mutex_enter(&(recv_sys->mutex));

			if (recv_addr->state == RECV_NOT_PROCESSED) {
				recv_addr->state = RECV_BEING_READ;
	
				page_nos[n] = page_no;

				n++;
			}
			
			mutex_exit(&(recv_sys->mutex));
		}
	}

	buf_read_recv_pages(FALSE, space, page_nos, n);
1308
	/*
1309
	fprintf(stderr, "Recv pages at %lu n %lu\n", page_nos[0], n);
1310
	*/
1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
	return(n);
}
			
/***********************************************************************
Empties the hash table of stored log records, applying them to appropriate
pages. */

void
recv_apply_hashed_log_recs(
/*=======================*/
	ibool	allow_ibuf)	/* in: if TRUE, also ibuf operations are
				allowed during the application; if FALSE,
				no ibuf operations are allowed, and after
				the application all file pages are flushed to
				disk and invalidated in buffer pool: this
				alternative means that no new log records
				can be generated during the application;
				the caller must in this case own the log
				mutex */
{
	recv_addr_t* recv_addr;
	page_t*	page;
	ulint	i;
	ulint	space;
	ulint	page_no;
	ulint	n_pages;
	ibool	has_printed	= FALSE;
	mtr_t	mtr;
loop:
	mutex_enter(&(recv_sys->mutex));

	if (recv_sys->apply_batch_on) {

		mutex_exit(&(recv_sys->mutex));

		os_thread_sleep(500000);

		goto loop;
	}

1351 1352 1353
#ifdef UNIV_SYNC_DEBUG
	ut_ad(!allow_ibuf == mutex_own(&log_sys->mutex));
#endif /* UNIV_SYNC_DEBUG */
1354 1355 1356
	if (!allow_ibuf) {
		recv_no_ibuf_operations = TRUE;
	}
1357

1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
	recv_sys->apply_log_recs = TRUE;
	recv_sys->apply_batch_on = TRUE;

	for (i = 0; i < hash_get_n_cells(recv_sys->addr_hash); i++) {
		
		recv_addr = HASH_GET_FIRST(recv_sys->addr_hash, i);

		while (recv_addr) {
			space = recv_addr->space;
			page_no = recv_addr->page_no;

			if (recv_addr->state == RECV_NOT_PROCESSED) {
				if (!has_printed) {
unknown's avatar
unknown committed
1371
	    				ut_print_timestamp(stderr);
unknown's avatar
unknown committed
1372
					fputs( 
unknown's avatar
unknown committed
1373
"  InnoDB: Starting an apply batch of log records to the database...\n"
unknown's avatar
unknown committed
1374
"InnoDB: Progress in percents: ",stderr);
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
					has_printed = TRUE;
				}
				
				mutex_exit(&(recv_sys->mutex));

				if (buf_page_peek(space, page_no)) {

					mtr_start(&mtr);

					page = buf_page_get(space, page_no,
							RW_X_LATCH, &mtr);

1387
#ifdef UNIV_SYNC_DEBUG
1388 1389
					buf_page_dbg_add_level(page,
							SYNC_NO_ORDER_CHECK);
1390
#endif /* UNIV_SYNC_DEBUG */
unknown's avatar
unknown committed
1391 1392
					recv_recover_page(FALSE, FALSE, page,
							space, page_no);
1393 1394 1395 1396 1397 1398 1399 1400 1401 1402
					mtr_commit(&mtr);
				} else {
					recv_read_in_area(space, page_no);
				}

				mutex_enter(&(recv_sys->mutex));
			}

			recv_addr = HASH_GET_NEXT(addr_hash, recv_addr);
		}
unknown's avatar
unknown committed
1403 1404 1405 1406 1407 1408 1409

		if (has_printed
		    && (i * 100) / hash_get_n_cells(recv_sys->addr_hash)
		    != ((i + 1) * 100)
		             / hash_get_n_cells(recv_sys->addr_hash)) {

		        fprintf(stderr, "%lu ",
1410
				(ulong) ((i * 100) / hash_get_n_cells(recv_sys->addr_hash)));
unknown's avatar
unknown committed
1411
		}
1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
	}

	/* Wait until all the pages have been processed */

	while (recv_sys->n_addrs != 0) {

		mutex_exit(&(recv_sys->mutex));

		os_thread_sleep(500000);

		mutex_enter(&(recv_sys->mutex));
	}	

unknown's avatar
unknown committed
1425 1426 1427 1428 1429
	if (has_printed) {

	        fprintf(stderr, "\n");
	}

1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
	if (!allow_ibuf) {
		/* Flush all the file pages to disk and invalidate them in
		the buffer pool */

		mutex_exit(&(recv_sys->mutex));
		mutex_exit(&(log_sys->mutex));

		n_pages = buf_flush_batch(BUF_FLUSH_LIST, ULINT_MAX,
								ut_dulint_max);
		ut_a(n_pages != ULINT_UNDEFINED);
		
		buf_flush_wait_batch_end(BUF_FLUSH_LIST);

		buf_pool_invalidate();

		mutex_enter(&(log_sys->mutex));
		mutex_enter(&(recv_sys->mutex));

		recv_no_ibuf_operations = FALSE;
	}

	recv_sys->apply_log_recs = FALSE;
	recv_sys->apply_batch_on = FALSE;
			
	recv_sys_empty_hash();

	if (has_printed) {
1457
		fprintf(stderr, "InnoDB: Apply batch completed\n");
1458 1459 1460 1461 1462
	}

	mutex_exit(&(recv_sys->mutex));
}

unknown's avatar
unknown committed
1463 1464
/* This page is allocated from the buffer pool and used in the function
below */
1465
static page_t* recv_backup_application_page	= NULL;
unknown's avatar
unknown committed
1466

unknown's avatar
unknown committed
1467 1468 1469 1470
/***********************************************************************
Applies log records in the hash table to a backup. */

void
unknown's avatar
unknown committed
1471 1472
recv_apply_log_recs_for_backup(void)
/*================================*/
unknown's avatar
unknown committed
1473 1474
{
	recv_addr_t*	recv_addr;
unknown's avatar
unknown committed
1475
	ulint		n_hash_cells;
unknown's avatar
unknown committed
1476
	byte*		page;
unknown's avatar
unknown committed
1477
	ulint		actual_size;
unknown's avatar
unknown committed
1478
	ibool		success;
unknown's avatar
unknown committed
1479
	ulint		error;
unknown's avatar
unknown committed
1480 1481 1482 1483 1484
	ulint		i;

	recv_sys->apply_log_recs = TRUE;
	recv_sys->apply_batch_on = TRUE;

unknown's avatar
unknown committed
1485 1486
	if (recv_backup_application_page == NULL) {
		recv_backup_application_page = buf_frame_alloc();
unknown's avatar
unknown committed
1487 1488
	}

unknown's avatar
unknown committed
1489
	page = recv_backup_application_page;
unknown's avatar
unknown committed
1490

1491
	fputs(
unknown's avatar
unknown committed
1492
"InnoDB: Starting an apply batch of log records to the database...\n"
1493
"InnoDB: Progress in percents: ", stderr);
unknown's avatar
unknown committed
1494
	
unknown's avatar
unknown committed
1495
	n_hash_cells = hash_get_n_cells(recv_sys->addr_hash);
unknown's avatar
unknown committed
1496

unknown's avatar
unknown committed
1497 1498 1499
	for (i = 0; i < n_hash_cells; i++) {
	        /* The address hash table is externally chained */
		recv_addr = hash_get_nth_cell(recv_sys->addr_hash, i)->node;
unknown's avatar
unknown committed
1500

unknown's avatar
unknown committed
1501 1502 1503 1504
		while (recv_addr != NULL) {

			if (!fil_tablespace_exists_in_mem(recv_addr->space)) {
/*
1505
				fprintf(stderr,
unknown's avatar
unknown committed
1506 1507 1508 1509 1510
"InnoDB: Warning: cannot apply log record to tablespace %lu page %lu,\n"
"InnoDB: because tablespace with that id does not exist.\n",
				      recv_addr->space, recv_addr->page_no);
*/
				recv_addr->state = RECV_PROCESSED;
unknown's avatar
unknown committed
1511

unknown's avatar
unknown committed
1512 1513 1514 1515
				ut_a(recv_sys->n_addrs);
				recv_sys->n_addrs--;

				goto skip_this_recv_addr;
unknown's avatar
unknown committed
1516
			}
unknown's avatar
unknown committed
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536

			/* We simulate a page read made by the buffer pool, to
			make sure the recovery apparatus works ok, for
			example, the buf_frame_align() function. We must init
			the block corresponding to buf_pool->frame_zero
			(== page). */

			buf_page_init_for_backup_restore(recv_addr->space,
						recv_addr->page_no,
						buf_block_align(page));

			/* Extend the tablespace's last file if the page_no
			does not fall inside its bounds; we assume the last
			file is auto-extending, and ibbackup copied the file
			when it still was smaller */

			success = fil_extend_space_to_desired_size(
						&actual_size,
						recv_addr->space,
						recv_addr->page_no + 1);
unknown's avatar
unknown committed
1537
			if (!success) {
unknown's avatar
unknown committed
1538
			  fprintf(stderr,
unknown's avatar
unknown committed
1539 1540 1541 1542 1543
"InnoDB: Fatal error: cannot extend tablespace %lu to hold %lu pages\n",
				     recv_addr->space, recv_addr->page_no);
				     
				exit(1);
			}
unknown's avatar
unknown committed
1544

unknown's avatar
unknown committed
1545 1546 1547 1548 1549 1550 1551
			/* Read the page from the tablespace file using the
			fil0fil.c routines */

			error = fil_io(OS_FILE_READ, TRUE, recv_addr->space,
					recv_addr->page_no, 0, UNIV_PAGE_SIZE,
					page, NULL);
			if (error != DB_SUCCESS) {
unknown's avatar
unknown committed
1552
			  fprintf(stderr,
unknown's avatar
unknown committed
1553
"InnoDB: Fatal error: cannot read from tablespace %lu page number %lu\n",
unknown's avatar
unknown committed
1554
				     (ulong) recv_addr->space, (ulong) recv_addr->page_no);
unknown's avatar
unknown committed
1555
				     
unknown's avatar
unknown committed
1556 1557
				exit(1);
			}
unknown's avatar
unknown committed
1558

unknown's avatar
unknown committed
1559 1560 1561
			/* Apply the log records to this page */
			recv_recover_page(TRUE, FALSE, page, recv_addr->space,
						       recv_addr->page_no);
unknown's avatar
unknown committed
1562

unknown's avatar
unknown committed
1563 1564
			/* Write the page back to the tablespace file using the
			fil0fil.c routines */
unknown's avatar
unknown committed
1565 1566 1567

			buf_flush_init_for_writing(page,
				mach_read_from_8(page + FIL_PAGE_LSN),
unknown's avatar
unknown committed
1568
				recv_addr->space, recv_addr->page_no);
unknown's avatar
unknown committed
1569

unknown's avatar
unknown committed
1570 1571 1572 1573 1574
			error = fil_io(OS_FILE_WRITE, TRUE, recv_addr->space,
					recv_addr->page_no, 0, UNIV_PAGE_SIZE,
					page, NULL);
skip_this_recv_addr:
			recv_addr = HASH_GET_NEXT(addr_hash, recv_addr);
unknown's avatar
unknown committed
1575 1576
		}

unknown's avatar
unknown committed
1577 1578
		if ((100 * i) / n_hash_cells
				!= (100 * (i + 1)) / n_hash_cells) {
unknown's avatar
unknown committed
1579 1580
			fprintf(stderr, "%lu ",
                                (ulong) ((100 * i) / n_hash_cells));
1581
			fflush(stderr);
unknown's avatar
unknown committed
1582 1583 1584 1585 1586 1587
		}
	}

	recv_sys_empty_hash();
}

unknown's avatar
unknown committed
1588
#ifdef notdefined
1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
/***********************************************************************
In the debug version, updates the replica of a file page, based on a log
record. */
static
void
recv_update_replicate(
/*==================*/
	byte	type,	/* in: log record type */
	ulint	space,	/* in: space id */
	ulint	page_no,/* in: page number */
	byte*	body,	/* in: log record body */
	byte*	end_ptr)/* in: log record end */
{
	page_t*	replica;
	mtr_t	mtr;
	byte*	ptr;

	mtr_start(&mtr);

	mtr_set_log_mode(&mtr, MTR_LOG_NONE);

	replica = buf_page_get(space + RECV_REPLICA_SPACE_ADD, page_no,
							RW_X_LATCH, &mtr);
1612
#ifdef UNIV_SYNC_DEBUG
1613
	buf_page_dbg_add_level(replica, SYNC_NO_ORDER_CHECK);
1614
#endif /* UNIV_SYNC_DEBUG */
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
							
	ptr = recv_parse_or_apply_log_rec_body(type, body, end_ptr, replica,
									&mtr);
	ut_a(ptr == end_ptr);

	/* Notify the buffer manager that the page has been updated */

	buf_flush_recv_note_modification(buf_block_align(replica),
					log_sys->old_lsn, log_sys->old_lsn);

	/* Make sure that committing mtr does not call log routines, as
	we currently own the log mutex */
	
	mtr.modifications = FALSE;

	mtr_commit(&mtr);
}

/***********************************************************************
Checks that two strings are identical. */
static
void
recv_check_identical(
/*=================*/
	byte*	str1,	/* in: first string */
	byte*	str2,	/* in: second string */
	ulint	len)	/* in: length of strings */
{
	ulint	i;

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

		if (str1[i] != str2[i]) {
unknown's avatar
unknown committed
1648 1649
			fprintf(stderr,
				"Strings do not match at offset %lu\n", i);
1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682
			ut_print_buf(str1 + i, 16);
			fprintf(stderr, "\n");
			ut_print_buf(str2 + i, 16);

			ut_error;
		}
	}
}	
			
/***********************************************************************
In the debug version, checks that the replica of a file page is identical
to the original page. */
static
void
recv_compare_replicate(
/*===================*/
	ulint	space,	/* in: space id */
	ulint	page_no)/* in: page number */
{
	page_t*	replica;
	page_t*	page;
	mtr_t	mtr;

	mtr_start(&mtr);

	mutex_enter(&(buf_pool->mutex));

	page = buf_page_hash_get(space, page_no)->frame;

	mutex_exit(&(buf_pool->mutex));

	replica = buf_page_get(space + RECV_REPLICA_SPACE_ADD, page_no,
							RW_X_LATCH, &mtr);
1683
#ifdef UNIV_SYNC_DEBUG
1684
	buf_page_dbg_add_level(replica, SYNC_NO_ORDER_CHECK);
1685
#endif /* UNIV_SYNC_DEBUG */
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722

	recv_check_identical(page + FIL_PAGE_DATA,
			replica + FIL_PAGE_DATA,
			PAGE_HEADER + PAGE_MAX_TRX_ID - FIL_PAGE_DATA);

	recv_check_identical(page + PAGE_HEADER + PAGE_MAX_TRX_ID + 8,
			replica + PAGE_HEADER + PAGE_MAX_TRX_ID + 8,
			UNIV_PAGE_SIZE - FIL_PAGE_DATA_END
			- PAGE_HEADER - PAGE_MAX_TRX_ID - 8);
	mtr_commit(&mtr);
}

/***********************************************************************
Checks that a replica of a space is identical to the original space. */

void
recv_compare_spaces(
/*================*/
	ulint	space1,	/* in: space id */
	ulint	space2,	/* in: space id */
	ulint	n_pages)/* in: number of pages */
{
	page_t*	replica;
	page_t*	page;
	mtr_t	mtr;
	page_t*	frame;
	ulint	page_no;

	replica = buf_frame_alloc();
	page = buf_frame_alloc();

	for (page_no = 0; page_no < n_pages; page_no++) {
	
		mtr_start(&mtr);

		frame = buf_page_get_gen(space1, page_no, RW_S_LATCH, NULL,
						BUF_GET_IF_IN_POOL,
unknown's avatar
unknown committed
1723
						__FILE__, __LINE__,
1724 1725
						&mtr);
		if (frame) {
1726
#ifdef UNIV_SYNC_DEBUG
1727
			buf_page_dbg_add_level(frame, SYNC_NO_ORDER_CHECK);
1728
#endif /* UNIV_SYNC_DEBUG */
1729 1730 1731 1732 1733 1734 1735 1736 1737
			ut_memcpy(page, frame, UNIV_PAGE_SIZE);
		} else {
			/* Read it from file */
			fil_io(OS_FILE_READ, TRUE, space1, page_no, 0,
						UNIV_PAGE_SIZE, page, NULL);
		}

		frame = buf_page_get_gen(space2, page_no, RW_S_LATCH, NULL,
						BUF_GET_IF_IN_POOL,
unknown's avatar
unknown committed
1738
						__FILE__, __LINE__,
1739 1740
						&mtr);
		if (frame) {
1741
#ifdef UNIV_SYNC_DEBUG
1742
			buf_page_dbg_add_level(frame, SYNC_NO_ORDER_CHECK);
1743
#endif /* UNIV_SYNC_DEBUG */
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787
			ut_memcpy(replica, frame, UNIV_PAGE_SIZE);
		} else {
			/* Read it from file */
			fil_io(OS_FILE_READ, TRUE, space2, page_no, 0,
				UNIV_PAGE_SIZE, replica, NULL);
		}
		
		recv_check_identical(page + FIL_PAGE_DATA,
			replica + FIL_PAGE_DATA,
			PAGE_HEADER + PAGE_MAX_TRX_ID - FIL_PAGE_DATA);

		recv_check_identical(page + PAGE_HEADER + PAGE_MAX_TRX_ID + 8,
			replica + PAGE_HEADER + PAGE_MAX_TRX_ID + 8,
			UNIV_PAGE_SIZE - FIL_PAGE_DATA_END
			- PAGE_HEADER - PAGE_MAX_TRX_ID - 8);

		mtr_commit(&mtr);
	}

	buf_frame_free(replica);
	buf_frame_free(page);
}

/***********************************************************************
Checks that a replica of a space is identical to the original space. Disables
ibuf operations and flushes and invalidates the buffer pool pages after the
test. This function can be used to check the recovery before dict or trx
systems are initialized. */

void
recv_compare_spaces_low(
/*====================*/
	ulint	space1,	/* in: space id */
	ulint	space2,	/* in: space id */
	ulint	n_pages)/* in: number of pages */
{
	mutex_enter(&(log_sys->mutex));

	recv_apply_hashed_log_recs(FALSE);
	
	mutex_exit(&(log_sys->mutex));

	recv_compare_spaces(space1, space2, n_pages);
}
unknown's avatar
unknown committed
1788
#endif /* UNIV_LOG_REPLICATE */
1789 1790 1791 1792 1793 1794 1795 1796

/***********************************************************************
Tries to parse a single log record and returns its length. */
static
ulint
recv_parse_log_rec(
/*===============*/
			/* out: length of the record, or 0 if the record was
unknown's avatar
unknown committed
1797
			not complete */
1798 1799 1800 1801 1802 1803 1804 1805 1806
	byte*	ptr,	/* in: pointer to a buffer */
	byte*	end_ptr,/* in: pointer to the buffer end */
	byte*	type,	/* out: type */
	ulint*	space,	/* out: space id */
	ulint*	page_no,/* out: page number */
	byte**	body)	/* out: log record body start */
{
	byte*	new_ptr;

1807 1808
	*body = NULL;

1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823
	if (ptr == end_ptr) {

		return(0);
	}

	if (*ptr == MLOG_MULTI_REC_END) {
	
		*type = *ptr;

		return(1);
	}

	if (*ptr == MLOG_DUMMY_RECORD) {
		*type = *ptr;

unknown's avatar
unknown committed
1824
		*space = ULINT_UNDEFINED - 1; /* For debugging */
1825 1826 1827 1828 1829 1830

		return(1);
	}

	new_ptr = mlog_parse_initial_log_record(ptr, end_ptr, type, space,
								page_no);
1831 1832 1833
	*body = new_ptr;

	if (UNIV_UNLIKELY(!new_ptr)) {
unknown's avatar
unknown committed
1834

unknown's avatar
unknown committed
1835 1836 1837
	        return(0);
	}

unknown's avatar
unknown committed
1838
	/* Check that page_no is sensible */
unknown's avatar
unknown committed
1839

1840
	if (UNIV_UNLIKELY(*page_no > 0x8FFFFFFFUL)) {
unknown's avatar
unknown committed
1841 1842

		recv_sys->found_corrupt_log = TRUE;
1843 1844 1845 1846 1847 1848

		return(0);
	}

	new_ptr = recv_parse_or_apply_log_rec_body(*type, new_ptr, end_ptr,
								NULL, NULL);
1849
	if (UNIV_UNLIKELY(new_ptr == NULL)) {
1850 1851 1852 1853

		return(0);
	}

unknown's avatar
unknown committed
1854 1855 1856 1857
	if (*page_no > recv_max_parsed_page_no) {
		recv_max_parsed_page_no = *page_no;
	}
	
1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888
	return(new_ptr - ptr);
}

/***********************************************************
Calculates the new value for lsn when more data is added to the log. */
static
dulint
recv_calc_lsn_on_data_add(
/*======================*/
	dulint	lsn,	/* in: old lsn */
	ulint	len)	/* in: this many bytes of data is added, log block
			headers not included */
{
	ulint	frag_len;
	ulint	lsn_len;
	
	frag_len = (ut_dulint_get_low(lsn) % OS_FILE_LOG_BLOCK_SIZE)
		   					- LOG_BLOCK_HDR_SIZE;
	ut_ad(frag_len < OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_HDR_SIZE
		      					- LOG_BLOCK_TRL_SIZE);
	lsn_len = len + ((len + frag_len)
		    	 / (OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_HDR_SIZE
		      					- LOG_BLOCK_TRL_SIZE))
		     	 * (LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE);

	return(ut_dulint_add(lsn, lsn_len));
}

/***********************************************************
Checks that the parser recognizes incomplete initial segments of a log
record as incomplete. */
unknown's avatar
unknown committed
1889

1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907
void
recv_check_incomplete_log_recs(
/*===========================*/
	byte*	ptr,	/* in: pointer to a complete log record */
	ulint	len)	/* in: length of the log record */
{
	ulint	i;
	byte	type;
	ulint	space;
	ulint	page_no;
	byte*	body;
	
	for (i = 0; i < len; i++) {
		ut_a(0 == recv_parse_log_rec(ptr, ptr + i, &type, &space,
							&page_no, &body));
	}
}		

unknown's avatar
unknown committed
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921
/***********************************************************
Prints diagnostic info of corrupt log. */
static
void
recv_report_corrupt_log(
/*====================*/
	byte*	ptr,	/* in: pointer to corrupt log record */
	byte	type,	/* in: type of the record */
	ulint	space,	/* in: space id, this may also be garbage */
	ulint	page_no)/* in: page number, this may also be garbage */
{
	fprintf(stderr,
"InnoDB: ############### CORRUPT LOG RECORD FOUND\n"
"InnoDB: Log record type %lu, space id %lu, page number %lu\n"
1922
"InnoDB: Log parsing proceeded successfully up to %lu %lu\n"
unknown's avatar
unknown committed
1923 1924
"InnoDB: Previous log record type %lu, is multi %lu\n"
"InnoDB: Recv offset %lu, prev %lu\n",
1925 1926
	(ulong) type, (ulong) space, (ulong) page_no,
	(ulong) ut_dulint_get_high(recv_sys->recovered_lsn),
unknown's avatar
unknown committed
1927
        (ulong) ut_dulint_get_low(recv_sys->recovered_lsn),
unknown's avatar
unknown committed
1928 1929 1930 1931
	(ulong) recv_previous_parsed_rec_type,
	(ulong) recv_previous_parsed_rec_is_multi,
	(ulong) (ptr - recv_sys->buf),
	(ulong) recv_previous_parsed_rec_offset);
unknown's avatar
unknown committed
1932 1933 1934 1935 1936 1937

	if ((ulint)(ptr - recv_sys->buf + 100)
					> recv_previous_parsed_rec_offset
	    && (ulint)(ptr - recv_sys->buf + 100
					- recv_previous_parsed_rec_offset)
	       < 200000) {
1938 1939 1940 1941 1942
		fputs(
"InnoDB: Hex dump of corrupt log starting 100 bytes before the start\n"
"InnoDB: of the previous log rec,\n"
"InnoDB: and ending 100 bytes after the start of the corrupt rec:\n",
			stderr);
unknown's avatar
unknown committed
1943
 
1944
		ut_print_buf(stderr,
unknown's avatar
unknown committed
1945 1946 1947
		     recv_sys->buf + recv_previous_parsed_rec_offset - 100,
		     ptr - recv_sys->buf + 200 -
					recv_previous_parsed_rec_offset);
1948
		putc('\n', stderr);
unknown's avatar
unknown committed
1949 1950
	}

1951
	fputs(
unknown's avatar
unknown committed
1952 1953 1954
	"InnoDB: WARNING: the log file may have been corrupt and it\n"
	"InnoDB: is possible that the log scan did not proceed\n"
	"InnoDB: far enough in recovery! Please run CHECK TABLE\n"
unknown's avatar
unknown committed
1955 1956
	"InnoDB: on your InnoDB tables to check that they are ok!\n"
	"InnoDB: If mysqld crashes after this recovery, look at\n"
1957
	"InnoDB: http://dev.mysql.com/doc/mysql/en/Forcing_recovery.html\n"
1958
	"InnoDB: about forcing recovery.\n", stderr);
unknown's avatar
unknown committed
1959 1960

	fflush(stderr);
unknown's avatar
unknown committed
1961 1962
}

1963 1964
/***********************************************************
Parses log records from a buffer and stores them to a hash table to wait
unknown's avatar
unknown committed
1965 1966 1967
merging to file pages. */
static
ibool
1968 1969
recv_parse_log_recs(
/*================*/
unknown's avatar
unknown committed
1970
				/* out: currently always returns FALSE */
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987
	ibool	store_to_hash)	/* in: TRUE if the records should be stored
				to the hash table; this is set to FALSE if just
				debug checking is needed */
{
	byte*	ptr;
	byte*	end_ptr;
	ulint	single_rec;
	ulint	len;
	ulint	total_len;
	dulint	new_recovered_lsn;
	dulint	old_lsn;
	byte	type;
	ulint	space;
	ulint	page_no;
	byte*	body;
	ulint	n_recs;
	
1988
#ifdef UNIV_SYNC_DEBUG
1989
	ut_ad(mutex_own(&(log_sys->mutex)));
1990
#endif /* UNIV_SYNC_DEBUG */
1991 1992 1993 1994 1995 1996 1997 1998
	ut_ad(!ut_dulint_is_zero(recv_sys->parse_start_lsn));
loop:
	ptr = recv_sys->buf + recv_sys->recovered_offset;

	end_ptr = recv_sys->buf + recv_sys->len;

	if (ptr == end_ptr) {

unknown's avatar
unknown committed
1999
		return(FALSE);
2000 2001 2002 2003 2004
	}

	single_rec = (ulint)*ptr & MLOG_SINGLE_REC_FLAG;

	if (single_rec || *ptr == MLOG_DUMMY_RECORD) {
unknown's avatar
unknown committed
2005
		/* The mtr only modified a single page, or this is a file op */
2006 2007 2008

		old_lsn = recv_sys->recovered_lsn;

unknown's avatar
unknown committed
2009 2010 2011
		/* Try to parse a log record, fetching its type, space id,
		page no, and a pointer to the body of the log record */

2012 2013
		len = recv_parse_log_rec(ptr, end_ptr, &type, &space,
							&page_no, &body);
unknown's avatar
unknown committed
2014

unknown's avatar
unknown committed
2015
		if (len == 0 || recv_sys->found_corrupt_log) {
unknown's avatar
unknown committed
2016
			if (recv_sys->found_corrupt_log) {
unknown's avatar
unknown committed
2017 2018 2019

				recv_report_corrupt_log(ptr,
						type, space, page_no);
unknown's avatar
unknown committed
2020 2021
			}
		
unknown's avatar
unknown committed
2022
			return(FALSE);
2023 2024 2025 2026 2027 2028 2029 2030 2031 2032
		}

		new_recovered_lsn = recv_calc_lsn_on_data_add(old_lsn, len);

		if (ut_dulint_cmp(new_recovered_lsn, recv_sys->scanned_lsn)
								> 0) {
			/* The log record filled a log block, and we require
			that also the next log block should have been scanned
			in */

unknown's avatar
unknown committed
2033
			return(FALSE);
2034 2035
		}
		
unknown's avatar
unknown committed
2036 2037 2038 2039
		recv_previous_parsed_rec_type = (ulint)type;
		recv_previous_parsed_rec_offset = recv_sys->recovered_offset;
		recv_previous_parsed_rec_is_multi = 0;

2040 2041 2042
		recv_sys->recovered_offset += len;
		recv_sys->recovered_lsn = new_recovered_lsn;

2043
#ifdef UNIV_DEBUG
2044 2045
		if (log_debug_writes) {
			fprintf(stderr, 
2046
"InnoDB: Parsed a single log rec type %lu len %lu space %lu page no %lu\n",
2047 2048
				(ulong) type, (ulong) len, (ulong) space,
				(ulong) page_no);
2049
		}
2050
#endif /* UNIV_DEBUG */
2051 2052 2053 2054

		if (type == MLOG_DUMMY_RECORD) {
			/* Do nothing */
		
unknown's avatar
unknown committed
2055 2056 2057 2058
		} else if (store_to_hash && (type == MLOG_FILE_CREATE
					     || type == MLOG_FILE_RENAME
					     || type == MLOG_FILE_DELETE)) {
#ifdef UNIV_HOTBACKUP
unknown's avatar
unknown committed
2059 2060 2061 2062 2063 2064
			if (recv_replay_file_ops) {

				/* In ibbackup --apply-log, replay an .ibd file
				operation, if possible; note that
				fil_path_to_mysql_datadir is set in ibbackup to
				point to the datadir we should use there */
unknown's avatar
unknown committed
2065
			
unknown's avatar
unknown committed
2066 2067 2068
				if (NULL == fil_op_log_parse_or_replay(body,
						end_ptr, type, TRUE, space)) {
					fprintf(stderr,
unknown's avatar
unknown committed
2069 2070 2071
"InnoDB: Error: file op log record of type %lu space %lu not complete in\n"
"InnoDB: the replay phase. Path %s\n", (ulint)type, space, (char*)(body + 2));

unknown's avatar
unknown committed
2072 2073
					ut_a(0);
				}
unknown's avatar
unknown committed
2074 2075 2076 2077
			}
#endif
			/* In normal mysqld crash recovery we do not try to
			replay file operations */
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087
		} else if (store_to_hash) {
			recv_add_to_hash_table(type, space, page_no, body,
						ptr + len, old_lsn,
						recv_sys->recovered_lsn);
		} else {
			/* In debug checking, update a replicate page
			according to the log record, and check that it
			becomes identical with the original page */
#ifdef UNIV_LOG_DEBUG
			recv_check_incomplete_log_recs(ptr, len);
unknown's avatar
unknown committed
2088 2089 2090
#endif/* UNIV_LOG_DEBUG */
#ifdef UNIV_LOG_REPLICATE
			recv_update_replicate(type, space, page_no, body,
2091 2092
								ptr + len);
			recv_compare_replicate(space, page_no);
unknown's avatar
unknown committed
2093 2094
#endif /* UNIV_LOG_REPLICATE */

2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105
		}
	} else {
		/* Check that all the records associated with the single mtr
		are included within the buffer */

		total_len = 0;
		n_recs = 0;
		
		for (;;) {
			len = recv_parse_log_rec(ptr, end_ptr, &type, &space,
							&page_no, &body);
unknown's avatar
unknown committed
2106 2107 2108
			if (len == 0 || recv_sys->found_corrupt_log) {

			    	if (recv_sys->found_corrupt_log) {
unknown's avatar
unknown committed
2109

unknown's avatar
unknown committed
2110 2111 2112 2113 2114
					recv_report_corrupt_log(ptr,
						type, space, page_no);
			    	}

			    	return(FALSE);
2115 2116
			}

unknown's avatar
unknown committed
2117 2118 2119 2120 2121
			recv_previous_parsed_rec_type = (ulint)type;
			recv_previous_parsed_rec_offset
				= recv_sys->recovered_offset + total_len;
			recv_previous_parsed_rec_is_multi = 1;

2122 2123 2124 2125 2126
			if ((!store_to_hash) && (type != MLOG_MULTI_REC_END)) {
				/* In debug checking, update a replicate page
				according to the log record */
#ifdef UNIV_LOG_DEBUG
				recv_check_incomplete_log_recs(ptr, len);
unknown's avatar
unknown committed
2127 2128
#endif /* UNIV_LOG_DEBUG */
#ifdef UNIV_LOG_REPLICATE
2129 2130
				recv_update_replicate(type, space, page_no,
							body, ptr + len);
unknown's avatar
unknown committed
2131
#endif /* UNIV_LOG_REPLICATE */
2132
			}
2133 2134

#ifdef UNIV_DEBUG
2135 2136
			if (log_debug_writes) {
				fprintf(stderr, 
2137
"InnoDB: Parsed a multi log rec type %lu len %lu space %lu page no %lu\n",
2138 2139
				(ulong) type, (ulong) len, (ulong) space,
				(ulong) page_no);
2140
			}
2141
#endif /* UNIV_DEBUG */
2142

2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164
			total_len += len;
			n_recs++;

			ptr += len;

			if (type == MLOG_MULTI_REC_END) {

				/* Found the end mark for the records */

				break;
			}
		}

		new_recovered_lsn = recv_calc_lsn_on_data_add(
					recv_sys->recovered_lsn, total_len);

		if (ut_dulint_cmp(new_recovered_lsn, recv_sys->scanned_lsn)
								> 0) {
			/* The log record filled a log block, and we require
			that also the next log block should have been scanned
			in */

unknown's avatar
unknown committed
2165
			return(FALSE);
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175
		}

		/* Add all the records to the hash table */

		ptr = recv_sys->buf + recv_sys->recovered_offset;

		for (;;) {
			old_lsn = recv_sys->recovered_lsn;
			len = recv_parse_log_rec(ptr, end_ptr, &type, &space,
							&page_no, &body);
unknown's avatar
unknown committed
2176 2177 2178 2179 2180 2181
			if (recv_sys->found_corrupt_log) {

				recv_report_corrupt_log(ptr,
							type, space, page_no);
			}

2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198
			ut_a(len != 0);
			ut_a(0 == ((ulint)*ptr & MLOG_SINGLE_REC_FLAG));

			recv_sys->recovered_offset += len;
			recv_sys->recovered_lsn = recv_calc_lsn_on_data_add(
								old_lsn, len);
			if (type == MLOG_MULTI_REC_END) {

				/* Found the end mark for the records */

				break;
			}

			if (store_to_hash) {
				recv_add_to_hash_table(type, space, page_no,
						body, ptr + len, old_lsn,
						new_recovered_lsn);
unknown's avatar
unknown committed
2199
#ifdef UNIV_LOG_REPLICATE
2200 2201 2202 2203
			} else {
				/* In debug checking, check that the replicate
				page has become identical with the original
				page */
unknown's avatar
unknown committed
2204 2205
				recv_compare_replicate(space, page_no);
#endif /* UNIV_LOG_REPLICATE */
2206 2207 2208 2209 2210
			}
			
			ptr += len;
		}
	}
unknown's avatar
unknown committed
2211
   
2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
	goto loop;
}

/***********************************************************
Adds data from a new log block to the parsing buffer of recv_sys if
recv_sys->parse_start_lsn is non-zero. */
static
ibool
recv_sys_add_to_parsing_buf(
/*========================*/
				/* out: TRUE if more data added */
	byte*	log_block,	/* in: log block */
	dulint	scanned_lsn)	/* in: lsn of how far we were able to find
				data in this log block */
{
	ulint	more_len;
	ulint	data_len;
	ulint	start_offset;
	ulint	end_offset;

	ut_ad(ut_dulint_cmp(scanned_lsn, recv_sys->scanned_lsn) >= 0);

	if (ut_dulint_is_zero(recv_sys->parse_start_lsn)) {
		/* Cannot start parsing yet because no start point for
		it found */

		return(FALSE);
	}

	data_len = log_block_get_data_len(log_block);

	if (ut_dulint_cmp(recv_sys->parse_start_lsn, scanned_lsn) >= 0) {

		return(FALSE);

	} else if (ut_dulint_cmp(recv_sys->scanned_lsn, scanned_lsn) >= 0) {

		return(FALSE);
								
	} else if (ut_dulint_cmp(recv_sys->parse_start_lsn,
						recv_sys->scanned_lsn) > 0) {
		more_len = ut_dulint_minus(scanned_lsn,
						recv_sys->parse_start_lsn);
	} else {
		more_len = ut_dulint_minus(scanned_lsn, recv_sys->scanned_lsn);
	}

	if (more_len == 0) {

		return(FALSE);
	}
	
	ut_ad(data_len >= more_len);

	start_offset = data_len - more_len;

	if (start_offset < LOG_BLOCK_HDR_SIZE) {
		start_offset = LOG_BLOCK_HDR_SIZE;
	}

	end_offset = data_len;

	if (end_offset > OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE) {
		end_offset = OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_TRL_SIZE;
	}

	ut_ad(start_offset <= end_offset);

	if (start_offset < end_offset) {
		ut_memcpy(recv_sys->buf + recv_sys->len,
			log_block + start_offset, end_offset - start_offset);

		recv_sys->len += end_offset - start_offset;

unknown's avatar
unknown committed
2286
		ut_a(recv_sys->len <= RECV_PARSING_BUF_SIZE);
2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315
	}

	return(TRUE);
}

/***********************************************************
Moves the parsing buffer data left to the buffer start. */
static
void
recv_sys_justify_left_parsing_buf(void)
/*===================================*/
{	
	ut_memmove(recv_sys->buf, recv_sys->buf + recv_sys->recovered_offset,
				recv_sys->len - recv_sys->recovered_offset);

	recv_sys->len -= recv_sys->recovered_offset;

	recv_sys->recovered_offset = 0;
}

/***********************************************************
Scans log from a buffer and stores new log data to the parsing buffer. Parses
and hashes the log records if new data found. */

ibool
recv_scan_log_recs(
/*===============*/
				/* out: TRUE if limit_lsn has been reached, or
				not able to scan any more in this log group */
unknown's avatar
unknown committed
2316 2317 2318 2319 2320 2321 2322
	ibool	apply_automatically,/* in: TRUE if we want this function to
				apply log records automatically when the
				hash table becomes full; in the hot backup tool
				the tool does the applying, not this
				function */
	ulint	available_memory,/* in: we let the hash table of recs to grow
				to this size, at the maximum */
2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343
	ibool	store_to_hash,	/* in: TRUE if the records should be stored
				to the hash table; this is set to FALSE if just
				debug checking is needed */
	byte*	buf,		/* in: buffer containing a log segment or
				garbage */
	ulint	len,		/* in: buffer length */
	dulint	start_lsn,	/* in: buffer start lsn */
	dulint*	contiguous_lsn,	/* in/out: it is known that all log groups
				contain contiguous log data up to this lsn */
	dulint*	group_scanned_lsn)/* out: scanning succeeded up to this lsn */
{
	byte*	log_block;
	ulint	no;
	dulint	scanned_lsn;
	ibool	finished;
	ulint	data_len;
	ibool	more_data;

	ut_ad(ut_dulint_get_low(start_lsn) % OS_FILE_LOG_BLOCK_SIZE == 0);
	ut_ad(len % OS_FILE_LOG_BLOCK_SIZE == 0);
	ut_ad(len > 0);
unknown's avatar
unknown committed
2344 2345 2346
	ut_a(apply_automatically <= TRUE);
	ut_a(store_to_hash <= TRUE);
	
2347 2348 2349 2350 2351 2352 2353 2354 2355
	finished = FALSE;
	
	log_block = buf;
	scanned_lsn = start_lsn;
	more_data = FALSE;
	
	while (log_block < buf + len && !finished) {

		no = log_block_get_hdr_no(log_block);
2356 2357
/*
		fprintf(stderr, "Log block header no %lu\n", no);
2358

2359 2360 2361
		fprintf(stderr, "Scanned lsn no %lu\n",
				log_block_convert_lsn_to_no(scanned_lsn));
*/
unknown's avatar
unknown committed
2362
		if (no != log_block_convert_lsn_to_no(scanned_lsn)
unknown's avatar
unknown committed
2363 2364
		    || !log_block_checksum_is_ok_or_old_format(log_block)) {

unknown's avatar
unknown committed
2365
			if (no == log_block_convert_lsn_to_no(scanned_lsn)
unknown's avatar
unknown committed
2366 2367 2368 2369
			    && !log_block_checksum_is_ok_or_old_format(
								log_block)) {
				fprintf(stderr,
"InnoDB: Log block no %lu at lsn %lu %lu has\n"
unknown's avatar
unknown committed
2370
"InnoDB: ok header, but checksum field contains %lu, should be %lu\n",
2371 2372 2373 2374 2375
				(ulong) no,
				(ulong) ut_dulint_get_high(scanned_lsn),
				(ulong) ut_dulint_get_low(scanned_lsn),
				(ulong) log_block_get_checksum(log_block),
				(ulong) log_block_calc_checksum(log_block));
unknown's avatar
unknown committed
2376
			}
2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407

			/* Garbage or an incompletely written log block */

			finished = TRUE;

			break;
		}

		if (log_block_get_flush_bit(log_block)) {
			/* This block was a start of a log flush operation:
			we know that the previous flush operation must have
			been completed for all log groups before this block
			can have been flushed to any of the groups. Therefore,
			we know that log data is contiguous up to scanned_lsn
			in all non-corrupt log groups. */

			if (ut_dulint_cmp(scanned_lsn, *contiguous_lsn) > 0) {
				*contiguous_lsn = scanned_lsn;
			}
		}

		data_len = log_block_get_data_len(log_block);

		if ((store_to_hash || (data_len == OS_FILE_LOG_BLOCK_SIZE))
		    && (ut_dulint_cmp(ut_dulint_add(scanned_lsn, data_len),
						recv_sys->scanned_lsn) > 0)
		    && (recv_sys->scanned_checkpoint_no > 0)
		    && (log_block_get_checkpoint_no(log_block)
		       < recv_sys->scanned_checkpoint_no)
		    && (recv_sys->scanned_checkpoint_no
			- log_block_get_checkpoint_no(log_block)
unknown's avatar
unknown committed
2408
			> 0x80000000UL)) {
2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440

			/* Garbage from a log buffer flush which was made
			before the most recent database recovery */

			finished = TRUE;
#ifdef UNIV_LOG_DEBUG
			/* This is not really an error, but currently
			we stop here in the debug version: */

			ut_error;
#endif
			break;
		}		    
		
		if (ut_dulint_is_zero(recv_sys->parse_start_lsn)
			&& (log_block_get_first_rec_group(log_block) > 0)) {

			/* We found a point from which to start the parsing
			of log records */

			recv_sys->parse_start_lsn =
				ut_dulint_add(scanned_lsn,
				   log_block_get_first_rec_group(log_block));
			recv_sys->scanned_lsn = recv_sys->parse_start_lsn;
			recv_sys->recovered_lsn = recv_sys->parse_start_lsn;
		}

		scanned_lsn = ut_dulint_add(scanned_lsn, data_len);

		if (ut_dulint_cmp(scanned_lsn, recv_sys->scanned_lsn) > 0) {

			/* We were able to find more log data: add it to the
unknown's avatar
unknown committed
2441 2442
			parsing buffer if parse_start_lsn is already
			non-zero */
2443

unknown's avatar
unknown committed
2444 2445 2446 2447
			if (recv_sys->len + 4 * OS_FILE_LOG_BLOCK_SIZE
						>= RECV_PARSING_BUF_SIZE) {
				fprintf(stderr,
"InnoDB: Error: log parsing buffer overflow. Recovery may have failed!\n");
unknown's avatar
unknown committed
2448 2449 2450 2451 2452 2453

				recv_sys->found_corrupt_log = TRUE;

			} else if (!recv_sys->found_corrupt_log) {
				more_data = recv_sys_add_to_parsing_buf(
						log_block, scanned_lsn);
unknown's avatar
unknown committed
2454 2455
			}

2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471
			recv_sys->scanned_lsn = scanned_lsn;
			recv_sys->scanned_checkpoint_no =
					log_block_get_checkpoint_no(log_block);
		}
						
		if (data_len < OS_FILE_LOG_BLOCK_SIZE) {
			/* Log data for this group ends here */

			finished = TRUE;
		} else {
			log_block += OS_FILE_LOG_BLOCK_SIZE;
		}
	}

	*group_scanned_lsn = scanned_lsn;

unknown's avatar
unknown committed
2472 2473
	if (recv_needed_recovery
	    || (recv_is_from_backup && !recv_is_making_a_backup)) {
unknown's avatar
Merge  
unknown committed
2474 2475
		recv_scan_print_counter++;

unknown's avatar
unknown committed
2476 2477
		if (finished || (recv_scan_print_counter % 80 == 0)) {

unknown's avatar
Merge  
unknown committed
2478
			fprintf(stderr, 
2479
"InnoDB: Doing recovery: scanned up to log sequence number %lu %lu\n",
2480 2481
				(ulong) ut_dulint_get_high(*group_scanned_lsn),
				(ulong) ut_dulint_get_low(*group_scanned_lsn));
unknown's avatar
Merge  
unknown committed
2482
		}
unknown's avatar
unknown committed
2483
	}
2484

unknown's avatar
unknown committed
2485
	if (more_data && !recv_sys->found_corrupt_log) {
2486 2487 2488 2489
		/* Try to parse more log records */

		recv_parse_log_recs(store_to_hash);

unknown's avatar
unknown committed
2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502
		if (store_to_hash && mem_heap_get_size(recv_sys->heap)
						> available_memory
		    && apply_automatically) {
						
			/* Hash table of log records has grown too big:
			empty it; FALSE means no ibuf operations
			allowed, as we cannot add new records to the
			log yet: they would be produced by ibuf
			operations */
		
			recv_apply_hashed_log_recs(FALSE);
		} 

2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
		if (recv_sys->recovered_offset > RECV_PARSING_BUF_SIZE / 4) {
			/* Move parsing buffer data to the buffer start */

			recv_sys_justify_left_parsing_buf();
		}	
	}

	return(finished);
}	

/***********************************************************
Scans log from a buffer and stores new log data to the parsing buffer. Parses
and hashes the log records if new data found. */
static
void
recv_group_scan_log_recs(
/*=====================*/
	log_group_t* group,	/* in: log group */	
	dulint*	contiguous_lsn,	/* in/out: it is known that all log groups
				contain contiguous log data up to this lsn */
	dulint*	group_scanned_lsn)/* out: scanning succeeded up to this lsn */
{
	ibool	finished;
	dulint	start_lsn;
	dulint	end_lsn;
	
	finished = FALSE;

	start_lsn = *contiguous_lsn;
		
	while (!finished) {			
		end_lsn = ut_dulint_add(start_lsn, RECV_SCAN_SIZE);

		log_group_read_log_seg(LOG_RECOVER, log_sys->buf,
						group, start_lsn, end_lsn);

unknown's avatar
unknown committed
2539
		finished = recv_scan_log_recs(TRUE,
unknown's avatar
unknown committed
2540 2541
                                (buf_pool->n_frames
                                - recv_n_pool_free_frames) * UNIV_PAGE_SIZE,
unknown's avatar
unknown committed
2542 2543 2544
				TRUE, log_sys->buf,
				RECV_SCAN_SIZE, start_lsn,
				contiguous_lsn, group_scanned_lsn);
2545 2546 2547
		start_lsn = end_lsn;
	}

2548
#ifdef UNIV_DEBUG
2549 2550
	if (log_debug_writes) {
		fprintf(stderr,
2551
	"InnoDB: Scanned group %lu up to log sequence number %lu %lu\n",
2552 2553 2554
				(ulong) group->id,
				(ulong) ut_dulint_get_high(*group_scanned_lsn),
				(ulong) ut_dulint_get_low(*group_scanned_lsn));
2555
	}
2556
#endif /* UNIV_DEBUG */
2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585
}

/************************************************************
Recovers from a checkpoint. When this function returns, the database is able
to start processing of new user transactions, but the function
recv_recovery_from_checkpoint_finish should be called later to complete
the recovery and free the resources used in it. */

ulint
recv_recovery_from_checkpoint_start(
/*================================*/
				/* out: error code or DB_SUCCESS */
	ulint	type,		/* in: LOG_CHECKPOINT or LOG_ARCHIVE */
	dulint	limit_lsn,	/* in: recover up to this lsn if possible */
	dulint	min_flushed_lsn,/* in: min flushed lsn from data files */
	dulint	max_flushed_lsn)/* in: max flushed lsn from data files */
{
	log_group_t*	group;
	log_group_t*	max_cp_group;
	log_group_t*	up_to_date_group;
	ulint		max_cp_field;
	dulint		checkpoint_lsn;
	dulint		checkpoint_no;
	dulint		old_scanned_lsn;
	dulint		group_scanned_lsn;
	dulint		contiguous_lsn;
	dulint		archived_lsn;
	ulint		capacity;
	byte*		buf;
unknown's avatar
unknown committed
2586
	byte		log_hdr_buf[LOG_FILE_HDR_SIZE];
2587 2588 2589 2590 2591 2592 2593
	ulint		err;

	ut_ad((type != LOG_CHECKPOINT)
			|| (ut_dulint_cmp(limit_lsn, ut_dulint_max) == 0));
	
	if (type == LOG_CHECKPOINT) {
		recv_sys_create();
unknown's avatar
unknown committed
2594
		recv_sys_init(FALSE, buf_pool_get_curr_size());
2595 2596
	}

unknown's avatar
Merge  
unknown committed
2597 2598 2599 2600 2601 2602 2603 2604 2605
	if (srv_force_recovery >= SRV_FORCE_NO_LOG_REDO) {
		fprintf(stderr,
		"InnoDB: The user has set SRV_FORCE_NO_LOG_REDO on\n");
		fprintf(stderr,
		"InnoDB: Skipping log redo\n");
		
		return(DB_SUCCESS);
	}

2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
	recv_recovery_on = TRUE;

	recv_sys->limit_lsn = limit_lsn;

	mutex_enter(&(log_sys->mutex));

	/* Look for the latest checkpoint from any of the log groups */
	
	err = recv_find_max_checkpoint(&max_cp_group, &max_cp_field);

	if (err != DB_SUCCESS) {

		mutex_exit(&(log_sys->mutex));

		return(err);
	}
		
	log_group_read_checkpoint_info(max_cp_group, max_cp_field);

	buf = log_sys->checkpoint_buf;

	checkpoint_lsn = mach_read_from_8(buf + LOG_CHECKPOINT_LSN);
	checkpoint_no = mach_read_from_8(buf + LOG_CHECKPOINT_NO);
	archived_lsn = mach_read_from_8(buf + LOG_CHECKPOINT_ARCHIVED_LSN);

unknown's avatar
unknown committed
2631 2632 2633 2634 2635 2636 2637 2638
	/* Read the first log file header to print a note if this is
	a recovery from a restored InnoDB Hot Backup */
	
	fil_io(OS_FILE_READ | OS_FILE_LOG, TRUE, max_cp_group->space_id,
				0, 0, LOG_FILE_HDR_SIZE,
				log_hdr_buf, max_cp_group);

	if (0 == ut_memcmp(log_hdr_buf + LOG_FILE_WAS_CREATED_BY_HOT_BACKUP,
2639
			(byte*)"ibbackup", (sizeof "ibbackup") - 1)) {
unknown's avatar
unknown committed
2640 2641 2642 2643
		/* This log file was created by ibbackup --restore: print
		a note to the user about it */

		fprintf(stderr,
unknown's avatar
unknown committed
2644
	"InnoDB: The log file was created by ibbackup --apply-log at\n"
unknown's avatar
unknown committed
2645
	"InnoDB: %s\n", log_hdr_buf + LOG_FILE_WAS_CREATED_BY_HOT_BACKUP);
unknown's avatar
unknown committed
2646 2647
		fprintf(stderr,
"InnoDB: NOTE: the following crash recovery is part of a normal restore.\n");
unknown's avatar
unknown committed
2648 2649 2650
		
		/* Wipe over the label now */

unknown's avatar
unknown committed
2651 2652
		memset(log_hdr_buf + LOG_FILE_WAS_CREATED_BY_HOT_BACKUP,
					' ', 4);
unknown's avatar
unknown committed
2653 2654 2655 2656 2657 2658
		/* Write to the log file to wipe over the label */
		fil_io(OS_FILE_WRITE | OS_FILE_LOG, TRUE,
				max_cp_group->space_id,
				0, 0, OS_FILE_LOG_BLOCK_SIZE,
				log_hdr_buf, max_cp_group);
	}
unknown's avatar
unknown committed
2659 2660

#ifdef UNIV_LOG_ARCHIVE				
2661 2662 2663 2664 2665 2666 2667 2668 2669
	group = UT_LIST_GET_FIRST(log_sys->log_groups);

	while (group) {
		log_checkpoint_get_nth_group_info(buf, group->id,
						&(group->archived_file_no),
						&(group->archived_offset));

		group = UT_LIST_GET_NEXT(log_groups, group);
	}
unknown's avatar
unknown committed
2670
#endif /* UNIV_LOG_ARCHIVE */
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681

	if (type == LOG_CHECKPOINT) {
		/* Start reading the log groups from the checkpoint lsn up. The
		variable contiguous_lsn contains an lsn up to which the log is
		known to be contiguously written to all log groups. */

		recv_sys->parse_start_lsn = checkpoint_lsn;
		recv_sys->scanned_lsn = checkpoint_lsn;
		recv_sys->scanned_checkpoint_no = 0;
		recv_sys->recovered_lsn = checkpoint_lsn;

unknown's avatar
unknown committed
2682 2683 2684
		srv_start_lsn = checkpoint_lsn;

		/* NOTE: we always do a 'recovery' at startup, but only if
2685 2686 2687 2688 2689 2690
		there is something wrong we will print a message to the
		user about recovery: */
		
		if (ut_dulint_cmp(checkpoint_lsn, max_flushed_lsn) != 0
	    	   || ut_dulint_cmp(checkpoint_lsn, min_flushed_lsn) != 0) {

unknown's avatar
unknown committed
2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
			if (ut_dulint_cmp(checkpoint_lsn, max_flushed_lsn)
								< 0) {
				fprintf(stderr,
"InnoDB: ##########################################################\n"
"InnoDB:                          WARNING!\n"
"InnoDB: The log sequence number in ibdata files is higher\n"
"InnoDB: than the log sequence number in the ib_logfiles! Are you sure\n"
"InnoDB: you are using the right ib_logfiles to start up the database?\n"
"InnoDB: Log sequence number in ib_logfiles is %lu %lu, log\n"
"InnoDB: sequence numbers stamped to ibdata file headers are between\n"
"InnoDB: %lu %lu and %lu %lu.\n"
"InnoDB: ##########################################################\n",
2703 2704 2705 2706 2707 2708
				(ulong) ut_dulint_get_high(checkpoint_lsn),
				(ulong) ut_dulint_get_low(checkpoint_lsn),
				(ulong) ut_dulint_get_high(min_flushed_lsn),
				(ulong) ut_dulint_get_low(min_flushed_lsn),
				(ulong) ut_dulint_get_high(max_flushed_lsn),
				(ulong) ut_dulint_get_low(max_flushed_lsn));
unknown's avatar
unknown committed
2709 2710
			}

unknown's avatar
unknown committed
2711 2712 2713 2714
	    	   	recv_needed_recovery = TRUE;
	    	   
			ut_print_timestamp(stderr);

2715
	    		fprintf(stderr,
unknown's avatar
unknown committed
2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739
"  InnoDB: Database was not shut down normally!\n"
"InnoDB: Starting crash recovery.\n");

			fprintf(stderr,
"InnoDB: Reading tablespace information from the .ibd files...\n");

			fil_load_single_table_tablespaces();

			/* If we are using the doublewrite method, we will
			check if there are half-written pages in data files,
			and restore them from the doublewrite buffer if
			possible */
		
			if (srv_force_recovery < SRV_FORCE_NO_LOG_REDO) {
		
				fprintf(stderr,
"InnoDB: Restoring possible half-written data pages from the doublewrite\n"
"InnoDB: buffer...\n");
				trx_sys_doublewrite_init_or_restore_pages(
									TRUE);
			}

			ut_print_timestamp(stderr);

2740
			fprintf(stderr, 
unknown's avatar
unknown committed
2741 2742
"  InnoDB: Starting log scan based on checkpoint at\n"
"InnoDB: log sequence number %lu %lu.\n",
2743 2744
		 			(ulong) ut_dulint_get_high(checkpoint_lsn),
					(ulong) ut_dulint_get_low(checkpoint_lsn));
unknown's avatar
unknown committed
2745 2746 2747
		} else {
			/* Init the doublewrite buffer memory structure */
			trx_sys_doublewrite_init_or_restore_pages(FALSE);
2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802
		}
	}

	contiguous_lsn = ut_dulint_align_down(recv_sys->scanned_lsn,
						OS_FILE_LOG_BLOCK_SIZE);
	if (type == LOG_ARCHIVE) {
 		/* Try to recover the remaining part from logs: first from
		the logs of the archived group */

		group = recv_sys->archive_group;
		capacity = log_group_get_capacity(group);

		if ((ut_dulint_cmp(recv_sys->scanned_lsn,
				ut_dulint_add(checkpoint_lsn, capacity)) > 0)
		   || (ut_dulint_cmp(checkpoint_lsn,
			ut_dulint_add(recv_sys->scanned_lsn, capacity)) > 0)) {

			mutex_exit(&(log_sys->mutex));

			/* The group does not contain enough log: probably
			an archived log file was missing or corrupt */

			return(DB_ERROR);
		}
		
		recv_group_scan_log_recs(group, &contiguous_lsn,
							&group_scanned_lsn);
		if (ut_dulint_cmp(recv_sys->scanned_lsn, checkpoint_lsn) < 0) {

			mutex_exit(&(log_sys->mutex));

			/* The group did not contain enough log: an archived
			log file was missing or invalid, or the log group
			was corrupt */

			return(DB_ERROR);
		}

		group->scanned_lsn = group_scanned_lsn;
		up_to_date_group = group;
	} else {
		up_to_date_group = max_cp_group;
	}

	ut_ad(RECV_SCAN_SIZE <= log_sys->buf_size);

	group = UT_LIST_GET_FIRST(log_sys->log_groups);

	if ((type == LOG_ARCHIVE) && (group == recv_sys->archive_group)) {
		group = UT_LIST_GET_NEXT(log_groups, group);
	}		

	while (group) {		
		old_scanned_lsn = recv_sys->scanned_lsn;

unknown's avatar
Merge  
unknown committed
2803
		recv_group_scan_log_recs(group, &contiguous_lsn,
2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820
							&group_scanned_lsn);
		group->scanned_lsn = group_scanned_lsn;
		
		if (ut_dulint_cmp(old_scanned_lsn, group_scanned_lsn) < 0) {
			/* We found a more up-to-date group */

			up_to_date_group = group;
		}
		
		if ((type == LOG_ARCHIVE)
				&& (group == recv_sys->archive_group)) {
			group = UT_LIST_GET_NEXT(log_groups, group);
		}		

		group = UT_LIST_GET_NEXT(log_groups, group);
	}

2821 2822 2823 2824 2825 2826 2827
	/* We currently have only one log group */
	if (ut_dulint_cmp(group_scanned_lsn, checkpoint_lsn) < 0) {
		ut_print_timestamp(stderr);
		fprintf(stderr,
"  InnoDB: ERROR: We were only able to scan the log up to\n"
"InnoDB: %lu %lu, but a checkpoint was at %lu %lu.\n"
"InnoDB: It is possible that the database is now corrupt!\n",
2828 2829 2830 2831
			 (ulong) ut_dulint_get_high(group_scanned_lsn),
			 (ulong) ut_dulint_get_low(group_scanned_lsn),
			 (ulong) ut_dulint_get_high(checkpoint_lsn),
			 (ulong) ut_dulint_get_low(checkpoint_lsn));
2832 2833 2834 2835 2836 2837 2838 2839
	}

	if (ut_dulint_cmp(group_scanned_lsn, recv_max_page_lsn) < 0) {
		ut_print_timestamp(stderr);
		fprintf(stderr,
"  InnoDB: ERROR: We were only able to scan the log up to %lu %lu\n"
"InnoDB: but a database page a had an lsn %lu %lu. It is possible that the\n"
"InnoDB: database is now corrupt!\n",
2840 2841 2842 2843
			 (ulong) ut_dulint_get_high(group_scanned_lsn),
			 (ulong) ut_dulint_get_low(group_scanned_lsn),
			 (ulong) ut_dulint_get_high(recv_max_page_lsn),
			 (ulong) ut_dulint_get_low(recv_max_page_lsn));
2844 2845
	}

2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865
	if (ut_dulint_cmp(recv_sys->recovered_lsn, checkpoint_lsn) < 0) {

		mutex_exit(&(log_sys->mutex));

		if (ut_dulint_cmp(recv_sys->recovered_lsn, limit_lsn) >= 0) {

			return(DB_SUCCESS);
		}

		ut_error;

		return(DB_ERROR);
	}
	
	/* Synchronize the uncorrupted log groups to the most up-to-date log
	group; we also copy checkpoint info to groups */

	log_sys->next_checkpoint_lsn = checkpoint_lsn;
	log_sys->next_checkpoint_no = ut_dulint_add(checkpoint_no, 1);

unknown's avatar
unknown committed
2866
#ifdef UNIV_LOG_ARCHIVE
2867
	log_sys->archived_lsn = archived_lsn;
unknown's avatar
unknown committed
2868
#endif /* UNIV_LOG_ARCHIVE */
2869 2870
	
	recv_synchronize_groups(up_to_date_group);
unknown's avatar
unknown committed
2871 2872 2873 2874 2875 2876 2877

	if (!recv_needed_recovery) {
		if (ut_dulint_cmp(checkpoint_lsn, recv_sys->recovered_lsn)
								!= 0) {
			fprintf(stderr,
"InnoDB: Warning: we did not need to do crash recovery, but log scan\n"
"InnoDB: progressed past the checkpoint lsn %lu %lu up to lsn %lu %lu\n",
2878 2879 2880 2881
			 (ulong) ut_dulint_get_high(checkpoint_lsn),
			 (ulong) ut_dulint_get_low(checkpoint_lsn),
			 (ulong) ut_dulint_get_high(recv_sys->recovered_lsn),
			 (ulong) ut_dulint_get_low(recv_sys->recovered_lsn));
unknown's avatar
unknown committed
2882 2883 2884 2885
		}
	} else {
		srv_start_lsn = recv_sys->recovered_lsn;
	}
2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900
	
	log_sys->lsn = recv_sys->recovered_lsn;

	ut_memcpy(log_sys->buf, recv_sys->last_block, OS_FILE_LOG_BLOCK_SIZE);

	log_sys->buf_free = ut_dulint_get_low(log_sys->lsn)
						% OS_FILE_LOG_BLOCK_SIZE;
	log_sys->buf_next_to_write = log_sys->buf_free;
	log_sys->written_to_some_lsn = log_sys->lsn;
	log_sys->written_to_all_lsn = log_sys->lsn;

	log_sys->last_checkpoint_lsn = checkpoint_lsn;
	
	log_sys->next_checkpoint_no = ut_dulint_add(checkpoint_no, 1);
								
unknown's avatar
unknown committed
2901
#ifdef UNIV_LOG_ARCHIVE
2902 2903 2904 2905
	if (ut_dulint_cmp(archived_lsn, ut_dulint_max) == 0) {

		log_sys->archiving_state = LOG_ARCH_OFF;
	}
unknown's avatar
unknown committed
2906
#endif /* UNIV_LOG_ARCHIVE */
2907 2908 2909 2910 2911 2912 2913 2914 2915

	mutex_enter(&(recv_sys->mutex));
	
	recv_sys->apply_log_recs = TRUE;

 	mutex_exit(&(recv_sys->mutex));
	
	mutex_exit(&(log_sys->mutex));

2916 2917
	recv_lsn_checks_on = TRUE;

2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931
	/* The database is now ready to start almost normal processing of user
	transactions: transaction rollbacks and the application of the log
	records in the hash table can be run in background. */

	return(DB_SUCCESS);
}

/************************************************************
Completes recovery from a checkpoint. */

void
recv_recovery_from_checkpoint_finish(void)
/*======================================*/
{
2932 2933 2934
	int 		i;
	os_thread_id_t	recovery_thread_id;

2935
	/* Apply the hashed log records to the respective file pages */
2936
	
unknown's avatar
Merge  
unknown committed
2937 2938 2939 2940
	if (srv_force_recovery < SRV_FORCE_NO_LOG_REDO) {

		recv_apply_hashed_log_recs(TRUE);
	}
2941

2942
#ifdef UNIV_DEBUG
2943 2944
	if (log_debug_writes) {
		fprintf(stderr,
2945
		"InnoDB: Log records applied to the database\n");
2946
	}
2947
#endif /* UNIV_DEBUG */
2948

unknown's avatar
unknown committed
2949
	if (recv_needed_recovery) {
unknown's avatar
unknown committed
2950
		trx_sys_print_mysql_master_log_pos();
unknown's avatar
unknown committed
2951 2952 2953
		trx_sys_print_mysql_binlog_offset();
	}

unknown's avatar
unknown committed
2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964
	if (recv_sys->found_corrupt_log) {

		fprintf(stderr,
	"InnoDB: WARNING: the log file may have been corrupt and it\n"
	"InnoDB: is possible that the log scan or parsing did not proceed\n"
	"InnoDB: far enough in recovery. Please run CHECK TABLE\n"
	"InnoDB: on your InnoDB tables to check that they are ok!\n"
	"InnoDB: It may be safest to recover your InnoDB database from\n"
	"InnoDB: a backup!\n");
	}

2965 2966 2967
	/* Free the resources of the recovery system */

	recv_recovery_on = FALSE;
2968

2969 2970 2971
#ifndef UNIV_LOG_DEBUG
	recv_sys_free();
#endif
2972
	if (srv_force_recovery < SRV_FORCE_NO_TRX_UNDO) {
unknown's avatar
unknown committed
2973 2974 2975
		/* Rollback the uncommitted transactions which have no user
		session */

2976 2977 2978
		os_thread_create(trx_rollback_or_clean_all_without_sess,
				(void *)&i, &recovery_thread_id);
	}
2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989
}

/**********************************************************
Resets the logs. The contents of log files will be lost! */

void
recv_reset_logs(
/*============*/
	dulint	lsn,		/* in: reset to this lsn rounded up to
				be divisible by OS_FILE_LOG_BLOCK_SIZE,
				after which we add LOG_BLOCK_HDR_SIZE */
unknown's avatar
unknown committed
2990
#ifdef UNIV_LOG_ARCHIVE
2991
	ulint	arch_log_no,	/* in: next archived log file number */
unknown's avatar
unknown committed
2992
#endif /* UNIV_LOG_ARCHIVE */
2993 2994 2995 2996 2997 2998
	ibool	new_logs_created)/* in: TRUE if resetting logs is done
				at the log creation; FALSE if it is done
				after archive recovery */
{
	log_group_t*	group;

2999
#ifdef UNIV_SYNC_DEBUG
3000
	ut_ad(mutex_own(&(log_sys->mutex)));
3001
#endif /* UNIV_SYNC_DEBUG */
3002 3003 3004 3005 3006 3007 3008
	log_sys->lsn = ut_dulint_align_up(lsn, OS_FILE_LOG_BLOCK_SIZE);

	group = UT_LIST_GET_FIRST(log_sys->log_groups);

	while (group) {
		group->lsn = log_sys->lsn;
		group->lsn_offset = LOG_FILE_HDR_SIZE;
unknown's avatar
unknown committed
3009
#ifdef UNIV_LOG_ARCHIVE
3010 3011
		group->archived_file_no = arch_log_no;		
		group->archived_offset = 0;
unknown's avatar
unknown committed
3012
#endif /* UNIV_LOG_ARCHIVE */
3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028

		if (!new_logs_created) {
			recv_truncate_group(group, group->lsn, group->lsn,
						group->lsn, group->lsn);
		}
	
		group = UT_LIST_GET_NEXT(log_groups, group);
	}

	log_sys->buf_next_to_write = 0;
	log_sys->written_to_some_lsn = log_sys->lsn;
	log_sys->written_to_all_lsn = log_sys->lsn;

	log_sys->next_checkpoint_no = ut_dulint_zero;
	log_sys->last_checkpoint_lsn = ut_dulint_zero;

unknown's avatar
unknown committed
3029
#ifdef UNIV_LOG_ARCHIVE
3030
	log_sys->archived_lsn = log_sys->lsn;
unknown's avatar
unknown committed
3031
#endif /* UNIV_LOG_ARCHIVE */
3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048
	
	log_block_init(log_sys->buf, log_sys->lsn);
	log_block_set_first_rec_group(log_sys->buf, LOG_BLOCK_HDR_SIZE);

	log_sys->buf_free = LOG_BLOCK_HDR_SIZE;
	log_sys->lsn = ut_dulint_add(log_sys->lsn, LOG_BLOCK_HDR_SIZE);
	
	mutex_exit(&(log_sys->mutex));

	/* Reset the checkpoint fields in logs */
	
	log_make_checkpoint_at(ut_dulint_max, TRUE);
	log_make_checkpoint_at(ut_dulint_max, TRUE);
	
	mutex_enter(&(log_sys->mutex));
}

unknown's avatar
unknown committed
3049
#ifdef UNIV_HOTBACKUP
unknown's avatar
unknown committed
3050 3051 3052 3053 3054 3055
/**********************************************************
Creates new log files after a backup has been restored. */

void
recv_reset_log_files_for_backup(
/*============================*/
unknown's avatar
unknown committed
3056 3057 3058 3059 3060
	const char*	log_dir,	/* in: log file directory path */
	ulint		n_log_files,	/* in: number of log files */
	ulint		log_file_size,	/* in: log file size */
	dulint		lsn)		/* in: new start lsn, must be
					divisible by OS_FILE_LOG_BLOCK_SIZE */
unknown's avatar
unknown committed
3061 3062 3063 3064 3065
{
	os_file_t	log_file;
	ibool		success;
	byte*		buf;
	ulint		i;
3066 3067
	ulint		log_dir_len;
	char*		name;
unknown's avatar
unknown committed
3068 3069
	static const
	char		logfilename[] = "ib_logfile";
3070 3071 3072 3073 3074 3075 3076

	log_dir_len = strlen(log_dir);
	/* reserve space for log_dir, "ib_logfile" and a number */
	name = memcpy(mem_alloc(log_dir_len + ((sizeof logfilename) + 11)),
		log_dir, log_dir_len);
	memcpy(name + log_dir_len, logfilename, sizeof logfilename);

unknown's avatar
unknown committed
3077
	buf = ut_malloc(LOG_FILE_HDR_SIZE + OS_FILE_LOG_BLOCK_SIZE);
unknown's avatar
unknown committed
3078
        memset(buf, '\0', LOG_FILE_HDR_SIZE + OS_FILE_LOG_BLOCK_SIZE);
unknown's avatar
unknown committed
3079

unknown's avatar
unknown committed
3080 3081
	for (i = 0; i < n_log_files; i++) {

unknown's avatar
unknown committed
3082
		sprintf(name + log_dir_len + sizeof logfilename, "%lu", (ulong) i);
unknown's avatar
unknown committed
3083 3084 3085 3086

		log_file = os_file_create_simple(name, OS_FILE_CREATE,
						OS_FILE_READ_WRITE, &success);
		if (!success) {
3087
			fprintf(stderr,
unknown's avatar
unknown committed
3088 3089 3090 3091 3092
"InnoDB: Cannot create %s. Check that the file does not exist yet.\n", name);

			exit(1);
		}

3093 3094
		fprintf(stderr,
			"Setting log file size to %lu %lu\n",
unknown's avatar
unknown committed
3095 3096
			(ulong) ut_get_high32(log_file_size),
			(ulong) log_file_size & 0xFFFFFFFFUL);
unknown's avatar
unknown committed
3097 3098

		success = os_file_set_size(name, log_file,
unknown's avatar
unknown committed
3099
					log_file_size & 0xFFFFFFFFUL,
unknown's avatar
unknown committed
3100 3101 3102
					ut_get_high32(log_file_size));

		if (!success) {
3103
			fprintf(stderr,
unknown's avatar
unknown committed
3104 3105
"InnoDB: Cannot set %s size to %lu %lu\n", name, (ulong) ut_get_high32(log_file_size),
						(ulong) (log_file_size & 0xFFFFFFFFUL));
unknown's avatar
unknown committed
3106 3107 3108 3109 3110 3111 3112 3113 3114
			exit(1);
		}

		os_file_flush(log_file);
		os_file_close(log_file);
	}

	/* We pretend there is a checkpoint at lsn + LOG_BLOCK_HDR_SIZE */
	
unknown's avatar
unknown committed
3115
	log_reset_first_header_and_checkpoint(buf, lsn);
unknown's avatar
unknown committed
3116
	
unknown's avatar
unknown committed
3117
	log_block_init_in_old_format(buf + LOG_FILE_HDR_SIZE, lsn);
unknown's avatar
unknown committed
3118 3119
	log_block_set_first_rec_group(buf + LOG_FILE_HDR_SIZE,
							LOG_BLOCK_HDR_SIZE);
3120
	strcpy(name + log_dir_len + sizeof logfilename, "0");
unknown's avatar
unknown committed
3121 3122 3123 3124

	log_file = os_file_create_simple(name, OS_FILE_OPEN,
						OS_FILE_READ_WRITE, &success);
	if (!success) {
3125
		fprintf(stderr, "InnoDB: Cannot open %s.\n", name);
unknown's avatar
unknown committed
3126 3127 3128 3129 3130 3131 3132 3133 3134

		exit(1);
	}

	os_file_write(name, log_file, buf, 0, 0,
				LOG_FILE_HDR_SIZE + OS_FILE_LOG_BLOCK_SIZE);
	os_file_flush(log_file);
	os_file_close(log_file);

3135
	mem_free(name);
unknown's avatar
unknown committed
3136 3137
	ut_free(buf);
}
unknown's avatar
unknown committed
3138
#endif /* UNIV_HOTBACKUP */
unknown's avatar
unknown committed
3139

unknown's avatar
unknown committed
3140
#ifdef UNIV_LOG_ARCHIVE
3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162
/**********************************************************
Reads from the archive of a log group and performs recovery. */
static
ibool
log_group_recover_from_archive_file(
/*================================*/
					/* out: TRUE if no more complete
					consistent archive files */
	log_group_t*	group)		/* in: log group */
{
	os_file_t file_handle;
	dulint	start_lsn;
	dulint	file_end_lsn;
	dulint	dummy_lsn;
	dulint	scanned_lsn;
	ulint	len;
	ibool	ret;
	byte*	buf;
	ulint	read_offset;
	ulint	file_size;
	ulint	file_size_high;
	int	input_char;
unknown's avatar
unknown committed
3163
	char	name[10000];
3164

unknown's avatar
unknown committed
3165 3166
	ut_a(0);

3167 3168 3169 3170 3171 3172 3173
try_open_again:	
	buf = log_sys->buf;

	/* Add the file to the archive file space; open the file */
	
	log_archived_file_name_gen(name, group->id, group->archived_file_no);

3174 3175
	file_handle = os_file_create(name, OS_FILE_OPEN,
					OS_FILE_LOG, OS_FILE_AIO, &ret);
3176 3177 3178 3179

	if (ret == FALSE) {
ask_again:
		fprintf(stderr, 
3180 3181
	"InnoDB: Do you want to copy additional archived log files\n"
	"InnoDB: to the directory\n");
3182
		fprintf(stderr, 
3183
	"InnoDB: or were these all the files needed in recovery?\n");
3184
		fprintf(stderr, 
3185
	"InnoDB: (Y == copy more files; N == this is all)?");
3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204

		input_char = getchar();

		if (input_char == (int) 'N') {

			return(TRUE);
		} else if (input_char == (int) 'Y') {

			goto try_open_again;
		} else {
			goto ask_again;
		}
	}

	ret = os_file_get_size(file_handle, &file_size, &file_size_high);
	ut_a(ret);

	ut_a(file_size_high == 0);
	
3205
	fprintf(stderr, "InnoDB: Opened archived log file %s\n", name);
3206 3207 3208 3209 3210
			
	ret = os_file_close(file_handle);
	
	if (file_size < LOG_FILE_HDR_SIZE) {
		fprintf(stderr,
3211
			"InnoDB: Archive file header incomplete %s\n", name);
3212 3213 3214 3215 3216 3217 3218 3219 3220
	    
		return(TRUE);
	}

	ut_a(ret);
	
	/* Add the archive file as a node to the space */
		
	fil_node_create(name, 1 + file_size / UNIV_PAGE_SIZE,
unknown's avatar
unknown committed
3221
					    group->archive_space_id, FALSE);
3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233
	ut_a(RECV_SCAN_SIZE >= LOG_FILE_HDR_SIZE);

	/* Read the archive file header */
	fil_io(OS_FILE_READ | OS_FILE_LOG, TRUE, group->archive_space_id, 0, 0,
						LOG_FILE_HDR_SIZE, buf, NULL);

	/* Check if the archive file header is consistent */

	if (mach_read_from_4(buf + LOG_GROUP_ID) != group->id
	    || mach_read_from_4(buf + LOG_FILE_NO)
						!= group->archived_file_no) {
		fprintf(stderr,
3234
	"InnoDB: Archive file header inconsistent %s\n", name);
3235 3236 3237 3238 3239 3240
	    
		return(TRUE);
	}

	if (!mach_read_from_4(buf + LOG_FILE_ARCH_COMPLETED)) {
		fprintf(stderr,
3241
	"InnoDB: Archive file not completely written %s\n", name);
3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252

		return(TRUE);
	}
	
	start_lsn = mach_read_from_8(buf + LOG_FILE_START_LSN);
	file_end_lsn = mach_read_from_8(buf + LOG_FILE_END_LSN);

	if (ut_dulint_is_zero(recv_sys->scanned_lsn)) {

		if (ut_dulint_cmp(recv_sys->parse_start_lsn, start_lsn) < 0) {
			fprintf(stderr, 
3253
	"InnoDB: Archive log file %s starts from too big a lsn\n",
unknown's avatar
unknown committed
3254
								name);	    
3255 3256 3257 3258 3259 3260 3261 3262 3263
			return(TRUE);
		}
	
		recv_sys->scanned_lsn = start_lsn;
	}
	
	if (ut_dulint_cmp(recv_sys->scanned_lsn, start_lsn) != 0) {

		fprintf(stderr,
3264
	"InnoDB: Archive log file %s starts from a wrong lsn\n",
unknown's avatar
unknown committed
3265
								name);
3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283
		return(TRUE);
	}

	read_offset = LOG_FILE_HDR_SIZE;
	
	for (;;) {
		len = RECV_SCAN_SIZE;

		if (read_offset + len > file_size) {
			len = ut_calc_align_down(file_size - read_offset,
						OS_FILE_LOG_BLOCK_SIZE);
		}

		if (len == 0) {

			break;
		}
	
3284
#ifdef UNIV_DEBUG
3285 3286
		if (log_debug_writes) {
			fprintf(stderr, 
3287
"InnoDB: Archive read starting at lsn %lu %lu, len %lu from file %s\n",
3288 3289 3290
					(ulong) ut_dulint_get_high(start_lsn),
					(ulong) ut_dulint_get_low(start_lsn),
					(ulong) len, name);
3291
		}
3292
#endif /* UNIV_DEBUG */
3293 3294 3295 3296 3297

		fil_io(OS_FILE_READ | OS_FILE_LOG, TRUE,
			group->archive_space_id, read_offset / UNIV_PAGE_SIZE,
			read_offset % UNIV_PAGE_SIZE, len, buf, NULL);

unknown's avatar
unknown committed
3298
		ret = recv_scan_log_recs(TRUE,
unknown's avatar
unknown committed
3299 3300
                                (buf_pool->n_frames -
                                recv_n_pool_free_frames) * UNIV_PAGE_SIZE,
unknown's avatar
unknown committed
3301 3302
				TRUE, buf, len, start_lsn,
				&dummy_lsn, &scanned_lsn);
3303 3304 3305 3306 3307 3308 3309 3310

		if (ut_dulint_cmp(scanned_lsn, file_end_lsn) == 0) {

			return(FALSE);
		}

		if (ret) {
			fprintf(stderr,
3311
		"InnoDB: Archive log file %s does not scan right\n",
3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345
								name);	    
			return(TRUE);
		}
		
		read_offset += len;
		start_lsn = ut_dulint_add(start_lsn, len);

		ut_ad(ut_dulint_cmp(start_lsn, scanned_lsn) == 0);
	}

	return(FALSE);
}

/************************************************************
Recovers from archived log files, and also from log files, if they exist. */

ulint
recv_recovery_from_archive_start(
/*=============================*/
				/* out: error code or DB_SUCCESS */
	dulint	min_flushed_lsn,/* in: min flushed lsn field from the
				data files */
	dulint	limit_lsn,	/* in: recover up to this lsn if possible */
	ulint	first_log_no)	/* in: number of the first archived log file
				to use in the recovery; the file will be
				searched from INNOBASE_LOG_ARCH_DIR specified
				in server config file */
{
	log_group_t*	group;
	ulint		group_id;
	ulint		trunc_len;
	ibool		ret;
	ulint		err;
	
unknown's avatar
unknown committed
3346 3347
	ut_a(0);

3348
	recv_sys_create();
unknown's avatar
unknown committed
3349
	recv_sys_init(FALSE, buf_pool_get_curr_size());
3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370
	
	recv_recovery_on = TRUE;
	recv_recovery_from_backup_on = TRUE;

	recv_sys->limit_lsn = limit_lsn;

	group_id = 0;

	group = UT_LIST_GET_FIRST(log_sys->log_groups);

	while (group) {
		if (group->id == group_id) {

 			break;
		}
		
		group = UT_LIST_GET_NEXT(log_groups, group);
	}

	if (!group) {
		fprintf(stderr,
3371
		"InnoDB: There is no log group defined with id %lu!\n",
3372
							   (ulong) group_id);
3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449
		return(DB_ERROR);
	}

	group->archived_file_no = first_log_no;

	recv_sys->parse_start_lsn = min_flushed_lsn;

	recv_sys->scanned_lsn = ut_dulint_zero;
	recv_sys->scanned_checkpoint_no = 0;
	recv_sys->recovered_lsn = recv_sys->parse_start_lsn;

	recv_sys->archive_group = group;

	ret = FALSE;
	
	mutex_enter(&(log_sys->mutex));

	while (!ret) {
		ret = log_group_recover_from_archive_file(group);

		/* Close and truncate a possible processed archive file
		from the file space */
		
		trunc_len = UNIV_PAGE_SIZE
			    * fil_space_get_size(group->archive_space_id);
		if (trunc_len > 0) {
			fil_space_truncate_start(group->archive_space_id,
								trunc_len);
		}

		group->archived_file_no++;
	}

	if (ut_dulint_cmp(recv_sys->recovered_lsn, limit_lsn) < 0) {

		if (ut_dulint_is_zero(recv_sys->scanned_lsn)) {

			recv_sys->scanned_lsn = recv_sys->parse_start_lsn;
		}

		mutex_exit(&(log_sys->mutex));

		err = recv_recovery_from_checkpoint_start(LOG_ARCHIVE,
							limit_lsn,
							ut_dulint_max,
							ut_dulint_max);
		if (err != DB_SUCCESS) {

			return(err);
		}

		mutex_enter(&(log_sys->mutex));
	}

	if (ut_dulint_cmp(limit_lsn, ut_dulint_max) != 0) {

		recv_apply_hashed_log_recs(FALSE);

		recv_reset_logs(recv_sys->recovered_lsn, 0, FALSE);
	}

	mutex_exit(&(log_sys->mutex));

	return(DB_SUCCESS);
}

/************************************************************
Completes recovery from archive. */

void
recv_recovery_from_archive_finish(void)
/*===================================*/
{
	recv_recovery_from_checkpoint_finish();

	recv_recovery_from_backup_on = FALSE;
}
unknown's avatar
unknown committed
3450
#endif /* UNIV_LOG_ARCHIVE */