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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
|
/******************************************************
XtraBackup: hot backup tool for InnoDB
(c) 2009-2012 Percona Inc.
Originally Created 3/3/2009 Yasufumi Kinoshita
Written by Alexey Kopytov, Aleksandr Kuzminsky, Stewart Smith, Vadim Tkachenko,
Yasufumi Kinoshita, Ignacio Nin and Baron Schwartz.
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 Street, Fifth Floor, Boston, MA 02110-1335 USA
*******************************************************/
/* Changed page bitmap implementation */
#include "changed_page_bitmap.h"
#include "common.h"
#include "xtrabackup.h"
#include "srv0srv.h"
/* TODO: copy-pasted shared definitions from the XtraDB bitmap write code.
Remove these on the first opportunity, i.e. single-binary XtraBackup. */
/* log0online.h */
/** Single bitmap file information */
struct log_online_bitmap_file_t {
char name[FN_REFLEN]; /*!< Name with full path */
pfs_os_file_t file; /*!< Handle to opened file */
ib_uint64_t size; /*!< Size of the file */
ib_uint64_t offset; /*!< Offset of the next read,
or count of already-read bytes
*/
};
/** A set of bitmap files containing some LSN range */
struct log_online_bitmap_file_range_t {
size_t count; /*!< Number of files */
/*!< Dynamically-allocated array of info about individual files */
struct files_t {
char name[FN_REFLEN];/*!< Name of a file */
lsn_t start_lsn; /*!< Starting LSN of data in this
file */
ulong seq_num; /*!< Sequence number of this file */
} *files;
};
/* log0online.c */
/** File name stem for bitmap files. */
static const char* bmp_file_name_stem = "ib_modified_log_";
/** The bitmap file block size in bytes. All writes will be multiples of this.
*/
enum {
MODIFIED_PAGE_BLOCK_SIZE = 4096
};
/** Offsets in a file bitmap block */
enum {
MODIFIED_PAGE_IS_LAST_BLOCK = 0,/* 1 if last block in the current
write, 0 otherwise. */
MODIFIED_PAGE_START_LSN = 4, /* The starting tracked LSN of this and
other blocks in the same write */
MODIFIED_PAGE_END_LSN = 12, /* The ending tracked LSN of this and
other blocks in the same write */
MODIFIED_PAGE_SPACE_ID = 20, /* The space ID of tracked pages in
this block */
MODIFIED_PAGE_1ST_PAGE_ID = 24, /* The page ID of the first tracked
page in this block */
MODIFIED_PAGE_BLOCK_UNUSED_1 = 28,/* Unused in order to align the start
of bitmap at 8 byte boundary */
MODIFIED_PAGE_BLOCK_BITMAP = 32,/* Start of the bitmap itself */
MODIFIED_PAGE_BLOCK_UNUSED_2 = MODIFIED_PAGE_BLOCK_SIZE - 8,
/* Unused in order to align the end of
bitmap at 8 byte boundary */
MODIFIED_PAGE_BLOCK_CHECKSUM = MODIFIED_PAGE_BLOCK_SIZE - 4
/* The checksum of the current block */
};
/** Length of the bitmap data in a block */
enum { MODIFIED_PAGE_BLOCK_BITMAP_LEN
= MODIFIED_PAGE_BLOCK_UNUSED_2 - MODIFIED_PAGE_BLOCK_BITMAP };
/** Length of the bitmap data in a block in page ids */
enum { MODIFIED_PAGE_BLOCK_ID_COUNT = MODIFIED_PAGE_BLOCK_BITMAP_LEN * 8 };
typedef ib_uint64_t bitmap_word_t;
/****************************************************************//**
Calculate a bitmap block checksum. Algorithm borrowed from
log_block_calc_checksum.
@return checksum */
UNIV_INLINE
ulint
log_online_calc_checksum(
/*=====================*/
const byte* block); /*!<in: bitmap block */
/****************************************************************//**
Provide a comparisson function for the RB-tree tree (space,
block_start_page) pairs. Actual implementation does not matter as
long as the ordering is full.
@return -1 if p1 < p2, 0 if p1 == p2, 1 if p1 > p2
*/
static
int
log_online_compare_bmp_keys(
/*========================*/
const void* p1, /*!<in: 1st key to compare */
const void* p2) /*!<in: 2nd key to compare */
{
const byte *k1 = (const byte *)p1;
const byte *k2 = (const byte *)p2;
ulint k1_space = mach_read_from_4(k1 + MODIFIED_PAGE_SPACE_ID);
ulint k2_space = mach_read_from_4(k2 + MODIFIED_PAGE_SPACE_ID);
if (k1_space == k2_space) {
ulint k1_start_page
= mach_read_from_4(k1 + MODIFIED_PAGE_1ST_PAGE_ID);
ulint k2_start_page
= mach_read_from_4(k2 + MODIFIED_PAGE_1ST_PAGE_ID);
return k1_start_page < k2_start_page
? -1 : k1_start_page > k2_start_page ? 1 : 0;
}
return k1_space < k2_space ? -1 : 1;
}
/****************************************************************//**
Calculate a bitmap block checksum. Algorithm borrowed from
log_block_calc_checksum.
@return checksum */
UNIV_INLINE
ulint
log_online_calc_checksum(
/*=====================*/
const byte* block) /*!<in: bitmap block */
{
ulint sum;
ulint sh;
ulint i;
sum = 1;
sh = 0;
for (i = 0; i < MODIFIED_PAGE_BLOCK_CHECKSUM; i++) {
ulint b = block[i];
sum &= 0x7FFFFFFFUL;
sum += b;
sum += b << sh;
sh++;
if (sh > 24) {
sh = 0;
}
}
return sum;
}
/****************************************************************//**
Read one bitmap data page and check it for corruption.
@return TRUE if page read OK, FALSE if I/O error */
static
ibool
log_online_read_bitmap_page(
/*========================*/
log_online_bitmap_file_t *bitmap_file, /*!<in/out: bitmap
file */
byte *page, /*!<out: read page. Must be at
least MODIFIED_PAGE_BLOCK_SIZE
bytes long */
ibool *checksum_ok) /*!<out: TRUE if page
checksum OK */
{
ulint checksum;
ulint actual_checksum;
ut_a(bitmap_file->size >= MODIFIED_PAGE_BLOCK_SIZE);
ut_a(bitmap_file->offset
<= bitmap_file->size - MODIFIED_PAGE_BLOCK_SIZE);
ut_a(bitmap_file->offset % MODIFIED_PAGE_BLOCK_SIZE == 0);
if (DB_SUCCESS !=
os_file_read(IORequestRead, bitmap_file->file, page,
bitmap_file->offset, MODIFIED_PAGE_BLOCK_SIZE,
nullptr)) {
/* The following call prints an error message */
os_file_get_last_error(TRUE);
msg("InnoDB: Warning: failed reading changed page bitmap "
"file \'%s\'", bitmap_file->name);
return FALSE;
}
bitmap_file->offset += MODIFIED_PAGE_BLOCK_SIZE;
ut_ad(bitmap_file->offset <= bitmap_file->size);
checksum = mach_read_from_4(page + MODIFIED_PAGE_BLOCK_CHECKSUM);
actual_checksum = log_online_calc_checksum(page);
*checksum_ok = (checksum == actual_checksum);
return TRUE;
}
/*********************************************************************//**
Check the name of a given file if it's a changed page bitmap file and
return file sequence and start LSN name components if it is. If is not,
the values of output parameters are undefined.
@return TRUE if a given file is a changed page bitmap file. */
static
ibool
log_online_is_bitmap_file(
/*======================*/
const os_file_stat_t* file_info, /*!<in: file to
check */
ulong* bitmap_file_seq_num, /*!<out: bitmap file
sequence number */
lsn_t* bitmap_file_start_lsn) /*!<out: bitmap file
start LSN */
{
char stem[FN_REFLEN];
ut_ad (strlen(file_info->name) < OS_FILE_MAX_PATH);
return ((file_info->type == OS_FILE_TYPE_FILE
|| file_info->type == OS_FILE_TYPE_LINK)
&& (sscanf(file_info->name, "%[a-z_]%lu_" LSN_PF ".xdb", stem,
bitmap_file_seq_num, bitmap_file_start_lsn) == 3)
&& (!strcmp(stem, bmp_file_name_stem)));
}
/*********************************************************************//**
List the bitmap files in srv_data_home and setup their range that contains the
specified LSN interval. This range, if non-empty, will start with a file that
has the greatest LSN equal to or less than the start LSN and will include all
the files up to the one with the greatest LSN less than the end LSN. Caller
must free bitmap_files->files when done if bitmap_files set to non-NULL and
this function returned TRUE. Field bitmap_files->count might be set to a
larger value than the actual count of the files, and space for the unused array
slots will be allocated but cleared to zeroes.
@return TRUE if succeeded
*/
static
ibool
log_online_setup_bitmap_file_range(
/*===============================*/
log_online_bitmap_file_range_t *bitmap_files, /*!<in/out: bitmap file
range */
lsn_t range_start, /*!<in: start LSN */
lsn_t range_end) /*!<in: end LSN */
{
os_file_dir_t bitmap_dir;
os_file_stat_t bitmap_dir_file_info;
ulong first_file_seq_num = ULONG_MAX;
ulong last_file_seq_num = 0;
lsn_t first_file_start_lsn = LSN_MAX;
xb_ad(range_end >= range_start);
bitmap_files->count = 0;
bitmap_files->files = NULL;
/* 1st pass: size the info array */
bitmap_dir = os_file_opendir(srv_data_home);
if (UNIV_UNLIKELY(bitmap_dir == IF_WIN(INVALID_HANDLE_VALUE, NULL))) {
msg("InnoDB: Error: failed to open bitmap directory \'%s\'",
srv_data_home);
return FALSE;
}
while (!os_file_readdir_next_file(srv_data_home, bitmap_dir,
&bitmap_dir_file_info)) {
ulong file_seq_num;
lsn_t file_start_lsn;
if (!log_online_is_bitmap_file(&bitmap_dir_file_info,
&file_seq_num,
&file_start_lsn)
|| file_start_lsn >= range_end) {
continue;
}
if (file_seq_num > last_file_seq_num) {
last_file_seq_num = file_seq_num;
}
if (file_start_lsn >= range_start
|| file_start_lsn == first_file_start_lsn
|| first_file_start_lsn > range_start) {
/* A file that falls into the range */
if (file_start_lsn < first_file_start_lsn) {
first_file_start_lsn = file_start_lsn;
}
if (file_seq_num < first_file_seq_num) {
first_file_seq_num = file_seq_num;
}
} else if (file_start_lsn > first_file_start_lsn) {
/* A file that has LSN closer to the range start
but smaller than it, replacing another such file */
first_file_start_lsn = file_start_lsn;
first_file_seq_num = file_seq_num;
}
}
if (UNIV_UNLIKELY(os_file_closedir_failed(bitmap_dir))) {
os_file_get_last_error(TRUE);
msg("InnoDB: Error: cannot close \'%s\'",srv_data_home);
return FALSE;
}
if (first_file_seq_num == ULONG_MAX && last_file_seq_num == 0) {
bitmap_files->count = 0;
return TRUE;
}
bitmap_files->count = last_file_seq_num - first_file_seq_num + 1;
/* 2nd pass: get the file names in the file_seq_num order */
bitmap_dir = os_file_opendir(srv_data_home);
if (UNIV_UNLIKELY(bitmap_dir == IF_WIN(INVALID_HANDLE_VALUE, NULL))) {
msg("InnoDB: Error: failed to open bitmap directory \'%s\'",
srv_data_home);
return FALSE;
}
bitmap_files->files =
static_cast<log_online_bitmap_file_range_t::files_t *>
(malloc(bitmap_files->count * sizeof(bitmap_files->files[0])));
memset(bitmap_files->files, 0,
bitmap_files->count * sizeof(bitmap_files->files[0]));
while (!os_file_readdir_next_file(srv_data_home, bitmap_dir,
&bitmap_dir_file_info)) {
ulong file_seq_num;
lsn_t file_start_lsn;
size_t array_pos;
if (!log_online_is_bitmap_file(&bitmap_dir_file_info,
&file_seq_num,
&file_start_lsn)
|| file_start_lsn >= range_end
|| file_start_lsn < first_file_start_lsn) {
continue;
}
array_pos = file_seq_num - first_file_seq_num;
if (UNIV_UNLIKELY(array_pos >= bitmap_files->count)) {
msg("InnoDB: Error: inconsistent bitmap file "
"directory");
os_file_closedir(bitmap_dir);
free(bitmap_files->files);
return FALSE;
}
if (file_seq_num > bitmap_files->files[array_pos].seq_num) {
bitmap_files->files[array_pos].seq_num = file_seq_num;
strncpy(bitmap_files->files[array_pos].name,
bitmap_dir_file_info.name, FN_REFLEN - 1);
bitmap_files->files[array_pos].name[FN_REFLEN - 1]
= '\0';
bitmap_files->files[array_pos].start_lsn
= file_start_lsn;
}
}
if (UNIV_UNLIKELY(os_file_closedir_failed(bitmap_dir))) {
os_file_get_last_error(TRUE);
msg("InnoDB: Error: cannot close \'%s\'", srv_data_home);
free(bitmap_files->files);
return FALSE;
}
#ifdef UNIV_DEBUG
ut_ad(bitmap_files->files[0].seq_num == first_file_seq_num);
for (size_t i = 1; i < bitmap_files->count; i++) {
if (!bitmap_files->files[i].seq_num) {
break;
}
ut_ad(bitmap_files->files[i].seq_num
> bitmap_files->files[i - 1].seq_num);
ut_ad(bitmap_files->files[i].start_lsn
>= bitmap_files->files[i - 1].start_lsn);
}
#endif
return TRUE;
}
/****************************************************************//**
Open a bitmap file for reading.
@return whether opened successfully */
static
bool
log_online_open_bitmap_file_read_only(
/*==================================*/
const char* name, /*!<in: bitmap file
name without directory,
which is assumed to be
srv_data_home */
log_online_bitmap_file_t* bitmap_file) /*!<out: opened bitmap
file */
{
bool success = false;
xb_ad(name[0] != '\0');
snprintf(bitmap_file->name, FN_REFLEN, "%s%s", srv_data_home, name);
bitmap_file->file = os_file_create_simple_no_error_handling(
0, bitmap_file->name,
OS_FILE_OPEN, OS_FILE_READ_ONLY, true, &success);
if (UNIV_UNLIKELY(!success)) {
/* Here and below assume that bitmap file names do not
contain apostrophes, thus no need for ut_print_filename(). */
msg("InnoDB: Warning: error opening the changed page "
"bitmap \'%s\'", bitmap_file->name);
return success;
}
bitmap_file->size = os_file_get_size(bitmap_file->file);
bitmap_file->offset = 0;
#ifdef __linux__
posix_fadvise(bitmap_file->file, 0, 0, POSIX_FADV_SEQUENTIAL);
posix_fadvise(bitmap_file->file, 0, 0, POSIX_FADV_NOREUSE);
#endif
return success;
}
/****************************************************************//**
Diagnose one or both of the following situations if we read close to
the end of bitmap file:
1) Warn if the remainder of the file is less than one page.
2) Error if we cannot read any more full pages but the last read page
did not have the last-in-run flag set.
@return FALSE for the error */
static
ibool
log_online_diagnose_bitmap_eof(
/*===========================*/
const log_online_bitmap_file_t* bitmap_file, /*!< in: bitmap file */
ibool last_page_in_run)/*!< in: "last page in
run" flag value in the
last read page */
{
/* Check if we are too close to EOF to read a full page */
if ((bitmap_file->size < MODIFIED_PAGE_BLOCK_SIZE)
|| (bitmap_file->offset
> bitmap_file->size - MODIFIED_PAGE_BLOCK_SIZE)) {
if (UNIV_UNLIKELY(bitmap_file->offset != bitmap_file->size)) {
/* If we are not at EOF and we have less than one page
to read, it's junk. This error is not fatal in
itself. */
msg("InnoDB: Warning: junk at the end of changed "
"page bitmap file \'%s\'.", bitmap_file->name);
}
if (UNIV_UNLIKELY(!last_page_in_run)) {
/* We are at EOF but the last read page did not finish
a run */
/* It's a "Warning" here because it's not a fatal error
for the whole server */
msg("InnoDB: Warning: changed page bitmap "
"file \'%s\' does not contain a complete run "
"at the end.", bitmap_file->name);
return FALSE;
}
}
return TRUE;
}
/* End of copy-pasted definitions */
/** Iterator structure over changed page bitmap */
struct xb_page_bitmap_range_struct {
const xb_page_bitmap *bitmap; /* Bitmap with data */
ulint space_id; /* Space id for this
iterator */
ulint bit_i; /* Bit index of the iterator
position in the current page */
const ib_rbt_node_t *bitmap_node; /* Current bitmap tree node */
const byte *bitmap_page; /* Current bitmap page */
ulint current_page_id;/* Current page id */
};
/****************************************************************//**
Print a diagnostic message on missing bitmap data for an LSN range. */
static
void
xb_msg_missing_lsn_data(
/*====================*/
lsn_t missing_interval_start, /*!<in: interval start */
lsn_t missing_interval_end) /*!<in: interval end */
{
msg("mariabackup: warning: changed page data missing for LSNs between "
LSN_PF " and " LSN_PF, missing_interval_start,
missing_interval_end);
}
/****************************************************************//**
Scan a bitmap file until data for a desired LSN or EOF is found and check that
the page before the starting one is not corrupted to ensure that the found page
indeed contains the very start of the desired LSN data. The caller must check
the page LSN values to determine if the bitmap file was scanned until the data
was found or until EOF. Page must be at least MODIFIED_PAGE_BLOCK_SIZE big.
@return TRUE if the scan successful without corruption detected
*/
static
ibool
xb_find_lsn_in_bitmap_file(
/*=======================*/
log_online_bitmap_file_t *bitmap_file, /*!<in/out: bitmap
file */
byte *page, /*!<in/out: last read
bitmap page */
lsn_t *page_end_lsn, /*!<out: end LSN of the
last read page */
lsn_t lsn) /*!<in: LSN to find */
{
ibool last_page_ok = TRUE;
ibool next_to_last_page_ok = TRUE;
xb_ad (bitmap_file->size >= MODIFIED_PAGE_BLOCK_SIZE);
*page_end_lsn = 0;
while ((*page_end_lsn <= lsn)
&& (bitmap_file->offset
<= bitmap_file->size - MODIFIED_PAGE_BLOCK_SIZE)) {
next_to_last_page_ok = last_page_ok;
if (!log_online_read_bitmap_page(bitmap_file, page,
&last_page_ok)) {
return FALSE;
}
*page_end_lsn = mach_read_from_8(page + MODIFIED_PAGE_END_LSN);
}
/* We check two pages here because the last read page already contains
the required LSN data. If the next to the last one page is corrupted,
then we have no way of telling if that page contained the required LSN
range data too */
return last_page_ok && next_to_last_page_ok;
}
/****************************************************************//**
Read the disk bitmap and build the changed page bitmap tree for the
LSN interval incremental_lsn to log_sys.next_checkpoint_lsn.
@return the built bitmap tree or NULL if unable to read the full interval for
any reason. */
xb_page_bitmap*
xb_page_bitmap_init(void)
/*=====================*/
{
log_online_bitmap_file_t bitmap_file;
lsn_t bmp_start_lsn = incremental_lsn;
const lsn_t bmp_end_lsn{log_sys.next_checkpoint_lsn};
byte page[MODIFIED_PAGE_BLOCK_SIZE];
lsn_t current_page_end_lsn;
xb_page_bitmap *result;
ibool last_page_in_run= FALSE;
log_online_bitmap_file_range_t bitmap_files;
size_t bmp_i;
ibool last_page_ok = TRUE;
if (UNIV_UNLIKELY(bmp_start_lsn > bmp_end_lsn)) {
msg("mariabackup: incremental backup LSN " LSN_PF
" is larger than than the last checkpoint LSN " LSN_PF
, bmp_start_lsn, bmp_end_lsn);
return NULL;
}
if (!log_online_setup_bitmap_file_range(&bitmap_files, bmp_start_lsn,
bmp_end_lsn)) {
return NULL;
}
/* Only accept no bitmap files returned if start LSN == end LSN */
if (bitmap_files.count == 0 && bmp_end_lsn != bmp_start_lsn) {
return NULL;
}
result = rbt_create(MODIFIED_PAGE_BLOCK_SIZE,
log_online_compare_bmp_keys);
if (bmp_start_lsn == bmp_end_lsn) {
/* Empty range - empty bitmap */
return result;
}
bmp_i = 0;
if (UNIV_UNLIKELY(bitmap_files.files[bmp_i].start_lsn
> bmp_start_lsn)) {
/* The 1st file does not have the starting LSN data */
xb_msg_missing_lsn_data(bmp_start_lsn,
bitmap_files.files[bmp_i].start_lsn);
rbt_free(result);
free(bitmap_files.files);
return NULL;
}
/* Skip any zero-sized files at the start */
while ((bmp_i < bitmap_files.count - 1)
&& (bitmap_files.files[bmp_i].start_lsn
== bitmap_files.files[bmp_i + 1].start_lsn)) {
bmp_i++;
}
/* Is the 1st bitmap file missing? */
if (UNIV_UNLIKELY(bitmap_files.files[bmp_i].name[0] == '\0')) {
/* TODO: this is not the exact missing range */
xb_msg_missing_lsn_data(bmp_start_lsn, bmp_end_lsn);
rbt_free(result);
free(bitmap_files.files);
return NULL;
}
/* Open the 1st bitmap file */
if (UNIV_UNLIKELY(!log_online_open_bitmap_file_read_only(
bitmap_files.files[bmp_i].name,
&bitmap_file))) {
rbt_free(result);
free(bitmap_files.files);
return NULL;
}
/* If the 1st file is truncated, no data. Not merged with the case
below because zero-length file indicates not a corruption but missing
subsequent files instead. */
if (UNIV_UNLIKELY(bitmap_file.size < MODIFIED_PAGE_BLOCK_SIZE)) {
xb_msg_missing_lsn_data(bmp_start_lsn, bmp_end_lsn);
rbt_free(result);
free(bitmap_files.files);
os_file_close(bitmap_file.file);
return NULL;
}
/* Find the start of the required LSN range in the file */
if (UNIV_UNLIKELY(!xb_find_lsn_in_bitmap_file(&bitmap_file, page,
¤t_page_end_lsn,
bmp_start_lsn))) {
msg("mariabackup: Warning: changed page bitmap file "
"\'%s\' corrupted", bitmap_file.name);
rbt_free(result);
free(bitmap_files.files);
os_file_close(bitmap_file.file);
return NULL;
}
last_page_in_run
= mach_read_from_4(page + MODIFIED_PAGE_IS_LAST_BLOCK);
if (UNIV_UNLIKELY(!log_online_diagnose_bitmap_eof(&bitmap_file,
last_page_in_run))) {
rbt_free(result);
free(bitmap_files.files);
os_file_close(bitmap_file.file);
return NULL;
}
if (UNIV_UNLIKELY(current_page_end_lsn < bmp_start_lsn)) {
xb_msg_missing_lsn_data(current_page_end_lsn, bmp_start_lsn);
rbt_free(result);
free(bitmap_files.files);
os_file_close(bitmap_file.file);
return NULL;
}
/* 1st bitmap page found, add it to the tree. */
rbt_insert(result, page, page);
/* Read next pages/files until all required data is read */
while (last_page_ok
&& (current_page_end_lsn < bmp_end_lsn
|| (current_page_end_lsn == bmp_end_lsn
&& !last_page_in_run))) {
ib_rbt_bound_t tree_search_pos;
/* If EOF, advance the file skipping over any empty files */
while (bitmap_file.size < MODIFIED_PAGE_BLOCK_SIZE
|| (bitmap_file.offset
> bitmap_file.size - MODIFIED_PAGE_BLOCK_SIZE)) {
os_file_close(bitmap_file.file);
if (UNIV_UNLIKELY(
!log_online_diagnose_bitmap_eof(
&bitmap_file, last_page_in_run))) {
rbt_free(result);
free(bitmap_files.files);
return NULL;
}
bmp_i++;
if (UNIV_UNLIKELY(bmp_i == bitmap_files.count
|| (bitmap_files.files[bmp_i].seq_num
== 0))) {
xb_msg_missing_lsn_data(current_page_end_lsn,
bmp_end_lsn);
rbt_free(result);
free(bitmap_files.files);
return NULL;
}
/* Is the next file missing? */
if (UNIV_UNLIKELY(bitmap_files.files[bmp_i].name[0]
== '\0')) {
/* TODO: this is not the exact missing range */
xb_msg_missing_lsn_data(bitmap_files.files
[bmp_i - 1].start_lsn,
bmp_end_lsn);
rbt_free(result);
free(bitmap_files.files);
return NULL;
}
if (UNIV_UNLIKELY(
!log_online_open_bitmap_file_read_only(
bitmap_files.files[bmp_i].name,
&bitmap_file))) {
rbt_free(result);
free(bitmap_files.files);
return NULL;
}
}
if (UNIV_UNLIKELY(
!log_online_read_bitmap_page(&bitmap_file, page,
&last_page_ok))) {
rbt_free(result);
free(bitmap_files.files);
os_file_close(bitmap_file.file);
return NULL;
}
if (UNIV_UNLIKELY(!last_page_ok)) {
msg("mariabackup: warning: changed page bitmap file "
"\'%s\' corrupted.", bitmap_file.name);
rbt_free(result);
free(bitmap_files.files);
os_file_close(bitmap_file.file);
return NULL;
}
/* Merge the current page with an existing page or insert a new
page into the tree */
if (!rbt_search(result, &tree_search_pos, page)) {
/* Merge the bitmap pages */
byte *existing_page
= rbt_value(byte, tree_search_pos.last);
bitmap_word_t *bmp_word_1 = (bitmap_word_t *)
(existing_page + MODIFIED_PAGE_BLOCK_BITMAP);
bitmap_word_t *bmp_end = (bitmap_word_t *)
(existing_page + MODIFIED_PAGE_BLOCK_UNUSED_2);
bitmap_word_t *bmp_word_2 = (bitmap_word_t *)
(page + MODIFIED_PAGE_BLOCK_BITMAP);
while (bmp_word_1 < bmp_end) {
*bmp_word_1++ |= *bmp_word_2++;
}
xb_a (bmp_word_1 == bmp_end);
} else {
/* Add a new page */
rbt_add_node(result, &tree_search_pos, page);
}
current_page_end_lsn
= mach_read_from_8(page + MODIFIED_PAGE_END_LSN);
last_page_in_run
= mach_read_from_4(page + MODIFIED_PAGE_IS_LAST_BLOCK);
}
xb_a (current_page_end_lsn >= bmp_end_lsn);
free(bitmap_files.files);
os_file_close(bitmap_file.file);
return result;
}
/****************************************************************//**
Free the bitmap tree. */
void
xb_page_bitmap_deinit(
/*==================*/
xb_page_bitmap* bitmap) /*!<in/out: bitmap tree */
{
if (bitmap) {
rbt_free(bitmap);
}
}
/****************************************************************//**
Advance to the next bitmap page or setup the first bitmap page for the
given bitmap range. Assumes that bitmap_range->bitmap_page has been
already found/bumped by rbt_search()/rbt_next().
@return FALSE if no more bitmap data for the range space ID */
static
ibool
xb_page_bitmap_setup_next_page(
/*===========================*/
xb_page_bitmap_range* bitmap_range) /*!<in/out: the bitmap range */
{
ulint new_space_id;
ulint new_1st_page_id;
if (bitmap_range->bitmap_node == NULL) {
bitmap_range->current_page_id = ULINT_UNDEFINED;
return FALSE;
}
bitmap_range->bitmap_page = rbt_value(byte, bitmap_range->bitmap_node);
new_space_id = mach_read_from_4(bitmap_range->bitmap_page
+ MODIFIED_PAGE_SPACE_ID);
if (new_space_id != bitmap_range->space_id) {
/* No more data for the current page id. */
xb_a(new_space_id > bitmap_range->space_id);
bitmap_range->current_page_id = ULINT_UNDEFINED;
return FALSE;
}
new_1st_page_id = mach_read_from_4(bitmap_range->bitmap_page +
MODIFIED_PAGE_1ST_PAGE_ID);
xb_a (new_1st_page_id >= bitmap_range->current_page_id
|| bitmap_range->current_page_id == ULINT_UNDEFINED);
bitmap_range->current_page_id = new_1st_page_id;
bitmap_range->bit_i = 0;
return TRUE;
}
/** Find the node with the smallest key that greater than equal to search key.
@param[in] tree red-black tree
@param[in] key search key
@return node with the smallest greater-than-or-equal key
@retval NULL if none was found */
static
const ib_rbt_node_t*
rbt_lower_bound(const ib_rbt_t* tree, const void* key)
{
ut_ad(!tree->cmp_arg);
const ib_rbt_node_t* ge = NULL;
for (const ib_rbt_node_t *node = tree->root->left;
node != tree->nil; ) {
int result = tree->compare(node->value, key);
if (result < 0) {
node = node->right;
} else {
ge = node;
if (result == 0) {
break;
}
node = node->left;
}
}
return(ge);
}
/****************************************************************//**
Set up a new bitmap range iterator over a given space id changed
pages in a given bitmap.
@return bitmap range iterator */
xb_page_bitmap_range*
xb_page_bitmap_range_init(
/*======================*/
xb_page_bitmap* bitmap, /*!< in: bitmap to iterate over */
ulint space_id) /*!< in: space id */
{
byte search_page[MODIFIED_PAGE_BLOCK_SIZE];
xb_page_bitmap_range *result
= static_cast<xb_page_bitmap_range *>(malloc(sizeof(*result)));
memset(result, 0, sizeof(*result));
result->bitmap = bitmap;
result->space_id = space_id;
result->current_page_id = ULINT_UNDEFINED;
/* Search for the 1st page for the given space id */
/* This also sets MODIFIED_PAGE_1ST_PAGE_ID to 0, which is what we
want. */
memset(search_page, 0, MODIFIED_PAGE_BLOCK_SIZE);
mach_write_to_4(search_page + MODIFIED_PAGE_SPACE_ID, space_id);
result->bitmap_node = rbt_lower_bound(result->bitmap, search_page);
xb_page_bitmap_setup_next_page(result);
return result;
}
/****************************************************************//**
Get the value of the bitmap->range->bit_i bitmap bit
@return the current bit value */
static inline
ibool
is_bit_set(
/*=======*/
const xb_page_bitmap_range* bitmap_range) /*!< in: bitmap
range */
{
return ((*(((bitmap_word_t *)(bitmap_range->bitmap_page
+ MODIFIED_PAGE_BLOCK_BITMAP))
+ (bitmap_range->bit_i >> 6)))
& (1ULL << (bitmap_range->bit_i & 0x3F))) ? TRUE : FALSE;
}
/****************************************************************//**
Get the next page id that has its bit set or cleared, i.e. equal to
bit_value.
@return page id */
ulint
xb_page_bitmap_range_get_next_bit(
/*==============================*/
xb_page_bitmap_range* bitmap_range, /*!< in/out: bitmap range */
ibool bit_value) /*!< in: bit value */
{
if (UNIV_UNLIKELY(bitmap_range->current_page_id
== ULINT_UNDEFINED)) {
return ULINT_UNDEFINED;
}
do {
while (bitmap_range->bit_i < MODIFIED_PAGE_BLOCK_ID_COUNT) {
while (is_bit_set(bitmap_range) != bit_value
&& (bitmap_range->bit_i
< MODIFIED_PAGE_BLOCK_ID_COUNT)) {
bitmap_range->current_page_id++;
bitmap_range->bit_i++;
}
if (bitmap_range->bit_i
< MODIFIED_PAGE_BLOCK_ID_COUNT) {
ulint result = bitmap_range->current_page_id;
bitmap_range->current_page_id++;
bitmap_range->bit_i++;
return result;
}
}
bitmap_range->bitmap_node
= rbt_next(bitmap_range->bitmap,
bitmap_range->bitmap_node);
} while (xb_page_bitmap_setup_next_page(bitmap_range));
return ULINT_UNDEFINED;
}
/****************************************************************//**
Free the bitmap range iterator. */
void
xb_page_bitmap_range_deinit(
/*========================*/
xb_page_bitmap_range* bitmap_range) /*! in/out: bitmap range */
{
free(bitmap_range);
}
|