innochecksum.cc 54.1 KB
Newer Older
1
/*
2
   Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
3
   Copyright (c) 2014, 2021, MariaDB Corporation.
4 5 6 7 8 9 10 11 12 13 14 15

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
16
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
*/

/*
  InnoDB offline file checksum utility.  85% of the code in this utility
  is included from the InnoDB codebase.

  The final 15% was originally written by Mark Smith of Danga
  Interactive, Inc. <junior@danga.com>

  Published with a permission.
*/

#include <my_global.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
35
#ifndef __WIN__
36 37 38 39
# include <unistd.h>
#endif
#include <my_getopt.h>
#include <m_string.h>
40
#include <welcome_copyright_notice.h> /* ORACLE_WELCOME_COPYRIGHT_NOTICE */
41 42 43 44

/* Only parts of these files are included from the InnoDB codebase.
The parts not included are excluded by #ifndef UNIV_INNOCHECKSUM. */

45 46 47 48 49 50 51 52 53 54 55
typedef void fil_space_t;

#include "page0size.h"

#define FLST_BASE_NODE_SIZE (4 + 2 * FIL_ADDR_SIZE)
#define FLST_NODE_SIZE (2 * FIL_ADDR_SIZE)
#define FSEG_PAGE_DATA FIL_PAGE_DATA
#define FSEG_HEADER_SIZE	10
#define UT_BITS_IN_BYTES(b) (((b) + 7) / 8)

#include "ut0ut.h"
56
#include "ut0byte.h"
57
#include "mtr0types.h"
58
#include "mach0data.h"
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#include "fsp0types.h"
#include "rem0rec.h"
#include "buf0checksum.h"        /* buf_calc_page_*() */
#include "buf0buf.h"             /* buf_page_is_corrupted */
#include "fil0fil.h"             /* FIL_* */
#include "page0page.h"           /* PAGE_* */
#include "page0zip.h"            /* page_zip_*() */
#include "trx0undo.h"            /* TRX_* */
#include "fsp0fsp.h"             /* fsp_flags_get_page_size() &
                                    fsp_flags_get_zip_size() */
#include "ut0crc32.h"            /* ut_crc32_init() */
#include "fsp0pagecompress.h"    /* fil_get_compression_alg_name */
#include "fil0crypt.h"           /* fil_space_verify_crypt_checksum */

#include <string.h>

#ifdef UNIV_NONINL
76 77 78
# include "fsp0fsp.inl"
# include "mach0data.inl"
# include "ut0rnd.inl"
79
#endif
80

81 82 83 84
#ifndef PRIuMAX
#define PRIuMAX   "llu"
#endif

85
/* Global variables */
86 87
static bool			verbose;
static bool			just_count;
88 89 90
static uint32_t			start_page;
static uint32_t			end_page;
static uint32_t			do_page;
91 92
static bool			use_end_page;
static bool			do_one_page;
93 94
static my_bool do_leaf;
static my_bool per_page_details;
95
static ulint n_merge;
96
extern ulong			srv_checksum_algorithm;
97 98 99 100
static ulong physical_page_size;  /* Page size in bytes on disk. */
static ulong logical_page_size;   /* Page size when uncompressed. */
ulong srv_page_size;
page_size_t			univ_page_size(0, 0, false);
101
/* Current page number (0 based). */
102
uint32_t		cur_page_num;
103
/* Current space. */
104
uint32_t		cur_space;
105 106 107
/* Skip the checksum verification. */
static bool			no_check;
/* Enabled for strict checksum verification. */
108
bool				strict_verify = 0;
109 110 111
/* Enabled for rewrite checksum. */
static bool			do_write;
/* Mismatches count allowed (0 by default). */
112
static unsigned long long     	allow_mismatches=0;
113 114 115 116 117 118 119 120 121 122 123 124
static bool			page_type_summary;
static bool			page_type_dump;
/* Store filename for page-type-dump option. */
char*				page_dump_filename = 0;
/* skip the checksum verification & rewrite if page is doublewrite buffer. */
static bool			skip_page = 0;
const char			*dbug_setting = "FALSE";
char*				log_filename = NULL;
/* User defined filename for logging. */
FILE*				log_file = NULL;
/* Enabled for log write option. */
static bool			is_log_enabled = false;
125

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
#ifndef _WIN32
/* advisory lock for non-window system. */
struct flock			lk;
#endif /* _WIN32 */

/* Strict check algorithm name. */
static ulong			strict_check;
/* Rewrite checksum algorithm name. */
static ulong			write_check;

/* Innodb page type. */
struct innodb_page_type {
	int n_undo_state_active;
	int n_undo_state_cached;
	int n_undo_state_to_free;
	int n_undo_state_to_purge;
	int n_undo_state_prepared;
	int n_undo_state_other;
	int n_undo_insert;
	int n_undo_update;
	int n_undo_other;
	int n_fil_page_index;
	int n_fil_page_undo_log;
	int n_fil_page_inode;
	int n_fil_page_ibuf_free_list;
	int n_fil_page_ibuf_bitmap;
	int n_fil_page_type_sys;
	int n_fil_page_type_trx_sys;
	int n_fil_page_type_fsp_hdr;
	int n_fil_page_type_allocated;
	int n_fil_page_type_xdes;
	int n_fil_page_type_blob;
	int n_fil_page_type_zblob;
	int n_fil_page_type_other;
	int n_fil_page_type_zblob2;
161 162
	int n_fil_page_type_page_compressed;
	int n_fil_page_type_page_compressed_encrypted;
163 164 165 166 167 168 169 170 171 172 173 174
} page_type;

/* Possible values for "--strict-check" for strictly verify checksum
and "--write" for rewrite checksum. */
static const char *innochecksum_algorithms[] = {
	"crc32",
	"crc32",
	"innodb",
	"innodb",
	"none",
	"none",
	NullS
175 176
};

177 178 179 180
/* Used to define an enumerate type of the "innochecksum algorithm". */
static TYPELIB innochecksum_algorithms_typelib = {
	array_elements(innochecksum_algorithms)-1,"",
	innochecksum_algorithms, NULL
181 182
};

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
#define SIZE_RANGES_FOR_PAGE 10
#define NUM_RETRIES 3
#define DEFAULT_RETRY_DELAY 1000000

struct per_page_stats {
  ulint n_recs;
  ulint data_size;
  ulint left_page_no;
  ulint right_page_no;
  per_page_stats(ulint n, ulint data, ulint left, ulint right) :
      n_recs(n), data_size(data), left_page_no(left), right_page_no(right) {}
  per_page_stats() : n_recs(0), data_size(0), left_page_no(0), right_page_no(0) {}
};

struct per_index_stats {
  unsigned long long pages;
  unsigned long long leaf_pages;
  ulint first_leaf_page;
  ulint count;
  ulint free_pages;
  ulint max_data_size;
  unsigned long long total_n_recs;
  unsigned long long total_data_bytes;

  /*!< first element for empty pages,
  last element for pages with more than logical_page_size */
  unsigned long long pages_in_size_range[SIZE_RANGES_FOR_PAGE+2];

  std::map<unsigned long long, per_page_stats> leaves;

  per_index_stats():pages(0), leaf_pages(0), first_leaf_page(0),
                    count(0), free_pages(0), max_data_size(0), total_n_recs(0),
                    total_data_bytes(0)
  {
    memset(pages_in_size_range, 0, sizeof(pages_in_size_range));
  }
};

std::map<unsigned long long, per_index_stats> index_ids;

void print_index_leaf_stats(
	unsigned long long id,
	const per_index_stats& index,
	FILE*	fil_out)

{
	ulint page_no = index.first_leaf_page;
	std::map<unsigned long long, per_page_stats>::const_iterator it_page = index.leaves.find(page_no);
	fprintf(fil_out, "\nindex: %llu leaf page stats: n_pages = %llu\n",
		id, index.leaf_pages);
	fprintf(fil_out, "page_no\tdata_size\tn_recs\n");
	while (it_page != index.leaves.end()) {
		const per_page_stats& stat = it_page->second;
236
		fprintf(fil_out, "%llu\t" ULINTPF "\t" ULINTPF "\n", it_page->first, stat.data_size, stat.n_recs);
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
		page_no = stat.right_page_no;
		it_page = index.leaves.find(page_no);
	}
}

void defrag_analysis(
	unsigned long long id,
	const per_index_stats& index,
	FILE*	fil_out)
{
	// TODO: make it work for compressed pages too
	std::map<unsigned long long, per_page_stats>::const_iterator it = index.leaves.find(index.first_leaf_page);
	ulint n_pages = 0;
	ulint n_leaf_pages = 0;
	while (it != index.leaves.end()) {
		ulint data_size_total = 0;
		for (ulong i = 0; i < n_merge; i++) {
			const per_page_stats& stat = it->second;
			n_leaf_pages ++;
			data_size_total += stat.data_size;
			it = index.leaves.find(stat.right_page_no);
			if (it == index.leaves.end()) {
				break;
			}
		}
262

263 264 265 266 267 268 269 270 271
		if (index.max_data_size) {
			n_pages += data_size_total / index.max_data_size;
			if (data_size_total % index.max_data_size != 0) {
				n_pages += 1;
			}
		}
	}

	if (index.leaf_pages) {
272 273 274 275 276
		fprintf(fil_out, "count = " ULINTPF " free = " ULINTPF "\n", index.count, index.free_pages);
	}

	if (!n_leaf_pages) {
		n_leaf_pages = 1;
277 278
	}

279
	fprintf(fil_out, "%llu\t\t%llu\t\t" ULINTPF "\t\t" ULINTPF "\t\t" ULINTPF "\t\t%.2f\t" ULINTPF "\n",
280
		id, index.leaf_pages, n_leaf_pages, n_merge, n_pages,
281
		1.0 - (double)n_pages / (double)n_leaf_pages, index.max_data_size);
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
}

