[Commits] 5ccac04aed7: MDEV-12253: Buffer pool blocks are accessed after they have been freed

jan jan.lindstrom at mariadb.com
Tue Mar 28 13:37:11 EEST 2017


revision-id: 5ccac04aed7a25c6a813dc535d3227d11b912564 (mariadb-10.1.22-16-g5ccac04aed7)
parent(s): 3b252380d1e0df81102539857bc0f689243d12bb
author: Jan Lindström
committer: Jan Lindström
timestamp: 2017-03-28 13:34:55 +0300
message:

MDEV-12253: Buffer pool blocks are accessed after they have been freed

Problem was that bpage was referenced after it was already freed
from LRU. Fixed by adding a new variable encrypted that is
passed down to buf_page_check_corrupt() and used in
buf_page_get_gen() to stop processing page read.

Removed dict_table_t::is_encrypted and dict_table_t::ibd_file_missing
and replaced these with dict_table_t::file_unreadable. Table
ibd file is missing if fil_get_space(space_id) returns NULL
and encrypted if not. Removed buf_page_t::key_version as we can
always read key_version from page, note that reported key_version
in error message could be incorrect if page decryption was
done using incorrect key.

Added test cases when enrypted page could be read while doing
redo log crash recovery. Also added test case for row compressed
blobs.

btr_cur_open_at_index_side_func(),
btr_cur_open_at_rnd_pos_func(): Avoid referencing block that is
NULL.

buf_page_get_zip(): Issue error if page read fails.

buf_page_get_gen(): Use dberr_t for error detection and
do not reference bpage after we hare freed it.

buf_mark_space_corrupt(): remove bpage from LRU also when
it is encrypted.

buf_page_check_corrupt(): @return DB_SUCCESS if page has
been read and is not corrupted,
DB_PAGE_CORRUPTED if page based on checksum check is corrupted,
DB_DECRYPTION_FAILED if page post encryption checksum matches but
after decryption normal page checksum does not match. In read
case only DB_SUCCESS is possible.

buf_page_io_complete(): use dberr_t for error handling.

buf_flush_write_block_low(),
buf_read_ahead_random(),
buf_read_page_async(),
buf_read_ahead_linear(),
buf_read_ibuf_merge_pages(),
buf_read_recv_pages(),
fil_aio_wait():
        Issue error if page read fails.

btr_pcur_move_to_next_page(): Do not reference page if it is
NULL.

dict_stats_update_transient_for_index(),
dict_stats_update_transient()
        Do not continue if table decryption failed or table
        is corrupted.

fil_parse_write_crypt_data():
        Check that key read from redo log entry is found from
        encryptio plugin and if it is not, refuse to start.

Fixed error code on innodb.innodb test.

Merged test cased innodb-bad-key-change5 and innodb-bad-key-shutdown
to innodb-bad-key-change2.  Removed innodb-bad-key-change5 test.
Decreased unnecessary complexity on some long lasting tests.

---
 extra/innochecksum.cc                              |   8 +-
 mysql-test/std_data/keys.txt                       |   2 +-
 mysql-test/std_data/keys2.txt                      |   3 +-
 mysql-test/std_data/keys3.txt                      |   2 +-
 .../encryption/r/innodb-bad-key-change.result      |  29 +---
 .../encryption/r/innodb-bad-key-change2.result     |  43 ++++--
 .../encryption/r/innodb-bad-key-change4.result     |  24 ++--
 .../encryption/r/innodb-bad-key-change5.result     |  28 ----
 .../encryption/r/innodb-bad-key-shutdown.result    |  20 ---
 .../encryption/r/innodb-compressed-blob.result     |  21 +--
 .../encryption/r/innodb-encryption-disable.result  |   9 +-
 .../suite/encryption/r/innodb-force-corrupt.result |  29 +---
 .../suite/encryption/r/innodb-missing-key.result   |   4 +-
 .../r/innodb-page_encryption_compression.result    | 128 +++---------------
 .../r/innodb-page_encryption_log_encryption.result | 140 ++-----------------
 .../suite/encryption/r/innodb-redo-badkey.result   |  47 +++++++
 .../suite/encryption/r/innodb-redo-nokeys.result   |  37 ++++++
 .../r/innodb_encryption-page-compression.result    | 122 +++--------------
 .../suite/encryption/t/innodb-bad-key-change.test  |  36 ++---
 .../suite/encryption/t/innodb-bad-key-change2.test |  67 ++++------
 ...-key-change5.opt => innodb-bad-key-change4.opt} |   0
 .../suite/encryption/t/innodb-bad-key-change4.test |  82 +++---------
 .../suite/encryption/t/innodb-bad-key-change5.test |  88 ------------
 .../encryption/t/innodb-bad-key-shutdown.test      |  68 ----------
 .../suite/encryption/t/innodb-compressed-blob.test |  78 +++--------
 .../encryption/t/innodb-encryption-disable.test    |  58 ++------
 .../suite/encryption/t/innodb-force-corrupt.test   |  50 ++-----
 .../suite/encryption/t/innodb-missing-key.test     |   7 +-
 .../t/innodb-page_encryption_compression.test      |  83 ++----------
 .../t/innodb-page_encryption_log_encryption.test   |  95 +++----------
 .../suite/encryption/t/innodb-redo-badkey.opt      |   5 +
 .../suite/encryption/t/innodb-redo-badkey.test     |  93 +++++++++++++
 .../suite/encryption/t/innodb-redo-nokeys.opt      |   3 +
 .../suite/encryption/t/innodb-redo-nokeys.test     |  80 +++++++++++
 .../t/innodb_encryption-page-compression.test      |  63 +++------
 storage/innobase/api/api0api.cc                    |   6 +-
 storage/innobase/btr/btr0btr.cc                    |   6 +-
 storage/innobase/btr/btr0cur.cc                    |  28 +++-
 storage/innobase/btr/btr0defragment.cc             |   2 +-
 storage/innobase/btr/btr0pcur.cc                   |   9 ++
 storage/innobase/buf/buf0buf.cc                    | 131 +++++++++---------
 storage/innobase/buf/buf0rea.cc                    | 126 ++++++++++--------
 storage/innobase/dict/dict0crea.cc                 |   2 +-
 storage/innobase/dict/dict0dict.cc                 |  23 +++-
 storage/innobase/dict/dict0load.cc                 |  23 ++--
 storage/innobase/dict/dict0stats.cc                |  18 ++-
 storage/innobase/fil/fil0crypt.cc                  |  58 ++++----
 storage/innobase/fil/fil0fil.cc                    |  21 ++-
 storage/innobase/handler/ha_innodb.cc              |  43 ++++--
 storage/innobase/handler/handler0alter.cc          |  57 +++++++-
 storage/innobase/include/btr0btr.ic                |   2 +-
 storage/innobase/include/buf0buf.h                 |   1 -
 storage/innobase/include/buf0rea.h                 |   9 +-
 storage/innobase/include/dict0dict.h               |   9 ++
 storage/innobase/include/dict0mem.h                |  10 +-
 storage/innobase/include/fil0crypt.h               |  14 +-
 storage/innobase/include/log0recv.h                |   5 +-
 storage/innobase/log/log0recv.cc                   |  23 +++-
 storage/innobase/row/row0import.cc                 |   6 +-
 storage/innobase/row/row0ins.cc                    |   7 +-
 storage/innobase/row/row0merge.cc                  |  12 +-
 storage/innobase/row/row0mysql.cc                  |  41 +++---
 storage/innobase/row/row0purge.cc                  |   2 +-
 storage/innobase/row/row0sel.cc                    |   9 +-
 storage/innobase/row/row0uins.cc                   |   2 +-
 storage/innobase/row/row0umod.cc                   |   2 +-
 storage/innobase/srv/srv0start.cc                  |   7 +-
 storage/innobase/trx/trx0trx.cc                    |   2 +-
 storage/innobase/ut/ut0ut.cc                       |   2 +
 storage/xtradb/api/api0api.cc                      |   6 +-
 storage/xtradb/btr/btr0btr.cc                      |   7 +-
 storage/xtradb/btr/btr0cur.cc                      |  23 +++-
 storage/xtradb/btr/btr0defragment.cc               |   2 +-
 storage/xtradb/btr/btr0pcur.cc                     |  10 ++
 storage/xtradb/buf/buf0buf.cc                      | 148 +++++++++++----------
 storage/xtradb/buf/buf0rea.cc                      | 113 ++++++++++------
 storage/xtradb/dict/dict0crea.cc                   |   2 +-
 storage/xtradb/dict/dict0dict.cc                   |  23 +++-
 storage/xtradb/dict/dict0load.cc                   |  21 +--
 storage/xtradb/dict/dict0stats.cc                  |  17 ++-
 storage/xtradb/fil/fil0crypt.cc                    |  57 ++++----
 storage/xtradb/fil/fil0fil.cc                      |  20 ++-
 storage/xtradb/handler/ha_innodb.cc                |  61 ++++++---
 storage/xtradb/handler/handler0alter.cc            |  52 +++++++-
 storage/xtradb/include/btr0btr.ic                  |   2 +-
 storage/xtradb/include/buf0buf.h                   |   1 -
 storage/xtradb/include/buf0rea.h                   |   8 +-
 storage/xtradb/include/dict0dict.h                 |   9 ++
 storage/xtradb/include/dict0mem.h                  |  18 +--
 storage/xtradb/include/fil0crypt.h                 |  14 +-
 storage/xtradb/include/log0recv.h                  |   5 +-
 storage/xtradb/log/log0recv.cc                     |  23 +++-
 storage/xtradb/row/row0import.cc                   |   6 +-
 storage/xtradb/row/row0ins.cc                      |   7 +-
 storage/xtradb/row/row0merge.cc                    |  14 +-
 storage/xtradb/row/row0mysql.cc                    |  42 +++---
 storage/xtradb/row/row0purge.cc                    |   2 +-
 storage/xtradb/row/row0sel.cc                      |  10 +-
 storage/xtradb/row/row0uins.cc                     |   2 +-
 storage/xtradb/row/row0umod.cc                     |   2 +-
 storage/xtradb/srv/srv0start.cc                    |   7 +-
 storage/xtradb/trx/trx0trx.cc                      |   2 +-
 storage/xtradb/ut/ut0ut.cc                         |   2 +
 103 files changed, 1476 insertions(+), 1632 deletions(-)

diff --git a/extra/innochecksum.cc b/extra/innochecksum.cc
index 2536926513a..7038681fd81 100644
--- a/extra/innochecksum.cc
+++ b/extra/innochecksum.cc
@@ -582,9 +582,11 @@ void defrag_analysis(unsigned long long id, const per_index_stats& index)
       }
     }
   }
-  if (index.leaf_pages)
-    printf("count = %lu free = %lu\n", index.count, index.free_pages);
-    printf("%llu\t\t%llu\t\t%lu\t\t%lu\t\t%lu\t\t%.2f\t%lu\n",
+  if (!n_leaf_pages) {
+	  n_leaf_pages=1;
+  }
+  printf("count = %lu free = %lu\n", index.count, index.free_pages);
+  printf("%llu\t\t%llu\t\t%lu\t\t%lu\t\t%lu\t\t%.2f\t%lu\n",
            id, index.leaf_pages, n_leaf_pages, n_merge, n_pages,
            1.0 - (double)n_pages / (double)n_leaf_pages, index.max_data_size);
 }
diff --git a/mysql-test/std_data/keys.txt b/mysql-test/std_data/keys.txt
index e511521d7ab..2d83b7fba60 100644
--- a/mysql-test/std_data/keys.txt
+++ b/mysql-test/std_data/keys.txt
@@ -9,4 +9,4 @@
 
 5;966050D7777350B6FD5CCB3E5F648DA45C63BEFB6DEDDFA13443F156B7D35C84
 6;B5EA210C8C09EF20DB95EC584714A89F # and yet another
-
+10;770A8A65DA156D24EE2A093277530143
diff --git a/mysql-test/std_data/keys2.txt b/mysql-test/std_data/keys2.txt
index 5b98fbeebd2..93fc537cd3e 100644
--- a/mysql-test/std_data/keys2.txt
+++ b/mysql-test/std_data/keys2.txt
@@ -1,7 +1,8 @@
-1;593E580927F47AB530D3B1237CDEF6D6
+1;770A8A65DA156D24EE2A093277530142
 2;352E42F1B9DB5CB915C3262FE745520A
 3;CFE065600F5EB57481075C65180C3F8A
 4;205379930183490D3BECA139BDF4DB5B
 5;E2D944D5D837A1DCB22FF7FD397892EE
 6;BAFE99B0BB87F2CD33A6AF26A11F6BD1
 19;678D6B0063824BACCE33224B385104B35F30FF5749F0EBC030A0955DBC7FAC34
+20;BAFE99B0BB87F2CD33A6AF26A11F6BD1
diff --git a/mysql-test/std_data/keys3.txt b/mysql-test/std_data/keys3.txt
index 4f6c618d2ad..be30acc6add 100644
--- a/mysql-test/std_data/keys3.txt
+++ b/mysql-test/std_data/keys3.txt
@@ -1,4 +1,4 @@
-1;593E580927F47AB530D3B1237CDEF6D6
+1;770A8A65DA156D24EE2A093277530142
 2;E4B00A45BF775B4E07D634EC5CA5912B
 3;6E35ACB162B29D1FB9E178021DAF16ED
 4;971A664A88EE0022D408E40BFAB17E79
diff --git a/mysql-test/suite/encryption/r/innodb-bad-key-change.result b/mysql-test/suite/encryption/r/innodb-bad-key-change.result
index 5c4f43f028b..700c7de2496 100644
--- a/mysql-test/suite/encryption/r/innodb-bad-key-change.result
+++ b/mysql-test/suite/encryption/r/innodb-bad-key-change.result
@@ -1,13 +1,7 @@
-call mtr.add_suppression("Plugin 'file_key_management' init function returned error");
-call mtr.add_suppression("InnoDB: Database page corruption on disk or a failed.*");
-call mtr.add_suppression("Plugin 'file_key_management' registration.*failed");
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-call mtr.add_suppression(".*InnoDB: Cannot open table test/.* from the internal data dictionary of InnoDB though the .frm file for the table exists. See .* for how you can resolve the problem.");
-call mtr.add_suppression("InnoDB: .ibd file is missing for table test/.*");
+call mtr.add_suppression("InnoDB: The page .*");
+call mtr.add_suppression("InnoDB: Cannot calculate statistics for table .* ");
+call mtr.add_suppression("Plugin 'file_key_management' :*");
 call mtr.add_suppression("mysqld: File .*");
-call mtr.add_suppression("InnoDB: Tablespace id .* is encrypted but encryption service or used key_id .* is not available. Can't continue opening tablespace.");
 
 # Start server with keys2.txt
 SET GLOBAL innodb_file_format = `Barracuda`;
@@ -42,7 +36,8 @@ Error	1296	Got error 192 'Table encrypted but decryption failed. This could be b
 DROP TABLE t1;
 SHOW WARNINGS;
 Level	Code	Message
-# Start server with keys.txt
+# Start server with keys3.txt
+SET GLOBAL innodb_default_encryption_key_id=5;
 CREATE TABLE t2 (c VARCHAR(8), id int not null primary key, b int, key(b)) ENGINE=InnoDB ENCRYPTED=YES;
 INSERT INTO t2 VALUES ('foobar',1,2);
 
@@ -58,62 +53,53 @@ SELECT * FROM t2 where id = 1;
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 SHOW WARNINGS;
 Level	Code	Message
-Warning	1812	Tablespace is missing for table 'test/t2'
 Warning	192	Table test/t2 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 SELECT * FROM t2 where b = 1;
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 SHOW WARNINGS;
 Level	Code	Message
-Warning	1812	Tablespace is missing for table 'test/t2'
 Warning	192	Table test/t2 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 INSERT INTO t2 VALUES ('tmp',3,3);
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 SHOW WARNINGS;
 Level	Code	Message
-Warning	1812	Tablespace is missing for table 'test/t2'
 Warning	192	Table test/t2 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 DELETE FROM t2 where b = 3;
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 SHOW WARNINGS;
 Level	Code	Message
-Warning	1812	Tablespace is missing for table 'test/t2'
 Warning	192	Table test/t2 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 DELETE FROM t2 where id = 3;
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 SHOW WARNINGS;
 Level	Code	Message
-Warning	1812	Tablespace is missing for table 'test/t2'
 Warning	192	Table test/t2 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 UPDATE t2 set b = b +1;
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 SHOW WARNINGS;
 Level	Code	Message
-Warning	1812	Tablespace is missing for table 'test/t2'
 Warning	192	Table test/t2 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 OPTIMIZE TABLE t2;
 Table	Op	Msg_type	Msg_text
-test.t2	optimize	Warning	Tablespace is missing for table 'test/t2'
 test.t2	optimize	Warning	Table test/t2 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 test.t2	optimize	Error	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 test.t2	optimize	error	Corrupt
 SHOW WARNINGS;
 Level	Code	Message
-ALTER TABLE t2 ADD COLUMN c INT;
+ALTER TABLE t2 ADD COLUMN d INT;
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 SHOW WARNINGS;
 Level	Code	Message
-Warning	1812	Tablespace is missing for table 'test/t2'
 Warning	192	Table test/t2 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 ANALYZE TABLE t2;
 Table	Op	Msg_type	Msg_text
-test.t2	analyze	Warning	Tablespace is missing for table 'test/t2'
 test.t2	analyze	Warning	Table test/t2 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 test.t2	analyze	Error	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 test.t2	analyze	error	Corrupt
@@ -123,7 +109,6 @@ TRUNCATE TABLE t2;
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 SHOW WARNINGS;
 Level	Code	Message
-Warning	1812	Tablespace is missing for table 'test/t2'
 Warning	192	Table test/t2 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 DROP TABLE t2;
@@ -132,7 +117,7 @@ SHOW WARNINGS;
 Level	Code	Message
 Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 
-# Restart server with keys.txt
+# Restart server with keys3.txt
 DROP TABLE t2;
 SHOW WARNINGS;
 Level	Code	Message
diff --git a/mysql-test/suite/encryption/r/innodb-bad-key-change2.result b/mysql-test/suite/encryption/r/innodb-bad-key-change2.result
index deb0a5e8cc3..292186f2ca2 100644
--- a/mysql-test/suite/encryption/r/innodb-bad-key-change2.result
+++ b/mysql-test/suite/encryption/r/innodb-bad-key-change2.result
@@ -1,26 +1,45 @@
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-call mtr.add_suppression(".*InnoDB: Cannot open table test/.* from the internal data dictionary of InnoDB though the .frm file for the table exists. See .* for how you can resolve the problem.");
-call mtr.add_suppression("InnoDB: .ibd file is missing for table test/.*");
-call mtr.add_suppression("Couldn't load plugins from 'file_key_management*");
-CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB ENCRYPTION_KEY_ID=4;
+call mtr.add_suppression("InnoDB: The page .*");
+call mtr.add_suppression("Couldn't load plugins from 'file_key_management.*");
+SET GLOBAL innodb_file_format = `Barracuda`;
+SET GLOBAL innodb_file_per_table = ON;
+CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=4;
 INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
-select * from t1;
+SELECT * FROM t1;
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
-show warnings;
+SHOW WARNINGS;
 Level	Code	Message
-Warning	192	Table test/t1 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
+Warning	192	Table test/t1 in tablespace 4 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 Warning	192	Table test/t1 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 alter table t1 discard tablespace;
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
-show warnings;
+SHOW WARNINGS;
 Level	Code	Message
+Warning	1812	Tablespace is missing for table 't1'
 Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
-alter table t1 engine=InnoDB;
+ALTER TABLE t1 engine=InnoDB;
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 show warnings;
 Level	Code	Message
 Warning	192	Table test/t1 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
 Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
+OPTIMIZE TABLE t1;
+Table	Op	Msg_type	Msg_text
+test.t1	optimize	note	Table does not support optimize, doing recreate + analyze instead
+test.t1	optimize	error	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
+test.t1	optimize	status	Operation failed
+Warnings:
+Warning	192	Table test/t1 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
+Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
+SHOW WARNINGS;
+Level	Code	Message
+Warning	192	Table test/t1 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
+Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
+CHECK TABLE t1;
+Table	Op	Msg_type	Msg_text
+test.t1	check	Warning	Table test/t1 in tablespace 4 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
+test.t1	check	Warning	Table test/t1 is encrypted but encryption service or used key_id is not available.  Can't continue checking table.
+test.t1	check	error	Corrupt
+SHOW WARNINGS;
+Level	Code	Message
+drop table t1;
diff --git a/mysql-test/suite/encryption/r/innodb-bad-key-change4.result b/mysql-test/suite/encryption/r/innodb-bad-key-change4.result
index 36de9729a9b..9840d40f054 100644
--- a/mysql-test/suite/encryption/r/innodb-bad-key-change4.result
+++ b/mysql-test/suite/encryption/r/innodb-bad-key-change4.result
@@ -1,19 +1,23 @@
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-call mtr.add_suppression(".*InnoDB: Cannot open table test/.* from the internal data dictionary of InnoDB though the .frm file for the table exists. See .* for how you can resolve the problem.");
+call mtr.add_suppression("InnoDB: The page .*");
+call mtr.add_suppression(".*InnoDB: Cannot open table .*");
 call mtr.add_suppression("InnoDB: .ibd file is missing for table test/.*");
-call mtr.add_suppression("Couldn't load plugins from 'file_key_management*");
+call mtr.add_suppression("Couldn't load plugins from 'file_key_management.*");
 SET GLOBAL innodb_file_format = `Barracuda`;
 SET GLOBAL innodb_file_per_table = ON;
-CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB ENCRYPTION_KEY_ID=4;
+CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=4;
 INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
+OPTIMIZE TABLE t1;
+Table	Op	Msg_type	Msg_text
+test.t1	optimize	Warning	Table test/t1 in tablespace 4 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
+test.t1	optimize	Warning	Table test/t1 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
+test.t1	optimize	Error	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
+test.t1	optimize	error	Corrupt
+SHOW WARNINGS;
+Level	Code	Message
 CHECK TABLE t1;
 Table	Op	Msg_type	Msg_text
-test.t1	check	Warning	Table test/t1 in tablespace 4 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
-test.t1	check	Warning	Table test/t1 is encrypted but encryption service or used key_id is not available.  Can't continue checking table.
+test.t1	check	Warning	Table test/t1 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
+test.t1	check	Error	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 test.t1	check	error	Corrupt
 SHOW WARNINGS;
 Level	Code	Message
diff --git a/mysql-test/suite/encryption/r/innodb-bad-key-change5.result b/mysql-test/suite/encryption/r/innodb-bad-key-change5.result
deleted file mode 100644
index cb1073cbaae..00000000000
--- a/mysql-test/suite/encryption/r/innodb-bad-key-change5.result
+++ /dev/null
@@ -1,28 +0,0 @@
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-call mtr.add_suppression(".*InnoDB: Cannot open table test/.* from the internal data dictionary of InnoDB though the .frm file for the table exists. See .* for how you can resolve the problem.");
-call mtr.add_suppression("InnoDB: .ibd file is missing for table test/.*");
-call mtr.add_suppression("Couldn't load plugins from 'file_key_management*");
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
-CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB ENCRYPTION_KEY_ID=4;
-INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
-OPTIMIZE TABLE t1;
-Table	Op	Msg_type	Msg_text
-test.t1	optimize	Warning	Table test/t1 is encrypted but encryption service or used key_id is not available.  Can't continue checking table.
-test.t1	optimize	Warning	InnoDB: Cannot defragment table test/t1: returned error code 192
-
-test.t1	optimize	note	Table does not support optimize, doing recreate + analyze instead
-test.t1	optimize	error	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
-test.t1	optimize	status	Operation failed
-Warnings:
-Warning	192	Table test/t1 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
-Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
-SHOW WARNINGS;
-Level	Code	Message
-Warning	192	Table test/t1 is encrypted but encryption service or used key_id is not available.  Can't continue reading table.
-Error	1296	Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
-DROP TABLE t1;
diff --git a/mysql-test/suite/encryption/r/innodb-bad-key-shutdown.result b/mysql-test/suite/encryption/r/innodb-bad-key-shutdown.result
deleted file mode 100644
index ded4ad698b0..00000000000
--- a/mysql-test/suite/encryption/r/innodb-bad-key-shutdown.result
+++ /dev/null
@@ -1,20 +0,0 @@
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-call mtr.add_suppression(".*InnoDB: Cannot open table test/.* from the internal data dictionary of InnoDB though the .frm file for the table exists. See .* for how you can resolve the problem.");
-call mtr.add_suppression("InnoDB: .ibd file is missing for table test/.*");
-call mtr.add_suppression("Couldn't load plugins from 'file_key_management*");
-# 
-# Restart the server with key 4 in the key file
-# 
-CREATE TABLE t1 (i INT, KEY(i)) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=4;
-INSERT INTO t1 VALUES (1);
-# 
-# Restart the server with a different value for key 4 in the key file
-# 
-SELECT * FROM t1;
-ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
-SELECT * FROM t1;
-i
-1
-DROP TABLE t1;
diff --git a/mysql-test/suite/encryption/r/innodb-compressed-blob.result b/mysql-test/suite/encryption/r/innodb-compressed-blob.result
index a19707d8f77..8ef60cb3d78 100644
--- a/mysql-test/suite/encryption/r/innodb-compressed-blob.result
+++ b/mysql-test/suite/encryption/r/innodb-compressed-blob.result
@@ -1,23 +1,24 @@
 call mtr.add_suppression("InnoDB: Block in space_id .* in file .* encrypted.");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version 1 is not found or used encryption algorithm or method does not match.");
+call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
 call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
+call mtr.add_suppression("InnoDB: The page .* cannot be decrypted.");
+# Restart mysqld --file-key-management-filename=keys2.txt
 SET GLOBAL innodb_file_format = `Barracuda`;
 SET GLOBAL innodb_file_per_table = ON;
 set GLOBAL innodb_default_encryption_key_id=4;
-create table t1(a int not null primary key, b blob) engine=innodb row_format=compressed;
-create table t2(a int not null primary key, b blob) engine=innodb row_format=compressed encrypted=yes;
-create table t3(a int not null primary key, b blob) engine=innodb row_format=compressed encrypted=no;
-CREATE INDEX test ON t1 (b(10));
-CREATE INDEX test ON t2 (b(10));
-CREATE INDEX test ON t3 (b(10));
+create table t1(a int not null primary key, b blob, index(b(10))) engine=innodb row_format=compressed;
+create table t2(a int not null primary key, b blob, index(b(10))) engine=innodb row_format=compressed encrypted=yes;
+create table t3(a int not null primary key, b blob, index(b(10))) engine=innodb row_format=compressed encrypted=no;
 insert into t1 values (1, repeat('secret',6000));
 insert into t2 values (1, repeat('secret',6000));
 insert into t3 values (1, repeat('secret',6000));
-select count(*) from t1 FORCE INDEX (test) where b like 'secret%';
+# Restart mysqld --file-key-management-filename=keys3.txt
+select count(*) from t1 FORCE INDEX (b) where b like 'secret%';
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
-select count(*) from t2 FORCE INDEX (test) where b like 'secret%';
+select count(*) from t2 FORCE INDEX (b) where b like 'secret%';
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
-select count(*) from t3 FORCE INDEX (test) where b like 'secret%';
+select count(*) from t3 FORCE INDEX (b) where b like 'secret%';
 count(*)
 1
+# Restart mysqld --file-key-management-filename=keys2.txt
 drop table t1,t2,t3;
diff --git a/mysql-test/suite/encryption/r/innodb-encryption-disable.result b/mysql-test/suite/encryption/r/innodb-encryption-disable.result
index 74c568bdf3e..542046d306e 100644
--- a/mysql-test/suite/encryption/r/innodb-encryption-disable.result
+++ b/mysql-test/suite/encryption/r/innodb-encryption-disable.result
@@ -1,12 +1,5 @@
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-call mtr.add_suppression(".*InnoDB: Cannot open table test/.* from the internal data dictionary of InnoDB though the .frm file for the table exists. See .* for how you can resolve the problem.");
-call mtr.add_suppression("InnoDB: .ibd file is missing for table test/.*");
+call mtr.add_suppression("InnoDB: The page .*");
 call mtr.add_suppression("Couldn't load plugins from 'file_key_management*");
