log0recv.c 86.7 KB
Newer Older
osku's avatar
osku committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/******************************************************
Recovery

(c) 1997 Innobase Oy

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"
#include "srv0start.h"
#include "mtr0log.h"
#include "page0cur.h"
marko's avatar
marko committed
23
#include "page0zip.h"
osku's avatar
osku committed
24 25 26 27 28
#include "btr0cur.h"
#include "ibuf0ibuf.h"
#include "trx0undo.h"
#include "trx0rec.h"
#include "trx0roll.h"
29
#include "row0merge.h"
osku's avatar
osku committed
30 31 32 33 34

#ifdef UNIV_HOTBACKUP
/* 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 */
35
UNIV_INTERN ibool	recv_replay_file_ops	= TRUE;
osku's avatar
osku committed
36 37 38 39 40 41 42 43 44
#endif /* UNIV_HOTBACKUP */

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

45 46 47
UNIV_INTERN recv_sys_t*	recv_sys = NULL;
UNIV_INTERN ibool	recv_recovery_on = FALSE;
UNIV_INTERN ibool	recv_recovery_from_backup_on = FALSE;
osku's avatar
osku committed
48

49
UNIV_INTERN ibool	recv_needed_recovery = FALSE;
osku's avatar
osku committed
50

51
UNIV_INTERN ibool	recv_lsn_checks_on = FALSE;
osku's avatar
osku committed
52

53 54 55 56 57 58 59 60
/* There are two conditions under which we scan the logs, the first
is normal startup and the second is when we do a recovery from an
archive.
This flag is set if we are doing a scan from the last checkpoint during
startup. If we find log entries that were written after the last checkpoint
we know that the server was not cleanly shutdown. We must then initialize
the crash recovery environment before attempting to store these entries in
the log hash table. */
61
UNIV_INTERN ibool	recv_log_scan_is_startup_type = FALSE;
62

osku's avatar
osku committed
63 64 65 66 67 68 69 70 71 72
/* 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 */

73
UNIV_INTERN ibool	recv_no_ibuf_operations = FALSE;
osku's avatar
osku committed
74 75 76

/* The following counter is used to decide when to print info on
log scan */
77
UNIV_INTERN ulint	recv_scan_print_counter	= 0;
osku's avatar
osku committed
78

79
UNIV_INTERN ibool	recv_is_from_backup	= FALSE;
osku's avatar
osku committed
80
#ifdef UNIV_HOTBACKUP
81
UNIV_INTERN ibool	recv_is_making_a_backup = FALSE;
osku's avatar
osku committed
82 83 84 85
#else
# define recv_is_making_a_backup FALSE
#endif /* UNIV_HOTBACKUP */

86 87 88
UNIV_INTERN ulint	recv_previous_parsed_rec_type	= 999999;
UNIV_INTERN ulint	recv_previous_parsed_rec_offset	= 0;
UNIV_INTERN ulint	recv_previous_parsed_rec_is_multi = 0;
osku's avatar
osku committed
89

90
UNIV_INTERN ulint	recv_max_parsed_page_no		= 0;
osku's avatar
osku committed
91 92 93 94

/* 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
inaam's avatar
inaam committed
95 96 97
log records to the database.
This is the default value. If the actual size of the buffer pool is
larger than 10 MB we'll set this value to 512. */
osku's avatar
osku committed
98

99
UNIV_INTERN ulint	recv_n_pool_free_frames		= 256;
osku's avatar
osku committed
100 101 102 103 104

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

105
UNIV_INTERN ib_uint64_t	recv_max_page_lsn;
osku's avatar
osku committed
106

107 108 109 110 111 112 113 114 115 116
/* prototypes */

/***********************************************************
Initialize crash recovery environment. Can be called iff
recv_needed_recovery == FALSE. */
static
void
recv_init_crash_recovery(void);
/*===========================*/

osku's avatar
osku committed
117 118
/************************************************************
Creates the recovery system. */
119
UNIV_INTERN
osku's avatar
osku committed
120 121 122 123 124 125 126 127 128 129 130
void
recv_sys_create(void)
/*=================*/
{
	if (recv_sys != NULL) {

		return;
	}

	recv_sys = mem_alloc(sizeof(recv_sys_t));

131
	mutex_create(&recv_sys->mutex, SYNC_RECV);
osku's avatar
osku committed
132 133 134 135 136 137 138

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

/************************************************************
Inits the recovery system for a recovery operation. */
139
UNIV_INTERN
osku's avatar
osku committed
140 141 142 143 144 145 146 147 148 149 150 151
void
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 */
{
	if (recv_sys->heap != NULL) {

		return;
	}

inaam's avatar
inaam committed
152 153 154 155 156 157
	/* Initialize red-black tree for fast insertions into the
	flush_list during recovery process.
	As this initialization is done while holding the buffer pool
	mutex we perform it before acquiring recv_sys->mutex. */
	buf_flush_init_flush_rbt();

osku's avatar
osku committed
158 159 160 161 162 163 164 165 166
	mutex_enter(&(recv_sys->mutex));

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

inaam's avatar
inaam committed
167 168 169 170 171 172
	/* Set appropriate value of recv_n_pool_free_frames. */
	if (buf_pool_get_curr_size() >= (10 * 1024 * 1024)) {
		/* Buffer pool of size greater than 10 MB. */
		recv_n_pool_free_frames = 512;
	}

osku's avatar
osku committed
173 174 175 176 177 178
	recv_sys->buf = ut_malloc(RECV_PARSING_BUF_SIZE);
	recv_sys->len = 0;
	recv_sys->recovered_offset = 0;

	recv_sys->addr_hash = hash_create(available_memory / 64);
	recv_sys->n_addrs = 0;
179

osku's avatar
osku committed
180 181 182 183 184 185
	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,
186
					OS_FILE_LOG_BLOCK_SIZE);
osku's avatar
osku committed
187 188
	recv_sys->found_corrupt_log = FALSE;

189
	recv_max_page_lsn = 0;
osku's avatar
osku committed
190 191 192 193 194 195 196 197 198 199 200 201

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

/************************************************************
Empties the hash table when it has been fully processed. */
static
void
recv_sys_empty_hash(void)
/*=====================*/
{
	ut_ad(mutex_own(&(recv_sys->mutex)));
202

osku's avatar
osku committed
203 204
	if (recv_sys->n_addrs != 0) {
		fprintf(stderr,
205 206 207 208
			"InnoDB: Error: %lu pages with log records"
			" were left unprocessed!\n"
			"InnoDB: Maximum page number with"
			" log records on it %lu\n",
209
			(ulong) recv_sys->n_addrs,
osku's avatar
osku committed
210 211 212
			(ulong) recv_max_parsed_page_no);
		ut_error;
	}
213

osku's avatar
osku committed
214 215 216 217 218 219
	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);
}

220
#ifndef UNIV_LOG_DEBUG
osku's avatar
osku committed
221 222 223 224 225 226 227 228
/************************************************************
Frees the recovery system. */
static
void
recv_sys_free(void)
/*===============*/
{
	mutex_enter(&(recv_sys->mutex));
229

osku's avatar
osku committed
230 231 232 233 234 235 236 237 238
	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));
inaam's avatar
inaam committed
239 240 241

	/* Free up the flush_rbt. */
	buf_flush_free_flush_rbt();
osku's avatar
osku committed
242
}
243
#endif /* UNIV_LOG_DEBUG */
osku's avatar
osku committed
244 245 246 247 248 249 250 251

/************************************************************
Truncates possible corrupted or extra records from a log group. */
static
void
recv_truncate_group(
/*================*/
	log_group_t*	group,		/* in: log group */
252
	ib_uint64_t	recovered_lsn,	/* in: recovery succeeded up to this
osku's avatar
osku committed
253
					lsn */
254
	ib_uint64_t	limit_lsn,	/* in: this was the limit for
osku's avatar
osku committed
255
					recovery */
256
	ib_uint64_t	checkpoint_lsn,	/* in: recovery was started from this
osku's avatar
osku committed
257
					checkpoint */
258
	ib_uint64_t	archived_lsn)	/* in: the log has been archived up to
osku's avatar
osku committed
259 260
					this lsn */
{
261 262 263 264 265
	ib_uint64_t	start_lsn;
	ib_uint64_t	end_lsn;
	ib_uint64_t	finish_lsn1;
	ib_uint64_t	finish_lsn2;
	ib_uint64_t	finish_lsn;
266 267
	ulint		len;
	ulint		i;
osku's avatar
osku committed
268

269
	if (archived_lsn == IB_ULONGLONG_MAX) {
osku's avatar
osku committed
270 271 272 273
		/* Checkpoint was taken in the NOARCHIVELOG mode */
		archived_lsn = checkpoint_lsn;
	}

274 275
	finish_lsn1 = ut_uint64_align_down(archived_lsn,
					   OS_FILE_LOG_BLOCK_SIZE)
276
		+ log_group_get_capacity(group);
277

278 279
	finish_lsn2 = ut_uint64_align_up(recovered_lsn,
					 OS_FILE_LOG_BLOCK_SIZE)
280
		+ recv_sys->last_log_buf_size;
osku's avatar
osku committed
281

282
	if (limit_lsn != IB_ULONGLONG_MAX) {
osku's avatar
osku committed
283 284 285 286 287 288
		/* 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 */
289 290
		finish_lsn = finish_lsn1 < finish_lsn2
			? finish_lsn1 : finish_lsn2;
osku's avatar
osku committed
291
	}
292 293

	ut_a(RECV_SCAN_SIZE <= log_sys->buf_size);
osku's avatar
osku committed
294 295 296 297 298 299 300

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

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

301 302
	start_lsn = ut_uint64_align_down(recovered_lsn,
					 OS_FILE_LOG_BLOCK_SIZE);
303

304
	if (start_lsn != recovered_lsn) {
osku's avatar
osku committed
305 306 307 308
		/* Copy the last incomplete log block to the log buffer and
		edit its data length: */

		ut_memcpy(log_sys->buf, recv_sys->last_block,
309
			  OS_FILE_LOG_BLOCK_SIZE);
310
		log_block_set_data_len(log_sys->buf,
311
				       (ulint) (recovered_lsn - start_lsn));
osku's avatar
osku committed
312
	}
313

314
	if (start_lsn >= finish_lsn) {
osku's avatar
osku committed
315 316 317 318

		return;
	}

319
	for (;;) {
320
		end_lsn = start_lsn + RECV_SCAN_SIZE;
321

322
		if (end_lsn > finish_lsn) {
osku's avatar
osku committed
323 324 325 326

			end_lsn = finish_lsn;
		}

327
		len = (ulint) (end_lsn - start_lsn);
328

osku's avatar
osku committed
329
		log_group_write_buf(group, log_sys->buf, len, start_lsn, 0);
330
		if (end_lsn >= finish_lsn) {
osku's avatar
osku committed
331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

			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 */
	log_group_t*	group,			/* in: copy to this log
						group */
356
	ib_uint64_t	recovered_lsn)		/* in: recovery succeeded up
osku's avatar
osku committed
357 358
						to this lsn */
{
359 360
	ib_uint64_t	start_lsn;
	ib_uint64_t	end_lsn;
361
	ulint		len;
osku's avatar
osku committed
362

363
	if (group->scanned_lsn >= recovered_lsn) {
osku's avatar
osku committed
364 365 366

		return;
	}
367

osku's avatar
osku committed
368 369
	ut_a(RECV_SCAN_SIZE <= log_sys->buf_size);

370 371
	start_lsn = ut_uint64_align_down(group->scanned_lsn,
					 OS_FILE_LOG_BLOCK_SIZE);
372
	for (;;) {
373
		end_lsn = start_lsn + RECV_SCAN_SIZE;
374

375
		if (end_lsn > recovered_lsn) {
376 377
			end_lsn = ut_uint64_align_up(recovered_lsn,
						     OS_FILE_LOG_BLOCK_SIZE);
osku's avatar
osku committed
378 379 380
		}

		log_group_read_log_seg(LOG_RECOVER, log_sys->buf,
381
				       up_to_date_group, start_lsn, end_lsn);
osku's avatar
osku committed
382

383
		len = (ulint) (end_lsn - start_lsn);
384

osku's avatar
osku committed
385
		log_group_write_buf(group, log_sys->buf, len, start_lsn, 0);
386

387
		if (end_lsn >= recovered_lsn) {
osku's avatar
osku committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

			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. */
static
void
recv_synchronize_groups(
/*====================*/
	log_group_t*	up_to_date_group)	/* in: the most up-to-date
						log group */
{
	log_group_t*	group;
409 410 411 412
	ib_uint64_t	start_lsn;
	ib_uint64_t	end_lsn;
	ib_uint64_t	recovered_lsn;
	ib_uint64_t	limit_lsn;
osku's avatar
osku committed
413 414 415 416 417 418 419

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

420 421 422
	start_lsn = ut_uint64_align_down(recovered_lsn,
					 OS_FILE_LOG_BLOCK_SIZE);
	end_lsn = ut_uint64_align_up(recovered_lsn, OS_FILE_LOG_BLOCK_SIZE);
osku's avatar
osku committed
423

424
	ut_a(start_lsn != end_lsn);
osku's avatar
osku committed
425 426

	log_group_read_log_seg(LOG_RECOVER, recv_sys->last_block,
427
			       up_to_date_group, start_lsn, end_lsn);
osku's avatar
osku committed
428 429 430 431 432 433 434 435 436

	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,
437
					recovered_lsn);
osku's avatar
osku committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
		}

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

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

477 478
	if ((fold & 0xFFFFFFFFUL) != mach_read_from_4(
		    buf + LOG_CHECKPOINT_CHECKSUM_1)) {
osku's avatar
osku committed
479 480 481 482
		return(FALSE);
	}

	fold = ut_fold_binary(buf + LOG_CHECKPOINT_LSN,
483
			      LOG_CHECKPOINT_CHECKSUM_2 - LOG_CHECKPOINT_LSN);
osku's avatar
osku committed
484

485 486
	if ((fold & 0xFFFFFFFFUL) != mach_read_from_4(
		    buf + LOG_CHECKPOINT_CHECKSUM_2)) {
osku's avatar
osku committed
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
		return(FALSE);
	}

	return(TRUE);
}

/************************************************************
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;
505 506
	ib_uint64_t	max_no;
	ib_uint64_t	checkpoint_no;
osku's avatar
osku committed
507 508
	ulint		field;
	byte*		buf;
509

osku's avatar
osku committed
510 511
	group = UT_LIST_GET_FIRST(log_sys->log_groups);

512
	max_no = 0;
osku's avatar
osku committed
513 514
	*max_group = NULL;
	*max_field = 0;
515

osku's avatar
osku committed
516
	buf = log_sys->checkpoint_buf;
517

osku's avatar
osku committed
518 519
	while (group) {
		group->state = LOG_GROUP_CORRUPTED;
520

osku's avatar
osku committed
521
		for (field = LOG_CHECKPOINT_1; field <= LOG_CHECKPOINT_2;
522
		     field += LOG_CHECKPOINT_2 - LOG_CHECKPOINT_1) {
523

osku's avatar
osku committed
524 525 526 527 528
			log_group_read_checkpoint_info(group, field);

			if (!recv_check_cp_is_consistent(buf)) {
#ifdef UNIV_DEBUG
				if (log_debug_writes) {
529
					fprintf(stderr,
530 531 532 533
						"InnoDB: Checkpoint in group"
						" %lu at %lu invalid, %lu\n",
						(ulong) group->id,
						(ulong) field,
534 535 536
						(ulong) mach_read_from_4(
							buf
							+ LOG_CHECKPOINT_CHECKSUM_1));
osku's avatar
osku committed
537 538 539 540 541 542 543 544

				}
#endif /* UNIV_DEBUG */
				goto not_consistent;
			}

			group->state = LOG_GROUP_OK;

545
			group->lsn = mach_read_ull(
546 547 548
				buf + LOG_CHECKPOINT_LSN);
			group->lsn_offset = mach_read_from_4(
				buf + LOG_CHECKPOINT_OFFSET);
549
			checkpoint_no = mach_read_ull(
550
				buf + LOG_CHECKPOINT_NO);
osku's avatar
osku committed
551 552 553

#ifdef UNIV_DEBUG
			if (log_debug_writes) {
554
				fprintf(stderr,
555 556
					"InnoDB: Checkpoint number %lu"
					" found in group %lu\n",
557
					(ulong) checkpoint_no,
558
					(ulong) group->id);
osku's avatar
osku committed
559 560 561
			}
#endif /* UNIV_DEBUG */

562
			if (checkpoint_no >= max_no) {
osku's avatar
osku committed
563 564 565 566 567
				*max_group = group;
				*max_field = field;
				max_no = checkpoint_no;
			}

568
not_consistent:
osku's avatar
osku committed
569 570 571 572 573 574 575 576 577
			;
		}

		group = UT_LIST_GET_NEXT(log_groups, group);
	}

	if (*max_group == NULL) {

		fprintf(stderr,
578 579 580 581 582 583 584 585 586 587
			"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"
			"InnoDB: If that is the case, please refer to\n"
			"InnoDB: http://dev.mysql.com/doc/refman/5.1/en/"
			"error-creating-innodb.html\n");
osku's avatar
osku committed
588 589 590 591 592 593
		return(DB_ERROR);
	}

	return(DB_SUCCESS);
}

