forked from mysql/mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinnochecksum.cc
1512 lines (1318 loc) · 37.4 KB
/
innochecksum.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
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
684
685
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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
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
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
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. <[email protected]>
Published with a permission.
*/
#include <my_config.h>
#include <my_global.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <my_getopt.h>
#include <m_string.h>
#include <welcome_copyright_notice.h> /* ORACLE_WELCOME_COPYRIGHT_NOTICE */
/* Only parts of these files are included from the InnoDB codebase.
The parts not included are excluded by #ifndef UNIV_INNOCHECKSUM. */
#include "univ.i" /* include all of this */
#include "page0size.h" /* page_size_t */
#include "page0zip.h" /* page_zip_calc_checksum() */
#include "page0page.h" /* PAGE_* */
#include "trx0undo.h" /* TRX_UNDO_* */
#include "fut0lst.h" /* FLST_NODE_SIZE */
#include "buf0checksum.h" /* buf_calc_page_*() */
#include "fil0fil.h" /* FIL_* */
#include "os0file.h"
#include "fsp0fsp.h" /* fsp_flags_get_page_size() &
fsp_flags_get_zip_size() */
#include "mach0data.h" /* mach_read_from_4() */
#include "ut0crc32.h" /* ut_crc32_init() */
#ifdef UNIV_NONINL
# include "fsp0fsp.ic"
# include "mach0data.ic"
# include "ut0rnd.ic"
#endif
/* Global variables */
static bool verbose;
static bool just_count;
static uintmax_t start_page;
static uintmax_t end_page;
static uintmax_t do_page;
static bool use_end_page;
static bool do_one_page;
/* replaces declaration in srv0srv.c */
ulong srv_page_size;
page_size_t univ_page_size(0, 0, false);
extern ulong srv_checksum_algorithm;
/* Current page number (0 based). */
uintmax_t cur_page_num;
/* Skip the checksum verification. */
static bool no_check;
/* Enabled for strict checksum verification. */
bool strict_verify = 0;
/* Enabled for rewrite checksum. */
static bool do_write;
/* Mismatches count allowed (0 by default). */
static uintmax_t allow_mismatches;
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;
#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;
} 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
};
/* Used to define an enumerate type of the "innochecksum algorithm". */
static TYPELIB innochecksum_algorithms_typelib = {
array_elements(innochecksum_algorithms)-1,"",
innochecksum_algorithms, NULL
};
/** 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);
}
univ_page_size.copy_from(
page_size_t(srv_page_size, srv_page_size, false));
return(page_size_t(flags));
}
/** Decompress a page
@param[in,out] buf Page read from disk, uncompressed data will
also be copied to this page
@param[in, out] scratch Page to use for temporary decompress
@param[in] page_size scratch physical size
@return true if decompress succeeded */
static
bool page_decompress(
byte* buf,
byte* scratch,
page_size_t page_size)
{
dberr_t err;
/* Set the dblwr recover flag to false. */
err = os_file_decompress_page(
false, buf, scratch, page_size.physical());
return(err == DB_SUCCESS);
}
#ifdef _WIN32
/***********************************************//*
@param [in] error error no. from the getLastError().
@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 );
return (err_msg);
}
#endif /* _WIN32 */
/***********************************************//*
@param>>_______[in] name>_____name of file.
@retval file pointer; file pointer is NULL when error occured.
*/
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;
}
/* 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. */
fprintf(stderr, "Filename::%s %s\n", name,
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;
}
else {
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 */
if (do_write) {
fil_in = fdopen(fd, "rb+");
} else {
fil_in = fdopen(fd, "rb");
}
return (fil_in);
}
/************************************************************//*
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;
DBUG_ASSERT(physical_page_size >= UNIV_ZIP_SIZE_MIN);
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;
}
/** Check if page is corrupted or not.
@param[in] buf page frame
@param[in] page_size page size
@retval true if page is corrupted otherwise false. */
static
bool
is_page_corrupted(
const byte* buf,
const page_size_t& page_size)
{
/* enable if page is corrupted. */
bool is_corrupted;
/* use to store LSN values. */
ulint logseq;
ulint logseqfield;
if (!page_size.is_compressed()) {
/* 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,
"page::%" PRIuMAX
"; log sequence number:first = " ULINTPF
"; second = " ULINTPF "\n",
cur_page_num, logseq, logseqfield);
if (logseq != logseqfield) {
fprintf(log_file,
"Fail; page %" PRIuMAX
" invalid (fails log "
"sequence number check)\n",
cur_page_num);
}
}
}
is_corrupted = buf_page_is_corrupted(
true, buf, page_size, false, cur_page_num, strict_verify,
is_log_enabled, log_file);
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);
}
}
return (true);
}
/********************************************************************//**
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) {
fprintf(log_file, "page::%" PRIuMAX "; Updated checksum ="
" %u\n", cur_page_num, checksum);
}
} 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) {
fprintf(log_file, "page::%" PRIuMAX "; Updated checksum field1"
" = %u\n", cur_page_num, checksum);
}
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) {
fprintf(log_file, "page::%" PRIuMAX "; Updated checksum "
"field2 = %u\n", cur_page_num, checksum);
}
}
func_exit:
/* 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)) {
fprintf(stderr, "Failed to write page %" PRIuMAX " to %s: %s\n",
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);
}
/*
Parse the page and collect/dump the information about page type
@param [in] page buffer page
@param [in] file file for diagnosis.
*/
void
parse_page(
const byte* page,
FILE* file)
{
unsigned long long id;
ulint undo_page_type;
char str[20]={'\0'};
/* 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)) {
case FIL_PAGE_INDEX:
page_type.n_fil_page_index++;
id = mach_read_from_8(page + PAGE_HEADER + PAGE_INDEX_ID);
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tIndex page\t\t\t|"
"\tindex id=%llu,", cur_page_num, id);
fprintf(file,
" page level=" ULINTPF
", No. of records=" ULINTPF
", garbage=" ULINTPF ", %s\n",
page_header_get_field(page, PAGE_LEVEL),
page_header_get_field(page, PAGE_N_RECS),
page_header_get_field(page, PAGE_GARBAGE), str);
}
break;
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);
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tUndo log page\t\t\t|",
cur_page_num);
}
if (undo_page_type == TRX_UNDO_INSERT) {
page_type.n_undo_insert++;
if (page_type_dump) {
fprintf(file, "\t%s",
"Insert Undo log page");
}
} else if (undo_page_type == TRX_UNDO_UPDATE) {
page_type.n_undo_update++;
if (page_type_dump) {
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++;
if (page_type_dump) {
fprintf(file, ", %s", "Undo log of "
"an active transaction");
}
break;
case TRX_UNDO_CACHED:
page_type.n_undo_state_cached++;
if (page_type_dump) {
fprintf(file, ", %s", "Page is "
"cached for quick reuse");
}
break;
case TRX_UNDO_TO_FREE:
page_type.n_undo_state_to_free++;
if (page_type_dump) {
fprintf(file, ", %s", "Insert undo "
"segment that can be freed");
}
break;
case TRX_UNDO_TO_PURGE:
page_type.n_undo_state_to_purge++;
if (page_type_dump) {
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++;
if (page_type_dump) {
fprintf(file, ", %s", "Undo log of "
"an prepared transaction");
}
break;
default:
page_type.n_undo_state_other++;
break;
}
if(page_type_dump) {
fprintf(file, ", %s\n", str);
}
break;
case FIL_PAGE_INODE:
page_type.n_fil_page_inode++;
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tInode page\t\t\t|"
"\t%s\n",cur_page_num, str);
}
break;
case FIL_PAGE_IBUF_FREE_LIST:
page_type.n_fil_page_ibuf_free_list++;
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tInsert buffer free list"
" page\t|\t%s\n", cur_page_num, str);
}
break;
case FIL_PAGE_TYPE_ALLOCATED:
page_type.n_fil_page_type_allocated++;
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tFreshly allocated "
"page\t\t|\t%s\n", cur_page_num, str);
}
break;
case FIL_PAGE_IBUF_BITMAP:
page_type.n_fil_page_ibuf_bitmap++;
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tInsert Buffer "
"Bitmap\t\t|\t%s\n", cur_page_num, str);
}
break;
case FIL_PAGE_TYPE_SYS:
page_type.n_fil_page_type_sys++;
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tSystem page\t\t\t|"
"\t%s\n",cur_page_num, str);
}
break;
case FIL_PAGE_TYPE_TRX_SYS:
page_type.n_fil_page_type_trx_sys++;
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tTransaction system "
"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++;
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tFile Space "
"Header\t\t|\t%s\n", cur_page_num, str);
}
break;
case FIL_PAGE_TYPE_XDES:
page_type.n_fil_page_type_xdes++;
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tExtent descriptor "
"page\t\t|\t%s\n", cur_page_num, str);
}
break;
case FIL_PAGE_TYPE_BLOB:
page_type.n_fil_page_type_blob++;
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tBLOB page\t\t\t|\t%s\n",
cur_page_num, str);
}
break;
case FIL_PAGE_TYPE_ZBLOB:
page_type.n_fil_page_type_zblob++;
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tCompressed BLOB "
"page\t\t|\t%s\n", cur_page_num, str);
}
break;
case FIL_PAGE_TYPE_ZBLOB2:
page_type.n_fil_page_type_zblob2++;
if (page_type_dump) {
fprintf(file, "#::%8" PRIuMAX "\t\t|\t\tSubsequent Compressed "
"BLOB page\t|\t%s\n", cur_page_num, str);
}
break;
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);
fprintf(fil_out, "%8d\tOther type of page",
page_type.n_fil_page_type_other);
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);
}
/* command line argument for innochecksum tool. */
static struct my_option innochecksum_options[] = {
{"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},
#ifndef DBUG_OFF
{"debug", '#', "Output debug log. See " REFMAN "dbug-package.html",
&dbug_setting, &dbug_setting, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
#endif /* !DBUG_OFF */
{"count", 'c', "Print the count of pages in the file and exits.",
&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).",
&start_page, &start_page, 0, GET_ULL, REQUIRED_ARG,
0, 0, ULLONG_MAX, 0, 1, 0},
{"end_page", 'e', "End at this page number (0 based).",
&end_page, &end_page, 0, GET_ULL, REQUIRED_ARG,
0, 0, ULLONG_MAX, 0, 1, 0},
{"page", 'p', "Check only this page (0 based).",
&do_page, &do_page, 0, GET_ULL, REQUIRED_ARG,
0, 0, ULLONG_MAX, 0, 1, 0},
{"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},
{"log", 'l', "log output.",
&log_filename, &log_filename, 0,
GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
};
/* Print out the Innodb version and machine information. */
static void print_version(void)
{
#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 */
}
static void usage(void)
{
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>] "
"[-p <page>] [-v] [-a <allow mismatches>] [-n] "
"[-C <strict-check>] [-w <write>] [-S] [-D <page type dump>] "
"[-l <log>] <filename or [-]>\n", my_progname);
printf("See " REFMAN "innochecksum.html for usage hints.\n");
my_print_help(innochecksum_options);
my_print_variables(innochecksum_options);
}
extern "C" my_bool
innochecksum_get_one_option(
int optid,
const struct my_option *opt MY_ATTRIBUTE((unused)),
char *argument MY_ATTRIBUTE((unused)))
{
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);