void print_leaf_stats(
	FILE*	fil_out)
{
	fprintf(fil_out, "\n**************************************************\n");
	fprintf(fil_out, "index_id\t#leaf_pages\t#actual_leaf_pages\tn_merge\t"
		"#leaf_after_merge\tdefrag\n");
	for (std::map<unsigned long long, per_index_stats>::const_iterator it = index_ids.begin();
	     it != index_ids.end(); it++) {
		const per_index_stats& index = it->second;

		if (verbose) {
			print_index_leaf_stats(it->first, index, fil_out);
		}

		if (n_merge) {
			defrag_analysis(it->first, index, fil_out);
		}
	}
}

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
/** Get the page size of the filespace from the filespace header.
@param[in]	buf	buffer used to read the page.
@return page size */
static
const page_size_t
get_page_size(
	byte*	buf)
{
	const ulint	flags = mach_read_from_4(buf + FIL_PAGE_DATA
						 + FSP_SPACE_FLAGS);

	const ulint	ssize = FSP_FLAGS_GET_PAGE_SSIZE(flags);

	if (ssize == 0) {
		srv_page_size = UNIV_PAGE_SIZE_ORIG;
	} else {
		srv_page_size = ((UNIV_ZIP_SIZE_MIN >> 1) << ssize);
	}
322

323 324
	univ_page_size.copy_from(
		page_size_t(srv_page_size, srv_page_size, false));
325

326 327 328 329 330 331
	return(page_size_t(flags));
}

#ifdef _WIN32
/***********************************************//*
 @param		[in] error	error no. from the getLastError().
332

333 334 335 336 337 338 339 340 341 342 343
 @retval error message corresponding to error no.
*/
static
char*
error_message(
	int	error)
{
	static char err_msg[1024] = {'\0'};
	FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
		NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR)err_msg, sizeof(err_msg), NULL );
344

345 346 347
	return (err_msg);
}
#endif /* _WIN32 */
348

349 350 351 352
/***********************************************//*
 @param>>_______[in] name>_____name of file.
 @retval file pointer; file pointer is NULL when error occured.
*/
353

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
FILE*
open_file(
	const char*	name)
{
	int	fd;		/* file descriptor. */
	FILE*	fil_in;
#ifdef _WIN32
	HANDLE		hFile;		/* handle to open file. */
	DWORD		access;		/* define access control */
	int		flags = 0;	/* define the mode for file
					descriptor */

	if (do_write) {
		access =  GENERIC_READ | GENERIC_WRITE;
		flags =  _O_RDWR | _O_BINARY;
	} else {
		access = GENERIC_READ;
		flags = _O_RDONLY | _O_BINARY;
	}
373

374 375 376 377 378 379 380 381
	/* CreateFile() also provide advisory lock with the usage of
	access and share mode of the file.*/
	hFile = CreateFile(
			(LPCTSTR) name, access, 0L, NULL,
			OPEN_EXISTING, NULL, NULL);

	if (hFile == INVALID_HANDLE_VALUE) {
		/* print the error message. */
382
		fprintf(stderr, "Filename::%s %s\n", name,
383 384 385 386 387 388 389 390 391 392 393 394 395 396
			error_message(GetLastError()));

			return (NULL);
		}

	/* get the file descriptor. */
	fd= _open_osfhandle((intptr_t)hFile, flags);
#else /* _WIN32 */

	int	create_flag;
	/* define the advisory lock and open file mode. */
	if (do_write) {
		create_flag = O_RDWR;
		lk.l_type = F_WRLCK;
397
	} else {
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
		create_flag = O_RDONLY;
		lk.l_type = F_RDLCK;
	}

	fd = open(name, create_flag);

	lk.l_whence = SEEK_SET;
	lk.l_start = lk.l_len = 0;

	if (fcntl(fd, F_SETLK, &lk) == -1) {
		fprintf(stderr, "Error: Unable to lock file::"
			" %s\n", name);
		perror("fcntl");
		return (NULL);
	}
#endif /* _WIN32 */
414

415 416 417 418 419
	if (do_write) {
		fil_in = fdopen(fd, "rb+");
	} else {
		fil_in = fdopen(fd, "rb");
	}
420

421
	return (fil_in);
422 423
}

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
/************************************************************//*
 Read the content of file

 @param  [in,out]	buf			read the file in buffer
 @param  [in]		partial_page_read	enable when to read the
						remaining buffer for first page.
 @param  [in]		physical_page_size	Physical/Commpressed page size.
 @param  [in,out]	fil_in			file pointer created for the
						tablespace.
 @retval no. of bytes read.
*/
ulong read_file(
	byte*	buf,
	bool	partial_page_read,
	ulong	physical_page_size,
	FILE*	fil_in)
{
	ulong bytes = 0;
442

443
	DBUG_ASSERT(physical_page_size >= UNIV_ZIP_SIZE_MIN);
444

445 446 447 448 449 450 451 452 453 454 455
	if (partial_page_read) {
		buf += UNIV_ZIP_SIZE_MIN;
		physical_page_size -= UNIV_ZIP_SIZE_MIN;
		bytes = UNIV_ZIP_SIZE_MIN;
	}

	bytes += ulong(fread(buf, 1, physical_page_size, fil_in));

	return bytes;
}

456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
/** Check whether the page contains all zeroes.
@param[in]	buf	page
@param[in]	size	physical size of the page
@return true if the page is all zeroes; else false */
static bool is_page_all_zeroes(
	byte*	buf,
	ulint	size)
{
	/* On pages that are not all zero, the page number
	must match. */
	const ulint* p = reinterpret_cast<const ulint*>(buf);
	const ulint* const end = reinterpret_cast<const ulint*>(buf + size);
	do {
		if (*p++) {
			return false;
		}
	} while (p != end);

	return true;
}

477 478 479
/** Check if page is corrupted or not.
@param[in]	buf		page frame
@param[in]	page_size	page size
480 481 482 483
@param[in]	is_encrypted	true if page0 contained cryp_data
				with crypt_scheme encrypted
@param[in]	is_compressed	true if page0 fsp_flags contained
				page compression flag
484 485 486 487
@retval true if page is corrupted otherwise false. */
static
bool
is_page_corrupted(
488
	byte*			buf,
489
	const page_size_t&	page_size,
490 491
	bool			is_encrypted,
	bool			is_compressed)
492
{
493 494 495 496

	/* enable if page is corrupted. */
	bool is_corrupted;
	/* use to store LSN values. */
497 498
	uint32_t logseq;
	uint32_t logseqfield;
499
	ulint page_type = mach_read_from_2(buf+FIL_PAGE_TYPE);
500 501
	uint32_t key_version = mach_read_from_4(buf+FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION);
	uint32_t space_id = mach_read_from_4(
502 503
		buf + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);

504 505 506 507 508 509 510 511 512 513
	if (mach_read_from_4(buf + FIL_PAGE_OFFSET) != cur_page_num
	    || space_id != cur_space) {
		/* On pages that are not all zero, the page number
		must match. */
		if (is_page_all_zeroes(buf, page_size.physical())) {
			return false;
		}

		if (is_log_enabled) {
			fprintf(log_file,
514 515
				"page id mismatch space::" UINT32PF
				" page::" UINT32PF " \n",
516 517 518 519 520 521
				space_id, cur_page_num);
		}

		return true;
	}

522 523 524 525 526 527 528 529 530
	/* We can't trust only a page type, thus we take account
	also fsp_flags or crypt_data on page 0 */
	if ((page_type == FIL_PAGE_PAGE_COMPRESSED && is_compressed) ||
	    (page_type == FIL_PAGE_PAGE_COMPRESSED_ENCRYPTED &&
	     is_compressed && is_encrypted)) {
		/* Page compressed tables do not contain post compression
		checksum. */
		return (false);
	}
531

532
	if (page_size.is_compressed()) {
533 534 535 536 537 538 539 540 541
		/* check the stored log sequence numbers
		for uncompressed tablespace. */
		logseq = mach_read_from_4(buf + FIL_PAGE_LSN + 4);
		logseqfield = mach_read_from_4(
				buf + page_size.logical() -
				FIL_PAGE_END_LSN_OLD_CHKSUM + 4);

		if (is_log_enabled) {
			fprintf(log_file,
542 543 544
				"space::" UINT32PF " page::" UINT32PF
				"; log sequence number:first = " UINT32PF
				"; second = " UINT32PF "\n",
545
				space_id, cur_page_num, logseq, logseqfield);
546 547
			if (logseq != logseqfield) {
				fprintf(log_file,
548 549
					"Fail; space::" UINT32PF
					" page::" UINT32PF
550
					" invalid (fails log "
551
					"sequence number check)\n",
552
					space_id, cur_page_num);
553 554 555 556
			}
		}
	}

557 558 559
	/* Again we can't trust only FIL_PAGE_FILE_FLUSH_LSN field
	now repurposed as FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION,
	we need to check also crypt_data contents.
560

561
	If page is encrypted, use different checksum calculation
562 563 564
	as innochecksum can't decrypt pages. Note that some old InnoDB
	versions did not initialize FIL_PAGE_FILE_FLUSH_LSN field
	so if crypt checksum does not match we verify checksum using
565 566 567
	normal method. */
	if (is_encrypted && key_version != 0) {
		is_corrupted = !fil_space_verify_crypt_checksum(buf,
Marko Mäkelä's avatar
Marko Mäkelä committed
568
								page_size);
569 570
		if (is_corrupted && log_file) {
			fprintf(log_file,
571 572 573
				"[page id: space=" UINT32PF
				", page_number=" UINT32PF "] may be corrupted;"
				" key_version=" UINT32PF "\n",
574 575 576 577 578
				space_id, cur_page_num,
				mach_read_from_4(
					FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION
					+ buf));
		}
579 580 581 582 583 584
	} else {
		is_corrupted = true;
	}

	if (is_corrupted) {
		is_corrupted = buf_page_is_corrupted(
585
			true, buf, page_size, NULL);
586
	}
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628

	return(is_corrupted);
}