594
#ifdef UNIV_HOTBACKUP
osku's avatar
osku committed
595 596
/***********************************************************************
Reads the checkpoint info needed in hot backup. */
597
UNIV_INTERN
osku's avatar
osku committed
598 599 600
ibool
recv_read_cp_info_for_backup(
/*=========================*/
601 602 603
				/* out: TRUE if success */
	byte*		hdr,	/* in: buffer containing the log group
				header */
604
	ib_uint64_t*	lsn,	/* out: checkpoint lsn */
605 606 607 608
	ulint*		offset,	/* out: checkpoint offset in the log group */
	ulint*		fsp_limit,/* out: fsp limit of space 0,
				1000000000 if the database is running
				with < version 3.23.50 of InnoDB */
609 610
	ib_uint64_t*	cp_no,	/* out: checkpoint number */
	ib_uint64_t*	first_header_lsn)
611 612
				/* out: lsn of of the start of the
				first log file */
osku's avatar
osku committed
613
{
614
	ulint		max_cp		= 0;
615
	ib_uint64_t	max_cp_no	= 0;
616
	byte*		cp_buf;
osku's avatar
osku committed
617 618 619 620

	cp_buf = hdr + LOG_CHECKPOINT_1;

	if (recv_check_cp_is_consistent(cp_buf)) {
621
		max_cp_no = mach_read_ull(cp_buf + LOG_CHECKPOINT_NO);
osku's avatar
osku committed
622 623 624 625 626 627
		max_cp = LOG_CHECKPOINT_1;
	}

	cp_buf = hdr + LOG_CHECKPOINT_2;

	if (recv_check_cp_is_consistent(cp_buf)) {
628
		if (mach_read_ull(cp_buf + LOG_CHECKPOINT_NO) > max_cp_no) {
osku's avatar
osku committed
629 630 631 632 633 634 635 636 637
			max_cp = LOG_CHECKPOINT_2;
		}
	}

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

	cp_buf = hdr + max_cp;
638

639
	*lsn = mach_read_ull(cp_buf + LOG_CHECKPOINT_LSN);
osku's avatar
osku committed
640 641 642 643 644
	*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)
645
	    == LOG_CHECKPOINT_FSP_MAGIC_N_VAL) {
646

647 648
		*fsp_limit = mach_read_from_4(
			cp_buf + LOG_CHECKPOINT_FSP_FREE_LIMIT);
osku's avatar
osku committed
649 650 651

		if (*fsp_limit == 0) {
			*fsp_limit = 1000000000;
652
		}
osku's avatar
osku committed
653 654 655 656
	} else {
		*fsp_limit = 1000000000;
	}

657
	/*	fprintf(stderr, "fsp limit %lu MB\n", *fsp_limit); */
osku's avatar
osku committed
658

659
	*cp_no = mach_read_ull(cp_buf + LOG_CHECKPOINT_NO);
osku's avatar
osku committed
660

661
	*first_header_lsn = mach_read_ull(hdr + LOG_FILE_START_LSN);
osku's avatar
osku committed
662 663 664

	return(TRUE);
}
665
#endif /* UNIV_HOTBACKUP */
osku's avatar
osku committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690

/**********************************************************
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. */
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 */
{
#ifdef UNIV_LOG_DEBUG
	return(TRUE);
#endif /* UNIV_LOG_DEBUG */
	if (log_block_calc_checksum(block) == log_block_get_checksum(block)) {

		return(TRUE);
	}

	if (log_block_get_hdr_no(block) == log_block_get_checksum(block)) {

		/* We assume the log block is in the format of
		InnoDB version < 3.23.52 and the block is ok */
691
#if 0
osku's avatar
osku committed
692
		fprintf(stderr,
693 694
			"InnoDB: Scanned old format < InnoDB-3.23.52"
			" log block number %lu\n",
osku's avatar
osku committed
695
			log_block_get_hdr_no(block));
696
#endif
osku's avatar
osku committed
697 698 699 700 701 702
		return(TRUE);
	}

	return(FALSE);
}

703
#ifdef UNIV_HOTBACKUP
osku's avatar
osku committed
704 705 706
/***********************************************************************
Scans the log segment and n_bytes_scanned is set to the length of valid
log scanned. */
707
UNIV_INTERN
osku's avatar
osku committed
708 709 710 711 712
void
recv_scan_log_seg_for_backup(
/*=========================*/
	byte*		buf,		/* in: buffer containing log data */
	ulint		buf_len,	/* in: data length in that buffer */
713
	ib_uint64_t*	scanned_lsn,	/* in/out: lsn of buffer start,
osku's avatar
osku committed
714 715 716 717 718 719 720 721 722 723 724 725 726 727
					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;
728

osku's avatar
osku committed
729
	for (log_block = buf; log_block < buf + buf_len;
730
	     log_block += OS_FILE_LOG_BLOCK_SIZE) {
731

osku's avatar
osku committed
732 733
		no = log_block_get_hdr_no(log_block);

734 735 736
#if 0
		fprintf(stderr, "Log block header no %lu\n", no);
#endif
osku's avatar
osku committed
737 738

		if (no != log_block_convert_lsn_to_no(*scanned_lsn)
739 740
		    || !log_block_checksum_is_ok_or_old_format(log_block)) {
#if 0
osku's avatar
osku committed
741
			fprintf(stderr,
742 743 744
				"Log block n:o %lu, scanned lsn n:o %lu\n",
				no, log_block_convert_lsn_to_no(*scanned_lsn));
#endif
osku's avatar
osku committed
745 746 747
			/* Garbage or an incompletely written log block */

			log_block += OS_FILE_LOG_BLOCK_SIZE;
748
#if 0
osku's avatar
osku committed
749
			fprintf(stderr,
750 751 752
				"Next log block n:o %lu\n",
				log_block_get_hdr_no(log_block));
#endif
osku's avatar
osku committed
753 754 755 756
			break;
		}

		if (*scanned_checkpoint_no > 0
757 758 759 760 761
		    && log_block_get_checkpoint_no(log_block)
		    < *scanned_checkpoint_no
		    && *scanned_checkpoint_no
		    - log_block_get_checkpoint_no(log_block)
		    > 0x80000000UL) {
osku's avatar
osku committed
762 763 764

			/* Garbage from a log buffer flush which was made
			before the most recent database recovery */
765
#if 0
osku's avatar
osku committed
766 767 768 769
			fprintf(stderr,
				"Scanned cp n:o %lu, block cp n:o %lu\n",
				*scanned_checkpoint_no,
				log_block_get_checkpoint_no(log_block));
770
#endif
osku's avatar
osku committed
771 772 773 774 775 776
			break;
		}

		data_len = log_block_get_data_len(log_block);

		*scanned_checkpoint_no
777
			= log_block_get_checkpoint_no(log_block);
778
		*scanned_lsn += data_len;
osku's avatar
osku committed
779 780

		*n_bytes_scanned += data_len;
781

osku's avatar
osku committed
782 783 784
		if (data_len < OS_FILE_LOG_BLOCK_SIZE) {
			/* Log data ends here */

785 786 787 788
#if 0
			fprintf(stderr, "Log block data len %lu\n",
				data_len);
#endif
osku's avatar
osku committed
789 790 791 792
			break;
		}
	}
}
793
#endif /* UNIV_HOTBACKUP */
osku's avatar
osku committed
794 795 796 797 798 799 800 801

/***********************************************************************
Tries to parse a single log record body and also applies it to a page if
specified. File ops are parsed, but not applied in this function. */
static
byte*
recv_parse_or_apply_log_rec_body(
/*=============================*/
802 803 804 805 806 807 808 809 810 811 812
				/* out: log record end, NULL if not a
				complete record */
	byte		type,	/* in: type */
	byte*		ptr,	/* in: pointer to a buffer */
	byte*		end_ptr,/* in: pointer to the buffer end */
	buf_block_t*	block,	/* in/out: buffer block 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 block is non-NULL */
osku's avatar
osku committed
813
{
814 815 816 817 818 819 820 821 822 823 824 825 826
	dict_index_t*	index	= NULL;
	page_t*		page;
	page_zip_des_t*	page_zip;

	ut_ad(!block == !mtr);

	if (block) {
		page = block->frame;
		page_zip = buf_block_get_page_zip(block);
	} else {
		page = NULL;
		page_zip = NULL;
	}
osku's avatar
osku committed
827 828 829

	switch (type) {
	case MLOG_1BYTE: case MLOG_2BYTES: case MLOG_4BYTES: case MLOG_8BYTES:
830
		ptr = mlog_parse_nbytes(type, ptr, end_ptr, page, page_zip);
osku's avatar
osku committed
831 832
		break;
	case MLOG_REC_INSERT: case MLOG_COMP_REC_INSERT:
833 834 835 836
		if (NULL != (ptr = mlog_parse_index(
				     ptr, end_ptr,
				     type == MLOG_COMP_REC_INSERT,
				     &index))) {
osku's avatar
osku committed
837
			ut_a(!page
838 839
			     || (ibool)!!page_is_comp(page)
			     == dict_table_is_comp(index->table));
840
			ptr = page_cur_parse_insert_rec(FALSE, ptr, end_ptr,
841
							block, index, mtr);
osku's avatar
osku committed
842 843 844
		}
		break;
	case MLOG_REC_CLUST_DELETE_MARK: case MLOG_COMP_REC_CLUST_DELETE_MARK:
845 846 847 848
		if (NULL != (ptr = mlog_parse_index(
				     ptr, end_ptr,
				     type == MLOG_COMP_REC_CLUST_DELETE_MARK,
				     &index))) {
osku's avatar
osku committed
849
			ut_a(!page
850 851
			     || (ibool)!!page_is_comp(page)
			     == dict_table_is_comp(index->table));
852 853
			ptr = btr_cur_parse_del_mark_set_clust_rec(
				ptr, end_ptr, page, page_zip, index);
osku's avatar
osku committed
854 855 856 857 858 859
		}
		break;
	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));
860
		ut_a(!page_zip);
osku's avatar
osku committed
861 862 863 864 865 866
		ptr = mlog_parse_index(ptr, end_ptr, TRUE, &index);
		if (!ptr) {
			break;
		}
		/* Fall through */
	case MLOG_REC_SEC_DELETE_MARK:
marko's avatar
marko committed
867
		ptr = btr_cur_parse_del_mark_set_sec_rec(ptr, end_ptr,
868
							 page, page_zip);
osku's avatar
osku committed
869 870
		break;
	case MLOG_REC_UPDATE_IN_PLACE: case MLOG_COMP_REC_UPDATE_IN_PLACE:
871 872 873 874
		if (NULL != (ptr = mlog_parse_index(
				     ptr, end_ptr,
				     type == MLOG_COMP_REC_UPDATE_IN_PLACE,
				     &index))) {
osku's avatar
osku committed
875
			ut_a(!page
876 877
			     || (ibool)!!page_is_comp(page)
			     == dict_table_is_comp(index->table));
878 879
			ptr = btr_cur_parse_update_in_place(ptr, end_ptr, page,
							    page_zip, index);
osku's avatar
osku committed
880 881 882 883
		}
		break;
	case MLOG_LIST_END_DELETE: case MLOG_COMP_LIST_END_DELETE:
	case MLOG_LIST_START_DELETE: case MLOG_COMP_LIST_START_DELETE:
884 885 886 887 888
		if (NULL != (ptr = mlog_parse_index(
				     ptr, end_ptr,
				     type == MLOG_COMP_LIST_END_DELETE
				     || type == MLOG_COMP_LIST_START_DELETE,
				     &index))) {
osku's avatar
osku committed
889
			ut_a(!page
890 891
			     || (ibool)!!page_is_comp(page)
			     == dict_table_is_comp(index->table));
osku's avatar
osku committed
892
			ptr = page_parse_delete_rec_list(type, ptr, end_ptr,
893
							 block, index, mtr);
osku's avatar
osku committed
894 895 896
		}
		break;
	case MLOG_LIST_END_COPY_CREATED: case MLOG_COMP_LIST_END_COPY_CREATED:
897 898 899 900
		if (NULL != (ptr = mlog_parse_index(
				     ptr, end_ptr,
				     type == MLOG_COMP_LIST_END_COPY_CREATED,
				     &index))) {
osku's avatar
osku committed
901
			ut_a(!page
902 903
			     || (ibool)!!page_is_comp(page)
			     == dict_table_is_comp(index->table));
904
			ptr = page_parse_copy_rec_list_to_created_page(
905
				ptr, end_ptr, block, index, mtr);
osku's avatar
osku committed
906 907 908
		}
		break;
	case MLOG_PAGE_REORGANIZE: case MLOG_COMP_PAGE_REORGANIZE:
909 910 911 912
		if (NULL != (ptr = mlog_parse_index(
				     ptr, end_ptr,
				     type == MLOG_COMP_PAGE_REORGANIZE,
				     &index))) {
osku's avatar
osku committed
913
			ut_a(!page
914 915
			     || (ibool)!!page_is_comp(page)
			     == dict_table_is_comp(index->table));
osku's avatar
osku committed
916
			ptr = btr_parse_page_reorganize(ptr, end_ptr, index,
917
							block, mtr);
osku's avatar
osku committed
918 919 920
		}
		break;
	case MLOG_PAGE_CREATE: case MLOG_COMP_PAGE_CREATE:
921
		ut_a(!page_zip);
osku's avatar
osku committed
922
		ptr = page_parse_create(ptr, end_ptr,
923
					type == MLOG_COMP_PAGE_CREATE,
924
					block, mtr);
osku's avatar
osku committed
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
		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,
941
						 page, mtr);