-call mtr.add_suppression("InnoDB: Tablespace id.* is encrypted but encryption service or used key_id .* is not available. Can't continue opening tablespace.");
 create table t5 (
 `intcol1` int(32) DEFAULT NULL,
 `intcol2` int(32) DEFAULT NULL,
diff --git a/mysql-test/suite/encryption/r/innodb-force-corrupt.result b/mysql-test/suite/encryption/r/innodb-force-corrupt.result
index 0b9d39161f7..5929ef1bc3d 100644
--- a/mysql-test/suite/encryption/r/innodb-force-corrupt.result
+++ b/mysql-test/suite/encryption/r/innodb-force-corrupt.result
@@ -1,9 +1,6 @@
 call mtr.add_suppression("InnoDB: Database page corruption on disk or a failed.*");
 call mtr.add_suppression("InnoDB: Corruption: Block in space_id .* in file .* corrupted.");
-call mtr.add_suppression("InnoDB: Based on page type .*");
 call mtr.add_suppression("InnoDB: Unable to read tablespace .* page no .* into the buffer pool after .* attempts");
-CALL mtr.add_suppression("InnoDB: Error: Unable to read tablespace .* page no .* into the buffer pool after 100 attempts");
-CALL mtr.add_suppression("InnoDB: Database page corruption on disk or a failed");
 CALL mtr.add_suppression("InnoDB: Space .* file test/t1 read of page .*");
 CALL mtr.add_suppression("InnoDB: You may have to recover from a backup.");
 CALL mtr.add_suppression("InnoDB: It is also possible that your operatingsystem has corrupted its own file cache.");
@@ -13,9 +10,9 @@ CALL mtr.add_suppression("InnoDB: fix the corruption by dumping, dropping, and r
 CALL mtr.add_suppression("InnoDB: the corrupt table. You can use CHECK");
 CALL mtr.add_suppression("InnoDB: TABLE to scan your table for corruption.");
 CALL mtr.add_suppression("InnoDB: See also .* about forcing recovery.");
-CALL mtr.add_suppression("InnoDB: However key management plugin or used key_version 3221605118 is not found or used encryption algorithm or method does not match.");
+CALL mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
 CALL mtr.add_suppression("Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-CALL mtr.add_suppression("InnoDB: Block in space_id .* in file .* encrypted.");
+CALL mtr.add_suppression("InnoDB: The page .* cannot be decrypted.");
 SET GLOBAL innodb_file_format = `Barracuda`;
 SET GLOBAL innodb_file_per_table = ON;
 set global innodb_compression_algorithm = 1;
@@ -23,33 +20,19 @@ set global innodb_compression_algorithm = 1;
 CREATE TABLE t1 (a INT AUTO_INCREMENT PRIMARY KEY, b TEXT,c char(200)) ENGINE=InnoDB encrypted=yes;
 CREATE TABLE t2 (a INT AUTO_INCREMENT PRIMARY KEY, b TEXT,c char(200)) ENGINE=InnoDB row_format=compressed encrypted=yes;
 CREATE TABLE t3 (a INT AUTO_INCREMENT PRIMARY KEY, b TEXT, c char(200)) ENGINE=InnoDB page_compressed=yes encrypted=yes;
+BEGIN;
 INSERT INTO t1 (b,c) VALUES ('corrupt me','secret');
 INSERT INTO t1 (b,c) VALUES ('corrupt me','moresecretmoresecret');
 INSERT INTO t2 select * from t1;
 INSERT INTO t3 select * from t1;
+COMMIT;
 # Backup tables before corrupting
 # Corrupt tables
-# Write file to make mysql-test-run.pl expect the "crash", but don't
-# start it until it's told to
-# The below SELECT query will crash the server because some pages
-# on the disk are corrupted
 SELECT * FROM t1;
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 SELECT * FROM t2;
-a	b	c
-1	corrupt me	secret
-2	abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc	secretsecret
-3	abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc	secretsecret
-4	abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc	secretsecret
-5	abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc	secretsecret
-6	abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc	secretsecret
-7	abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc	secretsecret
-8	abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc	secretsecret
-9	abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc	secretsecret
-10	abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc	secretsecret
-11	abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc	secretsecret
-12	corrupt me	moresecretmoresecret
+ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
 SELECT * FROM t3;
 ERROR HY000: Got error 192 'Table encrypted but decryption failed. This could be because correct encryption management plugin is not loaded, used encryption key is not available or encryption method does not match.' from InnoDB
-# Restore the original t1.ibd
+# Restore the original tables
 DROP TABLE t1,t2,t3;
diff --git a/mysql-test/suite/encryption/r/innodb-missing-key.result b/mysql-test/suite/encryption/r/innodb-missing-key.result
index ed9f83770b6..e12853a23c0 100644
--- a/mysql-test/suite/encryption/r/innodb-missing-key.result
+++ b/mysql-test/suite/encryption/r/innodb-missing-key.result
@@ -1,6 +1,4 @@
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
+call mtr.add_suppression("InnoDB: The page .*");
 
 # Start server with keys2.txt
 CREATE TABLE t1(a int not null primary key auto_increment, b varchar(128)) engine=innodb ENCRYPTED=YES ENCRYPTION_KEY_ID=19;
diff --git a/mysql-test/suite/encryption/r/innodb-page_encryption_compression.result b/mysql-test/suite/encryption/r/innodb-page_encryption_compression.result
index f7ffc77fd66..ade0c09af9b 100644
--- a/mysql-test/suite/encryption/r/innodb-page_encryption_compression.result
+++ b/mysql-test/suite/encryption/r/innodb-page_encryption_compression.result
@@ -38,75 +38,22 @@ set current_num = current_num + 1;
 end while;
 end//
 commit;
-set autocommit=0;
-call innodb_insert_proc(5000);
-commit;
-set autocommit=1;
+begin;
+call innodb_insert_proc(2000);
 insert into innodb_compact select * from innodb_normal;
 insert into innodb_dynamic select * from innodb_normal;
-update innodb_normal set c1 = c1 + 1;
-update innodb_compact set c1 = c1 + 1;
-update innodb_dynamic set c1 = c1 + 1;
-select count(*) from innodb_normal;
-count(*)
-5000
-select count(*) from innodb_compact where c1 < 1500000;
-count(*)
-5000
-select count(*) from innodb_dynamic where c1 < 1500000;
-count(*)
-5000
-select count(*) from innodb_compact t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-5000
-select count(*) from innodb_dynamic t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-5000
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
-variable_value >= 0
-1
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
-variable_value >= 0
-1
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_compressed';
-variable_value >= 0
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decompressed';
-variable_value >= 0
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
-set global innodb_compression_algorithm = 1;
-update innodb_normal set c1 = c1 + 1;
-update innodb_compact set c1 = c1 + 1;
-update innodb_dynamic set c1 = c1 + 1;
-select count(*) from innodb_normal;
-count(*)
-5000
-select count(*) from innodb_compact where c1 < 1500000;
-count(*)
-5000
-select count(*) from innodb_dynamic where c1 < 1500000;
-count(*)
-5000
-select count(*) from innodb_compact t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-5000
-select count(*) from innodb_dynamic t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-5000
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
-variable_value >= 0
+commit;
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
+variable_value > 0
 1
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
 variable_value >= 0
 1
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_compressed';
-variable_value >= 0
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_compressed';
+variable_value > 0
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decompressed';
 variable_value >= 0
+SET GLOBAL innodb_encrypt_tables=OFF;
 alter table innodb_normal engine=innodb page_compressed=DEFAULT;
 show create table innodb_normal;
 Table	Create Table
@@ -128,56 +75,19 @@ innodb_dynamic	CREATE TABLE `innodb_dynamic` (
   `c1` bigint(20) NOT NULL,
   `b` char(200) DEFAULT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
-show create table innodb_normal;
-Table	Create Table
-innodb_normal	CREATE TABLE `innodb_normal` (
-  `c1` bigint(20) NOT NULL,
-  `b` char(200) DEFAULT NULL
-) ENGINE=InnoDB DEFAULT CHARSET=latin1
-show create table innodb_compact;
-Table	Create Table
-innodb_compact	CREATE TABLE `innodb_compact` (
-  `c1` bigint(20) NOT NULL,
-  `b` char(200) DEFAULT NULL
-) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT
-show create table innodb_dynamic;
-Table	Create Table
-innodb_dynamic	CREATE TABLE `innodb_dynamic` (
-  `c1` bigint(20) NOT NULL,
-  `b` char(200) DEFAULT NULL
-) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC
-update innodb_normal set c1 = c1 + 1;
-update innodb_compact set c1 = c1 + 1;
-update innodb_dynamic set c1 = c1 + 1;
-select count(*) from innodb_normal;
-count(*)
-5000
-select count(*) from innodb_compact where c1 < 1500000;
-count(*)
-5000
-select count(*) from innodb_dynamic where c1 < 1500000;
-count(*)
-5000
-select count(*) from innodb_compact t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-5000
-select count(*) from innodb_dynamic t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-5000
-SELECT variable_value = 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
-variable_value = 0
+Timeout in wait_condition.inc for select variable_value > 0 from information_schema.global_status where variable_name = 'INNODB_NUM_PAGES_PAGE_DECRYPTED'
+Id	User	Host	db	Command	Time	State	Info	Progress
+3	root	localhost	test	Query	0	init	show full processlist	0.000
+SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
+variable_value >= 0
 1
-SELECT variable_value = 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
-variable_value = 0
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
+variable_value > 0
 1
-SELECT variable_value = 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_compressed';
-variable_value = 0
-SELECT variable_value = 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decompressed';
-variable_value = 0
+SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_compressed';
+variable_value >= 0
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decompressed';
+variable_value > 0
 drop procedure innodb_insert_proc;
 drop table innodb_normal;
 drop table innodb_compact;
diff --git a/mysql-test/suite/encryption/r/innodb-page_encryption_log_encryption.result b/mysql-test/suite/encryption/r/innodb-page_encryption_log_encryption.result
index 92130da19e9..1fe223647e2 100644
--- a/mysql-test/suite/encryption/r/innodb-page_encryption_log_encryption.result
+++ b/mysql-test/suite/encryption/r/innodb-page_encryption_log_encryption.result
@@ -52,49 +52,15 @@ set current_num = current_num + 1;
 end while;
 end//
 commit;
-set autocommit=0;
+begin;
 call innodb_insert_proc(2000);
-commit;
-set autocommit=1;
 insert into innodb_compact select * from innodb_normal;
 insert into innodb_compressed select * from innodb_normal;
 insert into innodb_dynamic select * from innodb_normal;
 insert into innodb_redundant select * from innodb_normal;
-update innodb_normal set c1 = c1 +1;
-update innodb_compact set c1 = c1 + 1;
-update innodb_compressed set c1 = c1 + 1;
-update innodb_dynamic set c1 = c1 + 1;
-update innodb_redundant set c1 = c1 + 1;
-select count(*) from innodb_compact where c1 < 1500000;
-count(*)
-2000
-select count(*) from innodb_compressed where c1 < 1500000;
-count(*)
-2000
-select count(*) from innodb_dynamic where c1 < 1500000;
-count(*)
-2000
-select count(*) from innodb_redundant where c1 < 1500000;
-count(*)
-2000
-select count(*) from innodb_compact t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-2000
-select count(*) from innodb_dynamic t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-2000
-select count(*) from innodb_compressed t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-2000
-select count(*) from innodb_redundant t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-2000
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
-variable_value >= 0
+commit;
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
+variable_value > 0
 1
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
 variable_value >= 0
@@ -106,40 +72,13 @@ update innodb_compact set c1 = c1 + 1;
 update innodb_compressed set c1 = c1 + 1;
 update innodb_dynamic set c1 = c1 + 1;
 update innodb_redundant set c1 = c1 + 1;
-select count(*) from innodb_compact where c1 < 1500000;
-count(*)
-2000
-select count(*) from innodb_compressed where c1 < 1500000;
-count(*)
-2000
-select count(*) from innodb_dynamic where c1 < 1500000;
-count(*)
-2000
-select count(*) from innodb_redundant where c1 < 1500000;
-count(*)
-2000
-select count(*) from innodb_compact t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-2000
-select count(*) from innodb_dynamic t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-2000
-select count(*) from innodb_compressed t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-2000
-select count(*) from innodb_redundant t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-2000
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
-variable_value >= 0
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
+variable_value > 0
 1
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
-variable_value >= 0
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
+variable_value > 0
 1
+SET GLOBAL innodb_encrypt_tables=OFF;
 alter table innodb_compact engine=innodb encrypted=DEFAULT encryption_key_id=DEFAULT;
 show create table innodb_compact;
 Table	Create Table
@@ -168,65 +107,6 @@ innodb_redundant	CREATE TABLE `innodb_redundant` (
   `c1` bigint(20) NOT NULL,
   `b` char(200) DEFAULT NULL
 ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=REDUNDANT
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
-show create table innodb_compact;
-Table	Create Table
-innodb_compact	CREATE TABLE `innodb_compact` (
-  `c1` bigint(20) NOT NULL,
-  `b` char(200) DEFAULT NULL
-) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPACT
-show create table innodb_compressed;
-Table	Create Table
-innodb_compressed	CREATE TABLE `innodb_compressed` (
-  `c1` bigint(20) NOT NULL,
-  `b` char(200) DEFAULT NULL
-) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=COMPRESSED
-show create table innodb_dynamic;
-Table	Create Table
-innodb_dynamic	CREATE TABLE `innodb_dynamic` (
-  `c1` bigint(20) NOT NULL,
-  `b` char(200) DEFAULT NULL
-) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC
-show create table innodb_redundant;
-Table	Create Table
-innodb_redundant	CREATE TABLE `innodb_redundant` (
-  `c1` bigint(20) NOT NULL,
-  `b` char(200) DEFAULT NULL
-) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=REDUNDANT
-update innodb_normal set c1 = c1 +1;
-update innodb_compact set c1 = c1 + 1;
-update innodb_compressed set c1 = c1 + 1;
-update innodb_dynamic set c1 = c1 + 1;
-update innodb_redundant set c1 = c1 + 1;
-select count(*) from innodb_compact where c1 < 1500000;
-count(*)
-2000
-select count(*) from innodb_compressed where c1 < 1500000;
-count(*)
-2000
-select count(*) from innodb_dynamic where c1 < 1500000;
-count(*)
-2000
-select count(*) from innodb_redundant where c1 < 1500000;
-count(*)
-2000
-select count(*) from innodb_compact t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-2000
-select count(*) from innodb_dynamic t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-2000
-select count(*) from innodb_compressed t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-2000
-select count(*) from innodb_redundant t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-count(*)
-2000
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
 variable_value >= 0
 1
@@ -239,6 +119,8 @@ drop table innodb_compact;
 drop table innodb_compressed;
 drop table innodb_dynamic;
 drop table innodb_redundant;
+SET GLOBAL innodb_file_format = `Barracuda`;
+SET GLOBAL innodb_file_per_table = ON;
 CREATE TABLE t1 (pk INT PRIMARY KEY) ENGINE=InnoDB ENCRYPTION_KEY_ID=2 ENCRYPTED=YES;
 INSERT INTO t1 VALUES (1),(2);
 # Restarting server...
diff --git a/mysql-test/suite/encryption/r/innodb-redo-badkey.result b/mysql-test/suite/encryption/r/innodb-redo-badkey.result
new file mode 100644
index 00000000000..b9996e65064
--- /dev/null
+++ b/mysql-test/suite/encryption/r/innodb-redo-badkey.result
@@ -0,0 +1,47 @@
+call mtr.add_suppression("InnoDB: Block in space_id .* in file .* encrypted.");
+call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
+call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
+call mtr.add_suppression("mysqld: File .* not found .*");
+call mtr.add_suppression("Plugin 'file_key_management' init function returned error.");
+call mtr.add_suppression("Plugin 'file_key_management' registration as a ENCRYPTION failed.");
+call mtr.add_suppression("InnoDB: cannot enable encryption, encryption plugin is not available");
+call mtr.add_suppression("Plugin 'InnoDB' init function returned error.");
+call mtr.add_suppression("Plugin 'InnoDB' registration as a STORAGE ENGINE failed.");
+call mtr.add_suppression("InnoDB: Read operation failed for tablespace .*");
+call mtr.add_suppression("InnoDB: Database page corruption on disk or a failed.*");
+call mtr.add_suppression("InnoDB: Space .* file .* read of page .*.");
+call mtr.add_suppression("InnoDB: You may have to recover from a backup.");
+call mtr.add_suppression("InnoDB: Recovery read page .* from tablespace .*");
+call mtr.add_suppression("InnoDB: The page .* cannot be decrypted.");
+call mtr.add_suppression("InnoDB: Missing MLOG_FILE_NAME or MLOG_FILE_DELETE .*");
+call mtr.add_suppression("InnoDB: Plugin initialization aborted .*");
+call mtr.add_suppression("InnoDB: ############### CORRUPT LOG RECORD FOUND ##################");
+# Restart mysqld --file-key-management-filename=keys2.txt
+# Wait max 10 min for key encryption threads to encrypt all spaces
+SET GLOBAL innodb_file_format = `Barracuda`;
+SET GLOBAL innodb_file_per_table = ON;
+create table t1(a int not null primary key auto_increment, c char(250), b blob, index(b(10))) engine=innodb row_format=compressed encrypted=yes encryption_key_id=4;
+create table t2(a int not null primary key auto_increment, c char(250), b blob, index(b(10))) engine=innodb row_format=compressed;
+create table t3(a int not null primary key auto_increment, c char(250), b blob, index(b(10))) engine=innodb encrypted=yes encryption_key_id=4;
+create table t4(a int not null primary key auto_increment, c char(250), b blob, index(b(10))) engine=innodb;
+begin;
+insert into t2 select * from t1;
+insert into t3 select * from t1;
+insert into t4 select * from t1;
+commit;
+SET GLOBAL innodb_flush_log_at_trx_commit=1;
+begin;
+update t1 set c = repeat('secret3', 20);
+update t2 set c = repeat('secret4', 20);
+update t3 set c = repeat('secret4', 20);
+update t4 set c = repeat('secret4', 20);
+insert into t1 (c,b) values (repeat('secret5',20), repeat('secret6',6000));
+insert into t2 (c,b) values (repeat('secret7',20), repeat('secret8',6000));
+insert into t3 (c,b) values (repeat('secret9',20), repeat('secre10',6000));
+insert into t4 (c,b) values (repeat('secre11',20), repeat('secre12',6000));
+COMMIT;
+# Kill the server
+# restart
+# Kill the server
+# Restart mysqld --file-key-management-filename=keys2.txt
+drop table t1, t2,t3,t4;
diff --git a/mysql-test/suite/encryption/r/innodb-redo-nokeys.result b/mysql-test/suite/encryption/r/innodb-redo-nokeys.result
new file mode 100644
index 00000000000..1387089432a
--- /dev/null
+++ b/mysql-test/suite/encryption/r/innodb-redo-nokeys.result
@@ -0,0 +1,37 @@
+call mtr.add_suppression("InnoDB: Block in space_id .* in file .* encrypted.");
+call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
+call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
+call mtr.add_suppression("mysqld: File .* not found .*");
+call mtr.add_suppression("Plugin 'file_key_management' init function returned error.");
+call mtr.add_suppression("Plugin 'file_key_management' registration as a ENCRYPTION failed.");
+call mtr.add_suppression("InnoDB: cannot enable encryption, encryption plugin is not available");
+call mtr.add_suppression("Plugin 'InnoDB' init function returned error.");
+call mtr.add_suppression("Plugin 'InnoDB' registration as a STORAGE ENGINE failed.");
+call mtr.add_suppression("InnoDB: The page .* cannot be decrypted.");
+# Restart mysqld --file-key-management-filename=keys2.txt
+SET GLOBAL innodb_file_format = `Barracuda`;
+SET GLOBAL innodb_file_per_table = ON;
+create table t1(a int not null primary key auto_increment, c char(200), b blob, index(b(10))) engine=innodb row_format=compressed encrypted=yes encryption_key_id=20;
+create table t2(a int not null primary key auto_increment, c char(200), b blob, index(b(10))) engine=innodb row_format=compressed;
+create table t3(a int not null primary key auto_increment, c char(200), b blob, index(b(10))) engine=innodb encrypted=yes encryption_key_id=20;
+create table t4(a int not null primary key auto_increment, c char(200), b blob, index(b(10))) engine=innodb;
+begin;
+insert into t2 select * from t1;
+insert into t3 select * from t1;
+insert into t4 select * from t1;
+commit;
+SET GLOBAL innodb_flush_log_at_trx_commit=1;
+begin;
+update t1 set c = repeat('secret3', 20);
+update t2 set c = repeat('secret4', 20);
+update t3 set c = repeat('secret4', 20);
+update t4 set c = repeat('secret4', 20);
+insert into t1 (c,b) values (repeat('secret5',20), repeat('secret6',6000));
+insert into t2 (c,b) values (repeat('secret7',20), repeat('secret8',6000));
+insert into t3 (c,b) values (repeat('secret9',20), repeat('secre10',6000));
+insert into t4 (c,b) values (repeat('secre11',20), repeat('secre12',6000));
+COMMIT;
+# Kill the server
+# restart
+# Restart mysqld --file-key-management-filename=keys2.txt
+drop table t1, t2,t3,t4;
diff --git a/mysql-test/suite/encryption/r/innodb_encryption-page-compression.result b/mysql-test/suite/encryption/r/innodb_encryption-page-compression.result
index e3238990aaa..95affc1fe89 100644
--- a/mysql-test/suite/encryption/r/innodb_encryption-page-compression.result
+++ b/mysql-test/suite/encryption/r/innodb_encryption-page-compression.result
@@ -97,13 +97,8 @@ set current_num = current_num + 1;
 end while;
 end//
 commit;
-set autocommit=0;
-call innodb_insert_proc(5000);
-commit;
-set autocommit=1;
-select count(*) from innodb_normal;
-count(*)
-5000
+begin;
+call innodb_insert_proc(2000);
 insert into innodb_page_compressed1 select * from innodb_normal;
 insert into innodb_page_compressed2 select * from innodb_normal;
 insert into innodb_page_compressed3 select * from innodb_normal;
@@ -116,120 +111,45 @@ insert into innodb_page_compressed9 select * from innodb_normal;
 commit;
 select count(*) from innodb_page_compressed1 where c1 < 500000;
 count(*)
-5000
+2000
 select count(*) from innodb_page_compressed2 where c1 < 500000;
 count(*)
-5000
+2000
 select count(*) from innodb_page_compressed3 where c1 < 500000;
 count(*)
-5000
+2000
 select count(*) from innodb_page_compressed4 where c1 < 500000;
 count(*)
-5000
+2000
 select count(*) from innodb_page_compressed5 where c1 < 500000;
 count(*)
-5000
+2000
 select count(*) from innodb_page_compressed6 where c1 < 500000;
 count(*)
-5000
+2000
 select count(*) from innodb_page_compressed7 where c1 < 500000;
 count(*)
-5000
+2000
 select count(*) from innodb_page_compressed8 where c1 < 500000;
 count(*)
-5000
+2000
 select count(*) from innodb_page_compressed9 where c1 < 500000;
 count(*)
-5000
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
-variable_value >= 0
-1
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
-variable_value >= 0
-1
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_compressed';
-variable_value >= 0
-1
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_decompressed';
-variable_value >= 0
-1
-select count(*) from innodb_page_compressed1 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed2 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed3 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed4 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed5 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed6 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed7 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed8 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed9 where c1 < 500000;
-count(*)
-5000
-update innodb_page_compressed1 set c1 = c1 + 1;
-update innodb_page_compressed2 set c1 = c1 + 1;
-update innodb_page_compressed3 set c1 = c1 + 1;
-update innodb_page_compressed4 set c1 = c1 + 1;
-update innodb_page_compressed5 set c1 = c1 + 1;
-update innodb_page_compressed6 set c1 = c1 + 1;
-update innodb_page_compressed7 set c1 = c1 + 1;
-update innodb_page_compressed8 set c1 = c1 + 1;
-update innodb_page_compressed9 set c1 = c1 + 1;
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
-variable_value >= 0
+2000
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
+variable_value > 0
 1
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
 variable_value >= 0
 1
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_compressed';
-variable_value >= 0
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_compressed';
+variable_value > 0
 1
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_decompressed';
 variable_value >= 0
 1
 SET GLOBAL innodb_encryption_threads = 4;
 SET GLOBAL innodb_encrypt_tables = off;
-select count(*) from innodb_page_compressed1 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed2 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed3 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed4 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed5 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed6 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed7 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed8 where c1 < 500000;
-count(*)
-5000
-select count(*) from innodb_page_compressed9 where c1 < 500000;
-count(*)
-5000
 update innodb_page_compressed1 set c1 = c1 + 1;
 update innodb_page_compressed2 set c1 = c1 + 1;
 update innodb_page_compressed3 set c1 = c1 + 1;
@@ -242,14 +162,14 @@ update innodb_page_compressed9 set c1 = c1 + 1;
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
 variable_value >= 0
 1
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
-variable_value >= 0
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
+variable_value > 0
 1
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_compressed';
-variable_value >= 0
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_compressed';
+variable_value > 0
 1
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_decompressed';
-variable_value >= 0
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_decompressed';
+variable_value > 0
 1
 drop procedure innodb_insert_proc;
 drop table innodb_normal;
diff --git a/mysql-test/suite/encryption/t/innodb-bad-key-change.test b/mysql-test/suite/encryption/t/innodb-bad-key-change.test
index 7886529241f..d2fe34b036c 100644
--- a/mysql-test/suite/encryption/t/innodb-bad-key-change.test
+++ b/mysql-test/suite/encryption/t/innodb-bad-key-change.test
@@ -2,38 +2,26 @@
 -- source include/have_file_key_management_plugin.inc
 # embedded does not support restart
 -- source include/not_embedded.inc
--- source include/not_valgrind.inc
-# Avoid CrashReporter popup on Mac
--- source include/not_crashrep.inc
--- source include/not_windows.inc
 
 #
 # MDEV-8588: Assertion failure in file ha_innodb.cc line 21140 if at least one encrypted
 # table exists and encryption service is not available.
 #
 
-call mtr.add_suppression("Plugin 'file_key_management' init function returned error");
-call mtr.add_suppression("InnoDB: Database page corruption on disk or a failed.*");
-call mtr.add_suppression("Plugin 'file_key_management' registration.*failed");
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-call mtr.add_suppression(".*InnoDB: Cannot open table test/.* from the internal data dictionary of InnoDB though the .frm file for the table exists. See .* for how you can resolve the problem.");
-call mtr.add_suppression("InnoDB: .ibd file is missing for table test/.*");
+call mtr.add_suppression("InnoDB: The page .*");
+call mtr.add_suppression("InnoDB: Cannot calculate statistics for table .* ");
+call mtr.add_suppression("Plugin 'file_key_management' :*");
 call mtr.add_suppression("mysqld: File .*");
-call mtr.add_suppression("InnoDB: Tablespace id .* is encrypted but encryption service or used key_id .* is not available. Can't continue opening tablespace.");
+
 --echo
 --echo # Start server with keys2.txt
 -- let $restart_parameters=--file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
 -- source include/restart_mysqld.inc
 
---disable_query_log
-let $innodb_file_format_orig = `SELECT @@innodb_file_format`;
-let $innodb_file_per_table_orig = `SELECT @@innodb_file_per_table`;
---enable_query_log
-
+--disable_warnings
 SET GLOBAL innodb_file_format = `Barracuda`;
 SET GLOBAL innodb_file_per_table = ON;
+--enable_warnings
 
 CREATE TABLE t1 (c VARCHAR(8)) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=2;
 INSERT INTO t1 VALUES ('foobar');
@@ -70,10 +58,11 @@ SHOW WARNINGS;
 # line 2856 on querying a table using wrong default encryption key
 #
 
---echo # Start server with keys.txt
--- let $restart_parameters=--file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys.txt
+--echo # Start server with keys3.txt
+-- let $restart_parameters=--file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
 -- source include/restart_mysqld.inc
 
+SET GLOBAL innodb_default_encryption_key_id=5;
 CREATE TABLE t2 (c VARCHAR(8), id int not null primary key, b int, key(b)) ENGINE=InnoDB ENCRYPTED=YES;
 INSERT INTO t2 VALUES ('foobar',1,2);
 
@@ -94,6 +83,7 @@ SHOW WARNINGS;
 SELECT * FROM t2 where b = 1;
 --replace_regex /tablespace [0-9]*/tablespace /
 SHOW WARNINGS;
+--replace_regex /tablespace [0-9]*/tablespace /
 --error ER_GET_ERRMSG
 INSERT INTO t2 VALUES ('tmp',3,3);
 --replace_regex /tablespace [0-9]*/tablespace /
@@ -114,7 +104,7 @@ OPTIMIZE TABLE t2;
 --replace_regex /tablespace [0-9]*/tablespace /
 SHOW WARNINGS;
 --error ER_GET_ERRMSG
-ALTER TABLE t2 ADD COLUMN c INT;
+ALTER TABLE t2 ADD COLUMN d INT;
 --replace_regex /tablespace [0-9]*/tablespace /
 SHOW WARNINGS;
 ANALYZE TABLE t2;
@@ -130,8 +120,8 @@ DROP TABLE t2;
 SHOW WARNINGS;
 
 --echo
---echo # Restart server with keys.txt
--- let $restart_parameters=--file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys.txt
+--echo # Restart server with keys3.txt
+-- let $restart_parameters=--file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
 -- source include/restart_mysqld.inc
 
 DROP TABLE t2;
diff --git a/mysql-test/suite/encryption/t/innodb-bad-key-change2.test b/mysql-test/suite/encryption/t/innodb-bad-key-change2.test
index 7a401853296..e01a6a1a887 100644
--- a/mysql-test/suite/encryption/t/innodb-bad-key-change2.test
+++ b/mysql-test/suite/encryption/t/innodb-bad-key-change2.test
@@ -1,59 +1,48 @@
 --source include/have_innodb.inc
 # embedded does not support restart
 -- source include/not_embedded.inc
--- source include/not_valgrind.inc
-# Avoid CrashReporter popup on Mac
--- source include/not_crashrep.inc
 -- source filekeys_plugin_exists.inc
 #
 # MDEV-8750: Server crashes in page_cur_is_after_last on altering table using a wrong encryption key
+# MDEV-8769: Server crash at file btr0btr.ic line 122 when defragmenting encrypted table using incorrect keys
+# MDEV-8768: Server crash at file btr0btr.ic line 122 when checking encrypted table using incorrect keys
+# MDEV-8727: Server/InnoDB hangs on shutdown after trying to read an encrypted table with a wrong key
 #
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-call mtr.add_suppression(".*InnoDB: Cannot open table test/.* from the internal data dictionary of InnoDB though the .frm file for the table exists. See .* for how you can resolve the problem.");
-call mtr.add_suppression("InnoDB: .ibd file is missing for table test/.*");
+call mtr.add_suppression("InnoDB: The page .*");
 # Suppression for builds where file_key_management plugin is linked statically
-call mtr.add_suppression("Couldn't load plugins from 'file_key_management*");
+call mtr.add_suppression("Couldn't load plugins from 'file_key_management.*");
 
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
+--let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--source include/restart_mysqld.inc
 
---write_file $MYSQLTEST_VARDIR/keys1.txt
-1;770A8A65DA156D24EE2A093277530142
-4;770A8A65DA156D24EE2A093277530143
-EOF
+--disable_warnings
+SET GLOBAL innodb_file_format = `Barracuda`;
+SET GLOBAL innodb_file_per_table = ON;
+--enable_warnings
 
---exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
-
-CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB ENCRYPTION_KEY_ID=4;
+CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=4;
 INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
 
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---write_file $MYSQLTEST_VARDIR/keys2.txt
-1;770A8A65DA156D24EE2A093277530142
-4;770A8A65DA156D24EE2A093277530144
-EOF
-
---exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys2.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
+--let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
+--source include/restart_mysqld.inc
 
 --error ER_GET_ERRMSG
-select * from t1;
-show warnings;
+SELECT * FROM t1;
+SHOW WARNINGS;
 --error ER_GET_ERRMSG
 alter table t1 discard tablespace;
-show warnings;
+SHOW WARNINGS;
 --error ER_GET_ERRMSG
-alter table t1 engine=InnoDB;
+ALTER TABLE t1 engine=InnoDB;
 show warnings;
 
---remove_file $MYSQLTEST_VARDIR/keys1.txt
---remove_file $MYSQLTEST_VARDIR/keys2.txt
+OPTIMIZE TABLE t1;
+SHOW WARNINGS;
+
+CHECK TABLE t1;
+SHOW WARNINGS;
+
+--let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--source include/restart_mysqld.inc
+
+drop table t1;
diff --git a/mysql-test/suite/encryption/t/innodb-bad-key-change5.opt b/mysql-test/suite/encryption/t/innodb-bad-key-change4.opt
similarity index 100%
rename from mysql-test/suite/encryption/t/innodb-bad-key-change5.opt
rename to mysql-test/suite/encryption/t/innodb-bad-key-change4.opt
diff --git a/mysql-test/suite/encryption/t/innodb-bad-key-change4.test b/mysql-test/suite/encryption/t/innodb-bad-key-change4.test
index 77333b0666b..17f9ebce4e9 100644
--- a/mysql-test/suite/encryption/t/innodb-bad-key-change4.test
+++ b/mysql-test/suite/encryption/t/innodb-bad-key-change4.test
@@ -1,89 +1,39 @@
 --source include/have_innodb.inc
 # embedded does not support restart
 -- source include/not_embedded.inc
--- source include/not_valgrind.inc
-# Avoid CrashReporter popup on Mac
--- source include/not_crashrep.inc
 -- source filekeys_plugin_exists.inc
 #
+# MDEV-8769: Server crash at file btr0btr.ic line 122 when defragmenting encrypted table using incorrect keys
 # MDEV-8768: Server crash at file btr0btr.ic line 122 when checking encrypted table using incorrect keys
 #
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-call mtr.add_suppression(".*InnoDB: Cannot open table test/.* from the internal data dictionary of InnoDB though the .frm file for the table exists. See .* for how you can resolve the problem.");
+
+call mtr.add_suppression("InnoDB: The page .*");
+call mtr.add_suppression(".*InnoDB: Cannot open table .*");
 call mtr.add_suppression("InnoDB: .ibd file is missing for table test/.*");
 # Suppression for builds where file_key_management plugin is linked statically
-call mtr.add_suppression("Couldn't load plugins from 'file_key_management*");
-
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---write_file $MYSQLTEST_VARDIR/keys1.txt
-1;770A8A65DA156D24EE2A093277530142
-4;770A8A65DA156D24EE2A093277530143
-EOF
-
---exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
+call mtr.add_suppression("Couldn't load plugins from 'file_key_management.*");
 
---let $MYSQLD_TMPDIR = `SELECT @@tmpdir`
---let $MYSQLD_DATADIR = `SELECT @@datadir`
-
---disable_query_log
-let $innodb_file_format_orig = `SELECT @@innodb_file_format`;
-let $innodb_file_per_table_orig = `SELECT @@innodb_file_per_table`;
---enable_query_log
+--let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--source include/restart_mysqld.inc
 
+--disable_warnings
 SET GLOBAL innodb_file_format = `Barracuda`;
 SET GLOBAL innodb_file_per_table = ON;
+--enable_warnings
 
-CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB ENCRYPTION_KEY_ID=4;
+CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=4;
 INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
 
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---write_file $MYSQLTEST_VARDIR/keys2.txt
-1;770A8A65DA156D24EE2A093277530142
-4;770A8A65DA156D24EE2A093277530144
-EOF
+--let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
+--source include/restart_mysqld.inc
 
---exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys2.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
-
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
+OPTIMIZE TABLE t1;
+SHOW WARNINGS;
 
 CHECK TABLE t1;
 SHOW WARNINGS;
 
---remove_file $MYSQLTEST_VARDIR/keys1.txt
---remove_file $MYSQLTEST_VARDIR/keys2.txt
-
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---write_file $MYSQLTEST_VARDIR/keys1.txt
-1;770A8A65DA156D24EE2A093277530142
-4;770A8A65DA156D24EE2A093277530143
-EOF
-
---exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
+--let $restart_parameters=--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--source include/restart_mysqld.inc
 
 DROP TABLE t1;
-
---remove_file $MYSQLTEST_VARDIR/keys1.txt
-
-# reset system
---disable_query_log
-EVAL SET GLOBAL innodb_file_per_table = $innodb_file_per_table_orig;
-EVAL SET GLOBAL innodb_file_format = $innodb_file_format_orig;
---enable_query_log
diff --git a/mysql-test/suite/encryption/t/innodb-bad-key-change5.test b/mysql-test/suite/encryption/t/innodb-bad-key-change5.test
deleted file mode 100644
index 39b6ad2fee0..00000000000
--- a/mysql-test/suite/encryption/t/innodb-bad-key-change5.test
+++ /dev/null
@@ -1,88 +0,0 @@
---source include/have_innodb.inc
-# embedded does not support restart
--- source include/not_embedded.inc
--- source include/not_valgrind.inc
-# Avoid CrashReporter popup on Mac
--- source include/not_crashrep.inc
--- source filekeys_plugin_exists.inc
-#
-# MDEV-8769: Server crash at file btr0btr.ic line 122 when defragmenting encrypted table using incorrect keys
-#
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-call mtr.add_suppression(".*InnoDB: Cannot open table test/.* from the internal data dictionary of InnoDB though the .frm file for the table exists. See .* for how you can resolve the problem.");
-call mtr.add_suppression("InnoDB: .ibd file is missing for table test/.*");
-# Suppression for builds where file_key_management plugin is linked statically
-call mtr.add_suppression("Couldn't load plugins from 'file_key_management*");
-
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---write_file $MYSQLTEST_VARDIR/keys1.txt
-1;770A8A65DA156D24EE2A093277530142
-4;770A8A65DA156D24EE2A093277530143
-EOF
-
---exec echo "restart:--innodb-encrypt-tables --innodb-defragment=1 --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
-
---let $MYSQLD_TMPDIR = `SELECT @@tmpdir`
---let $MYSQLD_DATADIR = `SELECT @@datadir`
-
---disable_query_log
-let $innodb_file_format_orig = `SELECT @@innodb_file_format`;
-let $innodb_file_per_table_orig = `SELECT @@innodb_file_per_table`;
---enable_query_log
-
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
-
-CREATE TABLE t1 (pk INT PRIMARY KEY, f VARCHAR(8)) ENGINE=InnoDB ENCRYPTION_KEY_ID=4;
-INSERT INTO t1 VALUES (1,'foo'),(2,'bar');
-
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---write_file $MYSQLTEST_VARDIR/keys2.txt
-1;770A8A65DA156D24EE2A093277530142
-4;770A8A65DA156D24EE2A093277530144
-EOF
-
---exec echo "restart:--innodb-encrypt-tables --innodb-defragment=1 --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys2.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
-
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
-
-OPTIMIZE TABLE t1;
-SHOW WARNINGS;
-
---remove_file $MYSQLTEST_VARDIR/keys1.txt
---remove_file $MYSQLTEST_VARDIR/keys2.txt
-
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---write_file $MYSQLTEST_VARDIR/keys1.txt
-1;770A8A65DA156D24EE2A093277530142
-4;770A8A65DA156D24EE2A093277530143
-EOF
-
---exec echo "restart:--innodb-encrypt-tables --innodb-defragment=1 --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
-
-DROP TABLE t1;
---remove_file $MYSQLTEST_VARDIR/keys1.txt
-
-# reset system
---disable_query_log
-EVAL SET GLOBAL innodb_file_per_table = $innodb_file_per_table_orig;
-EVAL SET GLOBAL innodb_file_format = $innodb_file_format_orig;
---enable_query_log
diff --git a/mysql-test/suite/encryption/t/innodb-bad-key-shutdown.test b/mysql-test/suite/encryption/t/innodb-bad-key-shutdown.test
deleted file mode 100644
index a301951ea7f..00000000000
--- a/mysql-test/suite/encryption/t/innodb-bad-key-shutdown.test
+++ /dev/null
@@ -1,68 +0,0 @@
-#
-# MDEV-8727: Server/InnoDB hangs on shutdown after trying to read an encrypted table with a wrong key
-#
-
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-call mtr.add_suppression(".*InnoDB: Cannot open table test/.* from the internal data dictionary of InnoDB though the .frm file for the table exists. See .* for how you can resolve the problem.");
-call mtr.add_suppression("InnoDB: .ibd file is missing for table test/.*");
-# Suppression for builds where file_key_management plugin is linked statically
-call mtr.add_suppression("Couldn't load plugins from 'file_key_management*");
-
---echo # 
---echo # Restart the server with key 4 in the key file
---echo # 
-
---source include/have_innodb.inc
---source include/not_embedded.inc
-
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---write_file $MYSQLTEST_VARDIR/keys1.txt
-1;770A8A65DA156D24EE2A093277530142
-4;18420B5CBA31CCDFFE9716E91EB61374D05914F3ADE23E03
-EOF
-
---exec echo "restart:--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
-
-CREATE TABLE t1 (i INT, KEY(i)) ENGINE=InnoDB ENCRYPTED=YES ENCRYPTION_KEY_ID=4;
-INSERT INTO t1 VALUES (1);
-
---echo # 
---echo # Restart the server with a different value for key 4 in the key file
---echo # 
-
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---write_file $MYSQLTEST_VARDIR/keys2.txt
-1;770A8A65DA156D24EE2A093277530142
-4;22222222222222222222222222222222
-EOF
-
---exec echo "restart:--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys2.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
-
---error 1296
-SELECT * FROM t1;
-
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---exec echo "restart:--plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
-
-SELECT * FROM t1;
-DROP TABLE t1;
-
---remove_file $MYSQLTEST_VARDIR/keys2.txt
---remove_file $MYSQLTEST_VARDIR/keys1.txt
diff --git a/mysql-test/suite/encryption/t/innodb-compressed-blob.test b/mysql-test/suite/encryption/t/innodb-compressed-blob.test
index 5d6fbf56e2b..eeb3f024a81 100644
--- a/mysql-test/suite/encryption/t/innodb-compressed-blob.test
+++ b/mysql-test/suite/encryption/t/innodb-compressed-blob.test
@@ -5,80 +5,40 @@
 -- source include/not_embedded.inc
 
 call mtr.add_suppression("InnoDB: Block in space_id .* in file .* encrypted.");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version 1 is not found or used encryption algorithm or method does not match.");
+call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
 call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
+call mtr.add_suppression("InnoDB: The page .* cannot be decrypted.");
 
---disable_query_log
-let $innodb_file_format_orig = `SELECT @@innodb_file_format`;
-let $innodb_file_per_table_orig = `SELECT @@innodb_file_per_table`;
---enable_query_log
-
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---write_file $MYSQLTEST_VARDIR/keys1.txt
-1;770A8A65DA156D24EE2A093277530142
-4;770A8A65DA156D24EE2A093277530143
-EOF
-
---exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
-
+--echo # Restart mysqld --file-key-management-filename=keys2.txt
+-- let $restart_parameters=--file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+-- source include/restart_mysqld.inc
 
+--disable_warnings
 SET GLOBAL innodb_file_format = `Barracuda`;
 SET GLOBAL innodb_file_per_table = ON;
+--enable_warnings
 
 set GLOBAL innodb_default_encryption_key_id=4;
-create table t1(a int not null primary key, b blob) engine=innodb row_format=compressed;
-create table t2(a int not null primary key, b blob) engine=innodb row_format=compressed encrypted=yes;
-create table t3(a int not null primary key, b blob) engine=innodb row_format=compressed encrypted=no;
-CREATE INDEX test ON t1 (b(10));
-CREATE INDEX test ON t2 (b(10));
-CREATE INDEX test ON t3 (b(10));
-
+create table t1(a int not null primary key, b blob, index(b(10))) engine=innodb row_format=compressed;
+create table t2(a int not null primary key, b blob, index(b(10))) engine=innodb row_format=compressed encrypted=yes;
+create table t3(a int not null primary key, b blob, index(b(10))) engine=innodb row_format=compressed encrypted=no;
 
 insert into t1 values (1, repeat('secret',6000));
 insert into t2 values (1, repeat('secret',6000));
 insert into t3 values (1, repeat('secret',6000));
 
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---write_file $MYSQLTEST_VARDIR/keys2.txt
-1;770A8A65DA156D24EE2A093277530142
-4;770A8A65DA156D24EE2A093277530144
-EOF
-
---exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys2.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
---source include/restart_mysqld.inc
+--echo # Restart mysqld --file-key-management-filename=keys3.txt
+-- let $restart_parameters=--file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys3.txt
+-- source include/restart_mysqld.inc
 
 --error 1296
-select count(*) from t1 FORCE INDEX (test) where b like 'secret%';
+select count(*) from t1 FORCE INDEX (b) where b like 'secret%';
 --error 1296
-select count(*) from t2 FORCE INDEX (test) where b like 'secret%';
-select count(*) from t3 FORCE INDEX (test) where b like 'secret%';
-
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---remove_file $MYSQLTEST_VARDIR/keys1.txt
---remove_file $MYSQLTEST_VARDIR/keys2.txt
+select count(*) from t2 FORCE INDEX (b) where b like 'secret%';
+select count(*) from t3 FORCE INDEX (b) where b like 'secret%';
 
---write_file $MYSQLTEST_VARDIR/keys1.txt
-1;770A8A65DA156D24EE2A093277530142
-4;770A8A65DA156D24EE2A093277530143
-EOF
-
---exec echo "restart:--innodb-encrypt-tables --innodb-stats-persistent --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
+--echo # Restart mysqld --file-key-management-filename=keys2.txt
+-- let $restart_parameters=--file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+-- source include/restart_mysqld.inc
 
 drop table t1,t2,t3;
-
---remove_file $MYSQLTEST_VARDIR/keys1.txt
diff --git a/mysql-test/suite/encryption/t/innodb-encryption-disable.test b/mysql-test/suite/encryption/t/innodb-encryption-disable.test
index b5996a51eb6..68611e8aa98 100644
--- a/mysql-test/suite/encryption/t/innodb-encryption-disable.test
+++ b/mysql-test/suite/encryption/t/innodb-encryption-disable.test
@@ -1,43 +1,18 @@
 -- source include/have_innodb.inc
 # embedded does not support restart
 -- source include/not_embedded.inc
--- source include/not_valgrind.inc
-# Avoid CrashReporter popup on Mac
--- source include/not_crashrep.inc
 -- source filekeys_plugin_exists.inc
 
---disable_query_log
-let $innodb_file_format_orig = `SELECT @@innodb_file_format`;
-let $innodb_file_per_table_orig = `SELECT @@innodb_file_per_table`;
---enable_query_log
-
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
-
 #
 # MDEV-9559: Server without encryption configs crashes if selecting from an implicitly encrypted table
 #
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-call mtr.add_suppression(".*InnoDB: Cannot open table test/.* from the internal data dictionary of InnoDB though the .frm file for the table exists. See .* for how you can resolve the problem.");
-call mtr.add_suppression("InnoDB: .ibd file is missing for table test/.*");
+
+call mtr.add_suppression("InnoDB: The page .*");
 # Suppression for builds where file_key_management plugin is linked statically
 call mtr.add_suppression("Couldn't load plugins from 'file_key_management*");
-call mtr.add_suppression("InnoDB: Tablespace id.* is encrypted but encryption service or used key_id .* is not available. Can't continue opening tablespace.");
-
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---write_file $MYSQLTEST_VARDIR/keys1.txt
-1;770A8A65DA156D24EE2A093277530142
-4;770A8A65DA156D24EE2A093277530143
-EOF
 
---exec echo "restart:--innodb-encrypt-tables --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
+--let $restart_parameters=--innodb-encrypt-tables=ON --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--source include/restart_mysqld.inc
 
 create table t5 (
      `intcol1` int(32) DEFAULT NULL,
@@ -60,33 +35,16 @@ CREATE TABLE `t1` (
 insert into t1 values (1,2,'maria','db','encryption');
 alter table t1 encrypted='yes' `encryption_key_id`=1;
 
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---exec echo "restart:--innodb-encrypt-tables=OFF" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
+--let $restart_parameters=--innodb-encrypt-tables=OFF
+--source include/restart_mysqld.inc
 
 --error 1296
 select * from t1;
 --error 1296
 select * from t5;
 
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---shutdown_server
---source include/wait_until_disconnected.inc
-
---exec echo "restart:--innodb-encrypt-tables --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQLTEST_VARDIR/keys1.txt" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
---enable_reconnect
---source include/wait_until_connected_again.inc
+--let $restart_parameters=--innodb-encrypt-tables=ON --plugin-load-add=file_key_management.so --file-key-management --file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+--source include/restart_mysqld.inc
 
 drop table t1;
 drop table t5;
-
---disable_query_log
-EVAL SET GLOBAL innodb_file_per_table = $innodb_file_per_table_orig;
-EVAL SET GLOBAL innodb_file_format = $innodb_file_format_orig;
---enable_query_log
-
---remove_file $MYSQLTEST_VARDIR/keys1.txt
diff --git a/mysql-test/suite/encryption/t/innodb-force-corrupt.test b/mysql-test/suite/encryption/t/innodb-force-corrupt.test
index 1f05726830b..582e7ab668a 100644
--- a/mysql-test/suite/encryption/t/innodb-force-corrupt.test
+++ b/mysql-test/suite/encryption/t/innodb-force-corrupt.test
@@ -4,24 +4,12 @@
 
 -- source include/have_innodb.inc
 -- source include/have_file_key_management_plugin.inc
-# Don't test under valgrind, memory leaks will occur
-source include/not_valgrind.inc;
-# Avoid CrashReporter popup on Mac
-source include/not_crashrep.inc;
 # Don't test under embedded
-source include/not_embedded.inc;
-# Require InnoDB
-source include/have_innodb.inc;
-# Test could open crash reporter on Windows
-# if compiler set up
-source include/not_windows.inc;
+-- source include/not_embedded.inc
 
 call mtr.add_suppression("InnoDB: Database page corruption on disk or a failed.*");
 call mtr.add_suppression("InnoDB: Corruption: Block in space_id .* in file .* corrupted.");
-call mtr.add_suppression("InnoDB: Based on page type .*");
 call mtr.add_suppression("InnoDB: Unable to read tablespace .* page no .* into the buffer pool after .* attempts");
-CALL mtr.add_suppression("InnoDB: Error: Unable to read tablespace .* page no .* into the buffer pool after 100 attempts");
-CALL mtr.add_suppression("InnoDB: Database page corruption on disk or a failed");
 CALL mtr.add_suppression("InnoDB: Space .* file test/t1 read of page .*");
 CALL mtr.add_suppression("InnoDB: You may have to recover from a backup.");
 CALL mtr.add_suppression("InnoDB: It is also possible that your operatingsystem has corrupted its own file cache.");
@@ -31,9 +19,9 @@ CALL mtr.add_suppression("InnoDB: fix the corruption by dumping, dropping, and r
 CALL mtr.add_suppression("InnoDB: the corrupt table. You can use CHECK");
 CALL mtr.add_suppression("InnoDB: TABLE to scan your table for corruption.");
 CALL mtr.add_suppression("InnoDB: See also .* about forcing recovery.");
-CALL mtr.add_suppression("InnoDB: However key management plugin or used key_version 3221605118 is not found or used encryption algorithm or method does not match.");
+CALL mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
 CALL mtr.add_suppression("Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
-CALL mtr.add_suppression("InnoDB: Block in space_id .* in file .* encrypted.");
+CALL mtr.add_suppression("InnoDB: The page .* cannot be decrypted.");
 
 --disable_warnings
 SET GLOBAL innodb_file_format = `Barracuda`;
@@ -46,9 +34,10 @@ CREATE TABLE t1 (a INT AUTO_INCREMENT PRIMARY KEY, b TEXT,c char(200)) ENGINE=In
 CREATE TABLE t2 (a INT AUTO_INCREMENT PRIMARY KEY, b TEXT,c char(200)) ENGINE=InnoDB row_format=compressed encrypted=yes;
 CREATE TABLE t3 (a INT AUTO_INCREMENT PRIMARY KEY, b TEXT, c char(200)) ENGINE=InnoDB page_compressed=yes encrypted=yes;
 
+BEGIN;
 INSERT INTO t1 (b,c) VALUES ('corrupt me','secret');
 --disable_query_log
---let $i = 10
+--let $i = 100
 while ($i)
 {
   INSERT INTO t1 (b,c) VALUES (REPEAT('abcabcabc', 100),'secretsecret');
@@ -59,15 +48,12 @@ while ($i)
 INSERT INTO t1 (b,c) VALUES ('corrupt me','moresecretmoresecret');
 INSERT INTO t2 select * from t1;
 INSERT INTO t3 select * from t1;
+COMMIT;
 
-let $MYSQLD_DATADIR=`select @@datadir`;
 let INNODB_PAGE_SIZE=`select @@innodb_page_size`;
 let MYSQLD_DATADIR=`select @@datadir`;
 
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
-
 --source include/shutdown_mysqld.inc
---source include/wait_until_disconnected.inc
 
 --echo # Backup tables before corrupting
 --copy_file $MYSQLD_DATADIR/test/t1.ibd $MYSQLD_DATADIR/test/t1.ibd.backup
@@ -82,17 +68,11 @@ binmode FILE;
 seek(FILE, $ENV{'INNODB_PAGE_SIZE'} * 3 + 26, SEEK_SET) or die "seek";
 print FILE pack("H*", "c00lcafedeadb017");
 close FILE or die "close";
-EOF
-
-perl;
 open(FILE, "+<", "$ENV{MYSQLD_DATADIR}/test/t2.ibd") or die "open";
 binmode FILE;
 seek(FILE, $ENV{'INNODB_PAGE_SIZE'} * 3 + 26, SEEK_SET) or die "seek";
 print FILE pack("H*", "c00lcafedeadb017");
 close FILE or die "close";
-EOF
-
-perl;
 open(FILE, "+<", "$ENV{MYSQLD_DATADIR}/test/t3.ibd") or die "open";
 binmode FILE;
 seek(FILE, $ENV{'INNODB_PAGE_SIZE'} * 3 + 26, SEEK_SET) or die "seek";
@@ -102,26 +82,16 @@ EOF
 
 --source include/start_mysqld.inc
 
---echo # Write file to make mysql-test-run.pl expect the "crash", but don't
---echo # start it until it's told to
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
-
---echo # The below SELECT query will crash the server because some pages
---echo # on the disk are corrupted
-
---error 1296
+--error ER_GET_ERRMSG
 SELECT * FROM t1;
+--error ER_GET_ERRMSG
 SELECT * FROM t2;
---error 1296
+--error ER_GET_ERRMSG
 SELECT * FROM t3;
 
---exec echo "wait" > $MYSQLTEST_VARDIR/tmp/mysqld.1.expect
-
 --source include/shutdown_mysqld.inc
---source include/wait_until_disconnected.inc
 
---echo # Restore the original t1.ibd
---remove_file $MYSQLD_DATADIR/test/t1.ibd
+--echo # Restore the original tables
 --move_file $MYSQLD_DATADIR/test/t1.ibd.backup $MYSQLD_DATADIR/test/t1.ibd
 --move_file $MYSQLD_DATADIR/test/t2.ibd.backup $MYSQLD_DATADIR/test/t2.ibd
 --move_file $MYSQLD_DATADIR/test/t3.ibd.backup $MYSQLD_DATADIR/test/t3.ibd
diff --git a/mysql-test/suite/encryption/t/innodb-missing-key.test b/mysql-test/suite/encryption/t/innodb-missing-key.test
index 205d3fa2061..1c8d0a7ed21 100644
--- a/mysql-test/suite/encryption/t/innodb-missing-key.test
+++ b/mysql-test/suite/encryption/t/innodb-missing-key.test
@@ -2,16 +2,11 @@
 -- source include/have_file_key_management_plugin.inc
 # embedded does not support restart
 -- source include/not_embedded.inc
--- source include/not_valgrind.inc
-# Avoid CrashReporter popup on Mac
--- source include/not_crashrep.inc
 
 #
 # MDEV-11004: Unable to start (Segfault or os error 2) when encryption key missing
 #
-call mtr.add_suppression("InnoDB: Block in space_id .* in file test/.* encrypted");
-call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
-call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
+call mtr.add_suppression("InnoDB: The page .*");
 
 --echo
 --echo # Start server with keys2.txt
diff --git a/mysql-test/suite/encryption/t/innodb-page_encryption_compression.test b/mysql-test/suite/encryption/t/innodb-page_encryption_compression.test
index eb293e97693..e53d784980c 100644
--- a/mysql-test/suite/encryption/t/innodb-page_encryption_compression.test
+++ b/mysql-test/suite/encryption/t/innodb-page_encryption_compression.test
@@ -1,15 +1,10 @@
 -- source include/have_innodb.inc
--- source include/not_embedded.inc
 -- source include/have_file_key_management_plugin.inc
--- source include/big_test.inc
-# Test heavy not tested on valgrind
--- source include/not_valgrind.inc
 
 --disable_query_log
 let $innodb_compression_algorithm_orig=`SELECT @@innodb_compression_algorithm`;
 let $innodb_file_format_orig = `SELECT @@innodb_file_format`;
 let $innodb_file_per_table_orig = `SELECT @@innodb_file_per_table`;
-let $encryption = `SELECT @@innodb_encrypt_tables`;
 --enable_query_log
 
 SET GLOBAL innodb_file_format = `Barracuda`;
@@ -41,53 +36,21 @@ end//
 delimiter ;//
 commit;
 
-set autocommit=0;
-call innodb_insert_proc(5000);
-commit;
-set autocommit=1;
-
+begin;
+call innodb_insert_proc(2000);
 insert into innodb_compact select * from innodb_normal;
 insert into innodb_dynamic select * from innodb_normal;
+commit;
 
-update innodb_normal set c1 = c1 + 1;
-update innodb_compact set c1 = c1 + 1;
-update innodb_dynamic set c1 = c1 + 1;
-select count(*) from innodb_normal;
-select count(*) from innodb_compact where c1 < 1500000;
-select count(*) from innodb_dynamic where c1 < 1500000;
-select count(*) from innodb_compact t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-select count(*) from innodb_dynamic t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_compressed';
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decompressed';
-
---source include/restart_mysqld.inc
-
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
-# zlib
-set global innodb_compression_algorithm = 1;
-
-update innodb_normal set c1 = c1 + 1;
-update innodb_compact set c1 = c1 + 1;
-update innodb_dynamic set c1 = c1 + 1;
-select count(*) from innodb_normal;
-select count(*) from innodb_compact where c1 < 1500000;
-select count(*) from innodb_dynamic where c1 < 1500000;
-select count(*) from innodb_compact t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-select count(*) from innodb_dynamic t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
+let $wait_condition= select variable_value > 0 from information_schema.global_status where variable_name = 'INNODB_NUM_PAGES_PAGE_COMPRESSED';
+--source include/wait_condition.inc
 
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_compressed';
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_compressed';
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decompressed';
 
+SET GLOBAL innodb_encrypt_tables=OFF;
 alter table innodb_normal engine=innodb page_compressed=DEFAULT;
 show create table innodb_normal;
 alter table innodb_compact engine=innodb encrypted=DEFAULT encryption_key_id=DEFAULT page_compressed=DEFAULT;
@@ -95,30 +58,13 @@ show create table innodb_compact;
 alter table innodb_dynamic engine=innodb encrypted=DEFAULT encryption_key_id=DEFAULT page_compressed=DEFAULT;
 show create table innodb_dynamic;
 
---source include/restart_mysqld.inc
+let $wait_condition= select variable_value > 0 from information_schema.global_status where variable_name = 'INNODB_NUM_PAGES_PAGE_DECRYPTED';
+--source include/wait_condition.inc
 
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
-
-show create table innodb_normal;
-show create table innodb_compact;
-show create table innodb_dynamic;
-
-update innodb_normal set c1 = c1 + 1;
-update innodb_compact set c1 = c1 + 1;
-update innodb_dynamic set c1 = c1 + 1;
-select count(*) from innodb_normal;
-select count(*) from innodb_compact where c1 < 1500000;
-select count(*) from innodb_dynamic where c1 < 1500000;
-select count(*) from innodb_compact t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-select count(*) from innodb_dynamic t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-
-SELECT variable_value = 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
-SELECT variable_value = 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
-SELECT variable_value = 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_compressed';
-SELECT variable_value = 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decompressed';
+SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
+SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_compressed';
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decompressed';
 
 drop procedure innodb_insert_proc;
 drop table innodb_normal;
@@ -130,5 +76,4 @@ drop table innodb_dynamic;
 EVAL SET GLOBAL innodb_compression_algorithm = $innodb_compression_algorithm_orig;
 EVAL SET GLOBAL innodb_file_per_table = $innodb_file_per_table_orig;
 EVAL SET GLOBAL innodb_file_format = $innodb_file_format_orig;
-EVAL SET GLOBAL innodb_encrypt_tables = $encryption;
 --enable_query_log
diff --git a/mysql-test/suite/encryption/t/innodb-page_encryption_log_encryption.test b/mysql-test/suite/encryption/t/innodb-page_encryption_log_encryption.test
index b6fe0d10b65..1afd41db831 100644
--- a/mysql-test/suite/encryption/t/innodb-page_encryption_log_encryption.test
+++ b/mysql-test/suite/encryption/t/innodb-page_encryption_log_encryption.test
@@ -2,17 +2,14 @@
 -- source include/not_embedded.inc
 -- source include/have_file_key_management_plugin.inc
 
---disable_query_log
-let $innodb_file_format_orig = `SELECT @@innodb_file_format`;
-let $innodb_file_per_table_orig = `SELECT @@innodb_file_per_table`;
---enable_query_log
-
 call mtr.add_suppression("KeyID 0 not found or with error. Check the key and the log*");
 call mtr.add_suppression("Disabling redo log encryp*");
 call mtr.add_suppression("InnoDB: Redo log crypto: Can't initialize to key version*");
 
+--disable_warnings
 SET GLOBAL innodb_file_format = `Barracuda`;
 SET GLOBAL innodb_file_per_table = ON;
+--enable_warnings
 
 create table innodb_normal(c1 bigint not null, b char(200)) engine=innodb;
 show warnings;
@@ -43,64 +40,41 @@ end//
 delimiter ;//
 commit;
 
-set autocommit=0;
+begin;
 call innodb_insert_proc(2000);
-commit;
-set autocommit=1;
-
 insert into innodb_compact select * from innodb_normal;
 insert into innodb_compressed select * from innodb_normal;
 insert into innodb_dynamic select * from innodb_normal;
 insert into innodb_redundant select * from innodb_normal;
+commit;
 
-update innodb_normal set c1 = c1 +1;
-update innodb_compact set c1 = c1 + 1;
-update innodb_compressed set c1 = c1 + 1;
-update innodb_dynamic set c1 = c1 + 1;
-update innodb_redundant set c1 = c1 + 1;
-select count(*) from innodb_compact where c1 < 1500000;
-select count(*) from innodb_compressed where c1 < 1500000;
-select count(*) from innodb_dynamic where c1 < 1500000;
-select count(*) from innodb_redundant where c1 < 1500000;
-select count(*) from innodb_compact t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-select count(*) from innodb_dynamic t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-select count(*) from innodb_compressed t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-select count(*) from innodb_redundant t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
+let $wait_condition= select variable_value > 0 from information_schema.global_status where variable_name = 'INNODB_NUM_PAGES_ENCRYPTED';
+--source include/wait_condition.inc
 
 # Note there that these variables are updated only when real I/O is done, thus they are not reliable
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
 
 --source include/restart_mysqld.inc
 
+--disable_warnings
 SET GLOBAL innodb_file_format = `Barracuda`;
 SET GLOBAL innodb_file_per_table = ON;
+--enable_warnings
 
 update innodb_normal set c1 = c1 +1;
 update innodb_compact set c1 = c1 + 1;
 update innodb_compressed set c1 = c1 + 1;
 update innodb_dynamic set c1 = c1 + 1;
 update innodb_redundant set c1 = c1 + 1;
-select count(*) from innodb_compact where c1 < 1500000;
-select count(*) from innodb_compressed where c1 < 1500000;
-select count(*) from innodb_dynamic where c1 < 1500000;
-select count(*) from innodb_redundant where c1 < 1500000;
-select count(*) from innodb_compact t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-select count(*) from innodb_dynamic t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-select count(*) from innodb_compressed t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-select count(*) from innodb_redundant t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
 
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
+let $wait_condition= select variable_value > 0 from information_schema.global_status where variable_name = 'INNODB_NUM_PAGES_ENCRYPTED';
+--source include/wait_condition.inc
+
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
 
+SET GLOBAL innodb_encrypt_tables=OFF;
 alter table innodb_compact engine=innodb encrypted=DEFAULT encryption_key_id=DEFAULT;
 show create table innodb_compact;
 alter table innodb_compressed engine=innodb encrypted=DEFAULT encryption_key_id=DEFAULT;
@@ -110,33 +84,8 @@ show create table innodb_dynamic;
 alter table innodb_redundant engine=innodb encrypted=DEFAULT encryption_key_id=DEFAULT;
 show create table innodb_redundant;
 
---source include/restart_mysqld.inc
-
-SET GLOBAL innodb_file_format = `Barracuda`;
-SET GLOBAL innodb_file_per_table = ON;
-
-show create table innodb_compact;
-show create table innodb_compressed;
-show create table innodb_dynamic;
-show create table innodb_redundant;
-
-update innodb_normal set c1 = c1 +1;
-update innodb_compact set c1 = c1 + 1;
-update innodb_compressed set c1 = c1 + 1;
-update innodb_dynamic set c1 = c1 + 1;
-update innodb_redundant set c1 = c1 + 1;
-select count(*) from innodb_compact where c1 < 1500000;
-select count(*) from innodb_compressed where c1 < 1500000;
-select count(*) from innodb_dynamic where c1 < 1500000;
-select count(*) from innodb_redundant where c1 < 1500000;
-select count(*) from innodb_compact t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-select count(*) from innodb_dynamic t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-select count(*) from innodb_compressed t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
-select count(*) from innodb_redundant t1, innodb_normal t2 where
-t1.c1 = t2.c1 and t1.b = t2.b;
+let $wait_condition= select variable_value > 0 from information_schema.global_status where variable_name = 'INNODB_NUM_PAGES_DECRYPTED';
+--source include/wait_condition.inc
 
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
@@ -147,10 +96,12 @@ drop table innodb_compact;
 drop table innodb_compressed;
 drop table innodb_dynamic;
 drop table innodb_redundant;
-
 #
 # MDEV-8143: InnoDB: Database page corruption on disk or a failed file read
 #
+SET GLOBAL innodb_file_format = `Barracuda`;
+SET GLOBAL innodb_file_per_table = ON;
+
 CREATE TABLE t1 (pk INT PRIMARY KEY) ENGINE=InnoDB ENCRYPTION_KEY_ID=2 ENCRYPTED=YES;
 INSERT INTO t1 VALUES (1),(2);
 
@@ -160,9 +111,3 @@ INSERT INTO t1 VALUES (1),(2);
 SELECT * FROM t1;
 
 DROP TABLE t1;
-
-# reset system
---disable_query_log
-EVAL SET GLOBAL innodb_file_per_table = $innodb_file_per_table_orig;
-EVAL SET GLOBAL innodb_file_format = $innodb_file_format_orig;
---enable_query_log
diff --git a/mysql-test/suite/encryption/t/innodb-redo-badkey.opt b/mysql-test/suite/encryption/t/innodb-redo-badkey.opt
new file mode 100644
index 00000000000..343128e8803
--- /dev/null
+++ b/mysql-test/suite/encryption/t/innodb-redo-badkey.opt
@@ -0,0 +1,5 @@
+--innodb-change-buffering=all
+--innodb-encrypt-tables=on
+--innodb-tablespaces-encryption
+--innodb-encryption-threads=2
+--innodb-default-encryption-key-id=4
diff --git a/mysql-test/suite/encryption/t/innodb-redo-badkey.test b/mysql-test/suite/encryption/t/innodb-redo-badkey.test
new file mode 100644
index 00000000000..cd79a033375
--- /dev/null
+++ b/mysql-test/suite/encryption/t/innodb-redo-badkey.test
@@ -0,0 +1,93 @@
+-- source include/have_innodb.inc
+-- source include/have_file_key_management_plugin.inc
+# embedded does not support restart
+-- source include/not_embedded.inc
+
+call mtr.add_suppression("InnoDB: Block in space_id .* in file .* encrypted.");
+call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
+call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
+call mtr.add_suppression("mysqld: File .* not found .*");
+call mtr.add_suppression("Plugin 'file_key_management' init function returned error.");
+call mtr.add_suppression("Plugin 'file_key_management' registration as a ENCRYPTION failed.");
+call mtr.add_suppression("InnoDB: cannot enable encryption, encryption plugin is not available");
+call mtr.add_suppression("Plugin 'InnoDB' init function returned error.");
+call mtr.add_suppression("Plugin 'InnoDB' registration as a STORAGE ENGINE failed.");
+call mtr.add_suppression("InnoDB: Read operation failed for tablespace .*");
+call mtr.add_suppression("InnoDB: Database page corruption on disk or a failed.*");
+call mtr.add_suppression("InnoDB: Space .* file .* read of page .*.");
+call mtr.add_suppression("InnoDB: You may have to recover from a backup.");
+call mtr.add_suppression("InnoDB: Recovery read page .* from tablespace .*");
+call mtr.add_suppression("InnoDB: The page .* cannot be decrypted.");
+call mtr.add_suppression("InnoDB: Missing MLOG_FILE_NAME or MLOG_FILE_DELETE .*");
+call mtr.add_suppression("InnoDB: Plugin initialization aborted .*");
+call mtr.add_suppression("InnoDB: ############### CORRUPT LOG RECORD FOUND ##################");
+
+--echo # Restart mysqld --file-key-management-filename=keys2.txt
+-- let $restart_parameters=--file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+-- source include/restart_mysqld.inc
+
+--echo # Wait max 10 min for key encryption threads to encrypt all spaces
+--let $wait_timeout= 600
+--let $wait_condition=SELECT COUNT(*) = 0 FROM INFORMATION_SCHEMA.INNODB_TABLESPACES_ENCRYPTION WHERE MIN_KEY_VERSION = 0
+--source include/wait_condition.inc
+
+--disable_warnings
+SET GLOBAL innodb_file_format = `Barracuda`;
+SET GLOBAL innodb_file_per_table = ON;
+--enable_warnings
+
+create table t1(a int not null primary key auto_increment, c char(250), b blob, index(b(10))) engine=innodb row_format=compressed encrypted=yes encryption_key_id=4;
+create table t2(a int not null primary key auto_increment, c char(250), b blob, index(b(10))) engine=innodb row_format=compressed;
+create table t3(a int not null primary key auto_increment, c char(250), b blob, index(b(10))) engine=innodb encrypted=yes encryption_key_id=4;
+create table t4(a int not null primary key auto_increment, c char(250), b blob, index(b(10))) engine=innodb;
+
+begin;
+--disable_query_log
+--let $i = 20
+begin;
+while ($i)
+{
+  insert into t1(c,b) values (repeat('secret1',20), repeat('secret2',6000));
+  dec $i;
+}
+--enable_query_log
+
+insert into t2 select * from t1;
+insert into t3 select * from t1;
+insert into t4 select * from t1;
+commit;
+
+--source ../../suite/innodb/include/no_checkpoint_start.inc
+
+#
+# We test redo log page read at recv_read_page using
+# incorrect keys from std_data/keys.txt. If checkpoint
+# happens we will skip this test. If no checkpoint
+# happens, InnoDB refuses to start as used
+# encryption key is incorrect.
+#
+SET GLOBAL innodb_flush_log_at_trx_commit=1;
+begin;
+update t1 set c = repeat('secret3', 20);
+update t2 set c = repeat('secret4', 20);
+update t3 set c = repeat('secret4', 20);
+update t4 set c = repeat('secret4', 20);
+insert into t1 (c,b) values (repeat('secret5',20), repeat('secret6',6000));
+insert into t2 (c,b) values (repeat('secret7',20), repeat('secret8',6000));
+insert into t3 (c,b) values (repeat('secret9',20), repeat('secre10',6000));
+insert into t4 (c,b) values (repeat('secre11',20), repeat('secre12',6000));
+COMMIT;
+let $cleanup= drop table t1,t2,t3,t4;
+--let CLEANUP_IF_CHECKPOINT= $cleanup;
+--source ../../suite/innodb/include/no_checkpoint_end.inc
+
+--echo # restart
+--error 1
+-- source include/start_mysqld.inc
+--source include/kill_mysqld.inc
+
+--echo # Restart mysqld --file-key-management-filename=keys2.txt
+-- let $restart_parameters=--file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+-- source include/start_mysqld.inc
+
+drop table t1, t2,t3,t4;
diff --git a/mysql-test/suite/encryption/t/innodb-redo-nokeys.opt b/mysql-test/suite/encryption/t/innodb-redo-nokeys.opt
new file mode 100644
index 00000000000..21afc19fc5d
--- /dev/null
+++ b/mysql-test/suite/encryption/t/innodb-redo-nokeys.opt
@@ -0,0 +1,3 @@
+--innodb-change-buffering=none
+--innodb-encrypt-tables=on
+--innodb-default-encryption-key-id=20
diff --git a/mysql-test/suite/encryption/t/innodb-redo-nokeys.test b/mysql-test/suite/encryption/t/innodb-redo-nokeys.test
new file mode 100644
index 00000000000..5fff14c29f6
--- /dev/null
+++ b/mysql-test/suite/encryption/t/innodb-redo-nokeys.test
@@ -0,0 +1,80 @@
+-- source include/have_innodb.inc
+-- source include/have_file_key_management_plugin.inc
+# embedded does not support restart
+-- source include/not_embedded.inc
+
+call mtr.add_suppression("InnoDB: Block in space_id .* in file .* encrypted.");
+call mtr.add_suppression("InnoDB: However key management plugin or used key_version .* is not found or used encryption algorithm or method does not match.");
+call mtr.add_suppression("InnoDB: Marking tablespace as missing. You may drop this table or install correct key management plugin and key file.");
+call mtr.add_suppression("mysqld: File .* not found .*");
+call mtr.add_suppression("Plugin 'file_key_management' init function returned error.");
+call mtr.add_suppression("Plugin 'file_key_management' registration as a ENCRYPTION failed.");
+call mtr.add_suppression("InnoDB: cannot enable encryption, encryption plugin is not available");
+call mtr.add_suppression("Plugin 'InnoDB' init function returned error.");
+call mtr.add_suppression("Plugin 'InnoDB' registration as a STORAGE ENGINE failed.");
+call mtr.add_suppression("InnoDB: The page .* cannot be decrypted.");
+
+--echo # Restart mysqld --file-key-management-filename=keys2.txt
+-- let $restart_parameters=--file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+-- source include/restart_mysqld.inc
+
+--disable_warnings
+SET GLOBAL innodb_file_format = `Barracuda`;
+SET GLOBAL innodb_file_per_table = ON;
+--enable_warnings
+
+create table t1(a int not null primary key auto_increment, c char(200), b blob, index(b(10))) engine=innodb row_format=compressed encrypted=yes encryption_key_id=20;
+create table t2(a int not null primary key auto_increment, c char(200), b blob, index(b(10))) engine=innodb row_format=compressed;
+create table t3(a int not null primary key auto_increment, c char(200), b blob, index(b(10))) engine=innodb encrypted=yes encryption_key_id=20;
+create table t4(a int not null primary key auto_increment, c char(200), b blob, index(b(10))) engine=innodb;
+
+begin;
+--disable_query_log
+--let $i = 20
+begin;
+while ($i)
+{
+  insert into t1(c,b) values (repeat('secret1',20), repeat('secret2',6000));
+  dec $i;
+}
+--enable_query_log
+
+insert into t2 select * from t1;
+insert into t3 select * from t1;
+insert into t4 select * from t1;
+commit;
+
+--source ../../suite/innodb/include/no_checkpoint_start.inc
+#
+# We test redo log page read at recv_read_page using
+# keys that are not in std_data/keys.txt. If checkpoint
+# happens we will skip this test. If no checkpoint
+# happens, InnoDB refuses to start as used
+# encryption key is not found.
+#
+SET GLOBAL innodb_flush_log_at_trx_commit=1;
+begin;
+update t1 set c = repeat('secret3', 20);
+update t2 set c = repeat('secret4', 20);
+update t3 set c = repeat('secret4', 20);
+update t4 set c = repeat('secret4', 20);
+insert into t1 (c,b) values (repeat('secret5',20), repeat('secret6',6000));
+insert into t2 (c,b) values (repeat('secret7',20), repeat('secret8',6000));
+insert into t3 (c,b) values (repeat('secret9',20), repeat('secre10',6000));
+insert into t4 (c,b) values (repeat('secre11',20), repeat('secre12',6000));
+COMMIT;
+let $cleanup= drop table t1,t2,t3,t4;
+--let CLEANUP_IF_CHECKPOINT= $cleanup;
+--source ../../suite/innodb/include/no_checkpoint_end.inc
+
+--echo # restart
+-- source include/start_mysqld.inc
+#
+# In above server does start but InnoDB refuses to start
+# thus we need to restart server with correct key file
+#
+--echo # Restart mysqld --file-key-management-filename=keys2.txt
+-- let $restart_parameters=--file-key-management-filename=$MYSQL_TEST_DIR/std_data/keys2.txt
+-- source include/restart_mysqld.inc
+
+drop table t1, t2,t3,t4;
diff --git a/mysql-test/suite/encryption/t/innodb_encryption-page-compression.test b/mysql-test/suite/encryption/t/innodb_encryption-page-compression.test
index c2c7bd96d3e..01ca54835bf 100644
--- a/mysql-test/suite/encryption/t/innodb_encryption-page-compression.test
+++ b/mysql-test/suite/encryption/t/innodb_encryption-page-compression.test
@@ -1,9 +1,6 @@
 -- source include/have_innodb.inc
 -- source include/have_example_key_management_plugin.inc
 
-# embedded does not support restart
--- source include/not_embedded.inc
-
 --disable_query_log
 let $innodb_compression_algorithm_orig=`SELECT @@innodb_compression_algorithm`;
 let $innodb_file_format_orig = `SELECT @@innodb_file_format`;
@@ -12,10 +9,12 @@ let $innodb_encrypt_tables_orig = `SELECT @@innodb_encrypt_tables`;
 let $innodb_encryption_threads_orig = `SELECT @@innodb_encryption_threads`;
 --enable_query_log
 
+--disable_warnings
 SET GLOBAL innodb_file_format = `Barracuda`;
 SET GLOBAL innodb_file_per_table = ON;
 SET GLOBAL innodb_encryption_threads = 4;
 SET GLOBAL innodb_encrypt_tables = on;
+--enable_warnings
 
 # zlib
 set global innodb_compression_algorithm = 1;
@@ -62,11 +61,8 @@ end//
 delimiter ;//
 commit;
 
-set autocommit=0;
-call innodb_insert_proc(5000);
-commit;
-set autocommit=1;
-select count(*) from innodb_normal;
+begin;
+call innodb_insert_proc(2000);
 insert into innodb_page_compressed1 select * from innodb_normal;
 insert into innodb_page_compressed2 select * from innodb_normal;
 insert into innodb_page_compressed3 select * from innodb_normal;
@@ -77,23 +73,6 @@ insert into innodb_page_compressed7 select * from innodb_normal;
 insert into innodb_page_compressed8 select * from innodb_normal;
 insert into innodb_page_compressed9 select * from innodb_normal;
 commit;
-select count(*) from innodb_page_compressed1 where c1 < 500000;
-select count(*) from innodb_page_compressed2 where c1 < 500000;
-select count(*) from innodb_page_compressed3 where c1 < 500000;
-select count(*) from innodb_page_compressed4 where c1 < 500000;
-select count(*) from innodb_page_compressed5 where c1 < 500000;
-select count(*) from innodb_page_compressed6 where c1 < 500000;
-select count(*) from innodb_page_compressed7 where c1 < 500000;
-select count(*) from innodb_page_compressed8 where c1 < 500000;
-select count(*) from innodb_page_compressed9 where c1 < 500000;
-
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_compressed';
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_decompressed';
-
-
---source include/restart_mysqld.inc
 
 select count(*) from innodb_page_compressed1 where c1 < 500000;
 select count(*) from innodb_page_compressed2 where c1 < 500000;
@@ -104,33 +83,18 @@ select count(*) from innodb_page_compressed6 where c1 < 500000;
 select count(*) from innodb_page_compressed7 where c1 < 500000;
 select count(*) from innodb_page_compressed8 where c1 < 500000;
 select count(*) from innodb_page_compressed9 where c1 < 500000;
-update innodb_page_compressed1 set c1 = c1 + 1;
-update innodb_page_compressed2 set c1 = c1 + 1;
-update innodb_page_compressed3 set c1 = c1 + 1;
-update innodb_page_compressed4 set c1 = c1 + 1;
-update innodb_page_compressed5 set c1 = c1 + 1;
-update innodb_page_compressed6 set c1 = c1 + 1;
-update innodb_page_compressed7 set c1 = c1 + 1;
-update innodb_page_compressed8 set c1 = c1 + 1;
-update innodb_page_compressed9 set c1 = c1 + 1;
 
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
+let $wait_condition= select variable_value > 0 from information_schema.global_status where variable_name = 'INNODB_NUM_PAGES_PAGE_COMPRESSED';
+--source include/wait_condition.inc
+
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_compressed';
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_compressed';
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_decompressed';
 
 SET GLOBAL innodb_encryption_threads = 4;
 SET GLOBAL innodb_encrypt_tables = off;
 
-select count(*) from innodb_page_compressed1 where c1 < 500000;
-select count(*) from innodb_page_compressed2 where c1 < 500000;
-select count(*) from innodb_page_compressed3 where c1 < 500000;
-select count(*) from innodb_page_compressed4 where c1 < 500000;
-select count(*) from innodb_page_compressed5 where c1 < 500000;
-select count(*) from innodb_page_compressed6 where c1 < 500000;
-select count(*) from innodb_page_compressed7 where c1 < 500000;
-select count(*) from innodb_page_compressed8 where c1 < 500000;
-select count(*) from innodb_page_compressed9 where c1 < 500000;
 update innodb_page_compressed1 set c1 = c1 + 1;
 update innodb_page_compressed2 set c1 = c1 + 1;
 update innodb_page_compressed3 set c1 = c1 + 1;
@@ -141,10 +105,13 @@ update innodb_page_compressed7 set c1 = c1 + 1;
 update innodb_page_compressed8 set c1 = c1 + 1;
 update innodb_page_compressed9 set c1 = c1 + 1;
 
+let $wait_condition= select variable_value > 0 from information_schema.global_status where variable_name = 'INNODB_NUM_PAGES_DECRYPTED';
+--source include/wait_condition.inc
+
 SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_encrypted';
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_compressed';
-SELECT variable_value >= 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_decompressed';
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_decrypted';
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_compressed';
+SELECT variable_value > 0 FROM information_schema.global_status WHERE variable_name = 'innodb_num_pages_page_decompressed';
 
 drop procedure innodb_insert_proc;
 drop table innodb_normal;
diff --git a/storage/innobase/api/api0api.cc b/storage/innobase/api/api0api.cc
index 739ea9f7572..bc83e98374f 100644
--- a/storage/innobase/api/api0api.cc
+++ b/storage/innobase/api/api0api.cc
@@ -247,7 +247,7 @@ ib_open_table_by_id(
 
 	table = dict_table_open_on_id(table_id, TRUE, DICT_TABLE_OP_NORMAL);
 
-	if (table != NULL && table->ibd_file_missing) {
+	if (table != NULL && table->file_unreadable) {
 		table = NULL;
 	}
 
@@ -272,7 +272,7 @@ ib_open_table_by_name(
 	table = dict_table_open_on_name(name, FALSE, FALSE,
 					DICT_ERR_IGNORE_NONE);
 
-	if (table != NULL && table->ibd_file_missing) {
+	if (table != NULL && table->file_unreadable) {
 		table = NULL;
 	}
 
@@ -292,7 +292,7 @@ ib_lookup_table_by_name(
 
 	table = dict_table_get_low(name);
 
-	if (table != NULL && table->ibd_file_missing) {
+	if (table != NULL && table->file_unreadable) {
 		table = NULL;
 	}
 
diff --git a/storage/innobase/btr/btr0btr.cc b/storage/innobase/btr/btr0btr.cc
index 470825fa246..6520c714aaf 100644
--- a/storage/innobase/btr/btr0btr.cc
+++ b/storage/innobase/btr/btr0btr.cc
@@ -738,8 +738,7 @@ btr_root_block_get(
 
 	if (!block) {
 		if (index && index->table) {
-			index->table->is_encrypted = TRUE;
-			index->table->corrupted = FALSE;
+			index->table->file_unreadable = true;
 
 			ib_push_warning(index->table->thd, DB_DECRYPTION_FAILED,
 				"Table %s in tablespace %lu is encrypted but encryption service or"
@@ -752,6 +751,7 @@ btr_root_block_get(
 	}
 
 	btr_assert_not_corrupted(block, index);
+
 #ifdef UNIV_BTR_DEBUG
 	if (!dict_index_is_ibuf(index)) {
 		const page_t*	root = buf_block_get_frame(block);
@@ -5175,7 +5175,7 @@ btr_validate_index(
 
 	page_t*	root = btr_root_get(index, &mtr);
 
-	if (root == NULL && index->table->is_encrypted) {
+	if (root == NULL && index->table->file_unreadable) {
 		err = DB_DECRYPTION_FAILED;
 		mtr_commit(&mtr);
 		return err;
diff --git a/storage/innobase/btr/btr0cur.cc b/storage/innobase/btr/btr0cur.cc
index 98b7301574d..e145519678a 100644
--- a/storage/innobase/btr/btr0cur.cc
+++ b/storage/innobase/btr/btr0cur.cc
@@ -621,7 +621,7 @@ btr_cur_search_to_nth_level(
 				" used key_id is not available. "
 				" Can't continue reading table.",
 				index->table->name);
-			index->table->is_encrypted = true;
+			index->table->file_unreadable = true;
 		}
 
 		goto func_exit;
@@ -872,7 +872,7 @@ btr_cur_open_at_index_side_func(
 	ulint		offsets_[REC_OFFS_NORMAL_SIZE];
 	ulint*		offsets		= offsets_;
 	dberr_t		err = DB_SUCCESS;
-	
+
 	rec_offs_init(offsets_);
 
 	estimate = latch_mode & BTR_ESTIMATE;
@@ -924,7 +924,7 @@ btr_cur_open_at_index_side_func(
 					" used key_id is not available. "
 					" Can't continue reading table.",
 					index->table->name);
-				index->table->is_encrypted = true;
+				index->table->file_unreadable = true;
 			}
 
 			goto exit_loop;
@@ -1081,7 +1081,7 @@ btr_cur_open_at_rnd_pos_func(
 					" used key_id is not available. "
 					" Can't continue reading table.",
 					index->table->name);
-				index->table->is_encrypted = true;
+				index->table->file_unreadable = true;
 			}
 			goto exit_loop;
 		}
@@ -3675,7 +3675,7 @@ btr_estimate_n_rows_in_range_on_level(
 					" used key_id is not available. "
 					" Can't continue reading table.",
 					index->table->name);
-				index->table->is_encrypted = true;
+				index->table->file_unreadable = true;
 			}
 
 			mtr_commit(&mtr);
@@ -3793,10 +3793,22 @@ btr_estimate_n_rows_in_range(
 					    BTR_SEARCH_LEAF | BTR_ESTIMATE,
 					    &cursor, 0,
 					    __FILE__, __LINE__, &mtr);
+
+		if (index->table->file_unreadable) {
+			mtr_commit(&mtr);
+			return (0);
+		}
+
 	} else {
 		btr_cur_open_at_index_side(true, index,
 					   BTR_SEARCH_LEAF | BTR_ESTIMATE,
 					   &cursor, 0, &mtr);
+
+		if (index->table->file_unreadable) {
+			mtr_commit(&mtr);
+			return (0);
+		}
+
 	}
 
 	mtr_commit(&mtr);
@@ -4094,6 +4106,11 @@ btr_estimate_number_of_different_key_vals(
 		because otherwise our algorithm would give a wrong estimate
 		for an index where there is just one key value. */
 
+		if (index->table->file_unreadable) {
+			mtr_commit(&mtr);
+			goto exit_loop;
+		}
+
 		page = btr_cur_get_page(&cursor);
 
 		rec = page_rec_get_next(page_get_infimum_rec(page));
@@ -4180,6 +4197,7 @@ btr_estimate_number_of_different_key_vals(
 		mtr_commit(&mtr);
 	}
 
+exit_loop:
 	/* If we saw k borders between different key values on
 	n_sample_pages leaf pages, we can estimate how many
 	there will be in index->stat_n_leaf_pages */
diff --git a/storage/innobase/btr/btr0defragment.cc b/storage/innobase/btr/btr0defragment.cc
index 26bab936de5..214b596a33a 100644
--- a/storage/innobase/btr/btr0defragment.cc
+++ b/storage/innobase/btr/btr0defragment.cc
@@ -227,7 +227,7 @@ btr_defragment_add_index(
 		page = buf_block_get_frame(block);
 	}
 
-	if (page == NULL && index->table->is_encrypted) {
+	if (page == NULL && index->table->file_unreadable) {
 		mtr_commit(&mtr);
 		*err = DB_DECRYPTION_FAILED;
 		return NULL;
diff --git a/storage/innobase/btr/btr0pcur.cc b/storage/innobase/btr/btr0pcur.cc
index 01d2e1bb8e2..6976ba27040 100644
--- a/storage/innobase/btr/btr0pcur.cc
+++ b/storage/innobase/btr/btr0pcur.cc
@@ -416,6 +416,11 @@ btr_pcur_move_to_next_page(
 	cursor->old_stored = BTR_PCUR_OLD_NOT_STORED;
 
 	page = btr_pcur_get_page(cursor);
+
+	if (!page) {
+		return;
+	}
+
 	next_page_no = btr_page_get_next(page, mtr);
 	space = buf_block_get_space(btr_pcur_get_block(cursor));
 	zip_size = buf_block_get_zip_size(btr_pcur_get_block(cursor));
@@ -425,6 +430,10 @@ btr_pcur_move_to_next_page(
 	next_block = btr_block_get(space, zip_size, next_page_no,
 				   cursor->latch_mode,
 				   btr_pcur_get_btr_cur(cursor)->index, mtr);
+
+	if (!next_block) {
+		return;
+	}
 	next_page = buf_block_get_frame(next_block);
 #ifdef UNIV_BTR_DEBUG
 	ut_a(page_is_comp(next_page) == page_is_comp(page));
diff --git a/storage/innobase/buf/buf0buf.cc b/storage/innobase/buf/buf0buf.cc
index 34f56a5ce04..513351a25f9 100644
--- a/storage/innobase/buf/buf0buf.cc
+++ b/storage/innobase/buf/buf0buf.cc
@@ -1114,7 +1114,6 @@ buf_block_init(
 	block->page.state = BUF_BLOCK_NOT_USED;
 	block->page.buf_fix_count = 0;
 	block->page.io_fix = BUF_IO_NONE;
-	block->page.key_version = 0;
 	block->page.encrypted = false;
 	block->page.real_size = 0;
 	block->page.write_size = 0;
@@ -2363,9 +2362,9 @@ buf_page_get_zip(
 		if (err != DB_SUCCESS) {
 			ib_logf(IB_LOG_LEVEL_ERROR,
 				"Reading compressed page " ULINTPF
-				" from tablespace " ULINTPF
-				" failed with error: %d.",
-				offset, space, err);
+				":" ULINTPF
+				" failed with error: %s.",
+				space, offset, ut_strerr(err));
 
 			goto err_exit;
 		}
@@ -2981,40 +2980,42 @@ buf_page_get_gen(
 
 			DBUG_EXECUTE_IF(
 				"innodb_page_corruption_retries",
-				retries = BUF_PAGE_READ_MAX_RETRIES;
+				/* This is needed to avoid tablespace
+				being marked as corrupted. */
+				goto force_fail;
 			);
 		} else {
-			 if (*err) {
-				 *err = local_err;
-			 }
-
-			 /* Encrypted pages that are not corrupted are marked
-			 as encrypted and that fact is later pushed to
-			 user thread. */
-			 if (local_err == DB_DECRYPTION_FAILED) {
-				 return (NULL);
-			 }
-
-			 /* Try to set table as corrupted instead of
-			 asserting. */
-			 if (space > TRX_SYS_SPACE &&
-			     dict_set_corrupted_by_space(space)) {
-				 return (NULL);
+			if (err) {
+				*err = local_err;
 			}
 
+			/* Encrypted pages that are not corrupted are marked
+			as encrypted and that fact is later pushed to
+			user thread. */
+			if (local_err == DB_DECRYPTION_FAILED) {
+				return (NULL);
+			}
+
+			/* Try to set table as corrupted instead of
+			asserting. */
+			if (space > TRX_SYS_SPACE &&
+			    dict_set_corrupted_by_space(space)) {
+				return (NULL);
+			}
+
+force_fail:
 			ib_logf(IB_LOG_LEVEL_FATAL, "Unable"
 				" to read tablespace %lu page no"
 				" %lu into the buffer pool after"
-				" %lu attempts\n"
-				"InnoDB: The most probable cause"
+				" %lu attempts"
+				" The most probable cause"
 				" of this error may be that the"
-				" table has been corrupted.\n"
-				"InnoDB: You can try to fix this"
+				" table has been corrupted."
+				" You can try to fix this"
 				" problem by using"
-				" innodb_force_recovery.\n"
-				"InnoDB: Please see reference manual"
-				" for more details.\n"
-				"InnoDB: Aborting...\n",
+				" innodb_force_recovery."
+				" Please see " REFMAN " for more"
+				" details. Aborting...",
 				space, offset,
 				BUF_PAGE_READ_MAX_RETRIES);
 		}
@@ -3751,7 +3752,6 @@ buf_page_init_low(
 	bpage->newest_modification = 0;
 	bpage->oldest_modification = 0;
 	bpage->write_size = 0;
-	bpage->key_version = 0;
 	bpage->encrypted = false;
 	bpage->real_size = 0;
 	bpage->slot = NULL;
@@ -4392,7 +4392,7 @@ buf_page_monitor(
 /********************************************************************//**
 Mark a table with the specified space pointed by bpage->space corrupted.
 Also remove the bpage from LRU list.
-+ at param[in,out]		bpage			Block */
+ at param[in,out]		bpage			Block */
 static
 void
 buf_mark_space_corrupt(
@@ -4425,6 +4425,8 @@ buf_mark_space_corrupt(
 	are marked unusable later e.g. in ::open(). */
 	if (!bpage->encrypted) {
 		dict_set_corrupted_by_space(space);
+	} else {
+		dict_set_encrypted_by_space(space);
 	}
 
 	/* After this point bpage can't be referenced. */
@@ -4458,7 +4460,6 @@ buf_page_check_corrupt(
 	bool still_encrypted = false;
 	dberr_t err = DB_SUCCESS;
 	bool corrupted = false;
-	ulint page_type = mach_read_from_2(dst_frame + FIL_PAGE_TYPE);
 	fil_space_crypt_t* crypt_data = NULL;
 
 	ut_ad(space);
@@ -4490,26 +4491,27 @@ buf_page_check_corrupt(
 	/* Pages that we think are unencrypted but do not match the checksum
 	checks could be corrupted or encrypted or both. */
 	if (corrupted && !bpage->encrypted) {
-		ib_logf(IB_LOG_LEVEL_ERROR,
-			"%s: Block in space_id " ULINTPF " in file %s corrupted.",
-			page_type ==  FIL_PAGE_PAGE_COMPRESSED_ENCRYPTED ? "Maybe corruption" : "Corruption",
-			space_id, (space && space->name) ? space->name : "NULL");
-		ib_logf(IB_LOG_LEVEL_ERROR,
-			"Based on page type %s (" ULINTPF ")",
-			fil_get_page_type_name(page_type), page_type);
+		/* An error will be reported by
+		buf_page_io_complete(). */
 	} else if (still_encrypted || (bpage->encrypted && corrupted)) {
 		bpage->encrypted = true;
 		err = DB_DECRYPTION_FAILED;
 
 		ib_logf(IB_LOG_LEVEL_ERROR,
-			"Block in space_id " ULINTPF " in file %s encrypted.",
-			space_id, (space && space->name) ? space->name : "NULL");
-		ib_logf(IB_LOG_LEVEL_ERROR,
-			"However key management plugin or used key_version %u is not found or"
+			"The page [page id: space=" ULINTPF
+			", page number=%u]"
+			" in file %s cannot be decrypted.",
+			space_id, bpage->offset,
+			(space && space->name) ? space->name : "NULL");
+
+		ib_logf(IB_LOG_LEVEL_INFO,
+			"However key management plugin or used key_version " ULINTPF
+			" is not found or"
 			" used encryption algorithm or method does not match.",
-			bpage->key_version);
+			mach_read_from_4(dst_frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION));
+
 		if (space_id > TRX_SYS_SPACE) {
-			ib_logf(IB_LOG_LEVEL_ERROR,
+			ib_logf(IB_LOG_LEVEL_INFO,
 				"Marking tablespace as missing. You may drop this table or"
 				" install correct key management plugin and key file.");
 		}
@@ -4531,8 +4533,8 @@ the buffer pool.
 @return DB_SUCCESS if page has been read and is not corrupted,
 DB_PAGE_CORRUPTED if page based on checksum check is corrupted,
 DB_DECRYPTION_FAILED if page post encryption checksum matches but
-after decryption normal page checksum does not match.*/
-UNIV_INTERN
+after decryption normal page checksum does not match.
+in write only DB_SUCCESS is possible. */
 dberr_t
 buf_page_io_complete(
 	buf_page_t*	bpage,
@@ -4560,6 +4562,7 @@ buf_page_io_complete(
 	if (io_type == BUF_IO_READ) {
 		ulint	read_page_no;
 		ulint	read_space_id;
+		uint	key_version;
 
 		buf_page_decrypt_after_read(bpage);
 
@@ -4571,12 +4574,12 @@ buf_page_io_complete(
 
 		if (buf_page_get_zip_size(bpage)) {
 			frame = bpage->zip.data;
-			buf_pool->n_pend_unzip++;
+			os_atomic_increment_ulint(&buf_pool->n_pend_unzip, 1);
 			if (uncompressed
 			    && !buf_zip_decompress((buf_block_t*) bpage,
 						   FALSE)) {
 
-				buf_pool->n_pend_unzip--;
+				os_atomic_decrement_ulint(&buf_pool->n_pend_unzip, 1);
 
 				ib_logf(IB_LOG_LEVEL_INFO,
 					"Page %u in tablespace %u zip_decompress failure.",
@@ -4586,7 +4589,7 @@ buf_page_io_complete(
 
 				goto database_corrupted;
 			}
-			buf_pool->n_pend_unzip--;
+			os_atomic_decrement_ulint(&buf_pool->n_pend_unzip, 1);
 		} else {
 			ut_a(uncompressed);
 			frame = ((buf_block_t*) bpage)->frame;
@@ -4598,6 +4601,8 @@ buf_page_io_complete(
 		read_page_no = mach_read_from_4(frame + FIL_PAGE_OFFSET);
 		read_space_id = mach_read_from_4(
 			frame + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
+		key_version = mach_read_from_4(
+			frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION);
 
 		if (bpage->space == TRX_SYS_SPACE
 		    && buf_dblwr_page_inside(bpage->offset)) {
@@ -4716,11 +4721,11 @@ buf_page_io_complete(
 
 			if (bpage && bpage->encrypted) {
 				ib_logf(IB_LOG_LEVEL_WARN,
-					"Table in tablespace %lu encrypted."
+					"Table in tablespace " ULINTPF " encrypted."
 					"However key management plugin or used key_version %u is not found or"
 					" used encryption algorithm or method does not match."
-					" Can't continue opening the table.\n",
-					(ulint)bpage->space, bpage->key_version);
+					" Can't continue opening the table.",
+					read_space_id, key_version);
 			} else {
 
 				ibuf_merge_or_delete_for_page(
@@ -4850,7 +4855,7 @@ buf_all_freed_instance(
 
 		const buf_block_t* block = buf_chunk_not_freed(chunk);
 
-		if (UNIV_LIKELY_NULL(block) && block->page.key_version == 0) {
+		if (UNIV_LIKELY_NULL(block)) {
 			fil_space_t* space = fil_space_get(block->page.space);
 			ib_logf(IB_LOG_LEVEL_ERROR,
 				"Page %u %u still fixed or dirty.",
@@ -6065,13 +6070,11 @@ buf_page_encrypt_before_write(
 
 	if (bpage->offset == 0) {
 		/* Page 0 of a tablespace is not encrypted/compressed */
-		ut_ad(bpage->key_version == 0);
 		return src_frame;
 	}
 
 	if (bpage->space == TRX_SYS_SPACE && bpage->offset == TRX_SYS_PAGE_NO) {
 		/* don't encrypt/compress page as it contains address to dblwr buffer */
-		bpage->key_version = 0;
 		return src_frame;
 	}
 
@@ -6101,7 +6104,6 @@ buf_page_encrypt_before_write(
 	/* Is encryption needed? */
 	if (crypt_data == NULL || crypt_data->type == CRYPT_SCHEME_UNENCRYPTED) {
 		/* An unencrypted table */
-		bpage->key_version = 0;
 		encrypted = false;
 	}
 
@@ -6131,9 +6133,6 @@ buf_page_encrypt_before_write(
 					      src_frame,
 					      dst_frame);
 
-		ulint key_version = mach_read_from_4(dst_frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION);
-		ut_ad(key_version == 0 || key_version >= bpage->key_version);
-		bpage->key_version = key_version;
 		bpage->real_size = page_size;
 		slot->out_buf = dst_frame = tmp;
 
@@ -6207,8 +6206,6 @@ buf_page_decrypt_after_read(
 	buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
 	bool success = true;
 
-	bpage->key_version = key_version;
-
 	if (bpage->offset == 0) {
 		/* File header pages are not encrypted/compressed */
 		return (true);
@@ -6254,6 +6251,16 @@ buf_page_decrypt_after_read(
 			decrypt. */
 			if (!fil_space_verify_crypt_checksum(dst_frame,
 					zip_size, NULL, bpage->offset)) {
+
+				/* Mark page encrypted in case it should
+				be. */
+				if (key_version && space->crypt_data &&
+				    space->crypt_data->type != CRYPT_SCHEME_UNENCRYPTED) {
+					bpage->encrypted=true;
+				}
+
+				fil_space_release(space);
+
 				return (false);
 			}
 
diff --git a/storage/innobase/buf/buf0rea.cc b/storage/innobase/buf/buf0rea.cc
index 3d2dfb2f10b..bb64f4f565f 100644
--- a/storage/innobase/buf/buf0rea.cc
+++ b/storage/innobase/buf/buf0rea.cc
@@ -137,7 +137,6 @@ buf_read_page_low(
 	buf_page_t*	bpage;
 	ulint		wake_later;
 	ibool		ignore_nonexistent_pages;
-
 	*err = DB_SUCCESS;
 
 	wake_later = mode & OS_AIO_SIMULATED_WAKE_LATER;
@@ -365,22 +364,26 @@ buf_read_ahead_random(
 				space, zip_size, FALSE,
 				tablespace_version, i);
 
-			if (err == DB_TABLESPACE_DELETED) {
-					ib_logf(IB_LOG_LEVEL_WARN,
+			switch(err) {
+			case DB_SUCCESS:
+				break;
+			case DB_TABLESPACE_DELETED:
+				ib_logf(IB_LOG_LEVEL_WARN,
 					"In random"
 					"readahead trying to access"
 					"tablespace " ULINTPF " page " ULINTPF
 					" but the tablespace does not"
-					" exist or is just being dropped.\n",
+					" exist or is just being dropped.",
 					space, i);
-			}
-
-			if (err == DB_DECRYPTION_FAILED) {
+				break;
+			case DB_DECRYPTION_FAILED:
 				ib_logf(IB_LOG_LEVEL_ERROR,
-					"In random readahead read page " ULINTPF
-					" from tablespace " ULINTPF
-					" but page is still encrypted. ",
-					i, space);
+					"Random readahead failed to decrypt page "
+					ULINTPF "." ULINTPF " .",
+					space, i);
+				break;
+			default:
+				ut_error;
 			}
 		}
 	}
@@ -442,12 +445,15 @@ buf_read_page(
 				  zip_size, FALSE,
 				  tablespace_version, offset);
 
+	/* Page corruption and decryption failures are already reported
+	in above function. */
+
 	srv_stats.buf_pool_reads.add(count);
 
 	if (err == DB_TABLESPACE_DELETED) {
 		ib_logf(IB_LOG_LEVEL_ERROR,
 			"Trying to access"
-			" tablespace " ULINTPF " page no. " ULINTPF
+			" tablespace " ULINTPF ":" ULINTPF
 			" but the tablespace does not exist"
 			" or is just being dropped.",
 			space, offset);
@@ -465,13 +471,9 @@ buffer buf_pool if it is not already there. Sets the io_fix flag and sets
 an exclusive lock on the buffer frame. The flag is cleared and the x-lock
 released by the i/o-handler thread.
 @param[in]	space		Tablespace id
- at param[in]	offset		Page no
- at return DB_SUCCESS if page has been read and is not corrupted,
-DB_PAGE_CORRUPTED if page based on checksum check is corrupted,
-DB_DECRYPTION_FAILED if page post encryption checksum matches but
-after decryption normal page checksum does not match.*/
+ at param[in]	offset		Page no */
 UNIV_INTERN
-dberr_t
+void
 buf_read_page_async(
 	ulint	space,
 	ulint	offset)
@@ -484,7 +486,7 @@ buf_read_page_async(
 	zip_size = fil_space_get_zip_size(space);
 
 	if (zip_size == ULINT_UNDEFINED) {
-		return(err);
+		return;
 	}
 
 	tablespace_version = fil_space_get_version(space);
@@ -494,14 +496,26 @@ buf_read_page_async(
 				  | BUF_READ_IGNORE_NONEXISTENT_PAGES,
 				  space, zip_size, FALSE,
 				  tablespace_version, offset);
-
-	if (err != DB_SUCCESS) {
+	switch(err) {
+	case DB_SUCCESS:
+		break;
+	case DB_TABLESPACE_DELETED:
 		ib_logf(IB_LOG_LEVEL_ERROR,
 			"In async page read "
-			"trying to access"
-			"tablespace " ULINTPF " page " ULINTPF
-			" but failed on error %d.",
-			space, offset, err);
+			"trying to access "
+			"page " ULINTPF ":" ULINTPF
+			" in nonexisting or being-dropped tablespace",
+			space, offset);
+		break;
+
+	case DB_DECRYPTION_FAILED:
+		ib_logf(IB_LOG_LEVEL_ERROR,
+			"Async page read failed to decrypt page "
+			ULINTPF ":" ULINTPF ".",
+			space, offset);
+		break;
+	default:
+		ut_error;
 	}
 
 	srv_stats.buf_pool_reads.add(count);
@@ -512,8 +526,6 @@ buf_read_page_async(
 	buffer pool. Since this function is called from buffer pool load
 	these IOs are deliberate and are not part of normal workload we can
 	ignore these in our heuristics. */
-
-	return(err);
 }
 
 /********************************************************************//**
@@ -562,7 +574,7 @@ buf_read_ahead_linear(
 	ulint		fail_count;
 	ulint		ibuf_mode;
 	ulint		low, high;
-	dberr_t		err=DB_SUCCESS;
+	dberr_t		err = DB_SUCCESS;
 	ulint		i;
 	const ulint	buf_read_ahead_linear_area
 		= BUF_READ_AHEAD_AREA(buf_pool);
@@ -764,22 +776,26 @@ buf_read_ahead_linear(
 				ibuf_mode,
 				space, zip_size, FALSE, tablespace_version, i);
 
-			if (err == DB_TABLESPACE_DELETED) {
+			switch(err) {
+			case DB_SUCCESS:
+				break;
+			case DB_TABLESPACE_DELETED:
 				ib_logf(IB_LOG_LEVEL_WARN,
-					"In random"
-					"readahead trying to access"
-					"tablespace " ULINTPF " page " ULINTPF
+					"In linear "
+					"readahead trying to access "
+					"tablespace " ULINTPF ":" ULINTPF
 					" but the tablespace does not"
-					" exist or is just being dropped.\n",
+					" exist or is just being dropped.",
 					space, i);
-			}
-
-			if (err == DB_DECRYPTION_FAILED) {
+				break;
+			case DB_DECRYPTION_FAILED:
 				ib_logf(IB_LOG_LEVEL_ERROR,
-					"In random readahead read page " ULINTPF
-					" from tablespace " ULINTPF
-					" but page is still encrypted. ",
-					i, space);
+					"Linear readahead failed to decrypt page "
+					ULINTPF "." ULINTPF ".",
+					space, i);
+				break;
+			default:
+				ut_error;
 			}
 		}
 	}
@@ -841,9 +857,9 @@ buf_read_ibuf_merge_pages(
 #endif
 
 	for (i = 0; i < n_stored; i++) {
-		dberr_t		err=DB_SUCCESS;
 		buf_pool_t*	buf_pool;
 		ulint		zip_size = fil_space_get_zip_size(space_ids[i]);
+		dberr_t 	err = DB_SUCCESS;
 
 		buf_pool = buf_pool_get(space_ids[i], page_nos[i]);
 
@@ -853,7 +869,6 @@ buf_read_ibuf_merge_pages(
 		}
 
 		if (UNIV_UNLIKELY(zip_size == ULINT_UNDEFINED)) {
-
 			goto tablespace_deleted;
 		}
 
@@ -862,7 +877,11 @@ buf_read_ibuf_merge_pages(
 				  zip_size, TRUE, space_versions[i],
 				  page_nos[i]);
 
-		if (UNIV_UNLIKELY(err == DB_TABLESPACE_DELETED)) {
+		switch(err) {
+		case DB_SUCCESS:
+			break;
+		case DB_TABLESPACE_DELETED:
+
 tablespace_deleted:
 			/* We have deleted or are deleting the single-table
 			tablespace: remove the entries for that page */
@@ -870,13 +889,15 @@ buf_read_ibuf_merge_pages(
 			ibuf_merge_or_delete_for_page(NULL, space_ids[i],
 						      page_nos[i],
 						      zip_size, FALSE);
-		}
-
-		if (err == DB_DECRYPTION_FAILED) {
+			break;
+		case DB_DECRYPTION_FAILED:
 			ib_logf(IB_LOG_LEVEL_ERROR,
-				"Read page " ULINTPF " from tablespace " ULINTPF
-				" for insert buffer but page was encrypted.",
-				page_nos[i], space_ids[i]);
+				"Failed to decrypt insert buffer page "
+				ULINTPF ":" ULINTPF ".",
+				space_ids[i], page_nos[i]);
+			break;
+		default:
+			ut_error;
 		}
 	}
 
@@ -972,10 +993,9 @@ buf_read_recv_pages(
 
 		if (err == DB_DECRYPTION_FAILED) {
 			ib_logf(IB_LOG_LEVEL_ERROR,
-				"Recovery read page " ULINTPF
-				" from tablespace " ULINTPF
-				" but page was encrypted.",
-				page_nos[i], space);
+				"Recovery failed to decrypt read page "
+				ULINTPF ":" ULINTPF ".",
+				space, page_nos[i]);
 		}
 	}
 
diff --git a/storage/innobase/dict/dict0crea.cc b/storage/innobase/dict/dict0crea.cc
index d423f16f61c..f6cd294884b 100644
--- a/storage/innobase/dict/dict0crea.cc
+++ b/storage/innobase/dict/dict0crea.cc
@@ -690,7 +690,7 @@ dict_create_index_tree_step(
 	dberr_t		err = DB_SUCCESS;
 	ulint		zip_size = dict_table_zip_size(index->table);
 
-	if (node->index->table->ibd_file_missing
+	if (node->index->table->file_unreadable
 	    || dict_table_is_discarded(node->index->table)) {
 
 		node->page_no = FIL_NULL;
diff --git a/storage/innobase/dict/dict0dict.cc b/storage/innobase/dict/dict0dict.cc
index 2a11c15441a..07a936733d9 100644
--- a/storage/innobase/dict/dict0dict.cc
+++ b/storage/innobase/dict/dict0dict.cc
@@ -1187,7 +1187,7 @@ dict_table_open_on_name(
 
 		/* If table is encrypted return table */
 		if (ignore_err == DICT_ERR_IGNORE_NONE
-			&& table->is_encrypted) {
+			&& table->file_unreadable) {
 			/* Make life easy for drop table. */
 			if (table->can_be_evicted) {
 				dict_table_move_from_lru_to_non_lru(table);
@@ -6189,6 +6189,24 @@ dict_set_corrupted_by_space(
 }
 
 /**********************************************************************//**
+Flags a table with specified space_id encrypted in the data dictionary
+cache
+ at param[in]	space_id	Tablespace id */
+UNIV_INTERN
+void
+dict_set_encrypted_by_space(
+	ulint	space_id)
+{
+	dict_table_t*   table;
+
+	table = dict_find_table_by_space(space_id);
+
+	if (table) {
+		table->file_unreadable = true;
+	}
+}
+
+/**********************************************************************//**
 Flags an index corrupted both in the data dictionary cache
 and in the SYS_INDEXES */
 UNIV_INTERN
@@ -6645,7 +6663,8 @@ dict_table_schema_check(
 		}
 	}
 
-	if (table->ibd_file_missing) {
+	if (table->file_unreadable &&
+	    fil_space_get(table->space) == NULL) {
 		/* missing tablespace */
 
 		ut_snprintf(errstr, errstr_sz,
diff --git a/storage/innobase/dict/dict0load.cc b/storage/innobase/dict/dict0load.cc
index 0f9f4f153f5..a1d18690476 100644
--- a/storage/innobase/dict/dict0load.cc
+++ b/storage/innobase/dict/dict0load.cc
@@ -1963,7 +1963,7 @@ dict_load_indexes(
 			dict_mem_index_free(index);
 			goto func_exit;
 		} else if (index->page == FIL_NULL
-			   && !table->ibd_file_missing
+			   && !table->file_unreadable
 			   && (!(index->type & DICT_FTS))) {
 
 			fprintf(stderr,
@@ -2191,8 +2191,7 @@ dict_load_table_low(
 	ut_ad(len == 8); /* this was checked earlier */
 
 	(*table)->id = mach_read_from_8(field);
-
-	(*table)->ibd_file_missing = FALSE;
+	(*table)->file_unreadable = false;
 
 	return(NULL);
 }
@@ -2379,7 +2378,7 @@ dict_load_table(
 			"Table '%s' tablespace is set as discarded.",
 			table_name);
 
-		table->ibd_file_missing = TRUE;
+		table->file_unreadable = true;
 
 	} else if (!fil_space_for_table_exists_in_mem(
 			table->space, name, false, true, heap,
@@ -2387,7 +2386,7 @@ dict_load_table(
 
 		if (DICT_TF2_FLAG_IS_SET(table, DICT_TF2_TEMPORARY)) {
 			/* Do not bother to retry opening temporary tables. */
-			table->ibd_file_missing = TRUE;
+			table->file_unreadable = true;
 
 		} else {
 			if (!(ignore_err & DICT_ERR_IGNORE_RECOVER_LOCK)) {
@@ -2422,8 +2421,9 @@ dict_load_table(
 				/* We failed to find a sensible
 				tablespace file */
 
-				table->ibd_file_missing = TRUE;
+				table->file_unreadable = true;
 			}
+
 			if (filepath) {
 				mem_free(filepath);
 			}
@@ -2447,9 +2447,10 @@ dict_load_table(
 	were not allowed while the table is being locked by a transaction. */
 	dict_err_ignore_t index_load_err =
 		!(ignore_err & DICT_ERR_IGNORE_RECOVER_LOCK)
-		&& table->ibd_file_missing
+		&& table->file_unreadable
 		? DICT_ERR_IGNORE_ALL
 		: ignore_err;
+
 	err = dict_load_indexes(table, heap, index_load_err);
 
 	if (err == DB_INDEX_CORRUPT) {
@@ -2484,7 +2485,7 @@ dict_load_table(
 	of the error condition, since the user may want to dump data from the
 	clustered index. However we load the foreign key information only if
 	all indexes were loaded. */
-	if (!cached || table->ibd_file_missing) {
+	if (!cached || table->file_unreadable) {
 		/* Don't attempt to load the indexes from disk. */
 	} else if (err == DB_SUCCESS) {
 		err = dict_load_foreigns(table->name, NULL, true, true,
@@ -2517,12 +2518,12 @@ dict_load_table(
 			table = NULL;
 
 		} else if (dict_index_is_corrupted(index)
-			   && !table->ibd_file_missing) {
+			   && !table->file_unreadable) {
 
 			/* It is possible we force to load a corrupted
 			clustered index if srv_load_corrupted is set.
 			Mark the table as corrupted in this case */
-			table->corrupted = TRUE;
+			table->corrupted = true;
 		}
 	}
 
@@ -2531,7 +2532,7 @@ dict_load_table(
 
 	ut_ad(!table
 	      || ignore_err != DICT_ERR_IGNORE_NONE
-	      || table->ibd_file_missing
+	      || table->file_unreadable
 	      || !table->corrupted);
 
 	if (table && table->fts) {
diff --git a/storage/innobase/dict/dict0stats.cc b/storage/innobase/dict/dict0stats.cc
index 6a28f3cdf8f..7f1d6212695 100644
--- a/storage/innobase/dict/dict0stats.cc
+++ b/storage/innobase/dict/dict0stats.cc
@@ -920,7 +920,12 @@ dict_stats_update_transient_for_index(
 
 		index->stat_n_leaf_pages = size;
 
-		btr_estimate_number_of_different_key_vals(index);
+		/* Do not continue if table decryption has failed or
+		table is already marked corrupted. */
+		if (!index->table->file_unreadable &&
+		    !index->table->corrupted) {
+			btr_estimate_number_of_different_key_vals(index);
+		}
 	}
 }
 
@@ -974,8 +979,10 @@ dict_stats_update_transient(
 			continue;
 		}
 
-		/* Do not continue if table decryption has failed. */
-		if (index->table->is_encrypted) {
+		/* Do not continue if table decryption has failed or
+		table is already marked corrupted. */
+		if (index->table->file_unreadable ||
+		    index->table->corrupted) {
 			break;
 		}
 
@@ -3192,7 +3199,7 @@ dict_stats_update(
 
 	ut_ad(!mutex_own(&dict_sys->mutex));
 
-	if (table->ibd_file_missing) {
+	if (table->file_unreadable) {
 		ut_print_timestamp(stderr);
 		fprintf(stderr,
 			" InnoDB: cannot calculate statistics for table %s "
@@ -3946,13 +3953,14 @@ dict_stats_save_defrag_stats(
 {
 	dberr_t	ret;
 
-	if (index->table->ibd_file_missing) {
+	if (index->table->file_unreadable) {
 		ut_print_timestamp(stderr);
 		fprintf(stderr,
 			" InnoDB: Cannot save defragment stats because "
 			".ibd file is missing.\n");
 		return (DB_TABLESPACE_DELETED);
 	}
+
 	if (dict_index_is_corrupted(index)) {
 		ut_print_timestamp(stderr);
 		fprintf(stderr,
diff --git a/storage/innobase/fil/fil0crypt.cc b/storage/innobase/fil/fil0crypt.cc
index 7bef91f47ff..decd7f29101 100644
--- a/storage/innobase/fil/fil0crypt.cc
+++ b/storage/innobase/fil/fil0crypt.cc
@@ -474,16 +474,18 @@ fil_space_set_crypt_data(
 
 /******************************************************************
 Parse a MLOG_FILE_WRITE_CRYPT_DATA log entry
- at param[in]	ptr		Log entry start
+ at param[in,out]	ptr		Log entry start
 @param[in]	end_ptr		Log entry end
 @param[in]	block		buffer block
+ at param[out]	err		DB_SUCCESS or DB_DECRYPTION_FAILED
 @return position on log buffer */
 UNIV_INTERN
-const byte*
+byte*
 fil_parse_write_crypt_data(
-	const byte*		ptr,
+	byte*			ptr,
 	const byte*		end_ptr,
-	const buf_block_t*	block)
+	const buf_block_t*	block,
+	dberr_t*		err)
 {
 	/* check that redo log entry is complete */
 	uint entry_size =
@@ -495,6 +497,8 @@ fil_parse_write_crypt_data(
 		4 +  // size of key_id
 		1; // fil_encryption_t
 
+	*err = DB_SUCCESS;
+
 	if (ptr + entry_size > end_ptr) {
 		return NULL;
 	}
@@ -541,6 +545,11 @@ fil_parse_write_crypt_data(
 		fil_space_release(space);
 	}
 
+	/* Check is used key found from encryption plugin */
+	if (crypt_data->should_encrypt() && !crypt_data->is_key_found()) {
+		*err = DB_DECRYPTION_FAILED;
+	}
+
 	return ptr;
 }
 
@@ -960,11 +969,12 @@ fil_space_verify_crypt_checksum(
 
 	/* If page is not encrypted, return false */
 	if (key_version == 0) {
-		return false;
+		return(false);
 	}
 
 	srv_checksum_algorithm_t algorithm =
 			static_cast<srv_checksum_algorithm_t>(srv_checksum_algorithm);
+
 	/* If no checksum is used, can't continue checking. */
 	if (algorithm == SRV_CHECKSUM_ALGORITHM_NONE) {
 		return(true);
@@ -988,28 +998,31 @@ fil_space_verify_crypt_checksum(
 		return (true);
 	}
 
-	/* Compressed pages use different checksum method. We first store
-	the post encryption checksum on checksum location and after function
-	restore the original. */
-	if (zip_size) {
-		ib_uint32_t old = static_cast<ib_uint32_t>(mach_read_from_4(
-				page + FIL_PAGE_SPACE_OR_CHKSUM));
-
-		mach_write_to_4(page + FIL_PAGE_SPACE_OR_CHKSUM, checksum);
+	/* Calculate checksums */
+	ib_uint32_t cchecksum1=0;
+	ib_uint32_t cchecksum2=0;
 
-		bool valid = page_zip_verify_checksum(page, zip_size);
+	if (zip_size) {
+		cchecksum1 = page_zip_calc_checksum(
+			page, zip_size, SRV_CHECKSUM_ALGORITHM_CRC32);
 
-		mach_write_to_4(page + FIL_PAGE_SPACE_OR_CHKSUM, old);
+		if(cchecksum1 != checksum) {
+			cchecksum2 = page_zip_calc_checksum(
+				page, zip_size,
+				SRV_CHECKSUM_ALGORITHM_INNODB);
+		}
+	} else {
+		cchecksum1 = buf_calc_page_crc32(page);
 
-		return (valid);
+		if (cchecksum1 != checksum) {
+			cchecksum2 = (ib_uint32_t) buf_calc_page_new_checksum(
+				page);
+		}
 	}
 
 	/* If stored checksum matches one of the calculated checksums
 	page is not corrupted. */
 
-	ib_uint32_t cchecksum1 = buf_calc_page_crc32(page);
-	ib_uint32_t cchecksum2 = (ib_uint32_t) buf_calc_page_new_checksum(
-				page);
 	bool encrypted = (checksum == cchecksum1 || checksum == cchecksum2
 		|| checksum == BUF_NO_CHECKSUM_MAGIC);
 
@@ -1967,11 +1980,11 @@ fil_crypt_rotate_page(
 		bool modified = false;
 		int needs_scrubbing = BTR_SCRUB_SKIP_PAGE;
 		lsn_t block_lsn = block->page.newest_modification;
-		uint kv =  block->page.key_version;
+		byte* frame = buf_block_get_frame(block);
+		uint kv =  mach_read_from_4(frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION);
 
 		/* check if tablespace is closing after reading page */
 		if (space->is_stopping()) {
-			byte* frame = buf_block_get_frame(block);
 
 			if (kv == 0 &&
 				fil_crypt_is_page_uninitialized(frame, zip_size)) {
@@ -1993,9 +2006,6 @@ fil_crypt_rotate_page(
 					FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID,
 					space_id, MLOG_4BYTES, &mtr);
 
-				/* update block */
-				block->page.key_version = key_state->key_version;
-
 				/* statistics */
 				state->crypt_stat.pages_modified++;
 			} else {
diff --git a/storage/innobase/fil/fil0fil.cc b/storage/innobase/fil/fil0fil.cc
index fe220fa5708..c036063f296 100644
--- a/storage/innobase/fil/fil0fil.cc
+++ b/storage/innobase/fil/fil0fil.cc
@@ -6292,6 +6292,7 @@ fil_aio_wait(
 	mutex_enter(&fil_system->mutex);
 
 	fil_node_complete_io(fil_node, fil_system, type);
+	ulint purpose = fil_node->space->purpose;
 
 	mutex_exit(&fil_system->mutex);
 
@@ -6303,16 +6304,28 @@ fil_aio_wait(
 	deadlocks in the i/o system. We keep tablespace 0 data files always
 	open, and use a special i/o thread to serve insert buffer requests. */
 
-	if (fil_node->space->purpose == FIL_TABLESPACE) {
+	if (purpose == FIL_TABLESPACE) {
 		srv_set_io_thread_op_info(segment, "complete io for buf page");
 		buf_page_t* bpage = static_cast<buf_page_t*>(message);
+		ulint offset = bpage ? bpage->offset : ULINT_UNDEFINED;
 		dberr_t err = buf_page_io_complete(bpage);
 
 		if (err != DB_SUCCESS) {
+
+			/* In crash recovery set log corruption on
+			and produce only an error to fail InnoDB startup. */
+			if (recv_recovery_is_on()) {
+				recv_sys->found_corrupt_log = true;
+			}
+
 			ib_logf(IB_LOG_LEVEL_ERROR,
-				"ibuf write operation failed for tablespace "
-				"%u offset %u error=%d.",
-				bpage->space, bpage->offset, err);
+				"%s operation failed for tablespace %s"
+				" offset " ULINTPF " error %s",
+				type == OS_FILE_WRITE ? "Write" : "Read",
+				fil_node->name,
+				offset,
+				ut_strerr(err));
+
 		}
 	} else {
 		srv_set_io_thread_op_info(segment, "complete io for log");
diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc
index e447af9bdad..d20b7180e2c 100644
--- a/storage/innobase/handler/ha_innodb.cc
+++ b/storage/innobase/handler/ha_innodb.cc
@@ -5890,7 +5890,7 @@ ha_innobase::open(
 	ib_table->thd = (void*)thd;
 
 	/* No point to init any statistics if tablespace is still encrypted. */
-	if (!ib_table->is_encrypted) {
+	if (!ib_table->file_unreadable) {
 		dict_stats_init(ib_table);
 	} else {
 		ib_table->stat_initialized = 1;
@@ -5898,7 +5898,8 @@ ha_innobase::open(
 
 	MONITOR_INC(MONITOR_TABLE_OPEN);
 
-	bool	no_tablespace;
+	bool	no_tablespace = false;
+	bool	ibd_missing = false;
 
 	if (dict_table_is_discarded(ib_table)) {
 
@@ -5913,7 +5914,8 @@ ha_innobase::open(
 
 		no_tablespace = false;
 
-	} else if (ib_table->ibd_file_missing) {
+	} else if (ib_table->file_unreadable &&
+		   fil_space_get(ib_table->space) == NULL) {
 
 		ib_senderrf(
 			thd, IB_LOG_LEVEL_WARN,
@@ -5923,11 +5925,11 @@ ha_innobase::open(
 		file, best to play it safe. */
 
 		no_tablespace = true;
-	} else if (ib_table->is_encrypted) {
+		ibd_missing = true;
+	} else if (ib_table->file_unreadable) {
 		/* This means that tablespace was found but we could not
 		decrypt encrypted page. */
 		no_tablespace = true;
-		ib_table->ibd_file_missing = true;
 	} else {
 		no_tablespace = false;
 	}
@@ -5940,7 +5942,8 @@ ha_innobase::open(
 		/* If table has no talespace but it has crypt data, check
 		is tablespace made unaccessible because encryption service
 		or used key_id is not available. */
-		if (ib_table) {
+		if (ib_table && !ibd_missing) {
+			bool warning_pushed = false;
 			fil_space_crypt_t* crypt_data = ib_table->crypt_data;
 
 			if (crypt_data && crypt_data->should_encrypt()) {
@@ -5953,8 +5956,14 @@ ha_innobase::open(
 						" Can't continue reading table.",
 						ib_table->name, crypt_data->key_id);
 					ret_err = HA_ERR_DECRYPTION_FAILED;
+					warning_pushed = true;
 				}
-			} else if (ib_table->is_encrypted) {
+			}
+
+			/* If table is marked as encrypted then we push
+			warning if it has not been already done as used
+			key_id might be found but it is incorrect. */
+			if (ib_table->file_unreadable && !warning_pushed) {
 				push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
 					HA_ERR_DECRYPTION_FAILED,
 					"Table %s is encrypted but encryption service or"
@@ -6096,7 +6105,7 @@ ha_innobase::open(
 
 	/* Only if the table has an AUTOINC column. */
 	if (prebuilt->table != NULL
-	    && !prebuilt->table->ibd_file_missing
+	    && !prebuilt->table->file_unreadable
 	    && table->found_next_number_field != NULL) {
 		dict_table_autoinc_lock(prebuilt->table);
 
@@ -9595,6 +9604,15 @@ ha_innobase::general_fetch(
 
 	ut_a(prebuilt->trx == thd_to_trx(user_thd));
 
+	if (prebuilt->table->corrupted) {
+		DBUG_RETURN(HA_ERR_CRASHED);
+	}
+
+	if (prebuilt->table->file_unreadable
+	    && fil_space_get(prebuilt->table->space) != NULL) {
+		DBUG_RETURN(HA_ERR_DECRYPTION_FAILED);
+	}
+
 	innobase_srv_conc_enter_innodb(prebuilt->trx);
 
 	ret = row_search_for_mysql(
@@ -12337,7 +12355,7 @@ ha_innobase::discard_or_import_tablespace(
 		user may want to set the DISCARD flag in order to IMPORT
 		a new tablespace. */
 
-		if (dict_table->ibd_file_missing) {
+		if (dict_table->file_unreadable) {
 			ib_senderrf(
 				prebuilt->trx->mysql_thd,
 				IB_LOG_LEVEL_WARN, ER_TABLESPACE_MISSING,
@@ -12347,7 +12365,7 @@ ha_innobase::discard_or_import_tablespace(
 		err = row_discard_tablespace_for_mysql(
 			dict_table->name, prebuilt->trx);
 
-	} else if (!dict_table->ibd_file_missing) {
+	} else if (!dict_table->file_unreadable) {
 		/* Commit the transaction in order to
 		release the table lock. */
 		trx_commit_for_mysql(prebuilt->trx);
@@ -13897,7 +13915,8 @@ ha_innobase::check(
 
 		DBUG_RETURN(HA_ADMIN_CORRUPT);
 
-	} else if (prebuilt->table->ibd_file_missing) {
+	} else if (prebuilt->table->file_unreadable &&
+		   fil_space_get(prebuilt->table->space) == NULL) {
 
 		ib_senderrf(
 			thd, IB_LOG_LEVEL_ERROR,
@@ -15021,7 +15040,7 @@ ha_innobase::transactional_table_lock(
 				ER_TABLESPACE_DISCARDED,
 				table->s->table_name.str);
 
-		} else if (prebuilt->table->ibd_file_missing) {
+		} else if (prebuilt->table->file_unreadable) {
 
 			ib_senderrf(
 				thd, IB_LOG_LEVEL_ERROR,
diff --git a/storage/innobase/handler/handler0alter.cc b/storage/innobase/handler/handler0alter.cc
index 0af8c765156..add8564a60f 100644
--- a/storage/innobase/handler/handler0alter.cc
+++ b/storage/innobase/handler/handler0alter.cc
@@ -3130,7 +3130,7 @@ prepare_inplace_alter_table_dict(
 		clustered index of the old table, later. */
 		if (new_clustered
 		    || !ctx->online
-		    || user_table->ibd_file_missing
+		    || user_table->file_unreadable
 		    || dict_table_is_discarded(user_table)) {
 			/* No need to allocate a modification log. */
 			ut_ad(!ctx->add_index[a]->online_log);
@@ -3541,6 +3541,38 @@ ha_innobase::prepare_inplace_alter_table(
 		goto func_exit;
 	}
 
+	indexed_table = prebuilt->table;
+
+	if (indexed_table->file_unreadable &&
+	    fil_space_get(indexed_table->space) != NULL) {
+		String str;
+		const char* engine= table_type();
+		push_warning_printf(user_thd, Sql_condition::WARN_LEVEL_WARN,
+			HA_ERR_DECRYPTION_FAILED,
+			"Table %s is encrypted but encryption service or"
+			" used key_id is not available. "
+			" Can't continue reading table.",
+			indexed_table->name);
+		get_error_message(HA_ERR_DECRYPTION_FAILED, &str);
+		my_error(ER_GET_ERRMSG, MYF(0), HA_ERR_DECRYPTION_FAILED, str.c_ptr(), engine);
+
+		DBUG_RETURN(true);
+	}
+
+	if (indexed_table->corrupted
+	    || dict_table_get_first_index(indexed_table) == NULL
+	    || dict_index_is_corrupted(
+		    dict_table_get_first_index(indexed_table))) {
+		/* The clustered index is corrupted. */
+		my_error(ER_CHECK_NO_SUCH_TABLE, MYF(0));
+		DBUG_RETURN(true);
+	}
+
+	if (!(ha_alter_info->handler_flags & ~INNOBASE_INPLACE_IGNORE)) {
+		/* Nothing to do */
+		goto func_exit;
+	}
+
 	if (ha_alter_info->handler_flags
 	    & Alter_inplace_info::CHANGE_CREATE_OPTION) {
 		/* Check engine specific table options */
@@ -4193,7 +4225,7 @@ ha_innobase::inplace_alter_table(
 	DBUG_ASSERT(ctx->trx);
 	DBUG_ASSERT(ctx->prebuilt == prebuilt);
 
-	if (prebuilt->table->ibd_file_missing
+	if (prebuilt->table->file_unreadable
 	    || dict_table_is_discarded(prebuilt->table)) {
 		goto all_done;
 	}
@@ -5215,7 +5247,7 @@ commit_try_rebuild(
 	/* The new table must inherit the flag from the
 	"parent" table. */
 	if (dict_table_is_discarded(user_table)) {
-		rebuilt_table->ibd_file_missing = true;
+		rebuilt_table->file_unreadable = true;
 		rebuilt_table->flags2 |= DICT_TF2_DISCARDED;
 	}
 
@@ -5745,9 +5777,9 @@ ha_innobase::commit_inplace_alter_table(
 	if (ha_alter_info->group_commit_ctx) {
 		ctx_array = ha_alter_info->group_commit_ctx;
 	} else {
-	ctx_single[0] = ctx0;
-	ctx_single[1] = NULL;
-	ctx_array = ctx_single;
+		ctx_single[0] = ctx0;
+		ctx_single[1] = NULL;
+		ctx_array = ctx_single;
 	}
 
 	DBUG_ASSERT(ctx0 == ctx_array[0]);
@@ -5776,6 +5808,19 @@ ha_innobase::commit_inplace_alter_table(
 			= static_cast<ha_innobase_inplace_ctx*>(*pctx);
 		DBUG_ASSERT(ctx->prebuilt->trx == prebuilt->trx);
 
+		/* If decryption failed for old table or new table
+		fail here. */
+		if ((ctx->old_table->file_unreadable &&
+		     fil_space_get(ctx->old_table->space) != NULL)||
+		    (ctx->new_table->file_unreadable &&
+		     fil_space_get(ctx->new_table->space) != NULL)) {
+			String str;
+			const char* engine= table_type();
+			get_error_message(HA_ERR_DECRYPTION_FAILED, &str);
+			my_error(ER_GET_ERRMSG, MYF(0), HA_ERR_DECRYPTION_FAILED, str.c_ptr(), engine);
+			DBUG_RETURN(true);
+		}
+
 		/* Exclusively lock the table, to ensure that no other
 		transaction is holding locks on the table while we
 		change the table definition. The MySQL meta-data lock
diff --git a/storage/innobase/include/btr0btr.ic b/storage/innobase/include/btr0btr.ic
index 5acbee1751e..98fa765c0b5 100644
--- a/storage/innobase/include/btr0btr.ic
+++ b/storage/innobase/include/btr0btr.ic
@@ -61,7 +61,7 @@ btr_block_get_func(
 
 	if (err == DB_DECRYPTION_FAILED) {
 		if (index && index->table) {
-			index->table->is_encrypted = true;
+			index->table->file_unreadable = true;
 		}
 	}
 
diff --git a/storage/innobase/include/buf0buf.h b/storage/innobase/include/buf0buf.h
index cf1ad8b133b..309501b28b4 100644
--- a/storage/innobase/include/buf0buf.h
+++ b/storage/innobase/include/buf0buf.h
@@ -1634,7 +1634,6 @@ struct buf_page_t{
 					if written again we check is TRIM
 					operation needed. */
 
-	unsigned        key_version;	/*!< key version for this block */
 	bool            encrypted;	/*!< page is still encrypted */
 
 	ulint           real_size;	/*!< Real size of the page
diff --git a/storage/innobase/include/buf0rea.h b/storage/innobase/include/buf0rea.h
index 3582885ef92..dfd2263c546 100644
--- a/storage/innobase/include/buf0rea.h
+++ b/storage/innobase/include/buf0rea.h
@@ -57,15 +57,10 @@ an exclusive lock on the buffer frame. The flag is cleared and the x-lock
 released by the i/o-handler thread.
 
 @param[in]	space		Tablespace id
- at param[in]	offset		Page number
- at return DB_SUCCESS if page has been read and is not corrupted,
-DB_PAGE_CORRUPTED if page based on checksum check is corrupted,
-DB_DECRYPTION_FAILED if page post encryption checksum matches but
-after decryption normal page checksum does not match.*/
+ at param[in]	offset		Page number */
 UNIV_INTERN
-dberr_t
+void
 buf_read_page_async(
-/*================*/
 	ulint	space,
 	ulint	offset);
 /********************************************************************//**
diff --git a/storage/innobase/include/dict0dict.h b/storage/innobase/include/dict0dict.h
index 42f93b5a889..f4ffba7ba34 100644
--- a/storage/innobase/include/dict0dict.h
+++ b/storage/innobase/include/dict0dict.h
@@ -1822,6 +1822,15 @@ dict_set_corrupted_by_space(
 /*========================*/
 	ulint		space_id);	/*!< in: space ID */
 
+/**********************************************************************//**
+Flags a table with specified space_id encrypted in the data dictionary
+cache
+ at param[in]	space_id	Tablespace id */
+UNIV_INTERN
+void
+dict_set_encrypted_by_space(
+	ulint	space_id);
+
 /********************************************************************//**
 Validate the table flags.
 @return	true if valid. */
diff --git a/storage/innobase/include/dict0mem.h b/storage/innobase/include/dict0mem.h
index 902b960eaa3..06decf582c7 100644
--- a/storage/innobase/include/dict0mem.h
+++ b/storage/innobase/include/dict0mem.h
@@ -2,7 +2,7 @@
 
 Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
 Copyright (c) 2012, Facebook Inc.
-Copyright (c) 2013, 2016, MariaDB Corporation.
+Copyright (c) 2013, 2017, MariaDB Corporation.
 
 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
@@ -1046,9 +1046,10 @@ struct dict_table_t{
 				table is placed */
 	unsigned	flags:DICT_TF_BITS;	/*!< DICT_TF_... */
 	unsigned	flags2:DICT_TF2_BITS;	/*!< DICT_TF2_... */
-	unsigned	ibd_file_missing:1;
-				/*!< TRUE if this is in a single-table
-				tablespace and the .ibd file is missing; then
+	unsigned	file_unreadable:1;
+				/*!< true if this is in a single-table
+				tablespace and the .ibd file is missing or
+				page decryption failed and page is corrupted; then
 				we must return in ha_innodb.cc an error if the
 				user tries to query such an orphaned table */
 	unsigned	cached:1;/*!< TRUE if the table object has been added
@@ -1349,7 +1350,6 @@ struct dict_table_t{
 			locks;	/*!< list of locks on the table; protected
 				by lock_sys->mutex */
 #endif /* !UNIV_HOTBACKUP */
-	ibool		is_encrypted;
 
 #ifdef UNIV_DEBUG
 	ulint		magic_n;/*!< magic number */
diff --git a/storage/innobase/include/fil0crypt.h b/storage/innobase/include/fil0crypt.h
index cfc2d850883..01fa6e523c3 100644
--- a/storage/innobase/include/fil0crypt.h
+++ b/storage/innobase/include/fil0crypt.h
@@ -120,7 +120,6 @@ struct fil_space_crypt_t : st_encryption_scheme
 		key_found(),
 		rotate_state()
 	{
-		key_found = new_min_key_version;
 		key_id = new_key_id;
 		my_random_bytes(iv, sizeof(iv));
 		mutex_create(fil_crypt_data_mutex_key,
@@ -136,6 +135,8 @@ struct fil_space_crypt_t : st_encryption_scheme
 			type = CRYPT_SCHEME_1;
 			min_key_version = key_get_latest_version();
 		}
+
+		key_found = min_key_version;
 	}
 
 	/** Destructor */
@@ -295,17 +296,18 @@ fil_space_destroy_crypt_data(
 
 /******************************************************************
 Parse a MLOG_FILE_WRITE_CRYPT_DATA log entry
- at param[in]	ptr		Log entry start
+ at param[in,out]	ptr		Log entry start
 @param[in]	end_ptr		Log entry end
 @param[in]	block		buffer block
+ at param[out]	err		DB_SUCCESS or DB_DECRYPTION_FAILED
 @return position on log buffer */
 UNIV_INTERN
-const byte*
+byte*
 fil_parse_write_crypt_data(
-	const byte*		ptr,
+	byte*			ptr,
 	const byte*		end_ptr,
-	const buf_block_t*	block)
-	MY_ATTRIBUTE((warn_unused_result));
+	const buf_block_t*	block,
+	dberr_t*		err);
 
 /******************************************************************
 Encrypt a buffer
diff --git a/storage/innobase/include/log0recv.h b/storage/innobase/include/log0recv.h
index 9ca1c46d72b..1ff5cf721bf 100644
--- a/storage/innobase/include/log0recv.h
+++ b/storage/innobase/include/log0recv.h
@@ -274,9 +274,10 @@ recv_sys_var_init(void);
 #endif /* !UNIV_HOTBACKUP */
 /** Apply the hash table of stored log records to persistent data pages.
 @param[in]	last_batch	whether the change buffer merge will be
-				performed as part of the operation */
+				performed as part of the operation
+ at return DB_SUCCESS or DB_DECRYPTION_FAILED */
 UNIV_INTERN
-void
+dberr_t
 recv_apply_hashed_log_recs(bool last_batch);
 #ifdef UNIV_HOTBACKUP
 /*******************************************************************//**
diff --git a/storage/innobase/log/log0recv.cc b/storage/innobase/log/log0recv.cc
index 45fd42848e8..8f1905c1ef2 100644
--- a/storage/innobase/log/log0recv.cc
+++ b/storage/innobase/log/log0recv.cc
@@ -1297,7 +1297,12 @@ recv_parse_or_apply_log_rec_body(
 		}
 		break;
 	case MLOG_FILE_WRITE_CRYPT_DATA:
-		ptr = const_cast<byte*>(fil_parse_write_crypt_data(ptr, end_ptr, block));
+		dberr_t err;
+		ptr = const_cast<byte*>(fil_parse_write_crypt_data(ptr, end_ptr, block, &err));
+
+		if (err != DB_SUCCESS) {
+			recv_sys->found_corrupt_log = TRUE;
+		}
 		break;
 	default:
 		ptr = NULL;
@@ -1768,9 +1773,10 @@ recv_read_in_area(
 
 /** Apply the hash table of stored log records to persistent data pages.
 @param[in]	last_batch	whether the change buffer merge will be
-				performed as part of the operation */
+				performed as part of the operation
+ at return DB_SUCCESS or DB_DECRYPTION_FAILED */
 UNIV_INTERN
-void
+dberr_t
 recv_apply_hashed_log_recs(bool last_batch)
 {
 	for (;;) {
@@ -1780,6 +1786,11 @@ recv_apply_hashed_log_recs(bool last_batch)
 			break;
 		}
 
+		if (recv_sys->found_corrupt_log) {
+			mutex_exit(&recv_sys->mutex);
+			return(DB_DECRYPTION_FAILED);
+		}
+
 		mutex_exit(&recv_sys->mutex);
 		os_thread_sleep(500000);
 	}
@@ -1844,6 +1855,10 @@ recv_apply_hashed_log_recs(bool last_batch)
 
 		mutex_exit(&(recv_sys->mutex));
 
+		if (recv_sys->found_corrupt_log) {
+			return(DB_DECRYPTION_FAILED);
+		}
+
 		os_thread_sleep(500000);
 
 		mutex_enter(&(recv_sys->mutex));
@@ -1890,6 +1905,8 @@ recv_apply_hashed_log_recs(bool last_batch)
 	recv_sys_empty_hash();
 
 	mutex_exit(&recv_sys->mutex);
+
+	return (DB_SUCCESS);
 }
 #else /* !UNIV_HOTBACKUP */
 /*******************************************************************//**
diff --git a/storage/innobase/row/row0import.cc b/storage/innobase/row/row0import.cc
index 9d3c695db12..6b00be0a73d 100644
--- a/storage/innobase/row/row0import.cc
+++ b/storage/innobase/row/row0import.cc
@@ -2203,7 +2203,7 @@ row_import_discard_changes(
 		index->space = FIL_NULL;
 	}
 
-	table->ibd_file_missing = TRUE;
+	table->file_unreadable = true;
 
 	fil_close_tablespace(trx, table->space);
 }
@@ -3449,7 +3449,7 @@ row_import_for_mysql(
 
 	ut_a(table->space);
 	ut_ad(prebuilt->trx);
-	ut_a(table->ibd_file_missing);
+	ut_a(table->file_unreadable);
 
 	trx_start_if_not_started(prebuilt->trx);
 
@@ -3754,7 +3754,7 @@ row_import_for_mysql(
 		return(row_import_error(prebuilt, trx, err));
 	}
 
-	table->ibd_file_missing = false;
+	table->file_unreadable = false;
 	table->flags2 &= ~DICT_TF2_DISCARDED;
 
 	if (autoinc != 0) {
diff --git a/storage/innobase/row/row0ins.cc b/storage/innobase/row/row0ins.cc
index e3817ea4de7..796e57b34b6 100644
--- a/storage/innobase/row/row0ins.cc
+++ b/storage/innobase/row/row0ins.cc
@@ -1512,7 +1512,7 @@ row_ins_check_foreign_constraint(
 	}
 
 	if (check_table == NULL
-	    || check_table->ibd_file_missing
+	    || check_table->file_unreadable
 	    || check_index == NULL) {
 
 		if (!srv_read_only_mode && check_ref) {
@@ -2375,8 +2375,7 @@ row_ins_clust_index_entry_low(
 				    &cursor, 0, __FILE__, __LINE__, &mtr);
 
 	if (err != DB_SUCCESS) {
-		index->table->is_encrypted = true;
-		index->table->ibd_file_missing = true;
+		index->table->file_unreadable = true;
 		mtr_commit(&mtr);
 		goto func_exit;
 	}
@@ -2698,7 +2697,7 @@ row_ins_sec_index_entry_low(
 				" used key_id is not available. "
 				" Can't continue reading table.",
 				index->table->name);
-			index->table->is_encrypted = true;
+			index->table->file_unreadable = true;
 		}
 		goto func_exit;
 	}
diff --git a/storage/innobase/row/row0merge.cc b/storage/innobase/row/row0merge.cc
index 5531263b248..7d051d147da 100644
--- a/storage/innobase/row/row0merge.cc
+++ b/storage/innobase/row/row0merge.cc
@@ -1,6 +1,7 @@
 /*****************************************************************************
 
 Copyright (c) 2005, 2016, Oracle and/or its affiliates. All Rights Reserved.
+Copyright (c) 2015, 2017, MariaDB Corporation.
 
 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
@@ -1617,7 +1618,8 @@ row_merge_read_clustered_index(
 		page_cur_t*	cur	= btr_pcur_get_page_cur(&pcur);
 
 		/* Do not continue if table pages are still encrypted */
-		if (old_table->is_encrypted || new_table->is_encrypted) {
+		if (old_table->file_unreadable ||
+		    new_table->file_unreadable) {
 			err = DB_DECRYPTION_FAILED;
 			trx->error_key_num = 0;
 			goto func_exit;
@@ -3691,7 +3693,7 @@ row_merge_rename_tables_dict(
 	table is in a non-system tablespace where space > 0. */
 	if (err == DB_SUCCESS
 	    && old_table->space != TRX_SYS_SPACE
-	    && !old_table->ibd_file_missing) {
+	    && fil_space_get(old_table->space) != NULL) {
 		/* Make pathname to update SYS_DATAFILES. */
 		char* tmp_path = row_make_new_pathname(old_table, tmp_name);
 
@@ -4001,6 +4003,7 @@ row_merge_build_indexes(
 			os_mem_alloc_large(&block_size));
 
 		if (crypt_block == NULL) {
+			fil_space_release(space);
 			DBUG_RETURN(DB_OUT_OF_MEMORY);
 		}
 	} else {
@@ -4064,13 +4067,14 @@ row_merge_build_indexes(
 	pct_cost = COST_READ_CLUSTERED_INDEX * 100 / (total_static_cost + total_dynamic_cost);
 
 	/* Do not continue if we can't encrypt table pages */
-	if (old_table->is_encrypted || new_table->is_encrypted) {
+	if (old_table->file_unreadable ||
+	    new_table->file_unreadable) {
 		error = DB_DECRYPTION_FAILED;
 		ib_push_warning(trx->mysql_thd, DB_DECRYPTION_FAILED,
 			"Table %s is encrypted but encryption service or"
 			" used key_id is not available. "
 			" Can't continue reading table.",
-			old_table->is_encrypted ? old_table->name : new_table->name);
+			old_table->file_unreadable ? old_table->name : new_table->name);
 		goto func_exit;
 	}
 
diff --git a/storage/innobase/row/row0mysql.cc b/storage/innobase/row/row0mysql.cc
index b479697d644..8eb0094b4ca 100644
--- a/storage/innobase/row/row0mysql.cc
+++ b/storage/innobase/row/row0mysql.cc
@@ -1,6 +1,7 @@
 /*****************************************************************************
 
 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All Rights Reserved.
+Copyright (c) 2015, 2017, MariaDB Corporation.
 
 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
@@ -663,6 +664,7 @@ row_mysql_handle_errors(
 		abort();
 
 	case DB_CORRUPTION:
+	case DB_PAGE_CORRUPTED:
 		fputs("InnoDB: We detected index corruption"
 		      " in an InnoDB type table.\n"
 		      "InnoDB: You have to dump + drop + reimport"
@@ -1308,14 +1310,15 @@ row_insert_for_mysql(
 
 		return(DB_TABLESPACE_DELETED);
 
-	} else if (prebuilt->table->ibd_file_missing) {
+	} else if (prebuilt->table->file_unreadable &&
+		   fil_space_get(prebuilt->table->space) == NULL) {
 
 		ib_logf(IB_LOG_LEVEL_ERROR,
 			".ibd file is missing for table %s",
 			prebuilt->table->name);
 
 		return(DB_TABLESPACE_NOT_FOUND);
-	} else if (prebuilt->table->is_encrypted) {
+	} else if (prebuilt->table->file_unreadable) {
 		ib_push_warning(trx, DB_DECRYPTION_FAILED,
 			"Table %s in tablespace %lu encrypted."
 			"However key management plugin or used key_id is not found or"
@@ -1707,7 +1710,8 @@ row_update_for_mysql(
 	ut_ad(trx != NULL);
 	UT_NOT_USED(mysql_rec);
 
-	if (prebuilt->table->ibd_file_missing) {
+	if (prebuilt->table->file_unreadable &&
+	    fil_space_get(prebuilt->table->space) == NULL) {
 		ut_print_timestamp(stderr);
 		fprintf(stderr, "  InnoDB: Error:\n"
 			"InnoDB: MySQL is trying to use a table handle"
@@ -1722,13 +1726,13 @@ row_update_for_mysql(
 			"InnoDB: how you can resolve the problem.\n",
 			prebuilt->table->name);
 		return(DB_ERROR);
-	} else if (prebuilt->table->is_encrypted) {
+	} else if (prebuilt->table->file_unreadable) {
 		ib_push_warning(trx, DB_DECRYPTION_FAILED,
 			"Table %s in tablespace %lu encrypted."
 			"However key management plugin or used key_id is not found or"
 			" used encryption algorithm or method does not match.",
 			prebuilt->table->name, prebuilt->table->space);
-		return (DB_TABLE_NOT_FOUND);
+		return(DB_DECRYPTION_FAILED);
 	}
 
 	if (UNIV_UNLIKELY(prebuilt->magic_n != ROW_PREBUILT_ALLOCATED)) {
@@ -3107,7 +3111,7 @@ row_discard_tablespace(
 		/* All persistent operations successful, update the
 		data dictionary memory cache. */
 
-		table->ibd_file_missing = TRUE;
+		table->file_unreadable = true;
 
 		table->flags2 |= DICT_TF2_DISCARDED;
 
@@ -3164,7 +3168,8 @@ row_discard_tablespace_for_mysql(
 
 	if (table == 0) {
 		err = DB_TABLE_NOT_FOUND;
-	} else if (table->is_encrypted) {
+	} else if (table->file_unreadable &&
+		   fil_space_get(table->space) != NULL) {
 		err = DB_DECRYPTION_FAILED;
 	} else if (table->space == TRX_SYS_SPACE) {
 		char	table_name[MAX_FULL_NAME_LEN + 1];
@@ -3380,10 +3385,11 @@ row_truncate_table_for_mysql(
 
 	if (dict_table_is_discarded(table)) {
 		return(DB_TABLESPACE_DELETED);
-	} else if (table->is_encrypted) {
-		return(DB_DECRYPTION_FAILED);
-	} else if (table->ibd_file_missing) {
+	} else if (table->file_unreadable &&
+		   fil_space_get(table->space) == NULL) {
 		return(DB_TABLESPACE_NOT_FOUND);
+	} else if (table->file_unreadable) {
+		return(DB_DECRYPTION_FAILED);
 	}
 
 	trx_start_for_ddl(trx, TRX_DICT_OP_TABLE);
@@ -3549,7 +3555,7 @@ row_truncate_table_for_mysql(
 					"create a new tablespace",
 					table->name);
 
-				table->ibd_file_missing = 1;
+				table->file_unreadable = true;
 				err = DB_ERROR;
 				goto funct_exit;
 			}
@@ -3969,7 +3975,8 @@ row_drop_table_for_mysql(
 
 	/* If table is encrypted and table page encryption failed
 	return error. */
-	if (table->is_encrypted) {
+	if (table->file_unreadable &&
+	    fil_space_get(table->space) != NULL) {
 
 		if (table->can_be_evicted) {
 			dict_table_move_from_lru_to_non_lru(table);
@@ -4351,7 +4358,7 @@ row_drop_table_for_mysql(
 		from table->heap, which will be freed by
 		dict_table_remove_from_cache(table) below. */
 		space_id = table->space;
-		ibd_file_missing = table->ibd_file_missing;
+		ibd_file_missing = table->file_unreadable;
 
 		table_flags = table->flags;
 		is_temp = DICT_TF2_FLAG_IS_SET(table, DICT_TF2_TEMPORARY);
@@ -4362,7 +4369,7 @@ row_drop_table_for_mysql(
 		have a temp flag but not know the temp path */
 		ut_a(table->dir_path_of_temp_table == NULL || is_temp);
 		if (dict_table_is_discarded(table)
-		    || table->ibd_file_missing) {
+		    || table->file_unreadable) {
 			/* Do not attempt to drop known-to-be-missing
 			tablespaces. */
 			space_id = 0;
@@ -4773,7 +4780,7 @@ row_drop_database_for_mysql(
 					"'%s.frm' was lost.", table->name);
 			}
 
-			if (table->ibd_file_missing) {
+			if (table->file_unreadable) {
 				ib_logf(IB_LOG_LEVEL_WARN,
 					"Missing %s.ibd file for table %s.",
 					table->name, table->name);
@@ -5047,7 +5054,7 @@ row_rename_table_for_mysql(
 		      stderr);
 		goto funct_exit;
 
-	} else if (table->ibd_file_missing
+	} else if (table->file_unreadable
 		   && !dict_table_is_discarded(table)) {
 
 		err = DB_TABLE_NOT_FOUND;
@@ -5117,7 +5124,7 @@ row_rename_table_for_mysql(
 	which have space IDs > 0. */
 	if (err == DB_SUCCESS
 	    && table->space != TRX_SYS_SPACE
-	    && !table->ibd_file_missing) {
+	    && !table->file_unreadable) {
 		/* Make a new pathname to update SYS_DATAFILES. */
 		char*	new_path = row_make_new_pathname(table, new_name);
 
diff --git a/storage/innobase/row/row0purge.cc b/storage/innobase/row/row0purge.cc
index bc2e0b0e1cb..e0f4599be06 100644
--- a/storage/innobase/row/row0purge.cc
+++ b/storage/innobase/row/row0purge.cc
@@ -777,7 +777,7 @@ row_purge_parse_undo_rec(
 		goto err_exit;
 	}
 
-	if (node->table->ibd_file_missing) {
+	if (node->table->file_unreadable) {
 		/* We skip purge of missing .ibd files */
 
 		dict_table_close(node->table, FALSE, FALSE);
diff --git a/storage/innobase/row/row0sel.cc b/storage/innobase/row/row0sel.cc
index dab1bc58db5..e9b3400cfad 100644
--- a/storage/innobase/row/row0sel.cc
+++ b/storage/innobase/row/row0sel.cc
@@ -3714,11 +3714,12 @@ row_search_for_mysql(
 
 		return(DB_TABLESPACE_DELETED);
 
-	} else if (prebuilt->table->ibd_file_missing) {
+	} else if (prebuilt->table->file_unreadable &&
+		   fil_space_get(prebuilt->table->space) == NULL) {
 
 		return(DB_TABLESPACE_NOT_FOUND);
 
-	} else if (prebuilt->table->is_encrypted) {
+	} else if (prebuilt->table->file_unreadable) {
 
 		return(DB_DECRYPTION_FAILED);
 	} else if (!prebuilt->index_usable) {
@@ -4200,7 +4201,7 @@ row_search_for_mysql(
 					" used key_id is not available. "
 					" Can't continue reading table.",
 					prebuilt->table->name);
-				index->table->is_encrypted = true;
+				index->table->file_unreadable = true;
 			}
 			rec = NULL;
 			goto lock_wait_or_error;
@@ -4220,7 +4221,7 @@ row_search_for_mysql(
 
 	rec = btr_pcur_get_rec(pcur);
 
-	if (!rec) {
+	if (!rec || index->table->file_unreadable) {
 		err = DB_DECRYPTION_FAILED;
 		goto lock_wait_or_error;
 	}
diff --git a/storage/innobase/row/row0uins.cc b/storage/innobase/row/row0uins.cc
index 651042fb820..f14a4ef9bcf 100644
--- a/storage/innobase/row/row0uins.cc
+++ b/storage/innobase/row/row0uins.cc
@@ -320,7 +320,7 @@ row_undo_ins_parse_undo_rec(
 
 	/* Skip the UNDO if we can't find the table or the .ibd file. */
 	if (UNIV_UNLIKELY(node->table == NULL)) {
-	} else if (UNIV_UNLIKELY(node->table->ibd_file_missing)) {
+	} else if (UNIV_UNLIKELY(node->table->file_unreadable)) {
 close_table:
 		dict_table_close(node->table, dict_locked, FALSE);
 		node->table = NULL;
diff --git a/storage/innobase/row/row0umod.cc b/storage/innobase/row/row0umod.cc
index a64e41786d6..632dc67dd9c 100644
--- a/storage/innobase/row/row0umod.cc
+++ b/storage/innobase/row/row0umod.cc
@@ -1098,7 +1098,7 @@ row_undo_mod_parse_undo_rec(
 		return;
 	}
 
-	if (node->table->ibd_file_missing) {
+	if (node->table->file_unreadable) {
 		dict_table_close(node->table, dict_locked, FALSE);
 
 		/* We skip undo operations to missing .ibd files */
diff --git a/storage/innobase/srv/srv0start.cc b/storage/innobase/srv/srv0start.cc
index 048fdb8cafd..bece6c4513b 100644
--- a/storage/innobase/srv/srv0start.cc
+++ b/storage/innobase/srv/srv0start.cc
@@ -2493,7 +2493,12 @@ innobase_start_or_create_for_mysql(void)
 			respective file pages, for the last batch of
 			recv_group_scan_log_recs(). */
 
-			recv_apply_hashed_log_recs(true);
+			err = recv_apply_hashed_log_recs(true);
+
+			if (err != DB_SUCCESS) {
+				return (err);
+			}
+
 			DBUG_PRINT("ib_log", ("apply completed"));
 		}
 
diff --git a/storage/innobase/trx/trx0trx.cc b/storage/innobase/trx/trx0trx.cc
index 318f1e284ec..93da964fc94 100644
--- a/storage/innobase/trx/trx0trx.cc
+++ b/storage/innobase/trx/trx0trx.cc
@@ -483,7 +483,7 @@ trx_resurrect_table_locks(
 	     i != tables.end(); i++) {
 		if (dict_table_t* table = dict_table_open_on_id(
 			    *i, FALSE, DICT_TABLE_OP_LOAD_TABLESPACE)) {
-			if (table->ibd_file_missing
+			if (table->file_unreadable
 			    || dict_table_is_temporary(table)) {
 				mutex_enter(&dict_sys->mutex);
 				dict_table_close(table, TRUE, FALSE);
diff --git a/storage/innobase/ut/ut0ut.cc b/storage/innobase/ut/ut0ut.cc
index bde40220db3..181e713cca9 100644
--- a/storage/innobase/ut/ut0ut.cc
+++ b/storage/innobase/ut/ut0ut.cc
@@ -849,6 +849,8 @@ ut_strerr(
 		return("BLOB record length is greater than 10%% of redo log");
 	case DB_DECRYPTION_FAILED:
 		return("Table is encrypted but decrypt failed.");
+	case DB_PAGE_CORRUPTED:
+		return("Page read from tablespace is corrupted.");
 
 	/* do not add default: in order to produce a warning if new code
 	is added to the enum but not added here */
diff --git a/storage/xtradb/api/api0api.cc b/storage/xtradb/api/api0api.cc
index 739ea9f7572..bc83e98374f 100644
--- a/storage/xtradb/api/api0api.cc
+++ b/storage/xtradb/api/api0api.cc
@@ -247,7 +247,7 @@ ib_open_table_by_id(
 
 	table = dict_table_open_on_id(table_id, TRUE, DICT_TABLE_OP_NORMAL);
 
-	if (table != NULL && table->ibd_file_missing) {
+	if (table != NULL && table->file_unreadable) {
 		table = NULL;
 	}
 
@@ -272,7 +272,7 @@ ib_open_table_by_name(
 	table = dict_table_open_on_name(name, FALSE, FALSE,
 					DICT_ERR_IGNORE_NONE);
 
-	if (table != NULL && table->ibd_file_missing) {
+	if (table != NULL && table->file_unreadable) {
 		table = NULL;
 	}
 
@@ -292,7 +292,7 @@ ib_lookup_table_by_name(
 
 	table = dict_table_get_low(name);
 
-	if (table != NULL && table->ibd_file_missing) {
+	if (table != NULL && table->file_unreadable) {
 		table = NULL;
 	}
 
diff --git a/storage/xtradb/btr/btr0btr.cc b/storage/xtradb/btr/btr0btr.cc
index 417eeb2c367..02b47147816 100644
--- a/storage/xtradb/btr/btr0btr.cc
+++ b/storage/xtradb/btr/btr0btr.cc
@@ -2,7 +2,7 @@
 
 Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
 Copyright (c) 2012, Facebook Inc.
-Copyright (c) 2014, 2016, MariaDB Corporation
+Copyright (c) 2014, 2017, MariaDB Corporation
 
 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
@@ -744,8 +744,7 @@ btr_root_block_get(
 
 	if (!block) {
 		if (index && index->table) {
-			index->table->is_encrypted = TRUE;
-			index->table->corrupted = FALSE;
+			index->table->file_unreadable = true;
 
 			ib_push_warning(index->table->thd, DB_DECRYPTION_FAILED,
 				"Table %s in tablespace %lu is encrypted but encryption service or"
@@ -5211,7 +5210,7 @@ btr_validate_index(
 
 	page_t*	root = btr_root_get(index, &mtr);
 
-	if (root == NULL && index->table->is_encrypted) {
+	if (root == NULL && index->table->file_unreadable) {
 		err = DB_DECRYPTION_FAILED;
 		mtr_commit(&mtr);
 		return err;
diff --git a/storage/xtradb/btr/btr0cur.cc b/storage/xtradb/btr/btr0cur.cc
index f270f3e2de3..926faffeb2e 100644
--- a/storage/xtradb/btr/btr0cur.cc
+++ b/storage/xtradb/btr/btr0cur.cc
@@ -666,7 +666,7 @@ btr_cur_search_to_nth_level(
 				" used key_id is not available. "
 				" Can't continue reading table.",
 				index->table->name);
-			index->table->is_encrypted = true;
+			index->table->file_unreadable = true;
 		}
 
 		goto func_exit;
@@ -996,7 +996,7 @@ btr_cur_open_at_index_side_func(
 					" used key_id is not available. "
 					" Can't continue reading table.",
 					index->table->name);
-				index->table->is_encrypted = true;
+				index->table->file_unreadable = true;
 			}
 
 			goto exit_loop;
@@ -1169,7 +1169,7 @@ btr_cur_open_at_rnd_pos_func(
 					" used key_id is not available. "
 					" Can't continue reading table.",
 					index->table->name);
-				index->table->is_encrypted = true;
+				index->table->file_unreadable = true;
 			}
 			goto exit_loop;
 		}
@@ -3861,7 +3861,7 @@ btr_estimate_n_rows_in_range_on_level(
 					" used key_id is not available. "
 					" Can't continue reading table.",
 					index->table->name);
-				index->table->is_encrypted = true;
+				index->table->file_unreadable = true;
 			}
 
 			mtr_commit(&mtr);
@@ -4002,10 +4002,20 @@ btr_estimate_n_rows_in_range_low(
 					    BTR_SEARCH_LEAF | BTR_ESTIMATE,
 					    &cursor, 0,
 					    __FILE__, __LINE__, &mtr);
+
+		if (index->table->file_unreadable) {
+			mtr_commit(&mtr);
+			return (0);
+		}
 	} else {
 		btr_cur_open_at_index_side(true, index,
 					   BTR_SEARCH_LEAF | BTR_ESTIMATE,
 					   &cursor, 0, &mtr);
+
+		if (index->table->file_unreadable) {
+			mtr_commit(&mtr);
+			return (0);
+		}
 	}
 
 	mtr_commit(&mtr);
@@ -4360,6 +4370,11 @@ btr_estimate_number_of_different_key_vals(
 		because otherwise our algorithm would give a wrong estimate
 		for an index where there is just one key value. */
 
+		if (index->table->file_unreadable) {
+			mtr_commit(&mtr);
+			goto exit_loop;
+		}
+
 		page = btr_cur_get_page(&cursor);
 
 		SRV_CORRUPT_TABLE_CHECK(page, goto exit_loop;);
diff --git a/storage/xtradb/btr/btr0defragment.cc b/storage/xtradb/btr/btr0defragment.cc
index 6e7a3fd2411..f0b1d05943c 100644
--- a/storage/xtradb/btr/btr0defragment.cc
+++ b/storage/xtradb/btr/btr0defragment.cc
@@ -227,7 +227,7 @@ btr_defragment_add_index(
 		page = buf_block_get_frame(block);
 	}
 
-	if (page == NULL && index->table->is_encrypted) {
+	if (page == NULL && index->table->file_unreadable) {
 		mtr_commit(&mtr);
 		*err = DB_DECRYPTION_FAILED;
 		return NULL;
diff --git a/storage/xtradb/btr/btr0pcur.cc b/storage/xtradb/btr/btr0pcur.cc
index dd6ef484fc3..23ad9ada935 100644
--- a/storage/xtradb/btr/btr0pcur.cc
+++ b/storage/xtradb/btr/btr0pcur.cc
@@ -420,6 +420,11 @@ btr_pcur_move_to_next_page(
 	cursor->old_stored = BTR_PCUR_OLD_NOT_STORED;
 
 	page = btr_pcur_get_page(cursor);
+
+	if (!page) {
+		return;
+	}
+
 	next_page_no = btr_page_get_next(page, mtr);
 	space = buf_block_get_space(btr_pcur_get_block(cursor));
 	zip_size = buf_block_get_zip_size(btr_pcur_get_block(cursor));
@@ -429,6 +434,11 @@ btr_pcur_move_to_next_page(
 	next_block = btr_block_get(space, zip_size, next_page_no,
 				   cursor->latch_mode,
 				   btr_pcur_get_btr_cur(cursor)->index, mtr);
+
+	if (!next_block) {
+		return;
+	}
+
 	next_page = buf_block_get_frame(next_block);
 
 	SRV_CORRUPT_TABLE_CHECK(next_page,
diff --git a/storage/xtradb/buf/buf0buf.cc b/storage/xtradb/buf/buf0buf.cc
index 313c5c9d696..d382820a0eb 100644
--- a/storage/xtradb/buf/buf0buf.cc
+++ b/storage/xtradb/buf/buf0buf.cc
@@ -1181,7 +1181,6 @@ buf_block_init(
 	block->page.buf_fix_count = 0;
 	block->page.io_fix = BUF_IO_NONE;
 	block->page.encrypted = false;
-	block->page.key_version = 0;
 	block->page.real_size = 0;
 	block->page.write_size = 0;
 	block->modify_clock = 0;
@@ -2356,9 +2355,9 @@ buf_page_get_zip(
 		if (err != DB_SUCCESS) {
 			ib_logf(IB_LOG_LEVEL_ERROR,
 				"Reading compressed page " ULINTPF
-				" from tablespace " ULINTPF
-				" failed with error: %d.",
-				offset, space, err);
+				":" ULINTPF
+				" failed with error: %s.",
+				space, offset, ut_strerr(err));
 
 			goto err_exit;
 		}
@@ -3023,10 +3022,12 @@ buf_page_get_gen(
 
 			DBUG_EXECUTE_IF(
 				"innodb_page_corruption_retries",
-				retries = BUF_PAGE_READ_MAX_RETRIES;
+				/* This is needed to avoid tablespace
+				being marked as corrupted. */
+				goto force_fail;
 			);
 		} else {
-			if (*err) {
+			if (err) {
 				*err = local_err;
 			}
 
@@ -3044,19 +3045,19 @@ buf_page_get_gen(
 				return (NULL);
 			}
 
+force_fail:
 			ib_logf(IB_LOG_LEVEL_FATAL, "Unable"
 				" to read tablespace %lu page no"
 				" %lu into the buffer pool after"
-				" %lu attempts\n"
-				"InnoDB: The most probable cause"
+				" %lu attempts"
+				" The most probable cause"
 				" of this error may be that the"
-				" table has been corrupted.\n"
-				"InnoDB: You can try to fix this"
+				" table has been corrupted."
+				" You can try to fix this"
 				" problem by using"
-				" innodb_force_recovery.\n"
-				"InnoDB: Please see reference manual"
-				" for more details.\n"
-				"InnoDB: Aborting...\n",
+				" innodb_force_recovery."
+				" Please see " REFMAN " for more"
+				" details. Aborting...",
 				space, offset,
 				BUF_PAGE_READ_MAX_RETRIES);
 		}
@@ -3831,7 +3832,6 @@ buf_page_init_low(
 	bpage->oldest_modification = 0;
 	bpage->write_size = 0;
 	bpage->encrypted = false;
-	bpage->key_version = 0;
 	bpage->real_size = 0;
 
 	HASH_INVALIDATE(bpage, hash);
@@ -4487,13 +4487,14 @@ buf_mark_space_corrupt(
 	const ulint	fold = buf_page_address_fold(bpage->space,
 						     bpage->offset);
 	prio_rw_lock_t*	hash_lock = buf_page_hash_lock_get(buf_pool, fold);
+	ib_mutex_t* block_mutex = buf_page_get_mutex(bpage);
 
 	/* First unfix and release lock on the bpage */
 	ut_ad(!mutex_own(&buf_pool->LRU_list_mutex));
 
 	mutex_enter(&buf_pool->LRU_list_mutex);
 	rw_lock_x_lock(hash_lock);
-	mutex_enter(buf_page_get_mutex(bpage));
+	mutex_enter(block_mutex);
 	ut_ad(buf_page_get_io_fix(bpage) == BUF_IO_READ);
 	ut_ad(bpage->buf_fix_count == 0);
 
@@ -4511,6 +4512,8 @@ buf_mark_space_corrupt(
 	are marked unusable later e.g. in ::open(). */
 	if (!bpage->encrypted) {
 		dict_set_corrupted_by_space(space);
+	} else {
+		dict_set_encrypted_by_space(space);
 	}
 
 	/* After this point bpage can't be referenced. */
@@ -4543,7 +4546,6 @@ buf_page_check_corrupt(
 	bool still_encrypted = false;
 	dberr_t err = DB_SUCCESS;
 	bool corrupted = false;
-	ulint page_type = mach_read_from_2(dst_frame + FIL_PAGE_TYPE);
 	fil_space_crypt_t* crypt_data = NULL;
 
 	ut_ad(space);
@@ -4576,26 +4578,27 @@ buf_page_check_corrupt(
 	/* Pages that we think are unencrypted but do not match the checksum
 	checks could be corrupted or encrypted or both. */
 	if (corrupted && !bpage->encrypted) {
-		ib_logf(IB_LOG_LEVEL_ERROR,
-			"%s: Block in space_id " ULINTPF " in file %s corrupted.",
-			page_type ==  FIL_PAGE_PAGE_COMPRESSED_ENCRYPTED ? "Maybe corruption" : "Corruption",
-			space_id, (space && space->name) ? space->name : "NULL");
-		ib_logf(IB_LOG_LEVEL_ERROR,
-			"Based on page type %s (" ULINTPF ")",
-			fil_get_page_type_name(page_type), page_type);
+		/* An error will be reported by
+		buf_page_io_complete(). */
 	} else if (still_encrypted || (bpage->encrypted && corrupted)) {
 		bpage->encrypted = true;
 		err = DB_DECRYPTION_FAILED;
 
 		ib_logf(IB_LOG_LEVEL_ERROR,
-			"Block in space_id " ULINTPF " in file %s encrypted.",
-			space_id, (space && space->name) ? space->name : "NULL");
-		ib_logf(IB_LOG_LEVEL_ERROR,
-			"However key management plugin or used key_version %u is not found or"
+			"The page [page id: space=" ULINTPF
+			", page number=%u]"
+			" in file %s cannot be decrypted.",
+			space_id, bpage->offset,
+			(space && space->name) ? space->name : "NULL");
+
+		ib_logf(IB_LOG_LEVEL_INFO,
+			"However key management plugin or used key_version " ULINTPF
+			" is not found or"
 			" used encryption algorithm or method does not match.",
-			bpage->key_version);
+			mach_read_from_4(dst_frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION));
+
 		if (space_id > TRX_SYS_SPACE) {
-			ib_logf(IB_LOG_LEVEL_ERROR,
+			ib_logf(IB_LOG_LEVEL_INFO,
 				"Marking tablespace as missing. You may drop this table or"
 				" install correct key management plugin and key file.");
 		}
@@ -4615,7 +4618,8 @@ the buffer pool.
 @return DB_SUCCESS if page has been read and is not corrupted,
 DB_PAGE_CORRUPTED if page based on checksum check is corrupted,
 DB_DECRYPTION_FAILED if page post encryption checksum matches but
-after decryption normal page checksum does not match.*/
+after decryption normal page checksum does not match.
+in write only DB_SUCCESS is possible. */
 UNIV_INTERN
 dberr_t
 buf_page_io_complete(
@@ -4644,6 +4648,7 @@ buf_page_io_complete(
 	if (io_type == BUF_IO_READ) {
 		ulint	read_page_no;
 		ulint	read_space_id;
+		uint	key_version;
 
 		buf_page_decrypt_after_read(bpage);
 
@@ -4683,6 +4688,8 @@ buf_page_io_complete(
 		read_page_no = mach_read_from_4(frame + FIL_PAGE_OFFSET);
 		read_space_id = mach_read_from_4(
 			frame + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
+		key_version = mach_read_from_4(
+			frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION);
 
 		if (bpage->space == TRX_SYS_SPACE
 		    && buf_dblwr_page_inside(bpage->offset)) {
@@ -4741,45 +4748,40 @@ buf_page_io_complete(
 				fil_system_enter();
 				space = fil_space_get_by_id(bpage->space);
 				fil_system_exit();
+
 				ib_logf(IB_LOG_LEVEL_ERROR,
 					"Database page corruption on disk"
-					" or a failed");
-				ib_logf(IB_LOG_LEVEL_ERROR,
-					"Space %u file %s read of page %u.",
-					bpage->space,
-					space->name ? space->name : "NULL",
-					bpage->offset);
-				ib_logf(IB_LOG_LEVEL_ERROR,
-					"You may have to recover"
-					" from a backup.");
+					" or a failed file read of tablespace %s"
+					" page  [page id: space=" ULINTPF
+					", page number= " ULINTPF "]"
+					". You may have to recover from "
+					"a backup.",
+					(space->name ? space->name : "NULL"),
+					read_space_id, read_page_no);
 
 				buf_page_print(frame, buf_page_get_zip_size(bpage),
 					BUF_PAGE_PRINT_NO_CRASH);
 
-				ib_logf(IB_LOG_LEVEL_ERROR,
-					"It is also possible that your operating"
-					"system has corrupted its own file cache.");
-				ib_logf(IB_LOG_LEVEL_ERROR,
-					"and rebooting your computer removes the error.");
-				ib_logf(IB_LOG_LEVEL_ERROR,
-					"If the corrupt page is an index page you can also try to");
-				ib_logf(IB_LOG_LEVEL_ERROR,
-					"fix the corruption by dumping, dropping, and reimporting");
-				ib_logf(IB_LOG_LEVEL_ERROR,
-					"the corrupt table. You can use CHECK");
-				ib_logf(IB_LOG_LEVEL_ERROR,
-					"TABLE to scan your table for corruption.");
-				ib_logf(IB_LOG_LEVEL_ERROR,
-					"See also "
-					REFMAN "forcing-innodb-recovery.html"
-					" about forcing recovery.");
+				ib_logf(IB_LOG_LEVEL_INFO,
+					"It is also possible that your"
+					" operating system has corrupted"
+					" its own file cache and rebooting"
+					" your computer removes the error."
+					" If the corrupt page is an index page."
+					" You can also try to fix the"
+					" corruption by dumping, dropping,"
+					" and reimporting the corrupt table."
+					" You can use CHECK TABLE to scan"
+					" your table for corruption. "
+					"Please refer to " REFMAN "forcing-innodb-recovery.html"
+					" for information about forcing recovery.");
 			}
 
 			if (srv_pass_corrupt_table && bpage->space != 0
 				&& bpage->space < SRV_LOG_SPACE_FIRST_ID) {
 				trx_t*	trx;
 
-				fprintf(stderr,
+				ib_logf(IB_LOG_LEVEL_INFO,
 					"InnoDB: space %u will be treated as corrupt.\n",
 					bpage->space);
 				fil_space_set_corrupt(bpage->space);
@@ -4803,10 +4805,8 @@ buf_page_io_complete(
 					buf_mark_space_corrupt(bpage);
 					return(err);
 				} else {
-					ib_logf(IB_LOG_LEVEL_ERROR,
+					ib_logf(IB_LOG_LEVEL_FATAL,
 						"Ending processing because of a corrupt database page.");
-
-					ut_error;
 				}
 			}
 		}
@@ -4826,11 +4826,11 @@ buf_page_io_complete(
 
 			if (bpage && bpage->encrypted) {
 				ib_logf(IB_LOG_LEVEL_WARN,
-					"Table in tablespace %lu encrypted."
+					"Table in tablespace " ULINTPF " encrypted."
 					"However key management plugin or used key_version %u is not found or"
 					" used encryption algorithm or method does not match."
-					" Can't continue opening the table.\n",
-					(ulint)bpage->space, bpage->key_version);
+					" Can't continue opening the table.",
+					read_space_id, key_version);
 			} else {
 
 				ibuf_merge_or_delete_for_page(
@@ -4980,7 +4980,7 @@ buf_all_freed_instance(
 
 		mutex_exit(&buf_pool->LRU_list_mutex);
 
-		if (UNIV_LIKELY_NULL(block) && block->page.key_version == 0) {
+		if (UNIV_LIKELY_NULL(block)) {
 			fil_space_t* space = fil_space_get(block->page.space);
 			ib_logf(IB_LOG_LEVEL_ERROR,
 				"Page %u %u still fixed or dirty.",
@@ -6218,13 +6218,11 @@ buf_page_encrypt_before_write(
 
 	if (bpage->offset == 0) {
 		/* Page 0 of a tablespace is not encrypted/compressed */
-		ut_ad(bpage->key_version == 0);
 		return src_frame;
 	}
 
 	if (bpage->space == TRX_SYS_SPACE && bpage->offset == TRX_SYS_PAGE_NO) {
 		/* don't encrypt/compress page as it contains address to dblwr buffer */
-		bpage->key_version = 0;
 		return src_frame;
 	}
 
@@ -6254,7 +6252,6 @@ buf_page_encrypt_before_write(
 	/* Is encryption needed? */
 	if (crypt_data == NULL || crypt_data->type == CRYPT_SCHEME_UNENCRYPTED) {
 		/* An unencrypted table */
-		bpage->key_version = 0;
 		encrypted = false;
 	}
 
@@ -6284,9 +6281,6 @@ buf_page_encrypt_before_write(
 					      src_frame,
 					      dst_frame);
 
-		ulint key_version = mach_read_from_4(dst_frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION);
-		ut_ad(key_version == 0 || key_version >= bpage->key_version);
-		bpage->key_version = key_version;
 		bpage->real_size = page_size;
 		slot->out_buf = dst_frame = tmp;
 
@@ -6360,8 +6354,6 @@ buf_page_decrypt_after_read(
 	buf_pool_t* buf_pool = buf_pool_from_bpage(bpage);
 	bool success = true;
 
-	bpage->key_version = key_version;
-
 	if (bpage->offset == 0) {
 		/* File header pages are not encrypted/compressed */
 		return (true);
@@ -6408,6 +6400,16 @@ buf_page_decrypt_after_read(
 			decrypt. */
 			if (!fil_space_verify_crypt_checksum(dst_frame,
 					zip_size, NULL, bpage->offset)) {
+
+				/* Mark page encrypted in case it should
+				be. */
+				if (key_version && space->crypt_data &&
+				    space->crypt_data->type != CRYPT_SCHEME_UNENCRYPTED) {
+					bpage->encrypted=true;
+				}
+
+				fil_space_release(space);
+
 				return (false);
 			}
 
diff --git a/storage/xtradb/buf/buf0rea.cc b/storage/xtradb/buf/buf0rea.cc
index 8e5619cedd4..d41b7fc2cd2 100644
--- a/storage/xtradb/buf/buf0rea.cc
+++ b/storage/xtradb/buf/buf0rea.cc
@@ -414,22 +414,26 @@ buf_read_ahead_random(
 				space, zip_size, FALSE,
 				tablespace_version, i, trx);
 
-			if (err == DB_TABLESPACE_DELETED) {
+			switch(err) {
+			case DB_SUCCESS:
+				break;
+			case DB_TABLESPACE_DELETED:
 				ib_logf(IB_LOG_LEVEL_WARN,
 					"In random"
-					"readahead trying to access"
-					"tablespace " ULINTPF " page " ULINTPF
+					" readahead trying to access"
+					" tablespace " ULINTPF ":" ULINTPF
 					" but the tablespace does not"
-					" exist or is just being dropped.\n",
+					" exist or is just being dropped.",
 					space, i);
-			}
-
-			if (err == DB_DECRYPTION_FAILED) {
+				break;
+			case DB_DECRYPTION_FAILED:
 				ib_logf(IB_LOG_LEVEL_ERROR,
-					"In random readahead read page " ULINTPF
-					" from tablespace " ULINTPF
-					" but page is still encrypted. ",
+					"Random readahead failed to decrypt page "
+					ULINTPF ":" ULINTPF ".",
 					i, space);
+				break;
+			default:
+				ut_error;
 			}
 		}
 	}
@@ -499,7 +503,7 @@ buf_read_page(
 	if (err == DB_TABLESPACE_DELETED) {
 		ib_logf(IB_LOG_LEVEL_ERROR,
 			"Trying to access"
-			" tablespace " ULINTPF " page no. " ULINTPF
+			" tablespace " ULINTPF ":" ULINTPF
 			" but the tablespace does not exist"
 			" or is just being dropped.",
 			space, offset);
@@ -517,13 +521,9 @@ buffer buf_pool if it is not already there. Sets the io_fix flag and sets
 an exclusive lock on the buffer frame. The flag is cleared and the x-lock
 released by the i/o-handler thread.
 @param[in]	space		Tablespace id
- at param[in]	offset		Page no
- at return DB_SUCCESS if page has been read and is not corrupted,
-DB_PAGE_CORRUPTED if page based on checksum check is corrupted,
-DB_DECRYPTION_FAILED if page post encryption checksum matches but
-after decryption normal page checksum does not match.*/
+ at param[in]	offset		Page no */
 UNIV_INTERN
-dberr_t
+void
 buf_read_page_async(
 	ulint	space,
 	ulint	offset)
@@ -536,7 +536,7 @@ buf_read_page_async(
 	zip_size = fil_space_get_zip_size(space);
 
 	if (zip_size == ULINT_UNDEFINED) {
-		return(err);
+		return;
 	}
 
 	tablespace_version = fil_space_get_version(space);
@@ -547,13 +547,26 @@ buf_read_page_async(
 				  space, zip_size, FALSE,
 				  tablespace_version, offset);
 
-	if (err != DB_SUCCESS) {
+	switch(err) {
+	case DB_SUCCESS:
+		break;
+	case DB_TABLESPACE_DELETED:
 		ib_logf(IB_LOG_LEVEL_ERROR,
 			"In async page read "
-			"trying to access"
-			"tablespace " ULINTPF " page " ULINTPF
-			" but failed on error %d.",
-			space, offset, err);
+			"trying to access "
+			"page " ULINTPF ":" ULINTPF
+			" in nonexisting or being-dropped tablespace",
+			space, offset);
+		break;
+
+	case DB_DECRYPTION_FAILED:
+		ib_logf(IB_LOG_LEVEL_ERROR,
+			"Async page read failed to decrypt page "
+			ULINTPF ":" ULINTPF ".",
+			space, offset);
+		break;
+	default:
+		ut_error;
 	}
 
 	srv_stats.buf_pool_reads.add(count);
@@ -564,8 +577,6 @@ buf_read_page_async(
 	buffer pool. Since this function is called from buffer pool load
 	these IOs are deliberate and are not part of normal workload we can
 	ignore these in our heuristics. */
-
-	return(err);
 }
 
 /********************************************************************//**
@@ -827,14 +838,27 @@ buf_read_ahead_linear(
 				space, zip_size, FALSE, tablespace_version,
 				i, trx);
 
-			if (err == DB_TABLESPACE_DELETED) {
+			switch(err) {
+			case DB_SUCCESS:
+				break;
+			case DB_TABLESPACE_DELETED:
 				ib_logf(IB_LOG_LEVEL_WARN,
-					"In random"
-					"readahead trying to access"
-					"tablespace " ULINTPF " page " ULINTPF
+					"In linear"
+					" readahead trying to access"
+					" tablespace " ULINTPF ":" ULINTPF
 					" but the tablespace does not"
-					" exist or is just being dropped.\n",
+					" exist or is just being dropped.",
 					space, i);
+				break;
+
+			case DB_DECRYPTION_FAILED:
+				ib_logf(IB_LOG_LEVEL_ERROR,
+					"Linear readahead failed to decrypt page "
+					ULINTPF ":" ULINTPF ".",
+					i, space);
+				break;
+			default:
+				ut_error;
 			}
 
 			if (err == DB_DECRYPTION_FAILED) {
@@ -905,9 +929,9 @@ buf_read_ibuf_merge_pages(
 #endif
 
 	for (i = 0; i < n_stored; i++) {
-		dberr_t		err=DB_SUCCESS;
 		buf_pool_t*	buf_pool;
 		ulint		zip_size = fil_space_get_zip_size(space_ids[i]);
+		dberr_t		err = DB_SUCCESS;
 
 		buf_pool = buf_pool_get(space_ids[i], page_nos[i]);
 
@@ -926,7 +950,11 @@ buf_read_ibuf_merge_pages(
 				  zip_size, TRUE, space_versions[i],
 				  page_nos[i], NULL);
 
-		if (UNIV_UNLIKELY(err == DB_TABLESPACE_DELETED)) {
+		switch(err) {
+		case DB_SUCCESS:
+			break;
+		case DB_TABLESPACE_DELETED:
+
 tablespace_deleted:
 			/* We have deleted or are deleting the single-table
 			tablespace: remove the entries for that page */
@@ -934,13 +962,15 @@ buf_read_ibuf_merge_pages(
 			ibuf_merge_or_delete_for_page(NULL, space_ids[i],
 						      page_nos[i],
 						      zip_size, FALSE);
-		}
-
-		if (err == DB_DECRYPTION_FAILED) {
+			break;
+		case DB_DECRYPTION_FAILED:
 			ib_logf(IB_LOG_LEVEL_ERROR,
-				"Read page " ULINTPF " from tablespace " ULINTPF
-				" for insert buffer but page was encrypted.",
-				page_nos[i], space_ids[i]);
+				"Failed to decrypt insert buffer page "
+				ULINTPF ":" ULINTPF ".",
+				space_ids[i], page_nos[i]);
+			break;
+		default:
+			ut_error;
 		}
 	}
 
@@ -1082,10 +1112,9 @@ buf_read_recv_pages(
 
 		if (err == DB_DECRYPTION_FAILED) {
 			ib_logf(IB_LOG_LEVEL_ERROR,
-				"Recovery read page " ULINTPF
-				" from tablespace " ULINTPF
-				" but page was encrypted.",
-				page_nos[i], space);
+				"Recovery failed to decrypt read page "
+				ULINTPF ":" ULINTPF ".",
+				space, page_nos[i]);
 		}
 	}
 
diff --git a/storage/xtradb/dict/dict0crea.cc b/storage/xtradb/dict/dict0crea.cc
index 1ada35a89a2..6d5b12474eb 100644
--- a/storage/xtradb/dict/dict0crea.cc
+++ b/storage/xtradb/dict/dict0crea.cc
@@ -692,7 +692,7 @@ dict_create_index_tree_step(
 	dberr_t		err = DB_SUCCESS;
 	ulint		zip_size = dict_table_zip_size(index->table);
 
-	if (node->index->table->ibd_file_missing
+	if (node->index->table->file_unreadable
 	    || dict_table_is_discarded(node->index->table)) {
 
 		node->page_no = FIL_NULL;
diff --git a/storage/xtradb/dict/dict0dict.cc b/storage/xtradb/dict/dict0dict.cc
index 49de1cf7ef8..b0757055262 100644
--- a/storage/xtradb/dict/dict0dict.cc
+++ b/storage/xtradb/dict/dict0dict.cc
@@ -1187,7 +1187,7 @@ dict_table_open_on_name(
 
 		/* If table is encrypted return table */
 		if (ignore_err == DICT_ERR_IGNORE_NONE
-			&& table->is_encrypted) {
+			&& table->file_unreadable) {
 			/* Make life easy for drop table. */
 			if (table->can_be_evicted) {
 				dict_table_move_from_lru_to_non_lru(table);
@@ -6189,6 +6189,24 @@ dict_set_corrupted_by_space(
 }
 
 /**********************************************************************//**
+Flags a table with specified space_id encrypted in the data dictionary
+cache
+ at param[in]	space_id	Tablespace id */
+UNIV_INTERN
+void
+dict_set_encrypted_by_space(
+	ulint	space_id)
+{
+	dict_table_t*   table;
+
+	table = dict_find_table_by_space(space_id);
+
+	if (table) {
+		table->file_unreadable = true;
+	}
+}
+
+/**********************************************************************//**
 Flags an index corrupted both in the data dictionary cache
 and in the SYS_INDEXES */
 UNIV_INTERN
@@ -6683,7 +6701,8 @@ dict_table_schema_check(
 		}
 	}
 
-	if (table->ibd_file_missing) {
+	if (table->file_unreadable &&
+	    fil_space_get(table->space) == NULL) {
 		/* missing tablespace */
 
 		ut_snprintf(errstr, errstr_sz,
diff --git a/storage/xtradb/dict/dict0load.cc b/storage/xtradb/dict/dict0load.cc
index 776bb873c5c..134c0d4f0b1 100644
--- a/storage/xtradb/dict/dict0load.cc
+++ b/storage/xtradb/dict/dict0load.cc
@@ -1964,7 +1964,7 @@ dict_load_indexes(
 			dict_mem_index_free(index);
 			goto func_exit;
 		} else if (index->page == FIL_NULL
-			   && !table->ibd_file_missing
+			   && !table->file_unreadable
 			   && (!(index->type & DICT_FTS))) {
 
 			fprintf(stderr,
@@ -2193,7 +2193,7 @@ dict_load_table_low(
 
 	(*table)->id = mach_read_from_8(field);
 
-	(*table)->ibd_file_missing = FALSE;
+	(*table)->file_unreadable = false;
 
 	return(NULL);
 }
@@ -2380,7 +2380,7 @@ dict_load_table(
 			"Table '%s' tablespace is set as discarded.",
 			table_name);
 
-		table->ibd_file_missing = TRUE;
+		table->file_unreadable = true;
 
 	} else if (!fil_space_for_table_exists_in_mem(
 			table->space, name, false, true, heap,
@@ -2388,7 +2388,7 @@ dict_load_table(
 
 		if (DICT_TF2_FLAG_IS_SET(table, DICT_TF2_TEMPORARY)) {
 			/* Do not bother to retry opening temporary tables. */
-			table->ibd_file_missing = TRUE;
+			table->file_unreadable = true;
 
 		} else {
 			if (!(ignore_err & DICT_ERR_IGNORE_RECOVER_LOCK)) {
@@ -2423,7 +2423,7 @@ dict_load_table(
 				/* We failed to find a sensible
 				tablespace file */
 
-				table->ibd_file_missing = TRUE;
+				table->file_unreadable = true;
 			}
 			if (filepath) {
 				mem_free(filepath);
@@ -2448,9 +2448,10 @@ dict_load_table(
 	were not allowed while the table is being locked by a transaction. */
 	dict_err_ignore_t index_load_err =
 		!(ignore_err & DICT_ERR_IGNORE_RECOVER_LOCK)
-		&& table->ibd_file_missing
+		&& table->file_unreadable
 		? DICT_ERR_IGNORE_ALL
 		: ignore_err;
+
 	err = dict_load_indexes(table, heap, index_load_err);
 
 	if (err == DB_INDEX_CORRUPT) {
@@ -2485,7 +2486,7 @@ dict_load_table(
 	of the error condition, since the user may want to dump data from the
 	clustered index. However we load the foreign key information only if
 	all indexes were loaded. */
-	if (!cached || table->ibd_file_missing) {
+	if (!cached || table->file_unreadable) {
 		/* Don't attempt to load the indexes from disk. */
 	} else if (err == DB_SUCCESS) {
 		err = dict_load_foreigns(table->name, NULL, true, true,
@@ -2518,12 +2519,12 @@ dict_load_table(
 			table = NULL;
 
 		} else if (dict_index_is_corrupted(index)
-			   && !table->ibd_file_missing) {
+			   && !table->file_unreadable) {
 
 			/* It is possible we force to load a corrupted
 			clustered index if srv_load_corrupted is set.
 			Mark the table as corrupted in this case */
-			table->corrupted = TRUE;
+			table->corrupted = true;
 		}
 	}
 
@@ -2532,7 +2533,7 @@ dict_load_table(
 
 	ut_ad(!table
 	      || ignore_err != DICT_ERR_IGNORE_NONE
-	      || table->ibd_file_missing
+	      || table->file_unreadable
 	      || !table->corrupted);
 
 	if (table && table->fts) {
diff --git a/storage/xtradb/dict/dict0stats.cc b/storage/xtradb/dict/dict0stats.cc
index 6a28f3cdf8f..ff8d4ee5323 100644
--- a/storage/xtradb/dict/dict0stats.cc
+++ b/storage/xtradb/dict/dict0stats.cc
@@ -920,7 +920,12 @@ dict_stats_update_transient_for_index(
 
 		index->stat_n_leaf_pages = size;
 
-		btr_estimate_number_of_different_key_vals(index);
+		/* Do not continue if table decryption has failed or
+		table is already marked as corrupted. */
+		if (!index->table->file_unreadable &&
+		    !index->table->corrupted) {
+			btr_estimate_number_of_different_key_vals(index);
+		}
 	}
 }
 
@@ -974,8 +979,10 @@ dict_stats_update_transient(
 			continue;
 		}
 
-		/* Do not continue if table decryption has failed. */
-		if (index->table->is_encrypted) {
+		/* Do not continue if table decryption has failed or
+		table is already marked as corrupted. */
+		if (index->table->file_unreadable ||
+		    index->table->corrupted) {
 			break;
 		}
 
@@ -3192,7 +3199,7 @@ dict_stats_update(
 
 	ut_ad(!mutex_own(&dict_sys->mutex));
 
-	if (table->ibd_file_missing) {
+	if (table->file_unreadable) {
 		ut_print_timestamp(stderr);
 		fprintf(stderr,
 			" InnoDB: cannot calculate statistics for table %s "
@@ -3946,7 +3953,7 @@ dict_stats_save_defrag_stats(
 {
 	dberr_t	ret;
 
-	if (index->table->ibd_file_missing) {
+	if (index->table->file_unreadable) {
 		ut_print_timestamp(stderr);
 		fprintf(stderr,
 			" InnoDB: Cannot save defragment stats because "
diff --git a/storage/xtradb/fil/fil0crypt.cc b/storage/xtradb/fil/fil0crypt.cc
index 7bef91f47ff..f9a2c23be78 100644
--- a/storage/xtradb/fil/fil0crypt.cc
+++ b/storage/xtradb/fil/fil0crypt.cc
@@ -474,16 +474,17 @@ fil_space_set_crypt_data(
 
 /******************************************************************
 Parse a MLOG_FILE_WRITE_CRYPT_DATA log entry
- at param[in]	ptr		Log entry start
+ at param[in,out]	ptr		Log entry start
 @param[in]	end_ptr		Log entry end
 @param[in]	block		buffer block
 @return position on log buffer */
 UNIV_INTERN
-const byte*
+byte*
 fil_parse_write_crypt_data(
-	const byte*		ptr,
+	byte*			ptr,
 	const byte*		end_ptr,
-	const buf_block_t*	block)
+	const buf_block_t*	block,
+	dberr_t*		err)
 {
 	/* check that redo log entry is complete */
 	uint entry_size =
@@ -495,6 +496,8 @@ fil_parse_write_crypt_data(
 		4 +  // size of key_id
 		1; // fil_encryption_t
 
+	*err = DB_SUCCESS;
+
 	if (ptr + entry_size > end_ptr) {
 		return NULL;
 	}
@@ -541,6 +544,11 @@ fil_parse_write_crypt_data(
 		fil_space_release(space);
 	}
 
+	/* Check is used key found from encryption plugin */
+	if (crypt_data->should_encrypt() && !crypt_data->is_key_found()) {
+		*err = DB_DECRYPTION_FAILED;
+	}
+
 	return ptr;
 }
 
@@ -960,11 +968,12 @@ fil_space_verify_crypt_checksum(
 
 	/* If page is not encrypted, return false */
 	if (key_version == 0) {
-		return false;
+		return(false);
 	}
 
 	srv_checksum_algorithm_t algorithm =
 			static_cast<srv_checksum_algorithm_t>(srv_checksum_algorithm);
+
 	/* If no checksum is used, can't continue checking. */
 	if (algorithm == SRV_CHECKSUM_ALGORITHM_NONE) {
 		return(true);
@@ -988,28 +997,31 @@ fil_space_verify_crypt_checksum(
 		return (true);
 	}
 
-	/* Compressed pages use different checksum method. We first store
-	the post encryption checksum on checksum location and after function
-	restore the original. */
-	if (zip_size) {
-		ib_uint32_t old = static_cast<ib_uint32_t>(mach_read_from_4(
-				page + FIL_PAGE_SPACE_OR_CHKSUM));
-
-		mach_write_to_4(page + FIL_PAGE_SPACE_OR_CHKSUM, checksum);
+	ib_uint32_t cchecksum1=0;
+	ib_uint32_t cchecksum2=0;
 
-		bool valid = page_zip_verify_checksum(page, zip_size);
+	/* Calculate checksums */
+	if (zip_size) {
+		cchecksum1 = page_zip_calc_checksum(
+			page, zip_size, SRV_CHECKSUM_ALGORITHM_CRC32);
 
-		mach_write_to_4(page + FIL_PAGE_SPACE_OR_CHKSUM, old);
+		if(cchecksum1 != checksum) {
+			cchecksum2 = page_zip_calc_checksum(
+				page, zip_size,
+				SRV_CHECKSUM_ALGORITHM_INNODB);
+		}
+	} else {
+		cchecksum1 = buf_calc_page_crc32(page);
 
-		return (valid);
+		if (cchecksum1 != checksum) {
+			cchecksum2 = (ib_uint32_t) buf_calc_page_new_checksum(
+				page);
+		}
 	}
 
 	/* If stored checksum matches one of the calculated checksums
 	page is not corrupted. */
 
-	ib_uint32_t cchecksum1 = buf_calc_page_crc32(page);
-	ib_uint32_t cchecksum2 = (ib_uint32_t) buf_calc_page_new_checksum(
-				page);
 	bool encrypted = (checksum == cchecksum1 || checksum == cchecksum2
 		|| checksum == BUF_NO_CHECKSUM_MAGIC);
 
@@ -1967,11 +1979,11 @@ fil_crypt_rotate_page(
 		bool modified = false;
 		int needs_scrubbing = BTR_SCRUB_SKIP_PAGE;
 		lsn_t block_lsn = block->page.newest_modification;
-		uint kv =  block->page.key_version;
+		byte* frame = buf_block_get_frame(block);
+		uint kv =  mach_read_from_4(frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION);
 
 		/* check if tablespace is closing after reading page */
 		if (space->is_stopping()) {
-			byte* frame = buf_block_get_frame(block);
 
 			if (kv == 0 &&
 				fil_crypt_is_page_uninitialized(frame, zip_size)) {
@@ -1993,9 +2005,6 @@ fil_crypt_rotate_page(
 					FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID,
 					space_id, MLOG_4BYTES, &mtr);
 
-				/* update block */
-				block->page.key_version = key_state->key_version;
-
 				/* statistics */
 				state->crypt_stat.pages_modified++;
 			} else {
diff --git a/storage/xtradb/fil/fil0fil.cc b/storage/xtradb/fil/fil0fil.cc
index 8385969117d..6c097bb40d7 100644
--- a/storage/xtradb/fil/fil0fil.cc
+++ b/storage/xtradb/fil/fil0fil.cc
@@ -6363,6 +6363,8 @@ fil_aio_wait(
 	mutex_enter(&fil_system->mutex);
 
 	fil_node_complete_io(fil_node, fil_system, type);
+	ulint purpose = fil_node->space->purpose;
+	space_id = fil_node->space->id;
 
 	mutex_exit(&fil_system->mutex);
 
@@ -6374,16 +6376,26 @@ fil_aio_wait(
 	deadlocks in the i/o system. We keep tablespace 0 data files always
 	open, and use a special i/o thread to serve insert buffer requests. */
 
-	if (fil_node->space->purpose == FIL_TABLESPACE) {
+	if (purpose == FIL_TABLESPACE) {
 		srv_set_io_thread_op_info(segment, "complete io for buf page");
 		buf_page_t* bpage = static_cast<buf_page_t*>(message);
+		ulint offset = bpage ? bpage->offset : ULINT_UNDEFINED;
 		dberr_t err = buf_page_io_complete(bpage);
 
 		if (err != DB_SUCCESS) {
+			/* In crash recovery set log corruption on
+			and produce only an error to fail InnoDB startup. */
+			if (recv_recovery_is_on()) {
+				recv_sys->found_corrupt_log = true;
+			}
+
 			ib_logf(IB_LOG_LEVEL_ERROR,
-				"ibuf write operation failed for tablespace "
-				"%u offset %u error=%d.",
-				bpage->space, bpage->offset, err);
+				"%s operation failed for tablespace %s"
+				" offset " ULINTPF " error %s",
+				type == OS_FILE_WRITE ? "Write" : "Read",
+				fil_node->name,
+				offset,
+				ut_strerr(err));
 		}
 	} else {
 		srv_set_io_thread_op_info(segment, "complete io for log");
diff --git a/storage/xtradb/handler/ha_innodb.cc b/storage/xtradb/handler/ha_innodb.cc
index 3130dc980d2..5708191303c 100644
--- a/storage/xtradb/handler/ha_innodb.cc
+++ b/storage/xtradb/handler/ha_innodb.cc
@@ -6462,7 +6462,7 @@ ha_innobase::open(
 	ib_table->thd = (void*)thd;
 
 	/* No point to init any statistics if tablespace is still encrypted. */
-	if (!ib_table->is_encrypted) {
+	if (!ib_table->file_unreadable) {
 		dict_stats_init(ib_table);
 	} else {
 		ib_table->stat_initialized = 1;
@@ -6470,7 +6470,8 @@ ha_innobase::open(
 
 	MONITOR_INC(MONITOR_TABLE_OPEN);
 
-	bool	no_tablespace;
+	bool	no_tablespace = false;
+	bool	ibd_missing = false;
 
 	if (dict_table_is_discarded(ib_table)) {
 
@@ -6485,7 +6486,8 @@ ha_innobase::open(
 
 		no_tablespace = false;
 
-	} else if (ib_table->ibd_file_missing) {
+	} else if (ib_table->file_unreadable &&
+		   fil_space_get(ib_table->space) == NULL) {
 
 		ib_senderrf(
 			thd, IB_LOG_LEVEL_WARN,
@@ -6495,11 +6497,11 @@ ha_innobase::open(
 		file, best to play it safe. */
 
 		no_tablespace = true;
-	} else if (ib_table->is_encrypted) {
+		ibd_missing = true;
+	} else if (ib_table->file_unreadable) {
 		/* This means that tablespace was found but we could not
 		decrypt encrypted page. */
 		no_tablespace = true;
-		ib_table->ibd_file_missing = true;
 	} else {
 		no_tablespace = false;
 	}
@@ -6512,7 +6514,8 @@ ha_innobase::open(
 		/* If table has no talespace but it has crypt data, check
 		is tablespace made unaccessible because encryption service
 		or used key_id is not available. */
-		if (ib_table) {
+		if (ib_table && !ibd_missing) {
+			bool warning_pushed = false;
 			fil_space_crypt_t* crypt_data = ib_table->crypt_data;
 
 			if (crypt_data && crypt_data->should_encrypt()) {
@@ -6525,8 +6528,14 @@ ha_innobase::open(
 						" Can't continue reading table.",
 						ib_table->name, crypt_data->key_id);
 					ret_err = HA_ERR_DECRYPTION_FAILED;
+					warning_pushed = true;
 				}
-			} else if (ib_table->is_encrypted) {
+			}
+
+			/* If table is marked as encrypted then we push
+			warning if it has not been already done as used
+			key_id might be found but it is incorrect. */
+			if (ib_table->file_unreadable && !warning_pushed) {
 				push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
 					HA_ERR_DECRYPTION_FAILED,
 					"Table %s is encrypted but encryption service or"
@@ -6668,7 +6677,7 @@ ha_innobase::open(
 
 	/* Only if the table has an AUTOINC column. */
 	if (prebuilt->table != NULL
-	    && !prebuilt->table->ibd_file_missing
+	    && !prebuilt->table->file_unreadable
 	    && table->found_next_number_field != NULL) {
 		dict_table_autoinc_lock(prebuilt->table);
 
@@ -8655,7 +8664,7 @@ ha_innobase::write_row(
 
 	if (share->ib_table != prebuilt->table) {
 		fprintf(stderr,
-			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %lu.",
+			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %d.",
 			share->ib_table, prebuilt->table, prebuilt->table->name, prebuilt->table->is_corrupt);
 	}
 
@@ -9002,7 +9011,7 @@ ha_innobase::write_row(
 
 	if (share->ib_table != prebuilt->table) {
 		fprintf(stderr,
-			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %lu.",
+			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %d.",
 			share->ib_table, prebuilt->table, prebuilt->table->name, prebuilt->table->is_corrupt);
 	}
 
@@ -9422,7 +9431,7 @@ ha_innobase::update_row(
 
 	if (share->ib_table != prebuilt->table) {
 		fprintf(stderr,
-			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %lu.",
+			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %d.",
 			share->ib_table, prebuilt->table, prebuilt->table->name, prebuilt->table->is_corrupt);
 	}
 
@@ -9537,7 +9546,7 @@ ha_innobase::update_row(
 
 	if (share->ib_table != prebuilt->table) {
 		fprintf(stderr,
-			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %lu.",
+			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %d.",
 			share->ib_table, prebuilt->table, prebuilt->table->name, prebuilt->table->is_corrupt);
 	}
 
@@ -10239,6 +10248,15 @@ ha_innobase::general_fetch(
 
 	ut_a(prebuilt->trx == thd_to_trx(user_thd));
 
+	if (prebuilt->table->corrupted) {
+		DBUG_RETURN(HA_ERR_CRASHED);
+	}
+
+	if (prebuilt->table->file_unreadable &&
+	    fil_space_get(prebuilt->table->space) != NULL) {
+		DBUG_RETURN(HA_ERR_DECRYPTION_FAILED);
+	}
+
 	innobase_srv_conc_enter_innodb(prebuilt->trx);
 
 	ret = row_search_for_mysql(
@@ -12979,7 +12997,7 @@ ha_innobase::discard_or_import_tablespace(
 		user may want to set the DISCARD flag in order to IMPORT
 		a new tablespace. */
 
-		if (dict_table->ibd_file_missing) {
+		if (dict_table->file_unreadable) {
 			ib_senderrf(
 				prebuilt->trx->mysql_thd,
 				IB_LOG_LEVEL_WARN, ER_TABLESPACE_MISSING,
@@ -12989,7 +13007,7 @@ ha_innobase::discard_or_import_tablespace(
 		err = row_discard_tablespace_for_mysql(
 			dict_table->name, prebuilt->trx);
 
-	} else if (!dict_table->ibd_file_missing) {
+	} else if (!dict_table->file_unreadable) {
 		/* Commit the transaction in order to
 		release the table lock. */
 		trx_commit_for_mysql(prebuilt->trx);
@@ -13068,7 +13086,7 @@ ha_innobase::truncate()
 
 	if (share->ib_table != prebuilt->table) {
 		fprintf(stderr,
-			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %lu.",
+			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %d.",
 			share->ib_table, prebuilt->table, prebuilt->table->name, prebuilt->table->is_corrupt);
 	}
 
@@ -13089,7 +13107,7 @@ ha_innobase::truncate()
 
 	if (share->ib_table != prebuilt->table) {
 		fprintf(stderr,
-			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %lu.",
+			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %d.",
 			share->ib_table, prebuilt->table, prebuilt->table->name, prebuilt->table->is_corrupt);
 	}
 
@@ -14488,7 +14506,7 @@ ha_innobase::analyze(
 
 	if (share->ib_table != prebuilt->table) {
 		fprintf(stderr,
-			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %lu.",
+			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %d.",
 			share->ib_table, prebuilt->table, prebuilt->table->name, prebuilt->table->is_corrupt);
 	}
 
@@ -14504,7 +14522,7 @@ ha_innobase::analyze(
 
 	if (share->ib_table != prebuilt->table) {
 		fprintf(stderr,
-			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %lu.",
+			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %d.",
 			share->ib_table, prebuilt->table, prebuilt->table->name, prebuilt->table->is_corrupt);
 	}
 
@@ -14614,7 +14632,8 @@ ha_innobase::check(
 
 		DBUG_RETURN(HA_ADMIN_CORRUPT);
 
-	} else if (prebuilt->table->ibd_file_missing) {
+	} else if (prebuilt->table->file_unreadable &&
+		   fil_space_get(prebuilt->table->space) == NULL) {
 
 		ib_senderrf(
 			thd, IB_LOG_LEVEL_ERROR,
@@ -15817,7 +15836,7 @@ ha_innobase::transactional_table_lock(
 
 	if (share->ib_table != prebuilt->table) {
 		fprintf(stderr,
-			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %lu.",
+			"InnoDB: Warning: share->ib_table %p prebuilt->table %p table %s is_corrupt %d.",
 			share->ib_table, prebuilt->table, prebuilt->table->name, prebuilt->table->is_corrupt);
 	}
 
@@ -15834,7 +15853,7 @@ ha_innobase::transactional_table_lock(
 				ER_TABLESPACE_DISCARDED,
 				table->s->table_name.str);
 
-		} else if (prebuilt->table->ibd_file_missing) {
+		} else if (prebuilt->table->file_unreadable) {
 
 			ib_senderrf(
 				thd, IB_LOG_LEVEL_ERROR,
diff --git a/storage/xtradb/handler/handler0alter.cc b/storage/xtradb/handler/handler0alter.cc
index 5654c23b941..9d740b74169 100644
--- a/storage/xtradb/handler/handler0alter.cc
+++ b/storage/xtradb/handler/handler0alter.cc
@@ -3138,7 +3138,7 @@ prepare_inplace_alter_table_dict(
 		clustered index of the old table, later. */
 		if (new_clustered
 		    || !ctx->online
-		    || user_table->ibd_file_missing
+		    || user_table->file_unreadable
 		    || dict_table_is_discarded(user_table)) {
 			/* No need to allocate a modification log. */
 			ut_ad(!ctx->add_index[a]->online_log);
@@ -3558,6 +3558,33 @@ ha_innobase::prepare_inplace_alter_table(
 		goto func_exit;
 	}
 
+	indexed_table = prebuilt->table;
+
+	if (indexed_table->file_unreadable &&
+	    fil_space_get(indexed_table->space) != NULL) {
+		String str;
+		const char* engine= table_type();
+		push_warning_printf(user_thd, Sql_condition::WARN_LEVEL_WARN,
+			HA_ERR_DECRYPTION_FAILED,
+			"Table %s is encrypted but encryption service or"
+			" used key_id is not available. "
+			" Can't continue reading table.",
+			indexed_table->name);
+		get_error_message(HA_ERR_DECRYPTION_FAILED, &str);
+		my_error(ER_GET_ERRMSG, MYF(0), HA_ERR_DECRYPTION_FAILED, str.c_ptr(), engine);
+
+		DBUG_RETURN(true);
+	}
+
+	if (indexed_table->corrupted
+	    || dict_table_get_first_index(indexed_table) == NULL
+	    || dict_index_is_corrupted(
+		    dict_table_get_first_index(indexed_table))) {
+		/* The clustered index is corrupted. */
+		my_error(ER_CHECK_NO_SUCH_TABLE, MYF(0));
+		DBUG_RETURN(true);
+	}
+
 	if (ha_alter_info->handler_flags
 	    & Alter_inplace_info::CHANGE_CREATE_OPTION) {
 		/* Check engine specific table options */
@@ -4210,7 +4237,7 @@ ha_innobase::inplace_alter_table(
 	DBUG_ASSERT(ctx->trx);
 	DBUG_ASSERT(ctx->prebuilt == prebuilt);
 
-	if (prebuilt->table->ibd_file_missing
+	if (prebuilt->table->file_unreadable
 	    || dict_table_is_discarded(prebuilt->table)) {
 		goto all_done;
 	}
@@ -5232,7 +5259,7 @@ commit_try_rebuild(
 	/* The new table must inherit the flag from the
 	"parent" table. */
 	if (dict_table_is_discarded(user_table)) {
-		rebuilt_table->ibd_file_missing = true;
+		rebuilt_table->file_unreadable = true;
 		rebuilt_table->flags2 |= DICT_TF2_DISCARDED;
 	}
 
@@ -5764,9 +5791,9 @@ ha_innobase::commit_inplace_alter_table(
 	if (ha_alter_info->group_commit_ctx) {
 		ctx_array = ha_alter_info->group_commit_ctx;
 	} else {
-	ctx_single[0] = ctx0;
-	ctx_single[1] = NULL;
-	ctx_array = ctx_single;
+		ctx_single[0] = ctx0;
+		ctx_single[1] = NULL;
+		ctx_array = ctx_single;
 	}
 
 	DBUG_ASSERT(ctx0 == ctx_array[0]);
@@ -5795,6 +5822,19 @@ ha_innobase::commit_inplace_alter_table(
 			= static_cast<ha_innobase_inplace_ctx*>(*pctx);
 		DBUG_ASSERT(ctx->prebuilt->trx == prebuilt->trx);
 
+		/* If decryption failed for old table or new table
+		fail here. */
+		if ((ctx->old_table->file_unreadable &&
+		     fil_space_get(ctx->old_table->space) != NULL)||
+		    (ctx->new_table->file_unreadable &&
+		     fil_space_get(ctx->new_table->space) != NULL)) {
+			String str;
+			const char* engine= table_type();
+			get_error_message(HA_ERR_DECRYPTION_FAILED, &str);
+			my_error(ER_GET_ERRMSG, MYF(0), HA_ERR_DECRYPTION_FAILED, str.c_ptr(), engine);
+			DBUG_RETURN(true);
+		}
+
 		/* Exclusively lock the table, to ensure that no other
 		transaction is holding locks on the table while we
 		change the table definition. The MySQL meta-data lock
diff --git a/storage/xtradb/include/btr0btr.ic b/storage/xtradb/include/btr0btr.ic
index 62a24873482..0f5f025d6a3 100644
--- a/storage/xtradb/include/btr0btr.ic
+++ b/storage/xtradb/include/btr0btr.ic
@@ -61,7 +61,7 @@ btr_block_get_func(
 
 	if (err == DB_DECRYPTION_FAILED) {
 		if (index && index->table) {
-			index->table->is_encrypted = true;
+			index->table->file_unreadable = true;
 		}
 	}
 
diff --git a/storage/xtradb/include/buf0buf.h b/storage/xtradb/include/buf0buf.h
index f232aed30c0..155556cd37c 100644
--- a/storage/xtradb/include/buf0buf.h
+++ b/storage/xtradb/include/buf0buf.h
@@ -1670,7 +1670,6 @@ struct buf_page_t{
 					if written again we check is TRIM
 					operation needed. */
 
-	unsigned        key_version;	/*!< key version for this block */
 	bool            encrypted;	/*!< page is still encrypted */
 
 	ulint           real_size;	/*!< Real size of the page
diff --git a/storage/xtradb/include/buf0rea.h b/storage/xtradb/include/buf0rea.h
index 7fd82a41762..6b9b2ccce75 100644
--- a/storage/xtradb/include/buf0rea.h
+++ b/storage/xtradb/include/buf0rea.h
@@ -58,13 +58,9 @@ buffer buf_pool if it is not already there. Sets the io_fix flag and sets
 an exclusive lock on the buffer frame. The flag is cleared and the x-lock
 released by the i/o-handler thread.
 @param[in]	space		Tablespace id
- at param[in]	offset		Page number
- at return DB_SUCCESS if page has been read and is not corrupted,
-DB_PAGE_CORRUPTED if page based on checksum check is corrupted,
-DB_DECRYPTION_FAILED if page post encryption checksum matches but
-after decryption normal page checksum does not match.*/
+ at param[in]	offset		Page number */
 UNIV_INTERN
-dberr_t
+void
 buf_read_page_async(
 	ulint	space,
 	ulint	offset);
diff --git a/storage/xtradb/include/dict0dict.h b/storage/xtradb/include/dict0dict.h
index 1622b927a76..7b0c9f4c465 100644
--- a/storage/xtradb/include/dict0dict.h
+++ b/storage/xtradb/include/dict0dict.h
@@ -1820,6 +1820,15 @@ dict_set_corrupted_by_space(
 /*========================*/
 	ulint		space_id);	/*!< in: space ID */
 
+/**********************************************************************//**
+Flags a table with specified space_id encrypted in the data dictionary
+cache
+ at param[in]	space_id	Tablespace id */
+UNIV_INTERN
+void
+dict_set_encrypted_by_space(
+	ulint	space_id);
+
 /********************************************************************//**
 Validate the table flags.
 @return	true if valid. */
diff --git a/storage/xtradb/include/dict0mem.h b/storage/xtradb/include/dict0mem.h
index 96c85cd8a99..9b347ff5741 100644
--- a/storage/xtradb/include/dict0mem.h
+++ b/storage/xtradb/include/dict0mem.h
@@ -2,7 +2,7 @@
 
 Copyright (c) 1996, 2016, Oracle and/or its affiliates. All Rights Reserved.
 Copyright (c) 2012, Facebook Inc.
-Copyright (c) 2013, 2016, MariaDB Corporation.
+Copyright (c) 2013, 2017, MariaDB Corporation.
 
 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
@@ -1062,11 +1062,13 @@ struct dict_table_t{
 				table is placed */
 	unsigned	flags:DICT_TF_BITS;	/*!< DICT_TF_... */
 	unsigned	flags2:DICT_TF2_BITS;	/*!< DICT_TF2_... */
-	unsigned	ibd_file_missing:1;
-				/*!< TRUE if this is in a single-table
-				tablespace and the .ibd file is missing; then
-				we must return in ha_innodb.cc an error if the
-				user tries to query such an orphaned table */
+	unsigned	file_unreadable:1;
+				/*!< true if this is in a single-table
+				tablespace and the .ibd file is missing
+				or page decryption failed and page is
+				corrupted; then we must return in
+				ha_innodb.cc an error if the
+				user tries to query such table */
 	unsigned	cached:1;/*!< TRUE if the table object has been added
 				to the dictionary cache */
 	unsigned	to_be_dropped:1;
@@ -1364,8 +1366,8 @@ struct dict_table_t{
 	UT_LIST_BASE_NODE_T(lock_t)
 			locks;	/*!< list of locks on the table; protected
 				by lock_sys->mutex */
-	ibool		is_corrupt;
-	ibool		is_encrypted;
+	bool		is_corrupt;
+
 #endif /* !UNIV_HOTBACKUP */
 
 #ifdef UNIV_DEBUG
diff --git a/storage/xtradb/include/fil0crypt.h b/storage/xtradb/include/fil0crypt.h
index cfc2d850883..4f476eca449 100644
--- a/storage/xtradb/include/fil0crypt.h
+++ b/storage/xtradb/include/fil0crypt.h
@@ -117,10 +117,8 @@ struct fil_space_crypt_t : st_encryption_scheme
 		min_key_version(new_min_key_version),
 		page0_offset(0),
 		encryption(new_encryption),
-		key_found(),
 		rotate_state()
 	{
-		key_found = new_min_key_version;
 		key_id = new_key_id;
 		my_random_bytes(iv, sizeof(iv));
 		mutex_create(fil_crypt_data_mutex_key,
@@ -136,6 +134,8 @@ struct fil_space_crypt_t : st_encryption_scheme
 			type = CRYPT_SCHEME_1;
 			min_key_version = key_get_latest_version();
 		}
+
+		key_found = min_key_version;
 	}
 
 	/** Destructor */
@@ -295,16 +295,18 @@ fil_space_destroy_crypt_data(
 
 /******************************************************************
 Parse a MLOG_FILE_WRITE_CRYPT_DATA log entry
- at param[in]	ptr		Log entry start
+ at param[in,out]	ptr		Log entry start
 @param[in]	end_ptr		Log entry end
 @param[in]	block		buffer block
+ at param[out]	err		DB_SUCCESS or DB_DECRYPTION_FAILED
 @return position on log buffer */
 UNIV_INTERN
-const byte*
+byte*
 fil_parse_write_crypt_data(
-	const byte*		ptr,
+	byte*			ptr,
 	const byte*		end_ptr,
-	const buf_block_t*	block)
+	const buf_block_t*	block,
+	dberr_t*		err)
 	MY_ATTRIBUTE((warn_unused_result));
 
 /******************************************************************
diff --git a/storage/xtradb/include/log0recv.h b/storage/xtradb/include/log0recv.h
index e7b6a937f01..8bbff97d7d0 100644
--- a/storage/xtradb/include/log0recv.h
+++ b/storage/xtradb/include/log0recv.h
@@ -303,9 +303,10 @@ recv_sys_var_init(void);
 #endif /* !UNIV_HOTBACKUP */
 /** Apply the hash table of stored log records to persistent data pages.
 @param[in]	last_batch	whether the change buffer merge will be
-				performed as part of the operation */
+				performed as part of the operation
+ at return DB_SUCCESS or DB_DECRYPTION_FAILED */
 UNIV_INTERN
-void
+dberr_t
 recv_apply_hashed_log_recs(bool last_batch);
 #ifdef UNIV_HOTBACKUP
 /*******************************************************************//**
diff --git a/storage/xtradb/log/log0recv.cc b/storage/xtradb/log/log0recv.cc
index 6b7c8d77824..0f99ba59512 100644
--- a/storage/xtradb/log/log0recv.cc
+++ b/storage/xtradb/log/log0recv.cc
@@ -1383,7 +1383,12 @@ recv_parse_or_apply_log_rec_body(
 		}
 		break;
 	case MLOG_FILE_WRITE_CRYPT_DATA:
-		ptr = const_cast<byte*>(fil_parse_write_crypt_data(ptr, end_ptr, block));
+		dberr_t err;
+		ptr = const_cast<byte*>(fil_parse_write_crypt_data(ptr, end_ptr, block, &err));
+
+		if (err != DB_SUCCESS) {
+			recv_sys->found_corrupt_log = TRUE;
+		}
 		break;
 	default:
 		ptr = NULL;
@@ -1856,9 +1861,10 @@ recv_read_in_area(
 
 /** Apply the hash table of stored log records to persistent data pages.
 @param[in]	last_batch	whether the change buffer merge will be
-				performed as part of the operation */
+				performed as part of the operation
+ at return DB_SUCCESS or DB_DECRYPTION_FAILED */
 UNIV_INTERN
-void
+dberr_t
 recv_apply_hashed_log_recs(bool last_batch)
 {
 	for (;;) {
@@ -1868,6 +1874,11 @@ recv_apply_hashed_log_recs(bool last_batch)
 			break;
 		}
 
+		if (recv_sys->found_corrupt_log) {
+			mutex_exit(&recv_sys->mutex);
+			return(DB_DECRYPTION_FAILED);
+		}
+
 		mutex_exit(&recv_sys->mutex);
 		os_thread_sleep(500000);
 	}
@@ -1932,6 +1943,10 @@ recv_apply_hashed_log_recs(bool last_batch)
 
 		mutex_exit(&(recv_sys->mutex));
 
+		if (recv_sys->found_corrupt_log) {
+			return(DB_DECRYPTION_FAILED);
+		}
+
 		os_thread_sleep(500000);
 
 		mutex_enter(&(recv_sys->mutex));
@@ -1978,6 +1993,8 @@ recv_apply_hashed_log_recs(bool last_batch)
 	recv_sys_empty_hash();
 
 	mutex_exit(&recv_sys->mutex);
+
+	return (DB_SUCCESS);
 }
 #else /* !UNIV_HOTBACKUP */
 /*******************************************************************//**
diff --git a/storage/xtradb/row/row0import.cc b/storage/xtradb/row/row0import.cc
index 6dc01907710..61fd55baf3f 100644
--- a/storage/xtradb/row/row0import.cc
+++ b/storage/xtradb/row/row0import.cc
@@ -2207,7 +2207,7 @@ row_import_discard_changes(
 		index->space = FIL_NULL;
 	}
 
-	table->ibd_file_missing = TRUE;
+	table->file_unreadable = true;
 
 	fil_close_tablespace(trx, table->space);
 }
@@ -3453,7 +3453,7 @@ row_import_for_mysql(
 
 	ut_a(table->space);
 	ut_ad(prebuilt->trx);
-	ut_a(table->ibd_file_missing);
+	ut_a(table->file_unreadable);
 
 	trx_start_if_not_started(prebuilt->trx);
 
@@ -3758,7 +3758,7 @@ row_import_for_mysql(
 		return(row_import_error(prebuilt, trx, err));
 	}
 
-	table->ibd_file_missing = false;
+	table->file_unreadable = false;
 	table->flags2 &= ~DICT_TF2_DISCARDED;
 
 	if (autoinc != 0) {
diff --git a/storage/xtradb/row/row0ins.cc b/storage/xtradb/row/row0ins.cc
index 8dbf093a17d..f4f96d32c50 100644
--- a/storage/xtradb/row/row0ins.cc
+++ b/storage/xtradb/row/row0ins.cc
@@ -1518,7 +1518,7 @@ row_ins_check_foreign_constraint(
 	}
 
 	if (check_table == NULL
-	    || check_table->ibd_file_missing
+	    || check_table->file_unreadable
 	    || check_index == NULL) {
 
 		if (!srv_read_only_mode && check_ref) {
@@ -2410,8 +2410,7 @@ row_ins_clust_index_entry_low(
 				    &cursor, 0, __FILE__, __LINE__, &mtr);
 
 	if (err != DB_SUCCESS) {
-		index->table->is_encrypted = true;
-		index->table->ibd_file_missing = true;
+		index->table->file_unreadable = true;
 		mtr_commit(&mtr);
 		goto func_exit;
 	}
@@ -2771,7 +2770,7 @@ row_ins_sec_index_entry_low(
 				" used key_id is not available. "
 				" Can't continue reading table.",
 				index->table->name);
-			index->table->is_encrypted = true;
+			index->table->file_unreadable = true;
 		}
 		goto func_exit;
 	}
diff --git a/storage/xtradb/row/row0merge.cc b/storage/xtradb/row/row0merge.cc
index 8752cf489c9..3844205d500 100644
--- a/storage/xtradb/row/row0merge.cc
+++ b/storage/xtradb/row/row0merge.cc
@@ -1,7 +1,7 @@
 /*****************************************************************************
 
 Copyright (c) 2005, 2016, Oracle and/or its affiliates. All Rights Reserved.
-Copyright (c) 2017, MariaDB Corporation.
+Copyright (c) 2015, 2017, MariaDB Corporation.
 
 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
@@ -1624,7 +1624,8 @@ row_merge_read_clustered_index(
 		page_cur_t*	cur	= btr_pcur_get_page_cur(&pcur);
 
 		/* Do not continue if table pages are still encrypted */
-		if (old_table->is_encrypted || new_table->is_encrypted) {
+		if (old_table->file_unreadable ||
+		    new_table->file_unreadable) {
 			err = DB_DECRYPTION_FAILED;
 			trx->error_key_num = 0;
 			goto func_exit;
@@ -3694,7 +3695,7 @@ row_merge_rename_tables_dict(
 	table is in a non-system tablespace where space > 0. */
 	if (err == DB_SUCCESS
 	    && old_table->space != TRX_SYS_SPACE
-	    && !old_table->ibd_file_missing) {
+	    && fil_space_get(old_table->space) != NULL) {
 		/* Make pathname to update SYS_DATAFILES. */
 		char* tmp_path = row_make_new_pathname(old_table, tmp_name);
 
@@ -4004,6 +4005,7 @@ row_merge_build_indexes(
 			os_mem_alloc_large(&block_size));
 
 		if (crypt_block == NULL) {
+			fil_space_release(space);
 			DBUG_RETURN(DB_OUT_OF_MEMORY);
 		}
 	} else {
@@ -4067,13 +4069,15 @@ row_merge_build_indexes(
 	pct_cost = COST_READ_CLUSTERED_INDEX * 100 / (total_static_cost + total_dynamic_cost);
 
 	/* Do not continue if we can't encrypt table pages */
-	if (old_table->is_encrypted || new_table->is_encrypted) {
+	if (old_table->file_unreadable ||
+	    new_table->file_unreadable) {
 		error = DB_DECRYPTION_FAILED;
 		ib_push_warning(trx->mysql_thd, DB_DECRYPTION_FAILED,
 			"Table %s is encrypted but encryption service or"
 			" used key_id is not available. "
 			" Can't continue reading table.",
-			old_table->is_encrypted ? old_table->name : new_table->name);
+			old_table->file_unreadable ? old_table->name :
+			    new_table->name);
 		goto func_exit;
 	}
 
diff --git a/storage/xtradb/row/row0mysql.cc b/storage/xtradb/row/row0mysql.cc
index 7c8636d354f..d1c19971435 100644
--- a/storage/xtradb/row/row0mysql.cc
+++ b/storage/xtradb/row/row0mysql.cc
@@ -1,7 +1,7 @@
 /*****************************************************************************
 
 Copyright (c) 2000, 2016, Oracle and/or its affiliates. All Rights Reserved.
-Copyright (c) 2017, MariaDB Corporation.
+Copyright (c) 2015, 2017, MariaDB Corporation.
 
 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
@@ -663,6 +663,7 @@ row_mysql_handle_errors(
 		abort();
 
 	case DB_CORRUPTION:
+	case DB_PAGE_CORRUPTED:
 		fputs("InnoDB: We detected index corruption"
 		      " in an InnoDB type table.\n"
 		      "InnoDB: You have to dump + drop + reimport"
@@ -1308,14 +1309,15 @@ row_insert_for_mysql(
 
 		return(DB_TABLESPACE_DELETED);
 
-	} else if (prebuilt->table->ibd_file_missing) {
+	} else if (prebuilt->table->file_unreadable &&
+		   fil_space_get(prebuilt->table->space) == NULL) {
 
 		ib_logf(IB_LOG_LEVEL_ERROR,
 			".ibd file is missing for table %s",
 			prebuilt->table->name);
 
 		return(DB_TABLESPACE_NOT_FOUND);
-	} else if (prebuilt->table->is_encrypted) {
+	} else if (prebuilt->table->file_unreadable) {
 		ib_push_warning(trx, DB_DECRYPTION_FAILED,
 			"Table %s in tablespace %lu encrypted."
 			"However key management plugin or used key_id is not found or"
@@ -1712,7 +1714,8 @@ row_update_for_mysql(
 	ut_ad(trx != NULL);
 	UT_NOT_USED(mysql_rec);
 
-	if (prebuilt->table->ibd_file_missing) {
+	if (prebuilt->table->file_unreadable &&
+	    fil_space_get(prebuilt->table->space) == NULL) {
 		ut_print_timestamp(stderr);
 		fprintf(stderr, "  InnoDB: Error:\n"
 			"InnoDB: MySQL is trying to use a table handle"
@@ -1727,13 +1730,13 @@ row_update_for_mysql(
 			"InnoDB: how you can resolve the problem.\n",
 			prebuilt->table->name);
 		return(DB_ERROR);
-	} else if (prebuilt->table->is_encrypted) {
+	} else if (prebuilt->table->file_unreadable) {
 		ib_push_warning(trx, DB_DECRYPTION_FAILED,
 			"Table %s in tablespace %lu encrypted."
 			"However key management plugin or used key_id is not found or"
 			" used encryption algorithm or method does not match.",
 			prebuilt->table->name, prebuilt->table->space);
-		return (DB_TABLE_NOT_FOUND);
+		return (DB_DECRYPTION_FAILED);
 	}
 
 	if (UNIV_UNLIKELY(prebuilt->magic_n != ROW_PREBUILT_ALLOCATED)) {
@@ -3128,7 +3131,7 @@ row_discard_tablespace(
 		/* All persistent operations successful, update the
 		data dictionary memory cache. */
 
-		table->ibd_file_missing = TRUE;
+		table->file_unreadable = true;
 
 		table->flags2 |= DICT_TF2_DISCARDED;
 
@@ -3185,7 +3188,8 @@ row_discard_tablespace_for_mysql(
 
 	if (table == 0) {
 		err = DB_TABLE_NOT_FOUND;
-	} else if (table->is_encrypted) {
+	} else if (table->file_unreadable &&
+		   fil_space_get(table->space) != NULL) {
 		err = DB_DECRYPTION_FAILED;
 	} else if (table->space == TRX_SYS_SPACE) {
 		char	table_name[MAX_FULL_NAME_LEN + 1];
@@ -3401,10 +3405,11 @@ row_truncate_table_for_mysql(
 
 	if (dict_table_is_discarded(table)) {
 		return(DB_TABLESPACE_DELETED);
-	} else if (table->is_encrypted) {
-		return(DB_DECRYPTION_FAILED);
-	} else if (table->ibd_file_missing) {
+	} else if (table->file_unreadable &&
+		   fil_space_get(table->space) == NULL) {
 		return(DB_TABLESPACE_NOT_FOUND);
+	} else if (table->file_unreadable) {
+		return(DB_DECRYPTION_FAILED);
 	}
 
 	trx_start_for_ddl(trx, TRX_DICT_OP_TABLE);
@@ -3570,7 +3575,7 @@ row_truncate_table_for_mysql(
 					"create a new tablespace",
 					table->name);
 
-				table->ibd_file_missing = 1;
+				table->file_unreadable = true;
 				err = DB_ERROR;
 				goto funct_exit;
 			}
@@ -3996,7 +4001,8 @@ row_drop_table_for_mysql(
 
 	/* If table is encrypted and table page encryption failed
 	return error. */
-	if (table->is_encrypted) {
+	if (table->file_unreadable &&
+	    fil_space_get(table->space) != NULL) {
 
 		if (table->can_be_evicted) {
 			dict_table_move_from_lru_to_non_lru(table);
@@ -4378,7 +4384,7 @@ row_drop_table_for_mysql(
 		from table->heap, which will be freed by
 		dict_table_remove_from_cache(table) below. */
 		space_id = table->space;
-		ibd_file_missing = table->ibd_file_missing;
+		ibd_file_missing = table->file_unreadable;
 
 		table_flags = table->flags;
 		is_temp = DICT_TF2_FLAG_IS_SET(table, DICT_TF2_TEMPORARY);
@@ -4389,7 +4395,7 @@ row_drop_table_for_mysql(
 		have a temp flag but not know the temp path */
 		ut_a(table->dir_path_of_temp_table == NULL || is_temp);
 		if (dict_table_is_discarded(table)
-		    || table->ibd_file_missing) {
+		    || table->file_unreadable) {
 			/* Do not attempt to drop known-to-be-missing
 			tablespaces. */
 			space_id = 0;
@@ -4801,7 +4807,7 @@ row_drop_database_for_mysql(
 					"'%s.frm' was lost.", table->name);
 			}
 
-			if (table->ibd_file_missing) {
+			if (table->file_unreadable) {
 				ib_logf(IB_LOG_LEVEL_WARN,
 					"Missing %s.ibd file for table %s.",
 					table->name, table->name);
@@ -5075,7 +5081,7 @@ row_rename_table_for_mysql(
 		      stderr);
 		goto funct_exit;
 
-	} else if (table->ibd_file_missing
+	} else if (table->file_unreadable
 		   && !dict_table_is_discarded(table)) {
 
 		err = DB_TABLE_NOT_FOUND;
@@ -5145,7 +5151,7 @@ row_rename_table_for_mysql(
 	which have space IDs > 0. */
 	if (err == DB_SUCCESS
 	    && table->space != TRX_SYS_SPACE
-	    && !table->ibd_file_missing) {
+	    && !table->file_unreadable) {
 		/* Make a new pathname to update SYS_DATAFILES. */
 		char*	new_path = row_make_new_pathname(table, new_name);
 
diff --git a/storage/xtradb/row/row0purge.cc b/storage/xtradb/row/row0purge.cc
index 35b3520749b..8a1dbd6f69f 100644
--- a/storage/xtradb/row/row0purge.cc
+++ b/storage/xtradb/row/row0purge.cc
@@ -777,7 +777,7 @@ row_purge_parse_undo_rec(
 		goto err_exit;
 	}
 
-	if (node->table->ibd_file_missing) {
+	if (node->table->file_unreadable) {
 		/* We skip purge of missing .ibd files */
 
 		dict_table_close(node->table, FALSE, FALSE);
diff --git a/storage/xtradb/row/row0sel.cc b/storage/xtradb/row/row0sel.cc
index 29569bb31e7..24c3f1234c5 100644
--- a/storage/xtradb/row/row0sel.cc
+++ b/storage/xtradb/row/row0sel.cc
@@ -62,6 +62,7 @@ Created 12/19/1997 Heikki Tuuri
 #include "srv0start.h"
 #include "m_string.h" /* for my_sys.h */
 #include "my_sys.h" /* DEBUG_SYNC_C */
+#include "fil0fil.h"
 
 #include "my_compare.h" /* enum icp_result */
 
@@ -3728,11 +3729,12 @@ row_search_for_mysql(
 
 		return(DB_TABLESPACE_DELETED);
 
-	} else if (prebuilt->table->ibd_file_missing) {
+	} else if (prebuilt->table->file_unreadable &&
+		fil_space_get(prebuilt->table->space) == NULL) {
 
 		return(DB_TABLESPACE_NOT_FOUND);
 
-	} else if (prebuilt->table->is_encrypted) {
+	} else if (prebuilt->table->file_unreadable) {
 
 		return(DB_DECRYPTION_FAILED);
 	} else if (!prebuilt->index_usable) {
@@ -4193,7 +4195,7 @@ row_search_for_mysql(
 					" used key_id is not available. "
 					" Can't continue reading table.",
 					prebuilt->table->name);
-				index->table->is_encrypted = true;
+				index->table->file_unreadable = true;
 			}
 			rec = NULL;
 			goto lock_wait_or_error;
@@ -4213,7 +4215,7 @@ row_search_for_mysql(
 
 	rec = btr_pcur_get_rec(pcur);
 
-	if (!rec) {
+	if (!rec || index->table->file_unreadable) {
 		err = DB_DECRYPTION_FAILED;
 		goto lock_wait_or_error;
 	}
diff --git a/storage/xtradb/row/row0uins.cc b/storage/xtradb/row/row0uins.cc
index 651042fb820..f14a4ef9bcf 100644
--- a/storage/xtradb/row/row0uins.cc
+++ b/storage/xtradb/row/row0uins.cc
@@ -320,7 +320,7 @@ row_undo_ins_parse_undo_rec(
 
 	/* Skip the UNDO if we can't find the table or the .ibd file. */
 	if (UNIV_UNLIKELY(node->table == NULL)) {
-	} else if (UNIV_UNLIKELY(node->table->ibd_file_missing)) {
+	} else if (UNIV_UNLIKELY(node->table->file_unreadable)) {
 close_table:
 		dict_table_close(node->table, dict_locked, FALSE);
 		node->table = NULL;
diff --git a/storage/xtradb/row/row0umod.cc b/storage/xtradb/row/row0umod.cc
index 19576d976bd..8deba4f00a5 100644
--- a/storage/xtradb/row/row0umod.cc
+++ b/storage/xtradb/row/row0umod.cc
@@ -1068,7 +1068,7 @@ row_undo_mod_parse_undo_rec(
 		return;
 	}
 
-	if (node->table->ibd_file_missing) {
+	if (node->table->file_unreadable) {
 		dict_table_close(node->table, dict_locked, FALSE);
 
 		/* We skip undo operations to missing .ibd files */
diff --git a/storage/xtradb/srv/srv0start.cc b/storage/xtradb/srv/srv0start.cc
index 18cde4e416f..7208d7b7218 100644
--- a/storage/xtradb/srv/srv0start.cc
+++ b/storage/xtradb/srv/srv0start.cc
@@ -2561,7 +2561,12 @@ innobase_start_or_create_for_mysql(void)
 			respective file pages, for the last batch of
 			recv_group_scan_log_recs(). */
 
-			recv_apply_hashed_log_recs(true);
+			err = recv_apply_hashed_log_recs(true);
+
+			if (err != DB_SUCCESS) {
+				return (err);
+			}
+
 			DBUG_PRINT("ib_log", ("apply completed"));
 		}
 
diff --git a/storage/xtradb/trx/trx0trx.cc b/storage/xtradb/trx/trx0trx.cc
index 439897a5b96..88bd6cfb689 100644
--- a/storage/xtradb/trx/trx0trx.cc
+++ b/storage/xtradb/trx/trx0trx.cc
@@ -661,7 +661,7 @@ trx_resurrect_table_locks(
 	     i != tables.end(); i++) {
 		if (dict_table_t* table = dict_table_open_on_id(
 			    *i, FALSE, DICT_TABLE_OP_LOAD_TABLESPACE)) {
-			if (table->ibd_file_missing
+			if (table->file_unreadable
 			    || dict_table_is_temporary(table)) {
 				mutex_enter(&dict_sys->mutex);
 				dict_table_close(table, TRUE, FALSE);
diff --git a/storage/xtradb/ut/ut0ut.cc b/storage/xtradb/ut/ut0ut.cc
index acedb56879a..fd52537ae11 100644
--- a/storage/xtradb/ut/ut0ut.cc
+++ b/storage/xtradb/ut/ut0ut.cc
@@ -852,6 +852,8 @@ ut_strerr(
 		return("BLOB record length is greater than 10%% of redo log");
 	case DB_DECRYPTION_FAILED:
 		return("Table is encrypted but decrypt failed.");
+	case DB_PAGE_CORRUPTED:
+		return("Page read from tablespace is corrupted.");
 
 	/* do not add default: in order to produce a warning if new code
 	is added to the enum but not added here */


More information about the commits mailing list