/********************************************//*
 Check if page is doublewrite buffer or not.
 @param [in] page	buffer page

 @retval true  if page is doublewrite buffer otherwise false.
*/
static
bool
is_page_doublewritebuffer(
	const byte*	page)
{
	if ((cur_page_num >= FSP_EXTENT_SIZE)
		&& (cur_page_num < FSP_EXTENT_SIZE * 3)) {
		/* page is doublewrite buffer. */
		return (true);
	}

	return (false);
}

/*******************************************************//*
Check if page is empty or not.
 @param		[in] page		page to checked for empty.
 @param		[in] len	size of page.

 @retval true if page is empty.
 @retval false if page is not empty.
*/
static
bool
is_page_empty(
	const byte*	page,
	size_t		len)
{
	while (len--) {
		if (*page++) {
			return (false);
		}
629 630 631
	}

	return (true);
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
}

/********************************************************************//**
Rewrite the checksum for the page.
@param	[in/out] page			page buffer
@param	[in] physical_page_size		page size in bytes on disk.
@param	[in] iscompressed		Is compressed/Uncompressed Page.

@retval true  : do rewrite
@retval false : skip the rewrite as checksum stored match with
		calculated or page is doublwrite buffer.
*/

bool
update_checksum(
	byte*	page,
	ulong	physical_page_size,
	bool	iscompressed)
{
	ib_uint32_t	checksum = 0;
	byte		stored1[4];	/* get FIL_PAGE_SPACE_OR_CHKSUM field checksum */
	byte		stored2[4];	/* get FIL_PAGE_END_LSN_OLD_CHKSUM field checksum */

	ut_ad(page);
	/* If page is doublewrite buffer, skip the rewrite of checksum. */
	if (skip_page) {
		return (false);
	}

	memcpy(stored1, page + FIL_PAGE_SPACE_OR_CHKSUM, 4);
	memcpy(stored2, page + physical_page_size -
	       FIL_PAGE_END_LSN_OLD_CHKSUM, 4);

	/* Check if page is empty, exclude the checksum field */
	if (is_page_empty(page + 4, physical_page_size - 12)
	    && is_page_empty(page + physical_page_size - 4, 4)) {

		memset(page + FIL_PAGE_SPACE_OR_CHKSUM, 0, 4);
		memset(page + physical_page_size -
		       FIL_PAGE_END_LSN_OLD_CHKSUM, 0, 4);

		goto func_exit;
	}

	if (iscompressed) {
		/* page is compressed */
		checksum = page_zip_calc_checksum(
			page, physical_page_size,
			static_cast<srv_checksum_algorithm_t>(write_check));

		mach_write_to_4(page + FIL_PAGE_SPACE_OR_CHKSUM, checksum);
		if (is_log_enabled) {
684 685
			fprintf(log_file, "page::" UINT32PF "; Updated checksum ="
				" " UINT32PF "\n", cur_page_num, checksum);
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
		}

	} else {
		/* page is uncompressed. */

		/* Store the new formula checksum */
		switch ((srv_checksum_algorithm_t) write_check) {

		case SRV_CHECKSUM_ALGORITHM_CRC32:
		case SRV_CHECKSUM_ALGORITHM_STRICT_CRC32:
			checksum = buf_calc_page_crc32(page);
			break;

		case SRV_CHECKSUM_ALGORITHM_INNODB:
		case SRV_CHECKSUM_ALGORITHM_STRICT_INNODB:
			checksum = (ib_uint32_t)
					buf_calc_page_new_checksum(page);
			break;

		case SRV_CHECKSUM_ALGORITHM_NONE:
		case SRV_CHECKSUM_ALGORITHM_STRICT_NONE:
			checksum = BUF_NO_CHECKSUM_MAGIC;
			break;
		/* no default so the compiler will emit a warning if new
		enum is added and not handled here */
		}

		mach_write_to_4(page + FIL_PAGE_SPACE_OR_CHKSUM, checksum);
		if (is_log_enabled) {
715 716
			fprintf(log_file, "page::" UINT32PF "; Updated checksum field1"
				" = " UINT32PF "\n", cur_page_num, checksum);
717 718 719 720 721 722 723 724 725 726 727 728
		}

		if (write_check == SRV_CHECKSUM_ALGORITHM_STRICT_INNODB
		    || write_check == SRV_CHECKSUM_ALGORITHM_INNODB) {
			checksum = (ib_uint32_t)
					buf_calc_page_old_checksum(page);
		}

		mach_write_to_4(page + physical_page_size -
				FIL_PAGE_END_LSN_OLD_CHKSUM,checksum);

		if (is_log_enabled) {
729 730
			fprintf(log_file, "page::" UINT32PF "; Updated checksum "
				"field2 = " UINT32PF "\n", cur_page_num, checksum);
731 732 733 734
		}

	}

735
func_exit:
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
	/* The following code is to check the stored checksum with the
	calculated checksum. If it matches, then return FALSE to skip
	the rewrite of checksum, otherwise return TRUE. */
	if (iscompressed) {
		if (!memcmp(stored1, page + FIL_PAGE_SPACE_OR_CHKSUM, 4)) {
			return (false);
		}
		return (true);
	}

	if (!memcmp(stored1, page + FIL_PAGE_SPACE_OR_CHKSUM, 4)
	    && !memcmp(stored2, page + physical_page_size -
		       FIL_PAGE_END_LSN_OLD_CHKSUM, 4)) {
		return (false);

	}

	return (true);
}

/**
 Write the content to the file
@param[in]		filename	name of the file.
@param[in,out]		file		file pointer where content
					have to be written
@param[in]		buf		file buffer read
@param[in]		compressed	Enabled if tablespace is
					compressed.
@param[in,out]		pos		current file position.
@param[in]		page_size	page size in bytes on disk.

@retval true	if successfully written
@retval false	if a non-recoverable error occurred
*/
static
bool
write_file(
	const char*	filename,
	FILE*		file,
	byte*		buf,
	bool		compressed,
	fpos_t*		pos,
	ulong		page_size)
{
	bool	do_update;

	do_update = update_checksum(buf, page_size, compressed);

	if (file != stdin) {
		if (do_update) {
			/* Set the previous file pointer position
			saved in pos to current file position. */
			if (0 != fsetpos(file, pos)) {
				perror("fsetpos");
				return(false);
			}
		} else {
			/* Store the current file position in pos */
			if (0 != fgetpos(file, pos)) {
				perror("fgetpos");
				return(false);
			}
			return(true);
		}
	}

	if (page_size
		!= fwrite(buf, 1, page_size, file == stdin ? stdout : file)) {
804
		fprintf(stderr, "Failed to write page::" UINT32PF " to %s: %s\n",
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
			cur_page_num, filename, strerror(errno));

		return(false);
	}
	if (file != stdin) {
		fflush(file);
		/* Store the current file position in pos */
		if (0 != fgetpos(file, pos)) {
			perror("fgetpos");
			return(false);
		}
	}

	return(true);
}

821
// checks using current xdes page whether the page is free
822 823
static inline bool is_page_free(const byte *xdes, page_size_t page_size,
                                uint32_t page_no)
824 825 826 827 828 829 830
{
  const byte *des=
      xdes + XDES_ARR_OFFSET +
      XDES_SIZE * ((page_no & (page_size.physical() - 1)) / FSP_EXTENT_SIZE);
  return xdes_get_bit(des, XDES_FREE_BIT, page_no % FSP_EXTENT_SIZE);
}