osku's avatar
osku committed
942 943
		break;
	case MLOG_REC_MIN_MARK: case MLOG_COMP_REC_MIN_MARK:
944 945 946 947 948
		/* On a compressed page, MLOG_COMP_REC_MIN_MARK
		will be followed by MLOG_COMP_REC_DELETE
		or MLOG_ZIP_WRITE_HEADER(FIL_PAGE_PREV, FIL_NULL)
		in the same mini-transaction. */
		ut_a(type == MLOG_COMP_REC_MIN_MARK || !page_zip);
949 950 951
		ptr = btr_parse_set_min_rec_mark(
			ptr, end_ptr, type == MLOG_COMP_REC_MIN_MARK,
			page, mtr);
osku's avatar
osku committed
952 953
		break;
	case MLOG_REC_DELETE: case MLOG_COMP_REC_DELETE:
954 955 956 957
		if (NULL != (ptr = mlog_parse_index(
				     ptr, end_ptr,
				     type == MLOG_COMP_REC_DELETE,
				     &index))) {
osku's avatar
osku committed
958
			ut_a(!page
959 960
			     || (ibool)!!page_is_comp(page)
			     == dict_table_is_comp(index->table));
961 962
			ptr = page_cur_parse_delete_rec(ptr, end_ptr,
							block, index, mtr);
osku's avatar
osku committed
963 964 965
		}
		break;
	case MLOG_IBUF_BITMAP_INIT:
966
		ptr = ibuf_parse_bitmap_init(ptr, end_ptr, block, mtr);
osku's avatar
osku committed
967 968
		break;
	case MLOG_INIT_FILE_PAGE:
969
		ptr = fsp_parse_init_file_page(ptr, end_ptr, block);
osku's avatar
osku committed
970 971
		break;
	case MLOG_WRITE_STRING:
972
		ptr = mlog_parse_string(ptr, end_ptr, page, page_zip);
osku's avatar
osku committed
973 974 975 976
		break;
	case MLOG_FILE_CREATE:
	case MLOG_FILE_RENAME:
	case MLOG_FILE_DELETE:
977
	case MLOG_FILE_CREATE2:
978
		ptr = fil_op_log_parse_or_replay(ptr, end_ptr, type, 0);
osku's avatar
osku committed
979
		break;
980
	case MLOG_ZIP_WRITE_NODE_PTR:
981 982
		ptr = page_zip_parse_write_node_ptr(ptr, end_ptr,
						    page, page_zip);
983
		break;
984
	case MLOG_ZIP_WRITE_BLOB_PTR:
985 986
		ptr = page_zip_parse_write_blob_ptr(ptr, end_ptr,
						    page, page_zip);
987
		break;
988
	case MLOG_ZIP_WRITE_HEADER:
989 990
		ptr = page_zip_parse_write_header(ptr, end_ptr,
						  page, page_zip);
991
		break;
992
	case MLOG_ZIP_PAGE_COMPRESS:
993 994
		ptr = page_zip_parse_compress(ptr, end_ptr,
					      page, page_zip);
marko's avatar
marko committed
995
		break;
osku's avatar
osku committed
996 997 998 999 1000 1001 1002
	default:
		ptr = NULL;
		recv_sys->found_corrupt_log = TRUE;
	}

	if (index) {
		dict_table_t*	table = index->table;
1003 1004 1005

		dict_mem_index_free(index);
		dict_mem_table_free(table);
osku's avatar
osku committed
1006
	}
1007

osku's avatar
osku committed
1008 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
	return(ptr);
}

/*************************************************************************
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,
1053
				   recv_hash(space, page_no));
osku's avatar
osku committed
1054 1055
	while (recv_addr) {
		if ((recv_addr->space == space)
1056
		    && (recv_addr->page_no == page_no)) {
osku's avatar
osku committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

			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(
/*===================*/
1073 1074 1075 1076 1077
	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 */
1078 1079
	ib_uint64_t	start_lsn,	/* in: start lsn of the mtr */
	ib_uint64_t	end_lsn)	/* in: end lsn of the mtr */
osku's avatar
osku committed
1080 1081 1082 1083 1084 1085
{
	recv_t*		recv;
	ulint		len;
	recv_data_t*	recv_data;
	recv_data_t**	prev_field;
	recv_addr_t*	recv_addr;
1086

osku's avatar
osku committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
	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;
	}

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

osku's avatar
osku committed
1104 1105
	if (recv_addr == NULL) {
		recv_addr = mem_heap_alloc(recv_sys->heap,
1106
					   sizeof(recv_addr_t));
osku's avatar
osku committed
1107 1108 1109 1110 1111 1112 1113
		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,
1114
			    recv_fold(space, page_no), recv_addr);
osku's avatar
osku committed
1115
		recv_sys->n_addrs++;
1116 1117 1118 1119
#if 0
		fprintf(stderr, "Inserting log rec for space %lu, page %lu\n",
			space, page_no);
#endif
osku's avatar
osku committed
1120 1121 1122 1123 1124 1125 1126 1127 1128
	}

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

osku's avatar
osku committed
1130 1131 1132
	while (rec_end > body) {

		len = rec_end - body;
1133

osku's avatar
osku committed
1134 1135 1136
		if (len > RECV_DATA_BLOCK_SIZE) {
			len = RECV_DATA_BLOCK_SIZE;
		}
1137

osku's avatar
osku committed
1138
		recv_data = mem_heap_alloc(recv_sys->heap,
1139
					   sizeof(recv_data_t) + len);
osku's avatar
osku committed
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
		*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),
1176
			  part_len);
osku's avatar
osku committed
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		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. */
1188
UNIV_INTERN
osku's avatar
osku committed
1189 1190 1191
void
recv_recover_page(
/*==============*/
1192 1193
	ibool		recover_backup,
				/* in: TRUE if we are recovering a backup
osku's avatar
osku committed
1194 1195 1196
				page: then we do not acquire any latches
				since the page was read in outside the
				buffer pool */
1197 1198
	ibool		just_read_in,
				/* in: TRUE if the i/o-handler calls this for
osku's avatar
osku committed
1199
				a freshly read page */
1200
	buf_block_t*	block)	/* in: buffer block */
osku's avatar
osku committed
1201
{
1202
	page_t*		page;
osku's avatar
osku committed
1203 1204 1205
	recv_addr_t*	recv_addr;
	recv_t*		recv;
	byte*		buf;
1206 1207 1208 1209
	ib_uint64_t	start_lsn;
	ib_uint64_t	end_lsn;
	ib_uint64_t	page_lsn;
	ib_uint64_t	page_newest_lsn;
osku's avatar
osku committed
1210 1211 1212 1213 1214 1215 1216 1217 1218
	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 */
1219

osku's avatar
osku committed
1220 1221 1222 1223
		mutex_exit(&(recv_sys->mutex));

		return;
	}
1224

1225 1226
	recv_addr = recv_get_fil_addr_struct(buf_block_get_space(block),
					     buf_block_get_page_no(block));
osku's avatar
osku committed
1227 1228

	if ((recv_addr == NULL)
1229 1230
	    || (recv_addr->state == RECV_BEING_PROCESSED)
	    || (recv_addr->state == RECV_PROCESSED)) {
osku's avatar
osku committed
1231 1232 1233 1234 1235 1236

		mutex_exit(&(recv_sys->mutex));

		return;
	}

1237
#if 0
1238
	fprintf(stderr, "Recovering space %lu, page %lu\n",
1239
		buf_block_get_space(block), buf_block_get_page_no(block));
1240
#endif
osku's avatar
osku committed
1241 1242

	recv_addr->state = RECV_BEING_PROCESSED;
1243

osku's avatar
osku committed
1244 1245 1246 1247 1248
	mutex_exit(&(recv_sys->mutex));

	mtr_start(&mtr);
	mtr_set_log_mode(&mtr, MTR_LOG_NONE);

1249
	page = block->frame;
osku's avatar
osku committed
1250

1251
	if (!recover_backup) {
osku's avatar
osku committed
1252
		if (just_read_in) {
1253 1254 1255 1256 1257
			/* 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. */
osku's avatar
osku committed
1258 1259 1260 1261

			rw_lock_x_lock_move_ownership(&(block->lock));
		}

1262
		success = buf_page_get_known_nowait(RW_X_LATCH, block,
1263 1264 1265
						    BUF_KEEP_OLD,
						    __FILE__, __LINE__,
						    &mtr);
osku's avatar
osku committed
1266 1267
		ut_a(success);

1268
		buf_block_dbg_add_level(block, SYNC_NO_ORDER_CHECK);
osku's avatar
osku committed
1269 1270 1271
	}

	/* Read the newest modification lsn from the page */
1272
	page_lsn = mach_read_ull(page + FIL_PAGE_LSN);
osku's avatar
osku committed
1273 1274 1275 1276

	if (!recover_backup) {
		/* It may be that the page has been modified in the buffer
		pool: read the newest modification lsn there */
1277

1278 1279
		page_newest_lsn
			= buf_page_get_newest_modification(&block->page);
osku's avatar
osku committed
1280

1281
		if (page_newest_lsn) {
1282

osku's avatar
osku committed
1283 1284 1285 1286 1287 1288
			page_lsn = page_newest_lsn;
		}
	} else {
		/* In recovery from a backup we do not really use the buffer
		pool */

1289
		page_newest_lsn = 0;
osku's avatar
osku committed
1290 1291 1292
	}

	modification_to_page = FALSE;
1293
	start_lsn = end_lsn = 0;
osku's avatar
osku committed
1294 1295

	recv = UT_LIST_GET_FIRST(recv_addr->rec_list);
1296

osku's avatar
osku committed
1297 1298
	while (recv) {
		end_lsn = recv->end_lsn;
1299

osku's avatar
osku committed
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
		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);
		}

		if (recv->type == MLOG_INIT_FILE_PAGE) {
			page_lsn = page_newest_lsn;

1314 1315 1316
			mach_write_ull(page + UNIV_PAGE_SIZE
				       - FIL_PAGE_END_LSN_OLD_CHKSUM, 0);
			mach_write_ull(page + FIL_PAGE_LSN, 0);
osku's avatar
osku committed
1317
		}
1318

1319
		if (recv->start_lsn >= page_lsn) {
osku's avatar
osku committed
1320 1321

			if (!modification_to_page) {
1322

osku's avatar
osku committed
1323 1324 1325 1326 1327 1328
				modification_to_page = TRUE;
				start_lsn = recv->start_lsn;
			}

#ifdef UNIV_DEBUG
			if (log_debug_writes) {
1329
				fprintf(stderr,
1330 1331 1332
					"InnoDB: Applying log rec"
					" type %lu len %lu"
					" to space %lu page no %lu\n",
osku's avatar
osku committed
1333 1334 1335 1336 1337 1338 1339
					(ulong) recv->type, (ulong) recv->len,
					(ulong) recv_addr->space,
					(ulong) recv_addr->page_no);
			}
#endif /* UNIV_DEBUG */

			recv_parse_or_apply_log_rec_body(recv->type, buf,
1340
							 buf + recv->len,
1341
							 block, &mtr);
1342 1343 1344 1345 1346
			mach_write_ull(page + UNIV_PAGE_SIZE
				       - FIL_PAGE_END_LSN_OLD_CHKSUM,
				       recv->start_lsn + recv->len);
			mach_write_ull(page + FIL_PAGE_LSN,
				       recv->start_lsn + recv->len);
osku's avatar
osku committed
1347
		}
