Skip to content

Commit e2e8098

Browse files
Jan Lindströmvuvova
authored andcommitted
Pass down the information should we encrypt the page at os0file.cc
when page compression and google encryption is used.
1 parent e109a66 commit e2e8098

28 files changed

+425
-249
lines changed

storage/innobase/buf/buf0buf.cc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ buf_block_init(
10061006
block->page.comp_buf = NULL;
10071007
block->page.comp_buf_free = NULL;
10081008
block->page.key_version = 0;
1009+
block->page.encrypt_later = false;
10091010

10101011
block->modify_clock = 0;
10111012

@@ -3498,6 +3499,7 @@ buf_page_init_low(
34983499
bpage->comp_buf = NULL;
34993500
bpage->comp_buf_free = NULL;
35003501
bpage->key_version = 0;
3502+
bpage->encrypt_later = false;
35013503

35023504
HASH_INVALIDATE(bpage, hash);
35033505
#if defined UNIV_DEBUG_FILE_ACCESSES || defined UNIV_DEBUG
@@ -5609,6 +5611,8 @@ buf_page_encrypt_before_write(
56095611
buf_page_t* bpage, /*!< in/out: buffer page to be flushed */
56105612
const byte* src_frame) /*!< in: src frame */
56115613
{
5614+
bpage->encrypt_later = false;
5615+
56125616
if (srv_encrypt_tables == FALSE) {
56135617
/* Encryption is disabled */
56145618
return const_cast<byte*>(src_frame);
@@ -5669,7 +5673,8 @@ buf_page_encrypt_before_write(
56695673
ut_ad(key_version == 0 || key_version >= bpage->key_version);
56705674
bpage->key_version = key_version;
56715675
} else {
5672-
// We do compression and encryption later on os0file.cc
5676+
/** Compression and encryption is done later at os0file.cc */
5677+
bpage->encrypt_later = true;
56735678
dst_frame = (byte *)src_frame;
56745679
}
56755680

storage/innobase/buf/buf0dblwr.cc

Lines changed: 86 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -521,10 +521,18 @@ buf_dblwr_process()
521521
ulint zip_size = fil_space_get_zip_size(space_id);
522522

523523
/* Read in the actual page from the file */
524-
fil_io(OS_FILE_READ, true, space_id, zip_size,
525-
page_no, 0,
526-
zip_size ? zip_size : UNIV_PAGE_SIZE,
527-
read_buf, NULL, 0, 0);
524+
fil_io(OS_FILE_READ,
525+
true,
526+
space_id,
527+
zip_size,
528+
page_no,
529+
0,
530+
zip_size ? zip_size : UNIV_PAGE_SIZE,
531+
read_buf,
532+
NULL,
533+
0,
534+
0,
535+
false);
528536

529537
if (fil_space_verify_crypt_checksum(read_buf, zip_size)) {
530538
/* page is encrypted and checksum is OK */
@@ -576,10 +584,18 @@ buf_dblwr_process()
576584
doublewrite buffer to the intended
577585
position */
578586

579-
fil_io(OS_FILE_WRITE, true, space_id,
580-
zip_size, page_no, 0,
581-
zip_size ? zip_size : UNIV_PAGE_SIZE,
582-
page, NULL, 0, 0);
587+
fil_io(OS_FILE_WRITE,
588+
true,
589+
space_id,
590+
zip_size,
591+
page_no,
592+
0,
593+
zip_size ? zip_size : UNIV_PAGE_SIZE,
594+
page,
595+
NULL,
596+
0,
597+
0,
598+
false);
583599

584600
ib_logf(IB_LOG_LEVEL_INFO,
585601
"Recovered the page from"
@@ -595,11 +611,18 @@ buf_dblwr_process()
595611
zeroes, while a valid copy is
596612
available in dblwr buffer. */
597613

598-
fil_io(OS_FILE_WRITE, true, space_id,
599-
zip_size, page_no, 0,
600-
zip_size ? zip_size
601-
: UNIV_PAGE_SIZE,
602-
page, NULL, 0, 0);
614+
fil_io(OS_FILE_WRITE,
615+
true,
616+
space_id,
617+
zip_size,
618+
page_no,
619+
0,
620+
zip_size ? zip_size : UNIV_PAGE_SIZE,
621+
page,
622+
NULL,
623+
0,
624+
0,
625+
false);
603626
}
604627
}
605628
}
@@ -621,9 +644,9 @@ buf_dblwr_process()
621644
memset(buf, 0, bytes);
622645

623646
fil_io(OS_FILE_WRITE, true, TRX_SYS_SPACE, 0,
624-
buf_dblwr->block1, 0, bytes, buf, NULL, NULL, 0);
647+
buf_dblwr->block1, 0, bytes, buf, NULL, NULL, 0, false);
625648
fil_io(OS_FILE_WRITE, true, TRX_SYS_SPACE, 0,
626-
buf_dblwr->block2, 0, bytes, buf, NULL, NULL, 0);
649+
buf_dblwr->block2, 0, bytes, buf, NULL, NULL, 0, false);
627650

628651
ut_free(unaligned_buf);
629652
}
@@ -828,12 +851,18 @@ buf_dblwr_write_block_to_datafile(
828851
void * frame = buf_page_get_frame(bpage);
829852

830853
if (bpage->zip.data) {
831-
fil_io(flags, sync, buf_page_get_space(bpage),
832-
buf_page_get_zip_size(bpage),
833-
buf_page_get_page_no(bpage), 0,
834-
buf_page_get_zip_size(bpage),
835-
frame,
836-
(void*) bpage, 0, bpage->newest_modification);
854+
fil_io(flags,
855+
sync,
856+
buf_page_get_space(bpage),
857+
buf_page_get_zip_size(bpage),
858+
buf_page_get_page_no(bpage),
859+
0,
860+
buf_page_get_zip_size(bpage),
861+
frame,
862+
(void*) bpage,
863+
0,
864+
bpage->newest_modification,
865+
bpage->encrypt_later);
837866

838867
return;
839868
}
@@ -843,12 +872,18 @@ buf_dblwr_write_block_to_datafile(
843872
ut_a(buf_block_get_state(block) == BUF_BLOCK_FILE_PAGE);
844873
buf_dblwr_check_page_lsn(block->frame);
845874

846-
fprintf(stderr, "JAN: space %lu page_type %lu\n", buf_block_get_space(block),
847-
mach_read_from_2((byte *)frame+FIL_PAGE_TYPE));
848-
849-
fil_io(flags, sync, buf_block_get_space(block), 0,
850-
buf_block_get_page_no(block), 0, UNIV_PAGE_SIZE,
851-
frame, (void*) block, (ulint *)&bpage->write_size, bpage->newest_modification );
875+
fil_io(flags,
876+
sync,
877+
buf_block_get_space(block),
878+
0,
879+
buf_block_get_page_no(block),
880+
0,
881+
UNIV_PAGE_SIZE,
882+
frame,
883+
(void*) block,
884+
(ulint *)&bpage->write_size,
885+
bpage->newest_modification,
886+
bpage->encrypt_later);
852887
}
853888

854889
/********************************************************************//**
@@ -942,7 +977,7 @@ buf_dblwr_flush_buffered_writes(void)
942977

943978
fil_io(OS_FILE_WRITE, true, TRX_SYS_SPACE, 0,
944979
buf_dblwr->block1, 0, len,
945-
(void*) write_buf, NULL, 0, 0);
980+
(void*) write_buf, NULL, 0, 0, false);
946981

947982
if (buf_dblwr->first_free <= TRX_SYS_DOUBLEWRITE_BLOCK_SIZE) {
948983
/* No unwritten pages in the second block. */
@@ -958,7 +993,7 @@ buf_dblwr_flush_buffered_writes(void)
958993

959994
fil_io(OS_FILE_WRITE, true, TRX_SYS_SPACE, 0,
960995
buf_dblwr->block2, 0, len,
961-
(void*) write_buf, NULL, 0, 0);
996+
(void*) write_buf, NULL, 0, 0, false);
962997