831 832 833
/*
Parse the page and collect/dump the information about page type
@param [in] page	buffer page
834
@param [out] xdes	extend descriptor page
835
@param [in] file	file for diagnosis.
836 837
@param [in] page_size	page_size
@param [in] is_encrypted  tablespace is encrypted
838 839 840 841
*/
void
parse_page(
	const byte*	page,
842
	byte*		xdes,
843
	FILE*		file,
844 845
	const page_size_t& page_size,
	bool is_encrypted)
846
{
847
	unsigned long long id;
848
	uint16_t undo_page_type;
849
	char str[20]={'\0'};
850
	ulint n_recs;
851
	uint32_t page_no, left_page_no, right_page_no;
852 853 854
	ulint data_bytes;
	bool is_leaf;
	int size_range_id;
855 856 857 858 859 860 861 862 863 864

	/* Check whether page is doublewrite buffer. */
	if(skip_page) {
		strcpy(str, "Double_write_buffer");
	} else {
		strcpy(str, "-");
	}

	switch (mach_read_from_2(page + FIL_PAGE_TYPE)) {

865
	case FIL_PAGE_INDEX: {
866
		uint32_t key_version = mach_read_from_4(page + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION);
867
		page_type.n_fil_page_index++;
868 869

		/* If page is encrypted we can't read index header */
870 871 872 873 874 875 876 877 878
		if (!is_encrypted) {
			id = mach_read_from_8(page + PAGE_HEADER + PAGE_INDEX_ID);
			n_recs = mach_read_from_2(page + PAGE_HEADER + PAGE_N_RECS);
			page_no = mach_read_from_4(page + FIL_PAGE_OFFSET);
			left_page_no = mach_read_from_4(page + FIL_PAGE_PREV);
			right_page_no = mach_read_from_4(page + FIL_PAGE_NEXT);
			ulint is_comp = mach_read_from_2(page + PAGE_HEADER + PAGE_N_HEAP) & 0x8000;
			ulint level = mach_read_from_2(page + PAGE_HEADER + PAGE_LEVEL);
			ulint garbage = mach_read_from_2(page + PAGE_HEADER + PAGE_GARBAGE);
879 880


881 882 883 884 885
			data_bytes = (ulint)(mach_read_from_2(page + PAGE_HEADER + PAGE_HEAP_TOP)
				- (is_comp
					? PAGE_NEW_SUPREMUM_END
					: PAGE_OLD_SUPREMUM_END)
				- garbage);
886

887
			is_leaf = (!*(const uint16*) (page + (PAGE_HEADER + PAGE_LEVEL)));
888

889
			if (file) {
890
				fprintf(file, "#::" UINT32PF "\t\t|\t\tIndex page\t\t\t|"
891 892 893 894 895 896 897
					"\tindex id=%llu,", cur_page_num, id);

				fprintf(file,
					" page level=" ULINTPF
					", No. of records=" ULINTPF
					", garbage=" ULINTPF ", %s\n",
					level, n_recs, garbage, str);
898 899
			}

900 901 902
			size_range_id = (data_bytes * SIZE_RANGES_FOR_PAGE
				+ page_size.logical() - 1) /
				page_size.logical();
903

904 905 906 907 908
			if (size_range_id > SIZE_RANGES_FOR_PAGE + 1) {
				/* data_bytes is bigger than logical_page_size */
				size_range_id = SIZE_RANGES_FOR_PAGE + 1;
			}
			if (per_page_details) {
909
				printf("index id=%llu page " UINT32PF " leaf %d n_recs " ULINTPF " data_bytes " ULINTPF
910 911 912 913
					"\n", id, page_no, is_leaf, n_recs, data_bytes);
			}
			/* update per-index statistics */
			{
914
				per_index_stats &index = index_ids[id];
915
				if (is_page_free(xdes, page_size, page_no)) {
916 917
					index.free_pages++;
					return;
918 919
				}

920
				index.pages++;
921

922 923 924 925 926 927 928
				if (is_leaf) {
					index.leaf_pages++;
					if (data_bytes > index.max_data_size) {
						index.max_data_size = data_bytes;
					}
					struct per_page_stats pp(n_recs, data_bytes,
						left_page_no, right_page_no);
929

930 931 932 933 934 935
					index.leaves[page_no] = pp;

					if (left_page_no == ULINT32_UNDEFINED) {
						index.first_leaf_page = page_no;
						index.count++;
					}
936 937
				}

938 939 940 941
				index.total_n_recs += n_recs;
				index.total_data_bytes += data_bytes;
				index.pages_in_size_range[size_range_id] ++;
			}
942
		} else if (file) {
943 944
			fprintf(file, "#::" UINT32PF "\t\t|\t\tEncrypted Index page\t\t\t|"
				"\tkey_version " UINT32PF ",%s\n", cur_page_num, key_version, str);
945
		}
946

947
		break;
948
	}
949 950 951 952
	case FIL_PAGE_UNDO_LOG:
		page_type.n_fil_page_undo_log++;
		undo_page_type = mach_read_from_2(page +
				     TRX_UNDO_PAGE_HDR + TRX_UNDO_PAGE_TYPE);
953
		if (file) {
954
			fprintf(file, "#::" UINT32PF "\t\t|\t\tUndo log page\t\t\t|",
955 956 957 958
				cur_page_num);
		}
		if (undo_page_type == TRX_UNDO_INSERT) {
			page_type.n_undo_insert++;
959
			if (file) {
960 961 962 963 964 965
				fprintf(file, "\t%s",
					"Insert Undo log page");
			}

		} else if (undo_page_type == TRX_UNDO_UPDATE) {
			page_type.n_undo_update++;
966
			if (file) {
967 968 969 970 971 972 973 974 975 976
				fprintf(file, "\t%s",
					"Update undo log page");
			}
		}

		undo_page_type = mach_read_from_2(page + TRX_UNDO_SEG_HDR +
						  TRX_UNDO_STATE);
		switch (undo_page_type) {
			case TRX_UNDO_ACTIVE:
				page_type.n_undo_state_active++;
977
				if (file) {
978 979 980 981 982 983 984
					fprintf(file, ", %s", "Undo log of "
						"an active transaction");
				}
				break;

			case TRX_UNDO_CACHED:
				page_type.n_undo_state_cached++;
985
				if (file) {
986 987 988 989 990 991 992
					fprintf(file, ", %s", "Page is "
						"cached for quick reuse");
				}
				break;

			case TRX_UNDO_TO_FREE:
				page_type.n_undo_state_to_free++;
993
				if (file) {
994 995 996 997 998 999 1000
					fprintf(file, ", %s", "Insert undo "
						"segment that can be freed");
				}
				break;

			case TRX_UNDO_TO_PURGE:
				page_type.n_undo_state_to_purge++;
1001
				if (file) {
1002 1003 1004 1005 1006 1007 1008 1009
					fprintf(file, ", %s", "Will be "
						"freed in purge when all undo"
					"data in it is removed");
				}
				break;

			case TRX_UNDO_PREPARED:
				page_type.n_undo_state_prepared++;
1010
				if (file) {
1011 1012 1013 1014 1015 1016 1017 1018 1019
					fprintf(file, ", %s", "Undo log of "
						"an prepared transaction");
				}
				break;

			default:
				page_type.n_undo_state_other++;
				break;
		}
1020
		if(file) {
1021 1022 1023 1024 1025 1026
			fprintf(file, ", %s\n", str);
		}
		break;

	case FIL_PAGE_INODE:
		page_type.n_fil_page_inode++;
1027
		if (file) {
1028
			fprintf(file, "#::" UINT32PF "\t\t|\t\tInode page\t\t\t|"
1029 1030 1031 1032 1033 1034
				"\t%s\n",cur_page_num, str);
		}
		break;

	case FIL_PAGE_IBUF_FREE_LIST:
		page_type.n_fil_page_ibuf_free_list++;
1035
		if (file) {
1036
			fprintf(file, "#::" UINT32PF "\t\t|\t\tInsert buffer free list"
1037 1038 1039 1040 1041 1042
				" page\t|\t%s\n", cur_page_num, str);
		}
		break;

	case FIL_PAGE_TYPE_ALLOCATED:
		page_type.n_fil_page_type_allocated++;
1043
		if (file) {
1044
			fprintf(file, "#::" UINT32PF "\t\t|\t\tFreshly allocated "
1045 1046 1047 1048 1049 1050
				"page\t\t|\t%s\n", cur_page_num, str);
		}
		break;

	case FIL_PAGE_IBUF_BITMAP:
		page_type.n_fil_page_ibuf_bitmap++;
1051
		if (file) {
1052
			fprintf(file, "#::" UINT32PF "\t\t|\t\tInsert Buffer "
1053 1054 1055 1056 1057 1058
				"Bitmap\t\t|\t%s\n", cur_page_num, str);
		}
		break;

	case FIL_PAGE_TYPE_SYS:
		page_type.n_fil_page_type_sys++;
1059
		if (file) {
1060 1061
			fprintf(file, "#::" UINT32PF "\t\t|\t\tSystem page\t\t\t|"
				"\t%s\n", cur_page_num, str);
1062 1063 1064 1065 1066
		}
		break;

	case FIL_PAGE_TYPE_TRX_SYS:
		page_type.n_fil_page_type_trx_sys++;
1067
		if (file) {
1068
			fprintf(file, "#::" UINT32PF "\t\t|\t\tTransaction system "
1069 1070 1071 1072 1073 1074
				"page\t\t|\t%s\n", cur_page_num, str);
		}
		break;

	case FIL_PAGE_TYPE_FSP_HDR:
		page_type.n_fil_page_type_fsp_hdr++;
1075
		if (file) {
1076
			fprintf(file, "#::" UINT32PF "\t\t|\t\tFile Space "
1077 1078 1079 1080 1081 1082
				"Header\t\t|\t%s\n", cur_page_num, str);
		}
		break;

	case FIL_PAGE_TYPE_XDES:
		page_type.n_fil_page_type_xdes++;
1083
		if (file) {
1084
			fprintf(file, "#::" UINT32PF "\t\t|\t\tExtent descriptor "
1085 1086 1087 1088 1089 1090
				"page\t\t|\t%s\n", cur_page_num, str);
		}
		break;

	case FIL_PAGE_TYPE_BLOB:
		page_type.n_fil_page_type_blob++;
1091
		if (file) {
1092
			fprintf(file, "#::" UINT32PF "\t\t|\t\tBLOB page\t\t\t|\t%s\n",
1093 1094 1095 1096 1097 1098
				cur_page_num, str);
		}
		break;

	case FIL_PAGE_TYPE_ZBLOB:
		page_type.n_fil_page_type_zblob++;
1099
		if (file) {
1100
			fprintf(file, "#::" UINT32PF "\t\t|\t\tCompressed BLOB "
1101 1102 1103 1104 1105 1106
				"page\t\t|\t%s\n", cur_page_num, str);
		}
		break;

	case FIL_PAGE_TYPE_ZBLOB2:
		page_type.n_fil_page_type_zblob2++;
1107
		if (file) {
1108
			fprintf(file, "#::" UINT32PF "\t\t|\t\tSubsequent Compressed "
1109 1110 1111 1112
				"BLOB page\t|\t%s\n", cur_page_num, str);
		}
			break;

1113 1114
	case FIL_PAGE_PAGE_COMPRESSED:
		page_type.n_fil_page_type_page_compressed++;
1115
		if (file) {
1116
			fprintf(file, "#::" UINT32PF "\t\t|\t\tPage compressed "
1117
				"page\t|\t%s\n", cur_page_num, str);
1118 1119 1120 1121 1122
		}
		break;

	case FIL_PAGE_PAGE_COMPRESSED_ENCRYPTED:
		page_type.n_fil_page_type_page_compressed_encrypted++;
1123
		if (file) {
1124
			fprintf(file, "#::" UINT32PF "\t\t|\t\tPage compressed encrypted "
1125
				"page\t|\t%s\n", cur_page_num, str);
1126 1127
		}
		break;
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 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 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
	default:
		page_type.n_fil_page_type_other++;
		break;
	}
}
/**
@param [in/out] file_name	name of the filename

@retval FILE pointer if successfully created else NULL when error occured.
*/
FILE*
create_file(
	char*	file_name)
{
	FILE*	file = NULL;

#ifndef _WIN32
	file = fopen(file_name, "wb");
	if (file == NULL) {
		fprintf(stderr, "Failed to create file: %s: %s\n",
			file_name, strerror(errno));
		return(NULL);
	}
#else
	HANDLE		hFile;		/* handle to open file. */
	int fd = 0;
	hFile = CreateFile((LPCTSTR) file_name,
			  GENERIC_READ | GENERIC_WRITE,
			  FILE_SHARE_READ | FILE_SHARE_DELETE,
			  NULL, CREATE_NEW, NULL, NULL);

	if (hFile == INVALID_HANDLE_VALUE) {
		/* print the error message. */
		fprintf(stderr, "Filename::%s %s\n",
			file_name,
			error_message(GetLastError()));

			return(NULL);
		}

	/* get the file descriptor. */
	fd= _open_osfhandle((intptr_t)hFile, _O_RDWR | _O_BINARY);
	file = fdopen(fd, "wb");
#endif /* _WIN32 */

	return(file);
}