1348

osku's avatar
osku committed
1349 1350 1351 1352 1353 1354 1355
		if (recv->len > RECV_DATA_BLOCK_SIZE) {
			mem_free(buf);
		}

		recv = UT_LIST_GET_NEXT(rec_list, recv);
	}

1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
#ifdef UNIV_ZIP_DEBUG
	if (fil_page_get_type(page) == FIL_PAGE_INDEX) {
		page_zip_des_t*	page_zip = buf_block_get_page_zip(block);

		if (page_zip) {
			ut_a(page_zip_validate_low(page_zip, page, FALSE));
		}
	}
#endif /* UNIV_ZIP_DEBUG */

osku's avatar
osku committed
1366
	mutex_enter(&(recv_sys->mutex));
1367

1368
	if (recv_max_page_lsn < page_lsn) {
osku's avatar
osku committed
1369 1370 1371 1372 1373 1374 1375 1376 1377
		recv_max_page_lsn = page_lsn;
	}

	recv_addr->state = RECV_PROCESSED;

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

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

osku's avatar
osku committed
1379 1380 1381 1382 1383
	if (!recover_backup && modification_to_page) {
		ut_a(block);

		buf_flush_recv_note_modification(block, start_lsn, end_lsn);
	}
1384

osku's avatar
osku committed
1385 1386
	/* Make sure that committing mtr does not change the modification
	lsn values of page */
1387

osku's avatar
osku committed
1388
	mtr.modifications = FALSE;
1389 1390

	mtr_commit(&mtr);
osku's avatar
osku committed
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
}

/***********************************************************************
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 */
1402
	ulint	zip_size,/* in: compressed page size in bytes, or 0 */
osku's avatar
osku committed
1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414
	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;
1415
	     page_no++) {
osku's avatar
osku committed
1416 1417 1418 1419 1420 1421 1422 1423
		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;
1424

osku's avatar
osku committed
1425 1426 1427 1428
				page_nos[n] = page_no;

				n++;
			}
1429

osku's avatar
osku committed
1430 1431 1432 1433
			mutex_exit(&(recv_sys->mutex));
		}
	}

1434
	buf_read_recv_pages(FALSE, space, zip_size, page_nos, n);
osku's avatar
osku committed
1435 1436 1437 1438 1439
	/*
	fprintf(stderr, "Recv pages at %lu n %lu\n", page_nos[0], n);
	*/
	return(n);
}
1440

osku's avatar
osku committed
1441 1442 1443
/***********************************************************************
Empties the hash table of stored log records, applying them to appropriate
pages. */
1444
UNIV_INTERN
osku's avatar
osku committed
1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475
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;
	ulint	i;
	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;
	}

	ut_ad(!allow_ibuf == mutex_own(&log_sys->mutex));
1476

osku's avatar
osku committed
1477 1478 1479 1480 1481 1482 1483 1484
	if (!allow_ibuf) {
		recv_no_ibuf_operations = TRUE;
	}

	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++) {
1485

osku's avatar
osku committed
1486 1487 1488
		recv_addr = HASH_GET_FIRST(recv_sys->addr_hash, i);

		while (recv_addr) {
1489 1490 1491
			ulint	space = recv_addr->space;
			ulint	zip_size = fil_space_get_zip_size(space);
			ulint	page_no = recv_addr->page_no;
osku's avatar
osku committed
1492 1493 1494

			if (recv_addr->state == RECV_NOT_PROCESSED) {
				if (!has_printed) {
1495
					ut_print_timestamp(stderr);
1496 1497 1498 1499 1500
					fputs("  InnoDB: Starting an"
					      " apply batch of log records"
					      " to the database...\n"
					      "InnoDB: Progress in percents: ",
					      stderr);
osku's avatar
osku committed
1501 1502
					has_printed = TRUE;
				}
1503

osku's avatar
osku committed
1504 1505 1506
				mutex_exit(&(recv_sys->mutex));

				if (buf_page_peek(space, page_no)) {
1507
					buf_block_t*	block;
osku's avatar
osku committed
1508 1509 1510

					mtr_start(&mtr);

1511 1512 1513
					block = buf_page_get(
						space, zip_size, page_no,
						RW_X_LATCH, &mtr);
1514 1515
					buf_block_dbg_add_level(
						block, SYNC_NO_ORDER_CHECK);
1516

1517
					recv_recover_page(FALSE, FALSE, block);
osku's avatar
osku committed
1518 1519
					mtr_commit(&mtr);
				} else {
1520 1521
					recv_read_in_area(space, zip_size,
							  page_no);
osku's avatar
osku committed
1522 1523 1524 1525 1526 1527 1528 1529 1530
				}

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

			recv_addr = HASH_GET_NEXT(addr_hash, recv_addr);
		}

		if (has_printed
1531 1532 1533
		    && (i * 100) / hash_get_n_cells(recv_sys->addr_hash)
		    != ((i + 1) * 100)
		    / hash_get_n_cells(recv_sys->addr_hash)) {
osku's avatar
osku committed
1534

1535 1536 1537
			fprintf(stderr, "%lu ", (ulong)
				((i * 100)
				 / hash_get_n_cells(recv_sys->addr_hash)));
osku's avatar
osku committed
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
		}
	}

	/* 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));
1550
	}
osku's avatar
osku committed
1551 1552 1553

	if (has_printed) {

1554
		fprintf(stderr, "\n");
osku's avatar
osku committed
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564
	}

	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,
1565
					  IB_ULONGLONG_MAX);
osku's avatar
osku committed
1566
		ut_a(n_pages != ULINT_UNDEFINED);
1567

osku's avatar
osku committed
1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579
		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;
1580

osku's avatar
osku committed
1581 1582 1583 1584 1585 1586 1587 1588 1589
	recv_sys_empty_hash();

	if (has_printed) {
		fprintf(stderr, "InnoDB: Apply batch completed\n");
	}

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

1590
#ifdef UNIV_HOTBACKUP
osku's avatar
osku committed
1591 1592
/***********************************************************************
Applies log records in the hash table to a backup. */
1593
UNIV_INTERN
osku's avatar
osku committed
1594 1595 1596 1597 1598 1599
void
recv_apply_log_recs_for_backup(void)
/*================================*/
{
	recv_addr_t*	recv_addr;
	ulint		n_hash_cells;
1600
	buf_block_t*	block;
osku's avatar
osku committed
1601 1602 1603 1604 1605 1606 1607 1608
	ulint		actual_size;
	ibool		success;
	ulint		error;
	ulint		i;

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

1609
	block = buf_LRU_get_free_block(UNIV_PAGE_SIZE);
osku's avatar
osku committed
1610

1611 1612 1613
	fputs("InnoDB: Starting an apply batch of log records"
	      " to the database...\n"
	      "InnoDB: Progress in percents: ", stderr);
1614

osku's avatar
osku committed
1615 1616 1617
	n_hash_cells = hash_get_n_cells(recv_sys->addr_hash);

	for (i = 0; i < n_hash_cells; i++) {
1618
		/* The address hash table is externally chained */
osku's avatar
osku committed
1619 1620 1621 1622
		recv_addr = hash_get_nth_cell(recv_sys->addr_hash, i)->node;

		while (recv_addr != NULL) {

1623 1624 1625 1626
			ulint	zip_size
				= fil_space_get_zip_size(recv_addr->space);

			if (zip_size == ULINT_UNDEFINED) {
1627
#if 0
osku's avatar
osku committed
1628
				fprintf(stderr,
1629 1630 1631 1632 1633 1634 1635
					"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);
#endif
osku's avatar
osku committed
1636 1637 1638 1639 1640 1641 1642 1643 1644
				recv_addr->state = RECV_PROCESSED;

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

				goto skip_this_recv_addr;
			}

			/* We simulate a page read made by the buffer pool, to
1645 1646
			make sure the recovery apparatus works ok. We must init
			the block. */
osku's avatar
osku committed
1647

1648 1649
			buf_page_init_for_backup_restore(
				recv_addr->space, recv_addr->page_no,
1650
				zip_size, block);
osku's avatar
osku committed
1651 1652 1653 1654 1655 1656

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

1657 1658 1659
			success = fil_extend_space_to_desired_size(
				&actual_size,
				recv_addr->space, recv_addr->page_no + 1);
osku's avatar
osku committed
1660
			if (!success) {
1661 1662 1663 1664 1665
				fprintf(stderr,
					"InnoDB: Fatal error: cannot extend"
					" tablespace %lu to hold %lu pages\n",
					recv_addr->space, recv_addr->page_no);

osku's avatar
osku committed
1666 1667 1668 1669 1670 1671
				exit(1);
			}

			/* Read the page from the tablespace file using the
			fil0fil.c routines */

1672 1673 1674 1675
			if (zip_size) {
				error = fil_io(OS_FILE_READ, TRUE,
					       recv_addr->space, zip_size,
					       recv_addr->page_no, 0, zip_size,
1676
					       block->page.zip.data, NULL);
1677 1678 1679 1680 1681 1682 1683 1684
			} else {
				error = fil_io(OS_FILE_READ, TRUE,
					       recv_addr->space, 0,
					       recv_addr->page_no, 0,
					       UNIV_PAGE_SIZE,
					       block->frame, NULL);
			}

osku's avatar
osku committed
1685
			if (error != DB_SUCCESS) {
1686
				fprintf(stderr,
1687 1688 1689
					"InnoDB: Fatal error: cannot read"
					" from tablespace"
					" %lu page number %lu\n",
1690 1691 1692
					(ulong) recv_addr->space,
					(ulong) recv_addr->page_no);

osku's avatar
osku committed
1693 1694 1695 1696
				exit(1);
			}

			/* Apply the log records to this page */
1697
			recv_recover_page(TRUE, FALSE, block);
osku's avatar
osku committed
1698 1699 1700 1701

			/* Write the page back to the tablespace file using the
			fil0fil.c routines */

1702
			buf_flush_init_for_writing(
1703
				block->frame, buf_block_get_page_zip(block),
1704
				mach_read_ull(block->frame + FIL_PAGE_LSN));
osku's avatar
osku committed
1705

1706 1707 1708 1709 1710
			if (zip_size) {
				error = fil_io(OS_FILE_WRITE, TRUE,
					       recv_addr->space, zip_size,
					       recv_addr->page_no, 0,
					       zip_size,
1711
					       block->page.zip.data, NULL);
1712 1713 1714 1715 1716 1717 1718
			} else {
				error = fil_io(OS_FILE_WRITE, TRUE,
					       recv_addr->space, 0,
					       recv_addr->page_no, 0,
					       UNIV_PAGE_SIZE,
					       block->frame, NULL);
			}
osku's avatar
osku committed
1719 1720 1721 1722 1723
skip_this_recv_addr:
			recv_addr = HASH_GET_NEXT(addr_hash, recv_addr);
		}

		if ((100 * i) / n_hash_cells
1724
		    != (100 * (i + 1)) / n_hash_cells) {
osku's avatar
osku committed
1725
			fprintf(stderr, "%lu ",
1726
				(ulong) ((100 * i) / n_hash_cells));
osku's avatar
osku committed
1727 1728 1729 1730
			fflush(stderr);
		}
	}

1731
	buf_block_free(block);
osku's avatar
osku committed
1732 1733
	recv_sys_empty_hash();
}
1734
#endif /* UNIV_HOTBACKUP */
osku's avatar
osku committed
1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760

/***********************************************************************
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
			not complete */
	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;

	*body = NULL;

	if (ptr == end_ptr) {

		return(0);
	}

	if (*ptr == MLOG_MULTI_REC_END) {
1761

osku's avatar
osku committed
1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775
		*type = *ptr;

		return(1);
	}

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

		*space = ULINT_UNDEFINED - 1; /* For debugging */

		return(1);
	}

	new_ptr = mlog_parse_initial_log_record(ptr, end_ptr, type, space,
1776
						page_no);
osku's avatar
osku committed
1777 1778 1779 1780
	*body = new_ptr;

	if (UNIV_UNLIKELY(!new_ptr)) {

1781
		return(0);
osku's avatar
osku committed
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793
	}

	/* Check that page_no is sensible */

	if (UNIV_UNLIKELY(*page_no > 0x8FFFFFFFUL)) {

		recv_sys->found_corrupt_log = TRUE;

		return(0);
	}

	new_ptr = recv_parse_or_apply_log_rec_body(*type, new_ptr, end_ptr,
1794
						   NULL, NULL);
osku's avatar
osku committed
1795 1796 1797 1798 1799 1800 1801 1802
	if (UNIV_UNLIKELY(new_ptr == NULL)) {

		return(0);
	}

	if (*page_no > recv_max_parsed_page_no) {
		recv_max_parsed_page_no = *page_no;
	}
1803

osku's avatar
osku committed
1804 1805 1806 1807 1808 1809
	return(new_ptr - ptr);
}

/***********************************************************
Calculates the new value for lsn when more data is added to the log. */
static
1810
ib_uint64_t
osku's avatar
osku committed
1811 1812
recv_calc_lsn_on_data_add(
/*======================*/
1813 1814
	ib_uint64_t	lsn,	/* in: old lsn */
	ib_uint64_t	len)	/* in: this many bytes of data is
1815
				added, log block headers not included */
osku's avatar
osku committed
1816 1817 1818
{
	ulint	frag_len;
	ulint	lsn_len;
1819

1820
	frag_len = (((ulint) lsn) % OS_FILE_LOG_BLOCK_SIZE)
1821
		- LOG_BLOCK_HDR_SIZE;
osku's avatar
osku committed
1822
	ut_ad(frag_len < OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_HDR_SIZE
1823
	      - LOG_BLOCK_TRL_SIZE);
1824 1825 1826 1827
	lsn_len = (ulint) len;
	lsn_len += (lsn_len + frag_len)
		/ (OS_FILE_LOG_BLOCK_SIZE - LOG_BLOCK_HDR_SIZE
		   - LOG_BLOCK_TRL_SIZE)
1828
		* (LOG_BLOCK_HDR_SIZE + LOG_BLOCK_TRL_SIZE);
osku's avatar
osku committed
1829

1830
	return(lsn + lsn_len);
osku's avatar
osku committed
1831 1832
}