963998
flush:
964999
/* increment the doublewrite flushed pages counter */
@@ -1187,17 +1222,31 @@ buf_dblwr_write_single_page(
11871222
memset(buf_dblwr->write_buf + UNIV_PAGE_SIZE * i
11881223
+ zip_size, 0, UNIV_PAGE_SIZE - zip_size);
11891224

1190-
fil_io(OS_FILE_WRITE, true, TRX_SYS_SPACE, 0,
1191-
offset, 0, UNIV_PAGE_SIZE,
1192-
(void*) (buf_dblwr->write_buf
1193-
+ UNIV_PAGE_SIZE * i), NULL, 0, bpage->newest_modification);
1225+
fil_io(OS_FILE_WRITE,
1226+
true,
1227+
TRX_SYS_SPACE, 0,
1228+
offset,
1229+
0,
1230+
UNIV_PAGE_SIZE,
1231+
(void*) (buf_dblwr->write_buf + UNIV_PAGE_SIZE * i),
1232+
NULL,
1233+
0,
1234+
bpage->newest_modification,
1235+
bpage->encrypt_later);
11941236
} else {
11951237
/* It is a regular page. Write it directly to the
11961238
doublewrite buffer */
1197-
fil_io(OS_FILE_WRITE, true, TRX_SYS_SPACE, 0,
1198-
offset, 0, UNIV_PAGE_SIZE,
1199-
frame,
1200-
NULL, 0, bpage->newest_modification);
1239+
fil_io(OS_FILE_WRITE,
1240+
true,
1241+
TRX_SYS_SPACE, 0,
1242+
offset,
1243+
0,
1244+
UNIV_PAGE_SIZE,
1245+
frame,
1246+
NULL,
1247+
0,
1248+
bpage->newest_modification,
1249+
bpage->encrypt_later);
12011250
}
12021251