/*
 Print the page type count of a tablespace.
 @param [in] fil_out	stream where the output goes.
*/
void
print_summary(
	FILE*	fil_out)
{
	fprintf(fil_out, "\n================PAGE TYPE SUMMARY==============\n");
	fprintf(fil_out, "#PAGE_COUNT\tPAGE_TYPE");
	fprintf(fil_out, "\n===============================================\n");
	fprintf(fil_out, "%8d\tIndex page\n",
		page_type.n_fil_page_index);
	fprintf(fil_out, "%8d\tUndo log page\n",
		page_type.n_fil_page_undo_log);
	fprintf(fil_out, "%8d\tInode page\n",
		page_type.n_fil_page_inode);
	fprintf(fil_out, "%8d\tInsert buffer free list page\n",
		page_type.n_fil_page_ibuf_free_list);
	fprintf(fil_out, "%8d\tFreshly allocated page\n",
		page_type.n_fil_page_type_allocated);
	fprintf(fil_out, "%8d\tInsert buffer bitmap\n",
		page_type.n_fil_page_ibuf_bitmap);
	fprintf(fil_out, "%8d\tSystem page\n",
		page_type.n_fil_page_type_sys);
	fprintf(fil_out, "%8d\tTransaction system page\n",
		page_type.n_fil_page_type_trx_sys);
	fprintf(fil_out, "%8d\tFile Space Header\n",
		page_type.n_fil_page_type_fsp_hdr);
	fprintf(fil_out, "%8d\tExtent descriptor page\n",
		page_type.n_fil_page_type_xdes);
	fprintf(fil_out, "%8d\tBLOB page\n",
		page_type.n_fil_page_type_blob);
	fprintf(fil_out, "%8d\tCompressed BLOB page\n",
		page_type.n_fil_page_type_zblob);
1211 1212 1213 1214
	fprintf(fil_out, "%8d\tPage compressed page\n",
		page_type.n_fil_page_type_page_compressed);
	fprintf(fil_out, "%8d\tPage compressed encrypted page\n",
		page_type.n_fil_page_type_page_compressed_encrypted);
1215
	fprintf(fil_out, "%8d\tOther type of page\n",
1216
		page_type.n_fil_page_type_other);
1217

1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
	fprintf(fil_out, "\n===============================================\n");
	fprintf(fil_out, "Additional information:\n");
	fprintf(fil_out, "Undo page type: %d insert, %d update, %d other\n",
		page_type.n_undo_insert,
		page_type.n_undo_update,
		page_type.n_undo_other);
	fprintf(fil_out, "Undo page state: %d active, %d cached, %d to_free, %d"
		" to_purge, %d prepared, %d other\n",
		page_type.n_undo_state_active,
		page_type.n_undo_state_cached,
		page_type.n_undo_state_to_free,
		page_type.n_undo_state_to_purge,
		page_type.n_undo_state_prepared,
		page_type.n_undo_state_other);
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260

	fprintf(fil_out, "index_id\t#pages\t\t#leaf_pages\t#recs_per_page"
		"\t#bytes_per_page\n");

	for (std::map<unsigned long long, per_index_stats>::const_iterator it = index_ids.begin();
	     it != index_ids.end(); it++) {
		const per_index_stats& index = it->second;
		fprintf(fil_out, "%lld\t\t%lld\t\t%lld\t\t%lld\t\t%lld\n",
			it->first, index.pages, index.leaf_pages,
			index.total_n_recs / index.pages,
			index.total_data_bytes / index.pages);
	}

	fprintf(fil_out, "\n");
	fprintf(fil_out, "index_id\tpage_data_bytes_histgram(empty,...,oversized)\n");

	for (std::map<unsigned long long, per_index_stats>::const_iterator it = index_ids.begin();
	     it != index_ids.end(); it++) {
		fprintf(fil_out, "%lld\t", it->first);
		const per_index_stats& index = it->second;
		for (ulint i = 0; i < SIZE_RANGES_FOR_PAGE+2; i++) {
			fprintf(fil_out, "\t%lld", index.pages_in_size_range[i]);
		}
		fprintf(fil_out, "\n");
	}

	if (do_leaf) {
		print_leaf_stats(fil_out);
	}
1261 1262 1263 1264
}