1833
#ifdef UNIV_LOG_DEBUG
osku's avatar
osku committed
1834 1835 1836
/***********************************************************
Checks that the parser recognizes incomplete initial segments of a log
record as incomplete. */
1837
static
osku's avatar
osku committed
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
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;
1849

osku's avatar
osku committed
1850 1851
	for (i = 0; i < len; i++) {
		ut_a(0 == recv_parse_log_rec(ptr, ptr + i, &type, &space,
1852
					     &page_no, &body));
osku's avatar
osku committed
1853
	}
1854
}
1855
#endif /* UNIV_LOG_DEBUG */
osku's avatar
osku committed
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868

/***********************************************************
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,
1869 1870
		"InnoDB: ############### CORRUPT LOG RECORD FOUND\n"
		"InnoDB: Log record type %lu, space id %lu, page number %lu\n"
1871
		"InnoDB: Log parsing proceeded successfully up to %llu\n"
1872 1873
		"InnoDB: Previous log record type %lu, is multi %lu\n"
		"InnoDB: Recv offset %lu, prev %lu\n",
1874
		(ulong) type, (ulong) space, (ulong) page_no,
1875
		recv_sys->recovered_lsn,
1876 1877 1878 1879
		(ulong) recv_previous_parsed_rec_type,
		(ulong) recv_previous_parsed_rec_is_multi,
		(ulong) (ptr - recv_sys->buf),
		(ulong) recv_previous_parsed_rec_offset);
osku's avatar
osku committed
1880 1881

	if ((ulint)(ptr - recv_sys->buf + 100)
1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
	    > recv_previous_parsed_rec_offset
	    && (ulint)(ptr - recv_sys->buf + 100
		       - recv_previous_parsed_rec_offset)
	    < 200000) {
		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);
1892

osku's avatar
osku committed
1893
		ut_print_buf(stderr,
1894 1895 1896 1897
			     recv_sys->buf
			     + recv_previous_parsed_rec_offset - 100,
			     ptr - recv_sys->buf + 200
			     - recv_previous_parsed_rec_offset);
osku's avatar
osku committed
1898 1899 1900
		putc('\n', stderr);
	}

1901 1902 1903 1904 1905 1906 1907 1908
	fputs("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"
	      "InnoDB: on your InnoDB tables to check that they are ok!\n"
	      "InnoDB: If mysqld crashes after this recovery, look at\n"
	      "InnoDB: http://dev.mysql.com/doc/refman/5.1/en/"
	      "forcing-recovery.html\n"
	      "InnoDB: about forcing recovery.\n", stderr);
osku's avatar
osku committed
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924

	fflush(stderr);
}

/***********************************************************
Parses log records from a buffer and stores them to a hash table to wait
merging to file pages. */
static
ibool
recv_parse_log_recs(
/*================*/
				/* out: currently always returns FALSE */
	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 */
{
1925 1926 1927 1928 1929
	byte*		ptr;
	byte*		end_ptr;
	ulint		single_rec;
	ulint		len;
	ulint		total_len;
1930 1931
	ib_uint64_t	new_recovered_lsn;
	ib_uint64_t	old_lsn;
1932 1933 1934 1935 1936
	byte		type;
	ulint		space;
	ulint		page_no;
	byte*		body;
	ulint		n_recs;
1937

osku's avatar
osku committed
1938
	ut_ad(mutex_own(&(log_sys->mutex)));
1939
	ut_ad(recv_sys->parse_start_lsn != 0);
osku's avatar
osku committed
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
loop:
	ptr = recv_sys->buf + recv_sys->recovered_offset;

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

	if (ptr == end_ptr) {

		return(FALSE);
	}

	single_rec = (ulint)*ptr & MLOG_SINGLE_REC_FLAG;

	if (single_rec || *ptr == MLOG_DUMMY_RECORD) {
		/* The mtr only modified a single page, or this is a file op */

		old_lsn = recv_sys->recovered_lsn;

		/* Try to parse a log record, fetching its type, space id,
		page no, and a pointer to the body of the log record */

		len = recv_parse_log_rec(ptr, end_ptr, &type, &space,
1961
					 &page_no, &body);
osku's avatar
osku committed
1962 1963 1964 1965 1966

		if (len == 0 || recv_sys->found_corrupt_log) {
			if (recv_sys->found_corrupt_log) {

				recv_report_corrupt_log(ptr,
1967
							type, space, page_no);
osku's avatar
osku committed
1968
			}
1969

osku's avatar
osku committed
1970 1971 1972 1973 1974
			return(FALSE);
		}

		new_recovered_lsn = recv_calc_lsn_on_data_add(old_lsn, len);

1975
		if (new_recovered_lsn > recv_sys->scanned_lsn) {
osku's avatar
osku committed
1976 1977 1978 1979 1980 1981
			/* The log record filled a log block, and we require
			that also the next log block should have been scanned
			in */

			return(FALSE);
		}
1982

osku's avatar
osku committed
1983 1984 1985 1986 1987 1988 1989 1990 1991
		recv_previous_parsed_rec_type = (ulint)type;
		recv_previous_parsed_rec_offset = recv_sys->recovered_offset;
		recv_previous_parsed_rec_is_multi = 0;

		recv_sys->recovered_offset += len;
		recv_sys->recovered_lsn = new_recovered_lsn;

#ifdef UNIV_DEBUG
		if (log_debug_writes) {
1992
			fprintf(stderr,
1993 1994
				"InnoDB: Parsed a single log rec"
				" type %lu len %lu space %lu page no %lu\n",
osku's avatar
osku committed
1995 1996 1997 1998 1999 2000 2001
				(ulong) type, (ulong) len, (ulong) space,
				(ulong) page_no);
		}
#endif /* UNIV_DEBUG */

		if (type == MLOG_DUMMY_RECORD) {
			/* Do nothing */
2002

2003 2004 2005 2006 2007 2008 2009 2010 2011
		} else if (!store_to_hash) {
			/* 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);
#endif/* UNIV_LOG_DEBUG */

		} else if (type == MLOG_FILE_CREATE
2012
			   || type == MLOG_FILE_CREATE2
2013 2014
			   || type == MLOG_FILE_RENAME
			   || type == MLOG_FILE_DELETE) {
2015
			ut_a(space);
osku's avatar
osku committed
2016 2017 2018 2019 2020 2021 2022
#ifdef UNIV_HOTBACKUP
			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 */
2023

2024 2025
				if (NULL == fil_op_log_parse_or_replay(
					    body, end_ptr, type, space)) {
osku's avatar
osku committed
2026
					fprintf(stderr,
2027 2028 2029 2030 2031 2032 2033
						"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));
osku's avatar
osku committed
2034

2035
					ut_error;
osku's avatar
osku committed
2036 2037 2038 2039 2040
				}
			}
#endif
			/* In normal mysqld crash recovery we do not try to
			replay file operations */
2041
		} else {
osku's avatar
osku committed
2042
			recv_add_to_hash_table(type, space, page_no, body,
2043 2044
					       ptr + len, old_lsn,
					       recv_sys->recovered_lsn);
osku's avatar
osku committed
2045 2046 2047 2048 2049 2050 2051
		}
	} else {
		/* Check that all the records associated with the single mtr
		are included within the buffer */

		total_len = 0;
		n_recs = 0;
2052

osku's avatar
osku committed
2053 2054
		for (;;) {
			len = recv_parse_log_rec(ptr, end_ptr, &type, &space,
2055
						 &page_no, &body);
osku's avatar
osku committed
2056 2057
			if (len == 0 || recv_sys->found_corrupt_log) {

2058
				if (recv_sys->found_corrupt_log) {
osku's avatar
osku committed
2059

2060 2061
					recv_report_corrupt_log(
						ptr, type, space, page_no);
2062
				}
osku's avatar
osku committed
2063

2064
				return(FALSE);
osku's avatar
osku committed
2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
			}

			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;

			if ((!store_to_hash) && (type != MLOG_MULTI_REC_END)) {
#ifdef UNIV_LOG_DEBUG
				recv_check_incomplete_log_recs(ptr, len);
#endif /* UNIV_LOG_DEBUG */
			}

#ifdef UNIV_DEBUG
			if (log_debug_writes) {
2080
				fprintf(stderr,
2081 2082 2083 2084 2085
					"InnoDB: Parsed a multi log rec"
					" type %lu len %lu"
					" space %lu page no %lu\n",
					(ulong) type, (ulong) len,
					(ulong) space, (ulong) page_no);
osku's avatar
osku committed
2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101
			}
#endif /* UNIV_DEBUG */

			total_len += len;
			n_recs++;

			ptr += len;

			if (type == MLOG_MULTI_REC_END) {

				/* Found the end mark for the records */

				break;
			}
		}

2102 2103
		new_recovered_lsn = recv_calc_lsn_on_data_add(
			recv_sys->recovered_lsn, total_len);
osku's avatar
osku committed
2104

2105
		if (new_recovered_lsn > recv_sys->scanned_lsn) {
osku's avatar
osku committed
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119
			/* The log record filled a log block, and we require
			that also the next log block should have been scanned
			in */

			return(FALSE);
		}

		/* 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,
2120
						 &page_no, &body);
osku's avatar
osku committed
2121 2122 2123 2124 2125 2126 2127 2128 2129 2130
			if (recv_sys->found_corrupt_log) {

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

			ut_a(len != 0);
			ut_a(0 == ((ulint)*ptr & MLOG_SINGLE_REC_FLAG));

			recv_sys->recovered_offset += len;
2131 2132
			recv_sys->recovered_lsn
				= recv_calc_lsn_on_data_add(old_lsn, len);
osku's avatar
osku committed
2133 2134 2135 2136 2137 2138 2139 2140 2141
			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,
2142 2143 2144
						       body, ptr + len,
						       old_lsn,
						       new_recovered_lsn);
osku's avatar
osku committed
2145
			}
2146

osku's avatar
osku committed
2147 2148 2149
			ptr += len;
		}
	}
2150

osku's avatar
osku committed
2151 2152 2153 2154 2155 2156 2157 2158 2159 2160
	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(
/*========================*/
2161 2162
					/* out: TRUE if more data added */
	byte*		log_block,	/* in: log block */
2163
	ib_uint64_t	scanned_lsn)	/* in: lsn of how far we were able
2164
					to find data in this log block */
osku's avatar
osku committed
2165 2166 2167 2168 2169 2170
{
	ulint	more_len;
	ulint	data_len;
	ulint	start_offset;
	ulint	end_offset;

2171
	ut_ad(scanned_lsn >= recv_sys->scanned_lsn);
osku's avatar
osku committed
2172

2173
	if (!recv_sys->parse_start_lsn) {
osku's avatar
osku committed
2174 2175 2176 2177 2178 2179 2180 2181
		/* Cannot start parsing yet because no start point for
		it found */

		return(FALSE);
	}

	data_len = log_block_get_data_len(log_block);

2182
	if (recv_sys->parse_start_lsn >= scanned_lsn) {
osku's avatar
osku committed
2183 2184 2185

		return(FALSE);

2186
	} else if (recv_sys->scanned_lsn >= scanned_lsn) {
osku's avatar
osku committed
2187 2188

		return(FALSE);
2189

2190 2191
	} else if (recv_sys->parse_start_lsn > recv_sys->scanned_lsn) {
		more_len = (ulint) (scanned_lsn - recv_sys->parse_start_lsn);
osku's avatar
osku committed
2192
	} else {
2193
		more_len = (ulint) (scanned_lsn - recv_sys->scanned_lsn);
osku's avatar
osku committed
2194 2195 2196 2197 2198 2199
	}

	if (more_len == 0) {

		return(FALSE);
	}
2200

osku's avatar
osku committed
2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218
	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,
2219
			  log_block + start_offset, end_offset - start_offset);
osku's avatar
osku committed
2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234

		recv_sys->len += end_offset - start_offset;

		ut_a(recv_sys->len <= RECV_PARSING_BUF_SIZE);
	}

	return(TRUE);
}