12031252
/* Now flush the doublewrite buffer data to disk */

storage/innobase/buf/buf0flu.cc

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -913,16 +913,17 @@ buf_flush_write_block_low(
913913

914914
if (!srv_use_doublewrite_buf || !buf_dblwr) {
915915
fil_io(OS_FILE_WRITE | OS_AIO_SIMULATED_WAKE_LATER,
916-
sync,
917-
buf_page_get_space(bpage),
918-
zip_size,
919-
buf_page_get_page_no(bpage),
920-
0,
921-
zip_size ? zip_size : UNIV_PAGE_SIZE,
922-
frame,
923-
bpage,
924-
&bpage->write_size,
925-
bpage->newest_modification);
916+
sync,
917+
buf_page_get_space(bpage),
918+
zip_size,
919+
buf_page_get_page_no(bpage),
920+
0,
921+
zip_size ? zip_size : UNIV_PAGE_SIZE,
922+
frame,
923+
bpage,
924+
&bpage->write_size,
925+
bpage->newest_modification,
926+
bpage->encrypt_later);
926927
} else {
927928

928929
/* InnoDB uses doublewrite buffer and doublewrite buffer
@@ -943,7 +944,8 @@ buf_flush_write_block_low(
943944
frame,
944945
bpage,
945946
&bpage->write_size,
946-
bpage->newest_modification);
947+
bpage->newest_modification,
948+
bpage->encrypt_later);
947949
} else if (flush_type == BUF_FLUSH_SINGLE_PAGE) {
948950
buf_dblwr_write_single_page(bpage, sync);
949951
} else {

storage/innobase/buf/buf0rea.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,17 +184,17 @@ buf_read_page_low(
184184

185185
if (zip_size) {
186186
*err = fil_io(OS_FILE_READ | wake_later
187-
| ignore_nonexistent_pages,
188-
sync, space, zip_size, offset, 0, zip_size,
189-
frame, bpage, &bpage->write_size, 0);
187+
| ignore_nonexistent_pages,
188+
sync, space, zip_size, offset, 0, zip_size,
189+
frame, bpage, &bpage->write_size, 0, false);
190190
} else {
191191
ut_a(buf_page_get_state(bpage) == BUF_BLOCK_FILE_PAGE);
192192

193193
*err = fil_io(OS_FILE_READ | wake_later
194-
| ignore_nonexistent_pages,
195-
sync, space, 0, offset, 0, UNIV_PAGE_SIZE,
196-
frame, bpage,
197-
&bpage->write_size, 0);
194+
| ignore_nonexistent_pages,
195+
sync, space, 0, offset, 0, UNIV_PAGE_SIZE,
196+
frame, bpage,
197+
&bpage->write_size, 0, false);
198198
}
199199

200200
if (sync) {

storage/innobase/fil/fil0crypt.cc

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -621,13 +621,25 @@ fil_space_encrypt(ulint space, ulint offset, lsn_t lsn,
621621
byte key[MY_AES_MAX_KEY_LENGTH];
622622
uint key_length;
623623

624+
ulint orig_page_type = mach_read_from_2(src_frame+FIL_PAGE_TYPE);
625+
if (orig_page_type==FIL_PAGE_TYPE_FSP_HDR
626+
|| orig_page_type==FIL_PAGE_TYPE_XDES
627+
|| orig_page_type== FIL_PAGE_PAGE_ENCRYPTED
628+
|| orig_page_type== FIL_PAGE_PAGE_COMPRESSED_ENCRYPTED) {
629+
//TODO: is this really needed ?
630+
memcpy(dst_frame, src_frame, page_size);
631+
return;
632+
}
633+
624634
if (srv_encrypt_tables) {
625635
crypt_data = fil_space_get_crypt_data(space);
636+
626637
if (crypt_data == NULL) {
627638
//TODO: Is this really needed ?
628639
memcpy(dst_frame, src_frame, page_size);
629640
return;
630641
}
642+
631643
fil_crypt_get_latest_key(key, &key_length, crypt_data, &key_version);
632644
} else {
633645
key_version = encryption_key;
@@ -646,9 +658,7 @@ fil_space_encrypt(ulint space, ulint offset, lsn_t lsn,
646658
src_frame + FIL_PAGE_OFFSET);
647659
mach_write_to_4(iv + 4, space_offset);
648660
mach_write_to_8(iv + 8, lsn);
649-
}
650-
else
651-
{
661+
} else {
652662
// take the iv from the key provider
653663

654664
int load_iv_rc = get_encryption_iv(key_version, (uchar *) iv, sizeof(iv));
@@ -666,21 +676,10 @@ fil_space_encrypt(ulint space, ulint offset, lsn_t lsn,
666676
}
667677
}
668678

669-
670679
ibool page_compressed = (mach_read_from_2(src_frame+FIL_PAGE_TYPE) == FIL_PAGE_PAGE_COMPRESSED);
671680
ibool page_encrypted = fil_space_is_page_encrypted(space);
672-
673681
ulint compression_alg = mach_read_from_8(src_frame+FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION);
674682

675-
ulint orig_page_type = mach_read_from_2(src_frame+FIL_PAGE_TYPE);
676-
if (orig_page_type==FIL_PAGE_TYPE_FSP_HDR
677-
|| orig_page_type==FIL_PAGE_TYPE_XDES
678-
|| orig_page_type== FIL_PAGE_PAGE_ENCRYPTED
679-
|| orig_page_type== FIL_PAGE_PAGE_COMPRESSED_ENCRYPTED) {
680-
memcpy(dst_frame, src_frame, page_size);
681-
return;
682-
}
683-
684683
// copy page header
685684
memcpy(dst_frame, src_frame, FIL_PAGE_DATA);
686685

@@ -711,7 +710,6 @@ fil_space_encrypt(ulint space, ulint offset, lsn_t lsn,
711710
srclen = page_size - FIL_PAGE_DATA;;
712711
}
713712

714-
715713
int rc = (* my_aes_encrypt_dynamic)(src, srclen,
716714
dst, &dstlen,
717715
(unsigned char*)key, key_length,
@@ -790,6 +788,7 @@ fil_space_check_encryption_read(
790788
ulint space) /*!< in: tablespace id */
791789
{
792790
fil_space_crypt_t* crypt_data = fil_space_get_crypt_data(space);
791+
793792
if (crypt_data == NULL)
794793
return false;
795794

@@ -816,6 +815,7 @@ fil_space_decrypt(fil_space_crypt_t* crypt_data,
816815
|| page_type == FIL_PAGE_PAGE_COMPRESSED);
817816

818817
ulint orig_page_type=0;
818+
819819
if (page_type == FIL_PAGE_PAGE_ENCRYPTED) {
820820
key_version = mach_read_from_2(
821821
src_frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION);
@@ -847,7 +847,6 @@ fil_space_decrypt(fil_space_crypt_t* crypt_data,
847847
mach_write_to_2(dst_frame+FIL_PAGE_TYPE, orig_page_type);
848848
}
849849

850-
851850
// get key
852851
byte key[MY_AES_MAX_KEY_LENGTH];
853852
uint key_length;
@@ -863,9 +862,7 @@ fil_space_decrypt(fil_space_crypt_t* crypt_data,
863862
mach_write_to_4(iv + 0, space);
864863
mach_write_to_4(iv + 4, offset);
865864
mach_write_to_8(iv + 8, lsn);
866-
}
867-
else
868-
{
865+
} else {
869866
// take the iv from the key provider
870867

871868
int load_iv_rc = get_encryption_iv(key_version, (uchar *) iv, sizeof(iv));
@@ -887,7 +884,6 @@ fil_space_decrypt(fil_space_crypt_t* crypt_data,
887884
byte* dst = dst_frame + FIL_PAGE_DATA;
888885
uint32 dstlen;
889886
ulint srclen = page_size - (FIL_PAGE_DATA + FIL_PAGE_DATA_END);
890-
891887
ulint compressed_len;
892888
ulint compression_method;
893889

0 commit comments

Comments
 (0)