/* command line argument for innochecksum tool. */
static struct my_option innochecksum_options[] = {
1265 1266 1267 1268 1269 1270 1271 1272
  {"help", '?', "Displays this help and exits.",
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  {"info", 'I', "Synonym for --help.",
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  {"version", 'V', "Displays version information and exits.",
    0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
  {"verbose", 'v', "Verbose (prints progress every 5 seconds).",
    &verbose, &verbose, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1273
#ifndef DBUG_OFF
1274
  {"debug", '#', "Output debug log. See https://mariadb.com/kb/en/library/creating-a-trace-file/",
1275
    &dbug_setting, &dbug_setting, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
1276
#endif /* !DBUG_OFF */
1277
  {"count", 'c', "Print the count of pages in the file and exits.",
1278 1279
    &just_count, &just_count, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
  {"start_page", 's', "Start on this page number (0 based).",
1280 1281
    &start_page, &start_page, 0, GET_UINT, REQUIRED_ARG,
    0, 0, FIL_NULL, 0, 1, 0},
1282
  {"end_page", 'e', "End at this page number (0 based).",
1283 1284
    &end_page, &end_page, 0, GET_UINT, REQUIRED_ARG,
    0, 0, FIL_NULL, 0, 1, 0},
1285
  {"page", 'p', "Check only this page (0 based).",
1286 1287
    &do_page, &do_page, 0, GET_UINT, REQUIRED_ARG,
    0, 0, FIL_NULL, 0, 1, 0},
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
  {"strict-check", 'C', "Specify the strict checksum algorithm by the user.",
    &strict_check, &strict_check, &innochecksum_algorithms_typelib,
    GET_ENUM, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  {"no-check", 'n', "Ignore the checksum verification.",
    &no_check, &no_check, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
  {"allow-mismatches", 'a', "Maximum checksum mismatch allowed.",
    &allow_mismatches, &allow_mismatches, 0,
    GET_ULL, REQUIRED_ARG, 0, 0, ULLONG_MAX, 0, 1, 0},
  {"write", 'w', "Rewrite the checksum algorithm by the user.",
    &write_check, &write_check, &innochecksum_algorithms_typelib,
    GET_ENUM, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
  {"page-type-summary", 'S', "Display a count of each page type "
   "in a tablespace.", &page_type_summary, &page_type_summary, 0,
   GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
  {"page-type-dump", 'D', "Dump the page type info for each page in a "
   "tablespace.", &page_dump_filename, &page_dump_filename, 0,
   GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1305 1306
  {"per-page-details", 'i', "Print out per-page detail information.",
   &per_page_details, &per_page_details, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
1307 1308 1309
   {"log", 'l', "log output.",
     &log_filename, &log_filename, 0,
      GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
1310
  {"leaf", 'f', "Examine leaf index pages",
1311 1312 1313
    &do_leaf, &do_leaf, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
  {"merge", 'm', "leaf page count if merge given number of consecutive pages",
   &n_merge, &n_merge, 0, GET_ULONG, REQUIRED_ARG, 0, 0, (longlong)10L, 0, 1, 0},
1314

1315 1316 1317
  {0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
};

1318
/* Print out the Innodb version and machine information. */
1319 1320
static void print_version(void)
{
1321 1322 1323 1324 1325 1326 1327 1328 1329
#ifdef DBUG_OFF
	printf("%s Ver %s, for %s (%s)\n",
		my_progname, INNODB_VERSION_STR,
		SYSTEM_TYPE, MACHINE_TYPE);
#else
	printf("%s-debug Ver %s, for %s (%s)\n",
		my_progname, INNODB_VERSION_STR,
		SYSTEM_TYPE, MACHINE_TYPE);
#endif /* DBUG_OFF */
1330 1331 1332 1333
}

static void usage(void)
{
1334 1335 1336 1337
	print_version();
	puts(ORACLE_WELCOME_COPYRIGHT_NOTICE("2000"));
	printf("InnoDB offline file checksum utility.\n");
	printf("Usage: %s [-c] [-s <start page>] [-e <end page>] "
1338
		"[-p <page>] [-i] [-v]  [-a <allow mismatches>] [-n] "
1339
		"[-C <strict-check>] [-w <write>] [-S] [-D <page type dump>] "
1340
		"[-l <log>] [-l] [-m <merge pages>] <filename or [-]>\n", my_progname);
1341 1342
	printf("See https://mariadb.com/kb/en/library/innochecksum/"
	       " for usage hints.\n");
1343 1344
	my_print_help(innochecksum_options);
	my_print_variables(innochecksum_options);
1345 1346 1347 1348
}

extern "C" my_bool
innochecksum_get_one_option(
1349
	int			optid,
1350 1351
	const struct my_option	*opt MY_ATTRIBUTE((unused)),
	char			*argument MY_ATTRIBUTE((unused)))
1352
{
1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372
	switch (optid) {
#ifndef DBUG_OFF
	case '#':
		dbug_setting = argument
			? argument
			: IF_WIN("d:O,innochecksum.trace",
				 "d:o,/tmp/innochecksum.trace");
		DBUG_PUSH(dbug_setting);
		break;
#endif /* !DBUG_OFF */
	case 'e':
		use_end_page = true;
		break;
	case 'p':
		end_page = start_page = do_page;
		use_end_page = true;
		do_one_page = true;
		break;
	case 'V':
		print_version();
1373
		my_end(0);
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
		exit(EXIT_SUCCESS);
		break;
	case 'C':
		strict_verify = true;
		switch ((srv_checksum_algorithm_t) strict_check) {

		case SRV_CHECKSUM_ALGORITHM_STRICT_CRC32:
		case SRV_CHECKSUM_ALGORITHM_CRC32:
			srv_checksum_algorithm =
				SRV_CHECKSUM_ALGORITHM_STRICT_CRC32;
			break;

		case SRV_CHECKSUM_ALGORITHM_STRICT_INNODB:
		case SRV_CHECKSUM_ALGORITHM_INNODB:
			srv_checksum_algorithm =
				SRV_CHECKSUM_ALGORITHM_STRICT_INNODB;
			break;

		case SRV_CHECKSUM_ALGORITHM_STRICT_NONE:
		case SRV_CHECKSUM_ALGORITHM_NONE:
			srv_checksum_algorithm =
				SRV_CHECKSUM_ALGORITHM_STRICT_NONE;
			break;
		default:
			return(true);
		}
		break;
	case 'n':
		no_check = true;
		break;
	case 'a':
	case 'S':
		break;
	case 'w':
		do_write = true;
		break;
	case 'D':
		page_type_dump = true;
		break;
	case 'l':
		is_log_enabled = true;
		break;
	case 'I':
	case '?':
		usage();
1419
		my_end(0);
1420 1421 1422
		exit(EXIT_SUCCESS);
		break;
	}
1423

1424
	return(false);
1425 1426
}

1427 1428 1429 1430 1431
static
bool
get_options(
	int	*argc,
	char	***argv)
1432
{
1433
	if (handle_options(argc, argv, innochecksum_options,
1434
			innochecksum_get_one_option)) {
1435
		my_end(0);
1436
		exit(true);
1437
	}
1438 1439 1440 1441

	/* The next arg must be the filename */
	if (!*argc) {
		usage();
1442
		my_end(0);
1443 1444
		return (true);
	}
1445

1446
	return (false);
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 1476 1477 1478 1479 1480
/** Check from page 0 if table is encrypted.
@param[in]	filename	Filename
@param[in]	page_size	page size
@param[in]	page		Page 0
@retval true if tablespace is encrypted, false if not
*/
static
bool check_encryption(
	const char* filename,
	const page_size_t& page_size,
	byte * page)
{
	ulint offset = (FSP_HEADER_OFFSET + (XDES_ARR_OFFSET + XDES_SIZE *
			(page_size.physical()) / FSP_EXTENT_SIZE));

	if (memcmp(page + offset, CRYPT_MAGIC, MAGIC_SZ) != 0) {
		return false;
	}

	ulint type = mach_read_from_1(page + offset + MAGIC_SZ + 0);

	if (! (type == CRYPT_SCHEME_UNENCRYPTED ||
	       type == CRYPT_SCHEME_1)) {
		return false;
	}

	ulint iv_length = mach_read_from_1(page + offset + MAGIC_SZ + 1);

	if (iv_length != CRYPT_SCHEME_1_IV_LEN) {
		return false;
	}

1481
	uint32_t min_key_version = mach_read_from_4
1482 1483
		(page + offset + MAGIC_SZ + 2 + iv_length);

1484
	uint32_t key_id = mach_read_from_4
1485 1486 1487
		(page + offset + MAGIC_SZ + 2 + iv_length + 4);

	if (type == CRYPT_SCHEME_1 && is_log_enabled) {
1488
		fprintf(log_file,"Tablespace %s encrypted key_version " UINT32PF " key_id " UINT32PF "\n",
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
			filename, min_key_version, key_id);
	}

	return (type == CRYPT_SCHEME_1);
}

/**
Verify page checksum.
@param[in] buf			page to verify
@param[in] page_size		page size
@param[in] is_encrypted		true if tablespace is encrypted
@param[in] is_compressed	true if tablespace is page compressed
@param[in,out] mismatch_count	Number of pages failed in checksum verify
@retval 0 if page checksum matches or 1 if it does not match
*/
static
int verify_checksum(
	byte* buf,
	const page_size_t& page_size,
	bool is_encrypted,
	bool is_compressed,
	unsigned long long* mismatch_count)
{
	int exit_status = 0;
	bool is_corrupted = false;

	is_corrupted = is_page_corrupted(
		buf, page_size, is_encrypted, is_compressed);

	if (is_corrupted) {
1519
		fprintf(stderr, "Fail: page::" UINT32PF " invalid\n",
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
			cur_page_num);

		(*mismatch_count)++;

		if (*mismatch_count > allow_mismatches) {
			fprintf(stderr,
				"Exceeded the "
				"maximum allowed "
				"checksum mismatch "
				"count::%llu current::%llu\n",
				*mismatch_count,
				allow_mismatches);

			exit_status = 1;
		}
	}

	return (exit_status);
}

/** Rewrite page checksum if needed.
@param[in]	filename	File name
@param[in]	fil_in		File pointer
@param[in]	buf		page
@param[in]	page_size	page size
@param[in]	pos		File position
@param[in]	is_encrypted	true if tablespace is encrypted
@param[in]	is_compressed	true if tablespace is page compressed
@retval 0 if checksum rewrite was successful, 1 if error was detected */
static
int
rewrite_checksum(
	const char*	filename,
	FILE*		fil_in,
	byte*		buf,
	const page_size_t& page_size,
	fpos_t*		pos,
	bool is_encrypted,
	bool is_compressed)
{
	int exit_status = 0;
	/* Rewrite checksum. Note that for encrypted and
	page compressed tables this is not currently supported. */
	if (do_write &&
		!is_encrypted &&
		!is_compressed
		&& !write_file(filename, fil_in, buf,
			page_size.is_compressed(), pos,
			static_cast<ulong>(page_size.physical()))) {

		exit_status = 1;
	}

	return (exit_status);
}

1576 1577 1578
int main(
	int	argc,
	char	**argv)
1579
{
1580 1581 1582 1583 1584
	/* our input file. */
	FILE*		fil_in = NULL;
	/* our input filename. */
	char*		filename;
	/* Buffer to store pages read. */
1585 1586
	byte*		buf_ptr = NULL;
	byte*		xdes_ptr = NULL;
1587
	byte*		buf = NULL;
1588
	byte*		xdes = NULL;
1589
	/* bytes read count */
1590
	ulint		bytes;
1591
	/* last time */
1592
	time_t		lastt = 0;
1593 1594 1595 1596 1597 1598 1599
	/* stat, to get file size. */
#ifdef _WIN32
	struct _stat64	st;
#else
	struct stat	st;
#endif /* _WIN32 */

1600 1601
	int exit_status = 0;

1602 1603 1604
	/* size of file (has to be 64 bits) */
	unsigned long long int	size		= 0;
	/* number of pages in file */
1605
	uint32_t	pages;
1606 1607 1608

	off_t		offset			= 0;
	/* count the no. of page corrupted. */
1609
	unsigned long long   mismatch_count		= 0;
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625

	bool		partial_page_read	= false;
	/* Enabled when read from stdin is done. */
	bool		read_from_stdin		= false;
	FILE*		fil_page_type		= NULL;
	fpos_t		pos;

	/* enable when space_id of given file is zero. */
	bool		is_system_tablespace = false;

	ut_crc32_init();
	MY_INIT(argv[0]);
	DBUG_ENTER("main");
	DBUG_PROCESS(argv[0]);

	if (get_options(&argc,&argv)) {
1626 1627
		exit_status = 1;
		goto my_exit;
1628
	}
1629

1630 1631 1632
	if (strict_verify && no_check) {
		fprintf(stderr, "Error: --strict-check option cannot be used "
			"together with --no-check option.\n");
1633 1634
		exit_status = 1;
		goto my_exit;
1635
	}
1636

1637 1638 1639
	if (no_check && !do_write) {
		fprintf(stderr, "Error: --no-check must be associated with "
			"--write option.\n");
1640 1641
		exit_status = 1;
		goto my_exit;
1642
	}
1643

1644 1645 1646
	if (page_type_dump) {
		fil_page_type = create_file(page_dump_filename);
		if (!fil_page_type) {
1647 1648
			exit_status = 1;
			goto my_exit;
1649 1650
		}
	}
1651

1652 1653 1654
	if (is_log_enabled) {
		log_file = create_file(log_filename);
		if (!log_file) {
1655 1656
			exit_status = 1;
			goto my_exit;
1657 1658 1659
		}
		fprintf(log_file, "InnoDB File Checksum Utility.\n");
	}
1660

1661 1662 1663
	if (verbose) {
		my_print_variables(innochecksum_options);
	}
1664

1665

1666 1667 1668 1669 1670
	buf_ptr = (byte*) malloc(UNIV_PAGE_SIZE_MAX * 2);
	xdes_ptr = (byte*)malloc(UNIV_PAGE_SIZE_MAX * 2);
	buf = (byte *) ut_align(buf_ptr, UNIV_PAGE_SIZE_MAX);
	xdes = (byte *) ut_align(xdes_ptr, UNIV_PAGE_SIZE_MAX);

1671 1672
	/* The file name is not optional. */
	for (int i = 0; i < argc; ++i) {
1673

1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
		/* Reset parameters for each file. */
		filename = argv[i];
		memset(&page_type, 0, sizeof(innodb_page_type));
		partial_page_read = false;
		skip_page = false;

		if (is_log_enabled) {
			fprintf(log_file, "Filename = %s\n", filename);
		}

		if (*filename == '-') {
			/* read from stdin. */
			fil_in = stdin;
			read_from_stdin = true;

		}

		/* stat the file to get size and page count. */
		if (!read_from_stdin &&
#ifdef _WIN32
			_stat64(filename, &st)) {
#else
			stat(filename, &st)) {
#endif /* _WIN32 */
			fprintf(stderr, "Error: %s cannot be found\n",
				filename);

1701 1702
			exit_status = 1;
			goto my_exit;
1703 1704 1705 1706 1707 1708 1709
		}

		if (!read_from_stdin) {
			size = st.st_size;
			fil_in = open_file(filename);
			/*If fil_in is NULL, terminate as some error encountered */
			if(fil_in == NULL) {
1710 1711
				exit_status = 1;
				goto my_exit;
1712 1713 1714 1715
			}
			/* Save the current file pointer in pos variable.*/
			if (0 != fgetpos(fil_in, &pos)) {
				perror("fgetpos");
1716 1717
				exit_status = 1;
				goto my_exit;
1718 1719 1720 1721
			}
		}

		/* Read the minimum page size. */
1722
		bytes = fread(buf, 1, UNIV_ZIP_SIZE_MIN, fil_in);
1723 1724 1725 1726 1727
		partial_page_read = true;

		if (bytes != UNIV_ZIP_SIZE_MIN) {
			fprintf(stderr, "Error: Was not able to read the "
				"minimum page size ");
1728
			fprintf(stderr, "of %d bytes.  Bytes read was " ULINTPF "\n",
1729 1730
				UNIV_ZIP_SIZE_MIN, bytes);

1731 1732
			exit_status = 1;
			goto my_exit;
1733 1734 1735 1736 1737
		}

		/* enable variable is_system_tablespace when space_id of given
		file is zero. Use to skip the checksum verification and rewrite
		for doublewrite pages. */
1738 1739
		cur_space = mach_read_from_4(buf + FIL_PAGE_SPACE_ID);
		cur_page_num = mach_read_from_4(buf + FIL_PAGE_OFFSET);
1740

1741 1742
		/* Determine page size, zip_size and page compression
		from fsp_flags and encryption metadata from page 0 */
1743 1744
		const page_size_t&	page_size = get_page_size(buf);

1745 1746 1747 1748 1749 1750 1751
		ulint flags = mach_read_from_4(FSP_HEADER_OFFSET + FSP_SPACE_FLAGS + buf);
		ulint zip_size = page_size.is_compressed() ? page_size.logical() : 0;
		logical_page_size = page_size.is_compressed() ? zip_size : 0;
		physical_page_size = page_size.physical();
		srv_page_size = page_size.logical();
		bool is_compressed = FSP_FLAGS_HAS_PAGE_COMPRESSION(flags);

1752 1753 1754
		if (physical_page_size == UNIV_ZIP_SIZE_MIN) {
			partial_page_read = false;
		} else {
1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769
			/* Read rest of the page 0 to determine crypt_data */
			bytes = ulong(read_file(buf, partial_page_read, page_size.physical(), fil_in));

			if (bytes != page_size.physical()) {
				fprintf(stderr, "Error: Was not able to read the "
					"rest of the page ");
				fprintf(stderr, "of " ULINTPF " bytes.  Bytes read was " ULINTPF "\n",
					page_size.physical() - UNIV_ZIP_SIZE_MIN, bytes);

				exit_status = 1;
				goto my_exit;
			}
			partial_page_read = false;
		}

1770

1771 1772 1773 1774 1775 1776 1777 1778 1779 1780
		/* Now that we have full page 0 in buffer, check encryption */
		bool is_encrypted = check_encryption(filename, page_size, buf);

		/* Verify page 0 contents. Note that we can't allow
		checksum mismatch on page 0, because that would mean we
		could not trust it content. */
		if (!no_check) {
			unsigned long long tmp_allow_mismatches = allow_mismatches;
			allow_mismatches = 0;

1781 1782 1783
			exit_status = verify_checksum(
				buf, page_size, is_encrypted,
				is_compressed, &mismatch_count);
1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811

			if (exit_status) {
				fprintf(stderr, "Error: Page 0 checksum mismatch, can't continue. \n");
				goto my_exit;
			}
			allow_mismatches = tmp_allow_mismatches;
		}

		if ((exit_status = rewrite_checksum(filename, fil_in, buf,
					page_size, &pos, is_encrypted, is_compressed))) {
			goto my_exit;
		}

		if (page_type_dump) {
			fprintf(fil_page_type,
				"\n\nFilename::%s\n", filename);
			fprintf(fil_page_type,
				"========================================"
				"======================================\n");
			fprintf(fil_page_type,
				"\tPAGE_NO\t\t|\t\tPAGE_TYPE\t\t"
				"\t|\tEXTRA INFO\n");
			fprintf(fil_page_type,
				"========================================"
				"======================================\n");
		}

		if (per_page_details) {
1812
			printf("page " UINT32PF " ", cur_page_num);
1813 1814
		}

1815 1816
		memcpy(xdes, buf, physical_page_size);

1817 1818 1819 1820
		if (page_type_summary || page_type_dump) {
			parse_page(buf, xdes, fil_page_type, page_size, is_encrypted);
		}

1821
		pages = uint32_t(size / page_size.physical());
1822 1823

		if (just_count) {
1824 1825
			fprintf(read_from_stdin ? stderr : stdout,
				"Number of pages:" UINT32PF "\n", pages);
1826 1827 1828 1829
			continue;
		} else if (verbose && !read_from_stdin) {
			if (is_log_enabled) {
				fprintf(log_file, "file %s = %llu bytes "
1830 1831
					"(" UINT32PF " pages)\n",
					filename, size, pages);
1832 1833
				if (do_one_page) {
					fprintf(log_file, "Innochecksum: "
1834 1835
						"checking page::"
						UINT32PF ";\n",
1836 1837 1838 1839 1840 1841
						do_page);
				}
			}
		} else {
			if (is_log_enabled) {
				fprintf(log_file, "Innochecksum: checking "
1842 1843
					"pages in range::" UINT32PF
					" to " UINT32PF "\n",
1844 1845 1846 1847 1848
					start_page, use_end_page ?
					end_page : (pages - 1));
			}
		}

1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878
		off_t cur_offset = 0;
		/* Find the first non all-zero page and fetch the
		space id from there. */
		while (is_page_all_zeroes(buf, physical_page_size)) {
			bytes = ulong(read_file(
					buf, false, physical_page_size,
					fil_in));

			if (feof(fil_in)) {
				fprintf(stderr, "All are "
					"zero-filled pages.");
				goto my_exit;
			}

			cur_offset++;
		}

		cur_space = mach_read_from_4(buf + FIL_PAGE_SPACE_ID);
		is_system_tablespace = (cur_space == 0);

		if (cur_offset > 0) {
			/* Re-read the non-zero page to check the
			checksum. So move the file pointer to
			previous position and reset the page number too. */
			cur_page_num = mach_read_from_4(buf + FIL_PAGE_OFFSET);
			if (!start_page) {
				goto first_non_zero;
			}
		}

1879 1880 1881 1882 1883 1884 1885 1886
		/* seek to the necessary position */
		if (start_page) {
			if (!read_from_stdin) {
				/* If read is not from stdin, we can use
				fseeko() to position the file pointer to
				the desired page. */
				partial_page_read = false;

1887 1888
				offset = off_t(ulonglong(start_page)
					       * page_size.physical());
1889 1890 1891 1892 1893 1894 1895 1896
#ifdef _WIN32
				if (_fseeki64(fil_in, offset, SEEK_SET)) {
#else
				if (fseeko(fil_in, offset, SEEK_SET)) {
#endif /* _WIN32 */
					perror("Error: Unable to seek to "
						"necessary offset");

1897 1898
					exit_status = 1;
					goto my_exit;
1899 1900 1901 1902 1903 1904
				}
				/* Save the current file pointer in
				pos variable. */
				if (0 != fgetpos(fil_in, &pos)) {
					perror("fgetpos");

1905 1906
					exit_status = 1;
					goto my_exit;
1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
				}
			} else {

				ulong count = 0;

				while (!feof(fil_in)) {
					if (start_page == count) {
						break;
					}
					/* We read a part of page to find the
					minimum page size. We cannot reset
					the file pointer to the beginning of
					the page if we are reading from stdin
					(fseeko() on stdin doesn't work). So
					read only the remaining part of page,
					if partial_page_read is enable. */
					bytes = read_file(buf,
							  partial_page_read,
							  static_cast<ulong>(
1926
								  page_size.physical()),
1927 1928 1929 1930 1931 1932
							  fil_in);

					partial_page_read = false;
					count++;

					if (!bytes || feof(fil_in)) {
1933
						goto unexpected_eof;
1934 1935 1936 1937 1938 1939
					}
				}
			}
		}

		/* main checksumming loop */
1940 1941
		cur_page_num = start_page ? start_page : cur_page_num + 1;

1942 1943 1944 1945
		while (!feof(fil_in)) {

			bytes = read_file(buf, partial_page_read,
					  static_cast<ulong>(
1946
						  page_size.physical()), fil_in);
1947 1948 1949
			partial_page_read = false;

			if (!bytes && feof(fil_in)) {
1950 1951 1952 1953 1954 1955 1956 1957 1958
				if (cur_page_num == start_page) {
unexpected_eof:
					fputs("Error: Unable "
					      "to seek to necessary offset\n",
					      stderr);

					exit_status = 1;
					goto my_exit;
				}
1959 1960 1961 1962
				break;
			}

			if (ferror(fil_in)) {
1963
				fprintf(stderr, "Error reading " ULINTPF " bytes",
1964 1965 1966
					page_size.physical());
				perror(" ");

1967 1968
				exit_status = 1;
				goto my_exit;
1969 1970 1971
			}

			if (bytes != page_size.physical()) {
1972
				fprintf(stderr, "Error: bytes read (" ULINTPF ") "
1973
					"doesn't match page size (" ULINTPF ")\n",
1974
					bytes, page_size.physical());
1975 1976
				exit_status = 1;
				goto my_exit;
1977 1978
			}

1979
first_non_zero:
1980 1981 1982 1983 1984
			if (is_system_tablespace) {
				/* enable when page is double write buffer.*/
				skip_page = is_page_doublewritebuffer(buf);
			} else {
				skip_page = false;
1985 1986
			}

1987
			ulint cur_page_type = mach_read_from_2(buf+FIL_PAGE_TYPE);
1988

1989 1990 1991 1992
			/* FIXME: Page compressed or Page compressed and encrypted
			pages do not contain checksum. */
			if (cur_page_type == FIL_PAGE_PAGE_COMPRESSED ||
			    cur_page_type == FIL_PAGE_PAGE_COMPRESSED_ENCRYPTED) {
1993
				skip_page = true;
1994 1995 1996 1997
			}

			/* If no-check is enabled, skip the
			checksum verification.*/
1998 1999 2000 2001
			if (!no_check &&
			    !is_page_free(xdes, page_size, cur_page_num) &&
			    !skip_page &&
			    (exit_status = verify_checksum(
2002 2003 2004
						buf, page_size,
						is_encrypted, is_compressed,
						&mismatch_count))) {
2005
				goto my_exit;
2006 2007
			}

2008 2009 2010
			if ((exit_status = rewrite_checksum(filename, fil_in, buf,
						page_size, &pos, is_encrypted, is_compressed))) {
				goto my_exit;
2011 2012 2013 2014 2015 2016 2017
			}

			/* end if this was the last page we were supposed to check */
			if (use_end_page && (cur_page_num >= end_page)) {
				break;
			}

2018
			if (per_page_details) {
2019
				printf("page " UINT32PF " ", cur_page_num);
2020 2021
			}

2022 2023 2024 2025
			if (page_get_page_no(buf) % physical_page_size == 0) {
				memcpy(xdes, buf, physical_page_size);
			}

2026
			if (page_type_summary || page_type_dump) {
2027
				parse_page(buf, xdes, fil_page_type, page_size, is_encrypted);
2028 2029 2030 2031
			}

			/* do counter increase and progress printing */
			cur_page_num++;
2032

2033 2034
			if (verbose && !read_from_stdin) {
				if ((cur_page_num % 64) == 0) {
2035
					time_t now = time(0);
2036 2037
					if (!lastt) {
						lastt= now;
2038
					} else if (now - lastt >= 1 && is_log_enabled) {
2039
						fprintf(log_file, "page::" UINT32PF " "
2040 2041
							"okay: %.3f%% done\n",
							(cur_page_num - 1),
2042
							(double) cur_page_num / pages * 100);
2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064
						lastt = now;
					}
				}
			}
		}

		if (!read_from_stdin) {
			/* flcose() will flush the data and release the lock if
			any acquired. */
			fclose(fil_in);
		}

		/* Enabled for page type summary. */
		if (page_type_summary) {
			if (!read_from_stdin) {
				fprintf(stdout, "\nFile::%s",filename);
				print_summary(stdout);
			} else {
				print_summary(stderr);
			}
		}
	}
2065

2066 2067
	if (is_log_enabled) {
		fclose(log_file);
2068 2069
	}

2070 2071
	free(buf_ptr);
	free(xdes_ptr);
2072

2073 2074
	my_end(exit_status);
	DBUG_RETURN(exit_status);
2075

2076 2077 2078
my_exit:
	if (buf_ptr) {
		free(buf_ptr);
2079 2080
	}

2081 2082
	if (xdes_ptr) {
		free(xdes_ptr);
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092
	}

	if (!read_from_stdin && fil_in) {
		fclose(fil_in);
	}

	if (log_file) {
		fclose(log_file);
	}

2093 2094
	my_end(exit_status);
	DBUG_RETURN(exit_status);
2095
}