/***********************************************************
Moves the parsing buffer data left to the buffer start. */
static
void
recv_sys_justify_left_parsing_buf(void)
/*===================================*/
2235
{
osku's avatar
osku committed
2236
	ut_memmove(recv_sys->buf, recv_sys->buf + recv_sys->recovered_offset,
2237
		   recv_sys->len - recv_sys->recovered_offset);
osku's avatar
osku committed
2238 2239 2240 2241 2242 2243 2244 2245 2246

	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. */
2247
UNIV_INTERN
osku's avatar
osku committed
2248 2249 2250
ibool
recv_scan_log_recs(
/*===============*/
2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268
					/* out: TRUE if limit_lsn has been
					reached, or not able to scan any more
					in this log group */
	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 */
	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 */
2269 2270
	ib_uint64_t	start_lsn,	/* in: buffer start lsn */
	ib_uint64_t*	contiguous_lsn,	/* in/out: it is known that all log
2271 2272
					groups contain contiguous log data up
					to this lsn */
2273
	ib_uint64_t*	group_scanned_lsn)/* out: scanning succeeded up to
2274
					this lsn */
osku's avatar
osku committed
2275
{
2276 2277
	byte*		log_block;
	ulint		no;
2278
	ib_uint64_t	scanned_lsn;
2279 2280 2281 2282 2283
	ibool		finished;
	ulint		data_len;
	ibool		more_data;

	ut_ad(start_lsn % OS_FILE_LOG_BLOCK_SIZE == 0);
osku's avatar
osku committed
2284 2285 2286 2287
	ut_ad(len % OS_FILE_LOG_BLOCK_SIZE == 0);
	ut_ad(len > 0);
	ut_a(apply_automatically <= TRUE);
	ut_a(store_to_hash <= TRUE);
2288

osku's avatar
osku committed
2289
	finished = FALSE;
2290

osku's avatar
osku committed
2291 2292 2293
	log_block = buf;
	scanned_lsn = start_lsn;
	more_data = FALSE;
2294

osku's avatar
osku committed
2295 2296 2297
	while (log_block < buf + len && !finished) {

		no = log_block_get_hdr_no(log_block);
2298
		/*
osku's avatar
osku committed
2299 2300 2301
		fprintf(stderr, "Log block header no %lu\n", no);

		fprintf(stderr, "Scanned lsn no %lu\n",
2302 2303
		log_block_convert_lsn_to_no(scanned_lsn));
		*/
osku's avatar
osku committed
2304
		if (no != log_block_convert_lsn_to_no(scanned_lsn)
2305
		    || !log_block_checksum_is_ok_or_old_format(log_block)) {
osku's avatar
osku committed
2306 2307

			if (no == log_block_convert_lsn_to_no(scanned_lsn)
2308 2309
			    && !log_block_checksum_is_ok_or_old_format(
				    log_block)) {
osku's avatar
osku committed
2310
				fprintf(stderr,
2311
					"InnoDB: Log block no %lu at"
2312
					" lsn %llu has\n"
2313 2314 2315
					"InnoDB: ok header, but checksum field"
					" contains %lu, should be %lu\n",
					(ulong) no,
2316
					scanned_lsn,
2317 2318 2319 2320
					(ulong) log_block_get_checksum(
						log_block),
					(ulong) log_block_calc_checksum(
						log_block));
osku's avatar
osku committed
2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337
			}

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

2338
			if (scanned_lsn > *contiguous_lsn) {
osku's avatar
osku committed
2339 2340 2341 2342 2343 2344 2345
				*contiguous_lsn = scanned_lsn;
			}
		}

		data_len = log_block_get_data_len(log_block);

		if ((store_to_hash || (data_len == OS_FILE_LOG_BLOCK_SIZE))
2346
		    && scanned_lsn + data_len > recv_sys->scanned_lsn
2347 2348 2349 2350 2351 2352
		    && (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)
			> 0x80000000UL)) {
osku's avatar
osku committed
2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364

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

2367
		if (!recv_sys->parse_start_lsn
2368
		    && (log_block_get_first_rec_group(log_block) > 0)) {
osku's avatar
osku committed
2369 2370 2371 2372

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

2373 2374
			recv_sys->parse_start_lsn = scanned_lsn
				+ log_block_get_first_rec_group(log_block);
osku's avatar
osku committed
2375 2376 2377 2378
			recv_sys->scanned_lsn = recv_sys->parse_start_lsn;
			recv_sys->recovered_lsn = recv_sys->parse_start_lsn;
		}

2379
		scanned_lsn += data_len;
osku's avatar
osku committed
2380

2381
		if (scanned_lsn > recv_sys->scanned_lsn) {
osku's avatar
osku committed
2382

2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396
			/* We have found more entries. If this scan is
 			of startup type, we must initiate crash recovery
			environment before parsing these log records. */

			if (recv_log_scan_is_startup_type
			    && !recv_needed_recovery) {

				fprintf(stderr,
					"InnoDB: Log scan progressed"
					" past the checkpoint lsn %llu\n",
					recv_sys->scanned_lsn);
				recv_init_crash_recovery();
			}

osku's avatar
osku committed
2397 2398 2399 2400 2401
			/* We were able to find more log data: add it to the
			parsing buffer if parse_start_lsn is already
			non-zero */

			if (recv_sys->len + 4 * OS_FILE_LOG_BLOCK_SIZE
2402
			    >= RECV_PARSING_BUF_SIZE) {
osku's avatar
osku committed
2403
				fprintf(stderr,
2404 2405 2406
					"InnoDB: Error: log parsing"
					" buffer overflow."
					" Recovery may have failed!\n");
osku's avatar
osku committed
2407 2408 2409 2410

				recv_sys->found_corrupt_log = TRUE;

			} else if (!recv_sys->found_corrupt_log) {
2411 2412
				more_data = recv_sys_add_to_parsing_buf(
					log_block, scanned_lsn);
osku's avatar
osku committed
2413 2414 2415
			}

			recv_sys->scanned_lsn = scanned_lsn;
2416 2417
			recv_sys->scanned_checkpoint_no
				= log_block_get_checkpoint_no(log_block);
osku's avatar
osku committed
2418
		}
2419

osku's avatar
osku committed
2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
		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;

	if (recv_needed_recovery
2432
	    || (recv_is_from_backup && !recv_is_making_a_backup)) {
osku's avatar
osku committed
2433 2434 2435 2436
		recv_scan_print_counter++;

		if (finished || (recv_scan_print_counter % 80 == 0)) {

2437
			fprintf(stderr,
2438
				"InnoDB: Doing recovery: scanned up to"
2439 2440
				" log sequence number %llu\n",
				*group_scanned_lsn);
osku's avatar
osku committed
2441 2442 2443 2444 2445 2446 2447 2448 2449
		}
	}

	if (more_data && !recv_sys->found_corrupt_log) {
		/* Try to parse more log records */

		recv_parse_log_recs(store_to_hash);

		if (store_to_hash && mem_heap_get_size(recv_sys->heap)
2450 2451
		    > available_memory
		    && apply_automatically) {
2452

osku's avatar
osku committed
2453 2454 2455 2456 2457
			/* 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 */
2458

osku's avatar
osku committed
2459
			recv_apply_hashed_log_recs(FALSE);
2460
		}
osku's avatar
osku committed
2461 2462 2463 2464 2465

		if (recv_sys->recovered_offset > RECV_PARSING_BUF_SIZE / 4) {
			/* Move parsing buffer data to the buffer start */

			recv_sys_justify_left_parsing_buf();
2466
		}
osku's avatar
osku committed
2467 2468 2469
	}

	return(finished);
2470
}
osku's avatar
osku committed
2471 2472 2473 2474 2475 2476 2477 2478

/***********************************************************
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(
/*=====================*/
2479
	log_group_t*	group,		/* in: log group */
2480
	ib_uint64_t*	contiguous_lsn,	/* in/out: it is known that all log
2481 2482
					groups contain contiguous log data up
					to this lsn */
2483
	ib_uint64_t*	group_scanned_lsn)/* out: scanning succeeded up to
2484
					this lsn */
osku's avatar
osku committed
2485
{
2486
	ibool		finished;
2487 2488
	ib_uint64_t	start_lsn;
	ib_uint64_t	end_lsn;
2489

osku's avatar
osku committed
2490 2491 2492
	finished = FALSE;

	start_lsn = *contiguous_lsn;
2493 2494

	while (!finished) {
2495
		end_lsn = start_lsn + RECV_SCAN_SIZE;
osku's avatar
osku committed
2496 2497

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

2500
		finished = recv_scan_log_recs(
2501
			TRUE, (buf_pool->curr_size - recv_n_pool_free_frames)
2502 2503
			* UNIV_PAGE_SIZE, TRUE, log_sys->buf, RECV_SCAN_SIZE,
			start_lsn, contiguous_lsn, group_scanned_lsn);
osku's avatar
osku committed
2504 2505 2506 2507 2508 2509
		start_lsn = end_lsn;
	}

#ifdef UNIV_DEBUG
	if (log_debug_writes) {
		fprintf(stderr,
2510
			"InnoDB: Scanned group %lu up to"
2511
			" log sequence number %llu\n",
2512
			(ulong) group->id,
2513
			*group_scanned_lsn);
osku's avatar
osku committed
2514 2515 2516 2517
	}
#endif /* UNIV_DEBUG */
}

2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558
/***********************************************************
Initialize crash recovery environment. Can be called iff
recv_needed_recovery == FALSE. */
static
void
recv_init_crash_recovery(void)
/*==========================*/
{
	ut_a(!recv_needed_recovery);

	recv_needed_recovery = TRUE;

	ut_print_timestamp(stderr);

	fprintf(stderr,
		"  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);
	}
}

osku's avatar
osku committed
2559 2560 2561 2562 2563
/************************************************************
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. */
2564
UNIV_INTERN
osku's avatar
osku committed
2565
ulint
2566 2567
recv_recovery_from_checkpoint_start_func(
/*=====================================*/
2568
					/* out: error code or DB_SUCCESS */
2569
#ifdef UNIV_LOG_ARCHIVE
2570
	ulint		type,		/* in: LOG_CHECKPOINT or LOG_ARCHIVE */
2571
	ib_uint64_t	limit_lsn,	/* in: recover up to this lsn
2572
					if possible */
2573
#endif /* UNIV_LOG_ARCHIVE */
2574
	ib_uint64_t	min_flushed_lsn,/* in: min flushed lsn from
2575
					data files */
2576
	ib_uint64_t	max_flushed_lsn)/* in: max flushed lsn from
2577
					data files */
osku's avatar
osku committed
2578 2579 2580 2581 2582
{
	log_group_t*	group;
	log_group_t*	max_cp_group;
	log_group_t*	up_to_date_group;
	ulint		max_cp_field;
2583 2584 2585 2586 2587 2588
	ib_uint64_t	checkpoint_lsn;
	ib_uint64_t	checkpoint_no;
	ib_uint64_t	old_scanned_lsn;
	ib_uint64_t	group_scanned_lsn;
	ib_uint64_t	contiguous_lsn;
	ib_uint64_t	archived_lsn;
osku's avatar
osku committed
2589 2590 2591 2592
	byte*		buf;
	byte		log_hdr_buf[LOG_FILE_HDR_SIZE];
	ulint		err;

2593
#ifdef UNIV_LOG_ARCHIVE
2594
	ut_ad(type != LOG_CHECKPOINT || limit_lsn == IB_ULONGLONG_MAX);
2595 2596 2597 2598 2599 2600
# define TYPE_CHECKPOINT	(type == LOG_CHECKPOINT)
# define LIMIT_LSN		limit_lsn
#else /* UNIV_LOG_ARCHIVE */
# define TYPE_CHECKPOINT	1
# define LIMIT_LSN		IB_ULONGLONG_MAX
#endif /* UNIV_LOG_ARCHIVE */
2601

2602
	if (TYPE_CHECKPOINT) {
osku's avatar
osku committed
2603 2604 2605 2606 2607 2608
		recv_sys_create();
		recv_sys_init(FALSE, buf_pool_get_curr_size());
	}

	if (srv_force_recovery >= SRV_FORCE_NO_LOG_REDO) {
		fprintf(stderr,
2609
			"InnoDB: The user has set SRV_FORCE_NO_LOG_REDO on\n");
osku's avatar
osku committed
2610
		fprintf(stderr,
2611
			"InnoDB: Skipping log redo\n");
2612

osku's avatar
osku committed
2613 2614 2615 2616 2617
		return(DB_SUCCESS);
	}

	recv_recovery_on = TRUE;

2618
	recv_sys->limit_lsn = LIMIT_LSN;
osku's avatar
osku committed
2619 2620 2621 2622

	mutex_enter(&(log_sys->mutex));

	/* Look for the latest checkpoint from any of the log groups */
2623

osku's avatar
osku committed
2624 2625 2626 2627 2628 2629 2630 2631
	err = recv_find_max_checkpoint(&max_cp_group, &max_cp_field);

	if (err != DB_SUCCESS) {

		mutex_exit(&(log_sys->mutex));

		return(err);
	}
2632

osku's avatar
osku committed
2633 2634 2635 2636
	log_group_read_checkpoint_info(max_cp_group, max_cp_field);

	buf = log_sys->checkpoint_buf;

2637 2638 2639
	checkpoint_lsn = mach_read_ull(buf + LOG_CHECKPOINT_LSN);
	checkpoint_no = mach_read_ull(buf + LOG_CHECKPOINT_NO);
	archived_lsn = mach_read_ull(buf + LOG_CHECKPOINT_ARCHIVED_LSN);
osku's avatar
osku committed
2640 2641 2642

	/* Read the first log file header to print a note if this is
	a recovery from a restored InnoDB Hot Backup */
2643

2644
	fil_io(OS_FILE_READ | OS_FILE_LOG, TRUE, max_cp_group->space_id, 0,
2645 2646
	       0, 0, LOG_FILE_HDR_SIZE,
	       log_hdr_buf, max_cp_group);
osku's avatar
osku committed
2647 2648

	if (0 == ut_memcmp(log_hdr_buf + LOG_FILE_WAS_CREATED_BY_HOT_BACKUP,
2649
			   (byte*)"ibbackup", (sizeof "ibbackup") - 1)) {
osku's avatar
osku committed
2650 2651 2652 2653
		/* This log file was created by ibbackup --restore: print
		a note to the user about it */

		fprintf(stderr,
2654 2655 2656 2657
			"InnoDB: The log file was created by"
			" ibbackup --apply-log at\n"
			"InnoDB: %s\n",
			log_hdr_buf + LOG_FILE_WAS_CREATED_BY_HOT_BACKUP);
osku's avatar
osku committed
2658
		fprintf(stderr,
2659 2660
			"InnoDB: NOTE: the following crash recovery"
			" is part of a normal restore.\n");
2661

osku's avatar
osku committed
2662 2663 2664
		/* Wipe over the label now */

		memset(log_hdr_buf + LOG_FILE_WAS_CREATED_BY_HOT_BACKUP,
2665
		       ' ', 4);
osku's avatar
osku committed
2666 2667
		/* Write to the log file to wipe over the label */
		fil_io(OS_FILE_WRITE | OS_FILE_LOG, TRUE,
2668 2669 2670
		       max_cp_group->space_id, 0,
		       0, 0, OS_FILE_LOG_BLOCK_SIZE,
		       log_hdr_buf, max_cp_group);
osku's avatar
osku committed
2671 2672
	}

2673
#ifdef UNIV_LOG_ARCHIVE
osku's avatar
osku committed
2674 2675 2676 2677
	group = UT_LIST_GET_FIRST(log_sys->log_groups);

	while (group) {
		log_checkpoint_get_nth_group_info(buf, group->id,
2678 2679
						  &(group->archived_file_no),
						  &(group->archived_offset));
osku's avatar
osku committed
2680 2681 2682 2683 2684

		group = UT_LIST_GET_NEXT(log_groups, group);
	}
#endif /* UNIV_LOG_ARCHIVE */

2685
	if (TYPE_CHECKPOINT) {
osku's avatar
osku committed
2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697
		/* 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;

		srv_start_lsn = checkpoint_lsn;
	}

2698 2699
	contiguous_lsn = ut_uint64_align_down(recv_sys->scanned_lsn,
					      OS_FILE_LOG_BLOCK_SIZE);
2700 2701 2702 2703 2704 2705
	if (TYPE_CHECKPOINT) {
		up_to_date_group = max_cp_group;
#ifdef UNIV_LOG_ARCHIVE
	} else {
		ulint	capacity;

2706
		/* Try to recover the remaining part from logs: first from
osku's avatar
osku committed
2707 2708 2709 2710 2711
		the logs of the archived group */

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

2712 2713
		if (recv_sys->scanned_lsn > checkpoint_lsn + capacity
		    || checkpoint_lsn > recv_sys->scanned_lsn + capacity) {
osku's avatar
osku committed
2714 2715 2716 2717 2718 2719 2720 2721

			mutex_exit(&(log_sys->mutex));

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

			return(DB_ERROR);
		}
2722

osku's avatar
osku committed
2723
		recv_group_scan_log_recs(group, &contiguous_lsn,
2724
					 &group_scanned_lsn);
2725
		if (recv_sys->scanned_lsn < checkpoint_lsn) {
osku's avatar
osku committed
2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737

			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;
2738
#endif /* UNIV_LOG_ARCHIVE */
osku's avatar
osku committed
2739 2740 2741 2742 2743 2744
	}

	ut_ad(RECV_SCAN_SIZE <= log_sys->buf_size);

	group = UT_LIST_GET_FIRST(log_sys->log_groups);

2745
#ifdef UNIV_LOG_ARCHIVE
osku's avatar
osku committed
2746 2747
	if ((type == LOG_ARCHIVE) && (group == recv_sys->archive_group)) {
		group = UT_LIST_GET_NEXT(log_groups, group);
2748
	}
2749
#endif /* UNIV_LOG_ARCHIVE */
osku's avatar
osku committed
2750

2751
	/* Set the flag to publish that we are doing startup scan. */
2752
	recv_log_scan_is_startup_type = TYPE_CHECKPOINT;
2753
	while (group) {
osku's avatar
osku committed
2754 2755 2756
		old_scanned_lsn = recv_sys->scanned_lsn;

		recv_group_scan_log_recs(group, &contiguous_lsn,
2757
					 &group_scanned_lsn);
osku's avatar
osku committed
2758
		group->scanned_lsn = group_scanned_lsn;
2759

2760
		if (old_scanned_lsn < group_scanned_lsn) {
osku's avatar
osku committed
2761 2762 2763 2764
			/* We found a more up-to-date group */

			up_to_date_group = group;
		}
2765

2766
#ifdef UNIV_LOG_ARCHIVE
osku's avatar
osku committed
2767
		if ((type == LOG_ARCHIVE)
2768
		    && (group == recv_sys->archive_group)) {
osku's avatar
osku committed
2769
			group = UT_LIST_GET_NEXT(log_groups, group);
2770
		}
2771
#endif /* UNIV_LOG_ARCHIVE */
osku's avatar
osku committed
2772 2773 2774 2775

		group = UT_LIST_GET_NEXT(log_groups, group);
	}

2776 2777
	/* Done with startup scan. Clear the flag. */
	recv_log_scan_is_startup_type = FALSE;
2778
	if (TYPE_CHECKPOINT) {
2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826
		/* NOTE: we always do a 'recovery' at startup, but only if
		there is something wrong we will print a message to the
		user about recovery: */

		if (checkpoint_lsn != max_flushed_lsn
		    || checkpoint_lsn != min_flushed_lsn) {

			if (checkpoint_lsn < max_flushed_lsn) {
				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 %llu, log\n"
					"InnoDB: sequence numbers stamped"
					" to ibdata file headers are between\n"
					"InnoDB: %llu and %llu.\n"
					"InnoDB: #########################"
					"#################################\n",
					checkpoint_lsn,
					min_flushed_lsn,
					max_flushed_lsn);
			}

			if (!recv_needed_recovery) {
				fprintf(stderr,
					"InnoDB: The log sequence number"
					" in ibdata files does not match\n"
					"InnoDB: the log sequence number"
					" in the ib_logfiles!\n");
				recv_init_crash_recovery();
			}
		}

		if (!recv_needed_recovery) {
			/* Init the doublewrite buffer memory structure */
			trx_sys_doublewrite_init_or_restore_pages(FALSE);
		}
	}

osku's avatar
osku committed
2827
	/* We currently have only one log group */
2828
	if (group_scanned_lsn < checkpoint_lsn) {
osku's avatar
osku committed
2829 2830
		ut_print_timestamp(stderr);
		fprintf(stderr,
2831 2832
			"  InnoDB: ERROR: We were only able to scan the log"
			" up to\n"
2833
			"InnoDB: %llu, but a checkpoint was at %llu.\n"
2834 2835
			"InnoDB: It is possible that"
			" the database is now corrupt!\n",
2836 2837
			group_scanned_lsn,
			checkpoint_lsn);
osku's avatar
osku committed
2838 2839
	}

2840
	if (group_scanned_lsn < recv_max_page_lsn) {
osku's avatar
osku committed
2841 2842
		ut_print_timestamp(stderr);
		fprintf(stderr,
2843
			"  InnoDB: ERROR: We were only able to scan the log"
2844 2845
			" up to %llu\n"
			"InnoDB: but a database page a had an lsn %llu."
2846 2847
			" It is possible that the\n"
			"InnoDB: database is now corrupt!\n",
2848 2849
			group_scanned_lsn,
			recv_max_page_lsn);
osku's avatar
osku committed
2850 2851
	}

2852
	if (recv_sys->recovered_lsn < checkpoint_lsn) {
osku's avatar
osku committed
2853 2854 2855

		mutex_exit(&(log_sys->mutex));

2856
		if (recv_sys->recovered_lsn >= LIMIT_LSN) {
osku's avatar
osku committed
2857 2858 2859 2860 2861 2862 2863 2864

			return(DB_SUCCESS);
		}

		ut_error;

		return(DB_ERROR);
	}
2865

osku's avatar
osku committed
2866 2867 2868 2869
	/* 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;
2870
	log_sys->next_checkpoint_no = checkpoint_no + 1;
osku's avatar
osku committed
2871 2872 2873 2874

#ifdef UNIV_LOG_ARCHIVE
	log_sys->archived_lsn = archived_lsn;
#endif /* UNIV_LOG_ARCHIVE */
2875

osku's avatar
osku committed
2876 2877 2878
	recv_synchronize_groups(up_to_date_group);

	if (!recv_needed_recovery) {
2879
		ut_a(checkpoint_lsn == recv_sys->recovered_lsn);
osku's avatar
osku committed
2880 2881 2882
	} else {
		srv_start_lsn = recv_sys->recovered_lsn;
	}
2883

osku's avatar
osku committed
2884 2885 2886 2887
	log_sys->lsn = recv_sys->recovered_lsn;

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

2888
	log_sys->buf_free = (ulint) log_sys->lsn % OS_FILE_LOG_BLOCK_SIZE;
osku's avatar
osku committed
2889 2890 2891 2892 2893
	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;
2894

2895
	log_sys->next_checkpoint_no = checkpoint_no + 1;
2896

osku's avatar
osku committed
2897
#ifdef UNIV_LOG_ARCHIVE
2898
	if (archived_lsn == IB_ULONGLONG_MAX) {
osku's avatar
osku committed
2899 2900 2901 2902 2903 2904

		log_sys->archiving_state = LOG_ARCH_OFF;
	}
#endif /* UNIV_LOG_ARCHIVE */

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

osku's avatar
osku committed
2906 2907
	recv_sys->apply_log_recs = TRUE;

2908 2909
	mutex_exit(&(recv_sys->mutex));

osku's avatar
osku committed
2910 2911 2912 2913 2914 2915 2916 2917 2918
	mutex_exit(&(log_sys->mutex));

	recv_lsn_checks_on = TRUE;

	/* 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);
2919 2920 2921

#undef TYPE_CHECKPOINT
#undef LIMIT_LSN
osku's avatar
osku committed
2922 2923 2924 2925
}

/************************************************************
Completes recovery from a checkpoint. */
2926
UNIV_INTERN
osku's avatar
osku committed
2927 2928 2929 2930
void
recv_recovery_from_checkpoint_finish(void)
/*======================================*/
{
2931
	int		i;
osku's avatar
osku committed
2932 2933

	/* Apply the hashed log records to the respective file pages */
2934

osku's avatar
osku committed
2935 2936 2937 2938 2939 2940 2941 2942
	if (srv_force_recovery < SRV_FORCE_NO_LOG_REDO) {

		recv_apply_hashed_log_recs(TRUE);
	}

#ifdef UNIV_DEBUG
	if (log_debug_writes) {
		fprintf(stderr,
2943
			"InnoDB: Log records applied to the database\n");
osku's avatar
osku committed
2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954
	}
#endif /* UNIV_DEBUG */

	if (recv_needed_recovery) {
		trx_sys_print_mysql_master_log_pos();
		trx_sys_print_mysql_binlog_offset();
	}

	if (recv_sys->found_corrupt_log) {

		fprintf(stderr,
2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965
			"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");
osku's avatar
osku committed
2966 2967 2968 2969 2970 2971 2972 2973 2974
	}

	/* Free the resources of the recovery system */

	recv_recovery_on = FALSE;

#ifndef UNIV_LOG_DEBUG
	recv_sys_free();
#endif
2975

2976 2977 2978
	/* Drop partially created indexes. */
	row_merge_drop_temp_indexes();

2979 2980 2981 2982 2983 2984 2985 2986
#ifdef UNIV_SYNC_DEBUG
	/* Wait for a while so that created threads have time to suspend
	themselves before we switch the latching order checks on */
	os_thread_sleep(1000000);

	/* Switch latching order checks on in sync0sync.c */
	sync_order_checks_on = TRUE;
#endif
osku's avatar
osku committed
2987 2988 2989 2990
	if (srv_force_recovery < SRV_FORCE_NO_TRX_UNDO) {
		/* Rollback the uncommitted transactions which have no user
		session */

2991
		os_thread_create(trx_rollback_or_clean_all_recovered,
2992
				 (void *)&i, NULL);
osku's avatar
osku committed
2993 2994 2995 2996 2997
	}
}

/**********************************************************
Resets the logs. The contents of log files will be lost! */
2998
UNIV_INTERN
osku's avatar
osku committed
2999 3000 3001
void
recv_reset_logs(
/*============*/
3002
	ib_uint64_t	lsn,		/* in: reset to this lsn
3003 3004 3005 3006
					rounded up to be divisible by
					OS_FILE_LOG_BLOCK_SIZE, after
					which we add
					LOG_BLOCK_HDR_SIZE */
osku's avatar
osku committed
3007
#ifdef UNIV_LOG_ARCHIVE
3008
	ulint		arch_log_no,	/* in: next archived log file number */
osku's avatar
osku committed
3009
#endif /* UNIV_LOG_ARCHIVE */
3010 3011 3012 3013
	ibool		new_logs_created)/* in: TRUE if resetting logs
					is done at the log creation;
					FALSE if it is done after
					archive recovery */
osku's avatar
osku committed
3014 3015 3016 3017
{
	log_group_t*	group;

	ut_ad(mutex_own(&(log_sys->mutex)));
3018

3019
	log_sys->lsn = ut_uint64_align_up(lsn, OS_FILE_LOG_BLOCK_SIZE);
osku's avatar
osku committed
3020 3021 3022 3023 3024 3025 3026

	group = UT_LIST_GET_FIRST(log_sys->log_groups);

	while (group) {
		group->lsn = log_sys->lsn;
		group->lsn_offset = LOG_FILE_HDR_SIZE;
#ifdef UNIV_LOG_ARCHIVE
3027
		group->archived_file_no = arch_log_no;
osku's avatar
osku committed
3028 3029 3030 3031 3032
		group->archived_offset = 0;
#endif /* UNIV_LOG_ARCHIVE */

		if (!new_logs_created) {
			recv_truncate_group(group, group->lsn, group->lsn,
3033
					    group->lsn, group->lsn);
osku's avatar
osku committed
3034
		}
3035

osku's avatar
osku committed
3036 3037 3038 3039 3040 3041 3042
		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;

3043 3044
	log_sys->next_checkpoint_no = 0;
	log_sys->last_checkpoint_lsn = 0;
osku's avatar
osku committed
3045 3046 3047 3048

#ifdef UNIV_LOG_ARCHIVE
	log_sys->archived_lsn = log_sys->lsn;
#endif /* UNIV_LOG_ARCHIVE */
3049

osku's avatar
osku committed
3050 3051 3052 3053
	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;
3054
	log_sys->lsn += LOG_BLOCK_HDR_SIZE;
3055

osku's avatar
osku committed
3056 3057 3058
	mutex_exit(&(log_sys->mutex));

	/* Reset the checkpoint fields in logs */
3059

3060 3061
	log_make_checkpoint_at(IB_ULONGLONG_MAX, TRUE);
	log_make_checkpoint_at(IB_ULONGLONG_MAX, TRUE);
3062

osku's avatar
osku committed
3063 3064 3065 3066 3067 3068
	mutex_enter(&(log_sys->mutex));
}

#ifdef UNIV_HOTBACKUP
/**********************************************************
Creates new log files after a backup has been restored. */
3069
UNIV_INTERN
osku's avatar
osku committed
3070 3071 3072 3073 3074 3075
void
recv_reset_log_files_for_backup(
/*============================*/
	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 */
3076
	ib_uint64_t	lsn)		/* in: new start lsn, must be
osku's avatar
osku committed
3077 3078 3079 3080 3081 3082 3083
					divisible by OS_FILE_LOG_BLOCK_SIZE */
{
	os_file_t	log_file;
	ibool		success;
	byte*		buf;
	ulint		i;
	ulint		log_dir_len;
3084 3085
	char		name[5000];
	static const char ib_logfile_basename[] = "ib_logfile";
osku's avatar
osku committed
3086 3087

	log_dir_len = strlen(log_dir);
3088
	/* full path name of ib_logfile consists of log dir path + basename
3089
	+ number. This must fit in the name buffer.
3090 3091
	*/
	ut_a(log_dir_len + strlen(ib_logfile_basename) + 11  < sizeof(name));
osku's avatar
osku committed
3092 3093

	buf = ut_malloc(LOG_FILE_HDR_SIZE + OS_FILE_LOG_BLOCK_SIZE);
3094
	memset(buf, '\0', LOG_FILE_HDR_SIZE + OS_FILE_LOG_BLOCK_SIZE);
osku's avatar
osku committed
3095 3096 3097

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

3098 3099
		sprintf(name, "%s%s%lu", log_dir,
			ib_logfile_basename, (ulong)i);
osku's avatar
osku committed
3100 3101

		log_file = os_file_create_simple(name, OS_FILE_CREATE,
3102
						 OS_FILE_READ_WRITE, &success);
osku's avatar
osku committed
3103 3104
		if (!success) {
			fprintf(stderr,
3105 3106
				"InnoDB: Cannot create %s. Check that"
				" the file does not exist yet.\n", name);
osku's avatar
osku committed
3107 3108 3109 3110 3111 3112 3113 3114 3115 3116

			exit(1);
		}

		fprintf(stderr,
			"Setting log file size to %lu %lu\n",
			(ulong) ut_get_high32(log_file_size),
			(ulong) log_file_size & 0xFFFFFFFFUL);

		success = os_file_set_size(name, log_file,
3117 3118
					   log_file_size & 0xFFFFFFFFUL,
					   ut_get_high32(log_file_size));
osku's avatar
osku committed
3119 3120 3121

		if (!success) {
			fprintf(stderr,
3122 3123 3124
				"InnoDB: Cannot set %s size to %lu %lu\n",
				name, (ulong) ut_get_high32(log_file_size),
				(ulong) (log_file_size & 0xFFFFFFFFUL));
osku's avatar
osku committed
3125 3126 3127 3128 3129 3130 3131 3132
			exit(1);
		}

		os_file_flush(log_file);
		os_file_close(log_file);
	}

	/* We pretend there is a checkpoint at lsn + LOG_BLOCK_HDR_SIZE */
3133

osku's avatar
osku committed
3134
	log_reset_first_header_and_checkpoint(buf, lsn);
3135

osku's avatar
osku committed
3136 3137
	log_block_init_in_old_format(buf + LOG_FILE_HDR_SIZE, lsn);
	log_block_set_first_rec_group(buf + LOG_FILE_HDR_SIZE,
3138
				      LOG_BLOCK_HDR_SIZE);
3139
	sprintf(name, "%s%s%lu", log_dir, ib_logfile_basename, (ulong)0);
osku's avatar
osku committed
3140 3141

	log_file = os_file_create_simple(name, OS_FILE_OPEN,
3142
					 OS_FILE_READ_WRITE, &success);
osku's avatar
osku committed
3143 3144 3145 3146 3147 3148 3149
	if (!success) {
		fprintf(stderr, "InnoDB: Cannot open %s.\n", name);

		exit(1);
	}

	os_file_write(name, log_file, buf, 0, 0,
3150
		      LOG_FILE_HDR_SIZE + OS_FILE_LOG_BLOCK_SIZE);
osku's avatar
osku committed
3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168
	os_file_flush(log_file);
	os_file_close(log_file);

	ut_free(buf);
}
#endif /* UNIV_HOTBACKUP */

#ifdef UNIV_LOG_ARCHIVE
/**********************************************************
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 */
{
3169
	os_file_t	file_handle;
3170 3171 3172 3173
	ib_uint64_t	start_lsn;
	ib_uint64_t	file_end_lsn;
	ib_uint64_t	dummy_lsn;
	ib_uint64_t	scanned_lsn;
3174 3175 3176 3177 3178 3179 3180 3181
	ulint		len;
	ibool		ret;
	byte*		buf;
	ulint		read_offset;
	ulint		file_size;
	ulint		file_size_high;
	int		input_char;
	char		name[10000];
osku's avatar
osku committed
3182 3183 3184

	ut_a(0);

3185
try_open_again:
osku's avatar
osku committed
3186 3187 3188
	buf = log_sys->buf;

	/* Add the file to the archive file space; open the file */
3189

osku's avatar
osku committed
3190 3191 3192
	log_archived_file_name_gen(name, group->id, group->archived_file_no);

	file_handle = os_file_create(name, OS_FILE_OPEN,
3193
				     OS_FILE_LOG, OS_FILE_AIO, &ret);
osku's avatar
osku committed
3194 3195 3196

	if (ret == FALSE) {
ask_again:
3197
		fprintf(stderr,
3198 3199 3200
			"InnoDB: Do you want to copy additional"
			" archived log files\n"
			"InnoDB: to the directory\n");
3201
		fprintf(stderr,
3202 3203
			"InnoDB: or were these all the files needed"
			" in recovery?\n");
3204
		fprintf(stderr,
3205
			"InnoDB: (Y == copy more files; N == this is all)?");
osku's avatar
osku committed
3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223

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

osku's avatar
osku committed
3225
	fprintf(stderr, "InnoDB: Opened archived log file %s\n", name);
3226

osku's avatar
osku committed
3227
	ret = os_file_close(file_handle);
3228

osku's avatar
osku committed
3229 3230 3231
	if (file_size < LOG_FILE_HDR_SIZE) {
		fprintf(stderr,
			"InnoDB: Archive file header incomplete %s\n", name);
3232

osku's avatar
osku committed
3233 3234 3235 3236
		return(TRUE);
	}

	ut_a(ret);
3237

osku's avatar
osku committed
3238
	/* Add the archive file as a node to the space */
3239

osku's avatar
osku committed
3240
	fil_node_create(name, 1 + file_size / UNIV_PAGE_SIZE,
3241
			group->archive_space_id, FALSE);
3242 3243 3244
#if RECV_SCAN_SIZE < LOG_FILE_HDR_SIZE
# error "RECV_SCAN_SIZE < LOG_FILE_HDR_SIZE"
#endif
osku's avatar
osku committed
3245 3246 3247

	/* Read the archive file header */
	fil_io(OS_FILE_READ | OS_FILE_LOG, TRUE, group->archive_space_id, 0, 0,
3248
	       LOG_FILE_HDR_SIZE, buf, NULL);
osku's avatar
osku committed
3249 3250 3251 3252

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

	if (mach_read_from_4(buf + LOG_GROUP_ID) != group->id
3253 3254
	    || mach_read_from_4(buf + LOG_FILE_NO)
	    != group->archived_file_no) {
osku's avatar
osku committed
3255
		fprintf(stderr,
3256
			"InnoDB: Archive file header inconsistent %s\n", name);
3257

osku's avatar
osku committed
3258 3259 3260 3261 3262
		return(TRUE);
	}

	if (!mach_read_from_4(buf + LOG_FILE_ARCH_COMPLETED)) {
		fprintf(stderr,
3263 3264
			"InnoDB: Archive file not completely written %s\n",
			name);
osku's avatar
osku committed
3265 3266 3267

		return(TRUE);
	}
3268

3269 3270
	start_lsn = mach_read_ull(buf + LOG_FILE_START_LSN);
	file_end_lsn = mach_read_ull(buf + LOG_FILE_END_LSN);
osku's avatar
osku committed
3271

3272
	if (!recv_sys->scanned_lsn) {
osku's avatar
osku committed
3273

3274
		if (recv_sys->parse_start_lsn < start_lsn) {
3275
			fprintf(stderr,
3276 3277
				"InnoDB: Archive log file %s"
				" starts from too big a lsn\n",
3278
				name);
osku's avatar
osku committed
3279 3280
			return(TRUE);
		}
3281

osku's avatar
osku committed
3282 3283
		recv_sys->scanned_lsn = start_lsn;
	}
3284

3285
	if (recv_sys->scanned_lsn != start_lsn) {
osku's avatar
osku committed
3286 3287

		fprintf(stderr,
3288 3289 3290
			"InnoDB: Archive log file %s starts from"
			" a wrong lsn\n",
			name);
osku's avatar
osku committed
3291 3292 3293 3294
		return(TRUE);
	}

	read_offset = LOG_FILE_HDR_SIZE;
3295

osku's avatar
osku committed
3296 3297 3298 3299 3300
	for (;;) {
		len = RECV_SCAN_SIZE;

		if (read_offset + len > file_size) {
			len = ut_calc_align_down(file_size - read_offset,
3301
						 OS_FILE_LOG_BLOCK_SIZE);
osku's avatar
osku committed
3302 3303 3304 3305 3306 3307
		}

		if (len == 0) {

			break;
		}
3308

osku's avatar
osku committed
3309 3310
#ifdef UNIV_DEBUG
		if (log_debug_writes) {
3311
			fprintf(stderr,
3312
				"InnoDB: Archive read starting at"
3313 3314
				" lsn %llu, len %lu from file %s\n",
				start_lsn,
3315
				(ulong) len, name);
osku's avatar
osku committed
3316 3317 3318 3319
		}
#endif /* UNIV_DEBUG */

		fil_io(OS_FILE_READ | OS_FILE_LOG, TRUE,
3320 3321
		       group->archive_space_id, read_offset / UNIV_PAGE_SIZE,
		       read_offset % UNIV_PAGE_SIZE, len, buf, NULL);
osku's avatar
osku committed
3322

3323 3324 3325 3326
		ret = recv_scan_log_recs(
			TRUE, (buf_pool->n_frames - recv_n_pool_free_frames)
			* UNIV_PAGE_SIZE, TRUE, buf, len, start_lsn,
			&dummy_lsn, &scanned_lsn);
osku's avatar
osku committed
3327

3328
		if (scanned_lsn == file_end_lsn) {
osku's avatar
osku committed
3329 3330 3331 3332 3333 3334

			return(FALSE);
		}

		if (ret) {
			fprintf(stderr,
3335 3336
				"InnoDB: Archive log file %s"
				" does not scan right\n",
3337
				name);
osku's avatar
osku committed
3338 3339
			return(TRUE);
		}
3340

osku's avatar
osku committed
3341
		read_offset += len;
3342
		start_lsn += len;
osku's avatar
osku committed
3343

3344
		ut_ad(start_lsn == scanned_lsn);
osku's avatar
osku committed
3345 3346 3347 3348 3349 3350 3351
	}

	return(FALSE);
}

/************************************************************
Recovers from archived log files, and also from log files, if they exist. */
3352
UNIV_INTERN
osku's avatar
osku committed
3353 3354 3355
ulint
recv_recovery_from_archive_start(
/*=============================*/
3356
					/* out: error code or DB_SUCCESS */
3357
	ib_uint64_t	min_flushed_lsn,/* in: min flushed lsn field from the
3358
					data files */
3359
	ib_uint64_t	limit_lsn,	/* in: recover up to this lsn if
3360 3361 3362 3363 3364 3365
					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 */
osku's avatar
osku committed
3366 3367 3368 3369 3370 3371
{
	log_group_t*	group;
	ulint		group_id;
	ulint		trunc_len;
	ibool		ret;
	ulint		err;
3372

osku's avatar
osku committed
3373 3374 3375 3376
	ut_a(0);

	recv_sys_create();
	recv_sys_init(FALSE, buf_pool_get_curr_size());
3377

osku's avatar
osku committed
3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389
	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) {

3390
			break;
osku's avatar
osku committed
3391
		}
3392

osku's avatar
osku committed
3393 3394 3395 3396 3397
		group = UT_LIST_GET_NEXT(log_groups, group);
	}

	if (!group) {
		fprintf(stderr,
3398 3399
			"InnoDB: There is no log group defined with id %lu!\n",
			(ulong) group_id);
osku's avatar
osku committed
3400 3401 3402 3403 3404 3405 3406
		return(DB_ERROR);
	}

	group->archived_file_no = first_log_no;

	recv_sys->parse_start_lsn = min_flushed_lsn;

3407
	recv_sys->scanned_lsn = 0;
osku's avatar
osku committed
3408 3409 3410 3411 3412 3413
	recv_sys->scanned_checkpoint_no = 0;
	recv_sys->recovered_lsn = recv_sys->parse_start_lsn;

	recv_sys->archive_group = group;

	ret = FALSE;
3414

osku's avatar
osku committed
3415 3416 3417 3418 3419 3420 3421
	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 */
3422

osku's avatar
osku committed
3423
		trunc_len = UNIV_PAGE_SIZE
3424
			* fil_space_get_size(group->archive_space_id);
osku's avatar
osku committed
3425 3426
		if (trunc_len > 0) {
			fil_space_truncate_start(group->archive_space_id,
3427
						 trunc_len);
osku's avatar
osku committed
3428 3429 3430 3431 3432
		}

		group->archived_file_no++;
	}

3433
	if (recv_sys->recovered_lsn < limit_lsn) {
osku's avatar
osku committed
3434

3435
		if (!recv_sys->scanned_lsn) {
osku's avatar
osku committed
3436 3437 3438 3439 3440 3441 3442

			recv_sys->scanned_lsn = recv_sys->parse_start_lsn;
		}

		mutex_exit(&(log_sys->mutex));

		err = recv_recovery_from_checkpoint_start(LOG_ARCHIVE,
3443
							  limit_lsn,
3444 3445
							  IB_ULONGLONG_MAX,
							  IB_ULONGLONG_MAX);
osku's avatar
osku committed
3446 3447 3448 3449 3450 3451 3452 3453
		if (err != DB_SUCCESS) {

			return(err);
		}

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

3454
	if (limit_lsn != IB_ULONGLONG_MAX) {
osku's avatar
osku committed
3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467

		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. */
3468
UNIV_INTERN
osku's avatar
osku committed
3469 3470 3471 3472 3473 3474 3475 3476 3477
void
recv_recovery_from_archive_finish(void)
/*===================================*/
{
	recv_recovery_from_checkpoint_finish();

	recv_recovery_from_backup_on = FALSE;
}
#endif /* UNIV_LOG_ARCHIVE */