[Commits] 96e54280645: Resolved merge conflicts on buf0buf.cc and fil0crypt.cc

jan jan.lindstrom at mariadb.com
Tue Mar 21 14:56:56 EET 2017


revision-id: 96e54280645b41c6c36f09eba5ce4c73ab1a672e (mariadb-10.2.4-76-g96e54280645)
parent(s): 66a8ec161893feb537ba84d01dc7098e36516111
author: Jan Lindström
committer: Jan Lindström
timestamp: 2017-03-21 14:56:03 +0200
message:

Resolved merge conflicts on buf0buf.cc and fil0crypt.cc

Fixed compiler errors on buf0buf.cc, fil0fil.h and fil0fil.cc.
Last file not does not yet compile.

---
 storage/innobase/buf/buf0buf.cc    | 318 ++++++-------------------
 storage/innobase/fil/fil0crypt.cc  | 475 +++++++++++++++----------------------
 storage/innobase/fil/fil0fil.cc    | 117 ++++-----
 storage/innobase/include/fil0fil.h |  10 +-
 4 files changed, 314 insertions(+), 606 deletions(-)

diff --git a/storage/innobase/buf/buf0buf.cc b/storage/innobase/buf/buf0buf.cc
index 530ac4d7d0b..e941836fa50 100644
--- a/storage/innobase/buf/buf0buf.cc
+++ b/storage/innobase/buf/buf0buf.cc
@@ -777,6 +777,7 @@ buf_page_is_checksum_valid_none(
 			 << " lsn " << mach_read_from_4(read_buf
 							+ FIL_PAGE_LSN));
 	}
+#endif /* DBUG_OFF */
 
 #ifdef UNIV_INNOCHECKSUM
 	if (is_log_enabled
@@ -810,7 +811,7 @@ buf_page_is_corrupted(
 	bool			check_lsn,
 	const byte*		read_buf,
 	const page_size_t&	page_size,
-	const fil_space_t* 	space,
+	const fil_space_t* 	space
 #ifdef UNIV_INNOCHECKSUM
 	,uintmax_t		page_no,
 	bool			strict_check,
@@ -819,17 +820,12 @@ buf_page_is_corrupted(
 #endif /* UNIV_INNOCHECKSUM */
 )
 {
-	ut_ad(!space || page_size == page_size_t(space->flags));
-
 	ulint		checksum_field1;
 	ulint		checksum_field2;
-	bool		no_checksum = false;
 
 #ifndef UNIV_INNOCHECKSUM
 	DBUG_EXECUTE_IF("buf_page_import_corrupt_failure", return(true); );
 
-	ulint 		space_id = mach_read_from_4(
-		read_buf + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
 	ulint page_type = mach_read_from_2(
 		read_buf + FIL_PAGE_TYPE);
 
@@ -5844,9 +5840,11 @@ buf_page_check_corrupt(
 {
 	byte* dst_frame = (bpage->zip.data) ? bpage->zip.data :
 		((buf_block_t*) bpage)->frame;
-	ulint space_id = bpage->space;
+	ulint space_id = bpage->id.space();
 	fil_space_t* space = fil_space_acquire_silent(space_id);
+	const page_size_t page_size(space->flags);
 	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;
@@ -5863,12 +5861,13 @@ buf_page_check_corrupt(
 	still_encrypted = (crypt_data &&
 		crypt_data->type != CRYPT_SCHEME_UNENCRYPTED &&
 		!bpage->encrypted &&
-		fil_space_verify_crypt_checksum(dst_frame, zip_size,
-					space, bpage->offset));
+		fil_space_verify_crypt_checksum(dst_frame, page_size,
+			space, bpage->id.page_no()));
+
 	if (!still_encrypted) {
 		/* If traditional checksums match, we assume that page is
 		not anymore encrypted. */
-		corrupted = buf_page_is_corrupted(true, dst_frame, zip_size, space);
+		corrupted = buf_page_is_corrupted(true, dst_frame, page_size, space);
 
 		if (!corrupted) {
 			bpage->encrypted = false;
@@ -5878,69 +5877,37 @@ 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);
+		ib::error()
+			<< (page_type == FIL_PAGE_PAGE_COMPRESSED_ENCRYPTED ?
+				"Maybe corruption" : "Corruption")
+			<< ": Block in space_id " << space_id
+			<< " in file "
+			<< (space && space->name ? space->name : "NULL")
+			<< " corrupted.";
+
+		ib::error()
+			<< "Based on page type "
+			<< fil_get_page_type_name(page_type)
+			<< "(" << page_type << ").";
+
 	} else if (still_encrypted || (bpage->encrypted && corrupted)) {
 		bpage->encrypted = true;
-		corrupted = true;
+		err = DB_DECRYPTION_FAILED;
 
-<<<<<<< HEAD
-		if (corrupted) {
-			ib::error() << (page_compressed_encrypted ? "Maybe corruption" : "Corruption")
-				    <<  ": Block in space_id " << space_id
-				    << " in file " << (space ? space->name : "NULL")
-				    << " corrupted.";
-
-			ib::error() <<  "Page based on contents "
-				    << ((key_version == 0 && page_compressed_encrypted == false) ? "not" : "maybe")
-				    << " encrypted.";
-
-			if (stored_checksum != BUF_NO_CHECKSUM_MAGIC ||
-			    calculated_checksum != BUF_NO_CHECKSUM_MAGIC) {
-				ib::error() << "Page stored checksum " << stored_checksum
-					    << " but calculated checksum "
-					    << calculated_checksum << " .";
-			}
+		ib::error()
+			<< "Block in space_id " << space_id << " in file "
+			<< (space && space->name ? space->name : "NULL")
+			<< " encrypted.";
+
+		ib::error()
+			<< "However key management plugin or used key_version "
+			<< bpage->key_version << " is not found or"
+			<< " used encryption algorithm or method does not match.";
 
-			ib::error() << "Reason could be that key_version " << key_version
-				<< " in page or in crypt_data " << crypt_data
-				<< " could not be found.";
-			ib::error() << "Reason could be also that key management plugin is not found or"
-				" used encryption algorithm or method does not match.";
-			ib::error() << "Based on page page compressed"
-				    << page_compressed
-				    << ", compressed and encrypted "
-				    << page_compressed_encrypted << " .";
-		} else {
-			ib::error() << "Block in space_id "
-				    << space_id
-				    << " in file "
-				    << (space ? space->name : "NULL")
-				    << " encrypted.";
-			ib::error() << "However key management plugin or used key_id "
-				    << key_version
-				    << " is not found or"
-				    << " used encryption algorithm or method does not match.";
-			ib::error() << "Marking tablespace as missing. You may drop this table or"
-				    << " install correct key management plugin and key file.";
-=======
-		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"
-				" used encryption algorithm or method does not match.",
-				bpage->key_version);
 		if (space_id > TRX_SYS_SPACE) {
-			ib_logf(IB_LOG_LEVEL_ERROR,
-				"Marking tablespace as missing. You may drop this table or"
-				" install correct key management plugin and key file.");
->>>>>>> origin/10.1
+			ib::error()
+				<< "Marking tablespace as missing. You may drop this table or"
+				<< " install correct key management plugin and key file.";
 		}
 	}
 
@@ -5948,7 +5915,7 @@ buf_page_check_corrupt(
 		fil_space_release(space);
 	}
 
-	return corrupted;
+	return (err);
 }
 
 /********************************************************************//**
@@ -5984,29 +5951,12 @@ buf_page_io_complete(
 	if (io_type == BUF_IO_READ) {
 		ulint	read_page_no;
 		ulint	read_space_id;
-<<<<<<< HEAD
-		byte*	frame = NULL;
 
 		ut_ad(bpage->zip.data != NULL || ((buf_block_t*)bpage)->frame != NULL);
 
-		if (!buf_page_decrypt_after_read(bpage)) {
-			/* encryption error! */
-			if (bpage->size.is_compressed()) {
-				frame = bpage->zip.data;
-			} else {
-				frame = ((buf_block_t*) bpage)->frame;
-			}
-
-			ib::info() << "Page "
-				   << bpage->id
-				   << " encryption error key_version "
-				   << bpage->key_version;
-=======
-
 		buf_page_decrypt_after_read(bpage);
->>>>>>> origin/10.1
 
-		if (buf_page_get_zip_size(bpage)) {
+		if (bpage->size.is_compressed()) {
 			frame = bpage->zip.data;
 		} else {
 			frame = ((buf_block_t*) bpage)->frame;
@@ -6053,7 +6003,6 @@ buf_page_io_complete(
 			    && bpage->id.space() != read_space_id)
 			   || bpage->id.page_no() != read_page_no) {
 			/* We did not compare space_id to read_space_id
-<<<<<<< HEAD
 			in the system tablespace, because the field
 			was written as garbage before MySQL 4.1.1,
 			which did not support innodb_file_per_table. */
@@ -6064,39 +6013,39 @@ buf_page_io_complete(
 				<< ", should be " << bpage->id;
 		}
 
-		/* From version 3.23.38 up we store the page checksum
-		to the 4 first bytes of the page end lsn field */
-		if (buf_page_is_corrupted(
-			    true, frame, bpage->size,
-			    fsp_is_checksum_disabled(bpage->id.space()))) {
+		corrupted = buf_page_check_corrupt(bpage);
+
+database_corrupted:
 
+		if (corrupted) {
 			/* Not a real corruption if it was triggered by
 			error injection */
-			DBUG_EXECUTE_IF(
-				"buf_page_import_corrupt_failure",
-				if (bpage->id.space()
-				    > srv_undo_tablespaces_open
-				    && bpage->id.space() != SRV_TMP_SPACE_ID
-				    && buf_mark_space_corrupt(bpage)) {
-					ib::info() << "Simulated IMPORT "
-						"corruption";
+			DBUG_EXECUTE_IF("buf_page_is_corrupt_failure",
+				if (bpage->id.space() > TRX_SYS_SPACE
+					&& buf_mark_space_corrupt(bpage)) {
+					ib::info()
+						<< "Simulated page corruption";
 					return(true);
 				}
-				goto page_not_corrupt;);
-database_corrupted:
-			bool corrupted = buf_page_check_corrupt(bpage);
+				goto page_not_corrupt;
+			);
 
-			/* Compressed and encrypted pages are basically gibberish avoid
-			printing the contents. */
-			if (corrupted) {
+			if (!bpage->encrypted) {
+				fil_system_enter();
+				fil_space_t* space = fil_space_get_by_id(bpage->id.space());
+				fil_system_exit();
 
 				ib::error()
 					<< "Database page corruption on disk"
-					" or a failed file read of page "
-					<< bpage->id
+					" or a failed file read of tablespace "
+					<< (space->name ? space->name : "NULL")
+					<< " page " << bpage->id
 					<< ". You may have to recover from "
 					<< "a backup.";
 
+				buf_page_print(frame, bpage->size,
+					BUF_PAGE_PRINT_NO_CRASH);
+
 				ib::info()
 					<< "It is also possible that your"
 					" operating system has corrupted"
@@ -6109,78 +6058,6 @@ buf_page_io_complete(
 					" You can use CHECK TABLE to scan"
 					" your table for corruption. "
 					<< FORCE_RECOVERY_MSG;
-=======
-			if bpage->space == 0, because the field on the
-			page may contain garbage in MySQL < 4.1.1,
-			which only supported bpage->space == 0. */
-
-			ut_print_timestamp(stderr);
-			fprintf(stderr,
-				"  InnoDB: Error: space id and page n:o"
-				" stored in the page\n"
-				"InnoDB: read in are " ULINTPF ":" ULINTPF ","
-				" should be %u:%u!\n",
-				read_space_id,
-				read_page_no,
-				bpage->space,
-				bpage->offset);
-		}
-
-		corrupted = buf_page_check_corrupt(bpage);
-
-database_corrupted:
-
-		if (corrupted) {
-
-			/* Not a real corruption if it was triggered by
-			error injection */
-			DBUG_EXECUTE_IF("buf_page_is_corrupt_failure",
-				if (bpage->space > TRX_SYS_SPACE
-					&& buf_mark_space_corrupt(bpage)) {
-					ib_logf(IB_LOG_LEVEL_INFO,
-						"Simulated page corruption");
-					return(true);
-				}
-				goto page_not_corrupt;
-			);
-
-			if (!bpage->encrypted) {
-				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.");
-
-				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.");
->>>>>>> origin/10.1
 			}
 
 			if (srv_force_recovery < SRV_FORCE_IGNORE_CORRUPT) {
@@ -6191,14 +6068,9 @@ buf_page_io_complete(
 
 				if (bpage->id.space() > TRX_SYS_SPACE
 				    && buf_mark_space_corrupt(bpage)) {
-
 					return(false);
 				} else {
-<<<<<<< HEAD
-					corrupted = buf_page_check_corrupt(bpage);
-					ulint key_version = bpage->key_version;
-
-					if (corrupted) {
+					if (!bpage->encrypted) {
 						ib::fatal()
 							<< "Aborting because of a"
 							" corrupt database page in"
@@ -6206,35 +6078,22 @@ buf_page_io_complete(
 							" there was a failure in"
 							" tagging the tablespace "
 							" as corrupt.";
-=======
-					if (!bpage->encrypted) {
-						ib::fatal()
-							<< "Corrupt database page";
->>>>>>> origin/10.1
 					}
 
-					ib_push_warning((void *)NULL, DB_DECRYPTION_FAILED,
-						"Table in tablespace %u encrypted."
-						"However key management plugin or used key_id %u is not found or"
+					ib_push_warning(innobase_get_trx(), DB_DECRYPTION_FAILED,
+						"Table in tablespace %lu encrypted."
+						"However key management plugin or used key_id %lu is not found or"
 						" used encryption algorithm or method does not match."
 						" Can't continue opening the table.",
-<<<<<<< HEAD
-						bpage->id.space(), key_version);
-
-					buf_page_print(frame, bpage->size, BUF_PAGE_PRINT_NO_CRASH);
+						bpage->id.space(), bpage->key_version);
 
-					return (false);
-=======
-						bpage->space, bpage->key_version);
-
-					if (bpage->encrypted && bpage->space > TRX_SYS_SPACE) {
+					if (bpage->encrypted && bpage->id.space() > TRX_SYS_SPACE) {
 						buf_mark_space_corrupt(bpage);
 					} else {
 						ut_error;
 					}
 
 					return(false);
->>>>>>> origin/10.1
 				}
 			}
 		}
@@ -6248,7 +6107,6 @@ buf_page_io_complete(
 			recv_recover_page(TRUE, (buf_block_t*) bpage);
 		}
 
-<<<<<<< HEAD
 		/* If space is being truncated then avoid ibuf operation.
 		During re-init we have already freed ibuf entries. */
 		if (uncompressed
@@ -6260,34 +6118,21 @@ buf_page_io_complete(
 		    && fil_page_get_type(frame) == FIL_PAGE_INDEX
 		    && page_is_leaf(frame)) {
 
-		    	if (bpage && bpage->encrypted) {
-				fprintf(stderr,
-					"InnoDB: Warning: Table in tablespace %u encrypted."
-					"However key management plugin or used key_id %u is not found or"
-=======
-		if (uncompressed && !recv_no_ibuf_operations
-		    && fil_page_get_type(frame) == FIL_PAGE_INDEX
-		    && page_is_leaf(frame)) {
-
 			if (bpage && bpage->encrypted) {
-				ib_logf(IB_LOG_LEVEL_WARN,
-					"Table in tablespace %lu encrypted."
-					"However key management plugin or used key_version %u is not found or"
->>>>>>> origin/10.1
+				ib::warn()
+					<< "Table in tablespace "
+					<< bpage->id.space()
+					<< " encrypted. However key "
+					"management plugin or used "
+					<< "key_version " << bpage->key_version
+					<< "is not found or"
 					" used encryption algorithm or method does not match."
-					" Can't continue opening the table.\n",
-					bpage->id.space(), bpage->key_version);
+					" Can't continue opening the table.";
 			} else {
 
 				ibuf_merge_or_delete_for_page(
-<<<<<<< HEAD
 					(buf_block_t*) bpage, bpage->id,
 					&bpage->size, TRUE);
-=======
-					(buf_block_t*)bpage, bpage->space,
-					bpage->offset, buf_page_get_zip_size(bpage),
-					TRUE);
->>>>>>> origin/10.1
 			}
 
 		}
@@ -7572,6 +7417,7 @@ buf_page_encrypt_before_write(
 		return src_frame;
 	}
 
+	const page_size_t page_size(space->flags);
 	fil_space_crypt_t* crypt_data = space->crypt_data;
 	bool encrypted = true;
 
@@ -7592,7 +7438,7 @@ buf_page_encrypt_before_write(
 		encrypted = false;
 	}
 
-	bool page_compressed = fil_space_is_page_compressed(bpage->space);
+	bool page_compressed = fil_space_is_page_compressed(bpage->id.space());
 
 	if (!encrypted && !page_compressed) {
 		/* No need to encrypt or page compress the page */
@@ -7693,7 +7539,7 @@ buf_page_decrypt_after_read(buf_page_t* bpage)
 		return (true);
 	}
 
-	fil_space_t* space = fil_space_acquire(bpage->space);
+	fil_space_t* space = fil_space_acquire(bpage->id.space());
 	fil_space_crypt_t* crypt_data = space->crypt_data;
 
 	/* Page is encrypted if encryption information is found from
@@ -7732,7 +7578,7 @@ buf_page_decrypt_after_read(buf_page_t* bpage)
 			/* Verify encryption checksum before we even try to
 			decrypt. */
 			if (!fil_space_verify_crypt_checksum(dst_frame,
-					zip_size, NULL, bpage->offset)) {
+					size, NULL, bpage->id.page_no())) {
 				return (false);
 			}
 
@@ -7743,24 +7589,12 @@ buf_page_decrypt_after_read(buf_page_t* bpage)
 			fil_page_type_validate(dst_frame);
 #endif
 
-<<<<<<< HEAD
-			/* Calculate checksum before decrypt, this will be
-			used later to find out if incorrect key was used. */
-			if (!page_compressed_encrypted) {
-				bpage->calculated_checksum = fil_crypt_calculate_checksum(size, dst_frame);
-			}
-
-			/* decrypt using crypt_buf to dst_frame */
-			byte* res = fil_space_decrypt(bpage->id.space(),
-=======
 			/* decrypt using crypt_buf to dst_frame */
 			byte* res = fil_space_decrypt(space,
->>>>>>> origin/10.1
 						slot->crypt_buf,
 						dst_frame,
 						&bpage->encrypted);
 
-
 			if (!res) {
 				success = false;
 			}
diff --git a/storage/innobase/fil/fil0crypt.cc b/storage/innobase/fil/fil0crypt.cc
index 3ad84d204ac..0695dfef078 100644
--- a/storage/innobase/fil/fil0crypt.cc
+++ b/storage/innobase/fil/fil0crypt.cc
@@ -24,16 +24,14 @@ Modified           Jan Lindström jan.lindstrom at mariadb.com
 *******************************************************/
 
 #include "fil0fil.h"
-#include "mach0data.h"
-#include "page0size.h"
-#include "page0zip.h"
-#ifndef UNIV_INNOCHECKSUM
 #include "fil0crypt.h"
 #include "srv0srv.h"
 #include "srv0start.h"
+#include "mach0data.h"
 #include "log0recv.h"
 #include "mtr0mtr.h"
 #include "mtr0log.h"
+#include "page0zip.h"
 #include "ut0ut.h"
 #include "btr0scrub.h"
 #include "fsp0fsp.h"
@@ -46,6 +44,10 @@ static ib_mutex_t fil_crypt_key_mutex;
 
 static bool fil_crypt_threads_inited = false;
 
+#ifdef UNIV_PFS_MUTEX
+static mysql_pfs_key_t fil_crypt_key_mutex_key;
+#endif
+
 /** Is encryption enabled/disabled */
 UNIV_INTERN ulong srv_encrypt_tables = 0;
 
@@ -70,6 +72,10 @@ static os_event_t fil_crypt_throttle_sleep_event;
 /** Mutex for key rotation threads. */
 UNIV_INTERN ib_mutex_t fil_crypt_threads_mutex;
 
+#ifdef UNIV_PFS_MUTEX
+static mysql_pfs_key_t fil_crypt_threads_mutex_key;
+#endif
+
 /** Variable ensuring only 1 thread at time does initial conversion */
 static bool fil_crypt_start_converting = false;
 
@@ -88,6 +94,15 @@ extern uint srv_background_scrub_data_check_interval;
 static fil_crypt_stat_t crypt_stat;
 static ib_mutex_t crypt_stat_mutex;
 
+#ifdef UNIV_PFS_MUTEX
+static mysql_pfs_key_t fil_crypt_stat_mutex_key;
+
+/**
+ * key for crypt data mutex
+*/
+UNIV_INTERN mysql_pfs_key_t fil_crypt_data_mutex_key;
+#endif
+
 /** Is background scrubbing enabled, defined on btr0scrub.cc */
 extern my_bool srv_background_scrub_data_uncompressed;
 extern my_bool srv_background_scrub_data_compressed;
@@ -106,11 +121,14 @@ UNIV_INTERN
 void
 fil_space_crypt_init()
 {
-	mutex_create(LATCH_ID_FIL_CRYPT_MUTEX, &fil_crypt_key_mutex);
+	mutex_create(fil_crypt_key_mutex_key,
+		     &fil_crypt_key_mutex, SYNC_NO_ORDER_CHECK);
+
+	fil_crypt_throttle_sleep_event = os_event_create();
 
-	fil_crypt_throttle_sleep_event = os_event_create(0);
+	mutex_create(fil_crypt_stat_mutex_key,
+		     &crypt_stat_mutex, SYNC_NO_ORDER_CHECK);
 
-	mutex_create(LATCH_ID_FIL_CRYPT_STAT_MUTEX, &crypt_stat_mutex);
 	memset(&crypt_stat, 0, sizeof(crypt_stat));
 }
 
@@ -120,7 +138,8 @@ UNIV_INTERN
 void
 fil_space_crypt_cleanup()
 {
-	os_event_destroy(fil_crypt_throttle_sleep_event);
+	os_event_free(fil_crypt_throttle_sleep_event);
+	fil_crypt_throttle_sleep_event = NULL;
 	mutex_free(&fil_crypt_key_mutex);
 	mutex_free(&crypt_stat_mutex);
 }
@@ -203,8 +222,10 @@ fil_space_create_crypt_data(
 	uint			min_key_version,
 	uint			key_id)
 {
+	void* buf = mem_zalloc(sizeof(fil_space_crypt_t));
 	fil_space_crypt_t* crypt_data = NULL;
-	if (void* buf = ut_zalloc_nokey(sizeof(fil_space_crypt_t))) {
+
+	if (buf) {
 		crypt_data = new(buf)
 			fil_space_crypt_t(
 				type,
@@ -260,43 +281,60 @@ fil_space_merge_crypt_data(
 	mutex_exit(&dst->mutex);
 }
 
-/** Initialize encryption parameters from a tablespace header page.
- at param[in]	space	tablespace
- at param[in]	page	first page of the tablespace
- at return crypt data from page 0
- at retval	NULL	if not present or not valid */
+/******************************************************************
+Read crypt data from a page (0)
+ at param[in]	space		space_id
+ at param[in]	page		Page 0
+ at param[in]	offset		Offset to crypt data
+ at return crypt data from page 0 or NULL. */
 UNIV_INTERN
 fil_space_crypt_t*
-fil_space_read_crypt_data(const fil_space_t* space, const byte* page)
+fil_space_read_crypt_data(
+	ulint		space,
+	const byte*	page,
+	ulint		offset)
 {
-	ut_ad(!space->crypt_data);
-	const ulint offset = FSP_HEADER_OFFSET
-		+ fsp_header_get_encryption_offset(page_size_t(space->flags));
-
 	if (memcmp(page + offset, CRYPT_MAGIC, MAGIC_SZ) != 0) {
 		/* Crypt data is not stored. */
 		return NULL;
 	}
 
 	ulint type = mach_read_from_1(page + offset + MAGIC_SZ + 0);
-	ulint iv_length = mach_read_from_1(page + offset + MAGIC_SZ + 1);
 
-	if (!(type == CRYPT_SCHEME_UNENCRYPTED ||
-	      type == CRYPT_SCHEME_1)
-	    || iv_length != sizeof space->crypt_data->iv) {
-		ib::error() << "Found non sensible crypt scheme: "
-			    << type << "," << iv_length << " for space: "
-			    << space << " offset: "
-			    << offset << " bytes: ["
-			    << page[offset + 2 + MAGIC_SZ]
-			    << page[offset + 3 + MAGIC_SZ]
-			    << page[offset + 4 + MAGIC_SZ]
-			    << page[offset + 5 + MAGIC_SZ]
-			    << "].";
-		return NULL;
+	if (! (type == CRYPT_SCHEME_UNENCRYPTED ||
+	       type == CRYPT_SCHEME_1)) {
+
+		ib_logf(IB_LOG_LEVEL_ERROR,
+			"Found non sensible crypt scheme: " ULINTPF " for space " ULINTPF
+			" offset: " ULINTPF " bytes: "
+			"[ %.2x %.2x %.2x %.2x %.2x %.2x ].",
+			type, space, offset,
+			page[offset + 0 + MAGIC_SZ],
+			page[offset + 1 + MAGIC_SZ],
+			page[offset + 2 + MAGIC_SZ],
+			page[offset + 3 + MAGIC_SZ],
+			page[offset + 4 + MAGIC_SZ],
+			page[offset + 5 + MAGIC_SZ]);
+		ut_error;
 	}
 
 	fil_space_crypt_t* crypt_data;
+	ulint iv_length = mach_read_from_1(page + offset + MAGIC_SZ + 1);
+
+	if (! (iv_length == sizeof(crypt_data->iv))) {
+		ib_logf(IB_LOG_LEVEL_ERROR,
+			"Found non sensible iv length: %lu for space %lu "
+			" offset: %lu type: %lu bytes: "
+			"[ %.2x %.2x %.2x %.2x %.2x %.2x ].",
+			iv_length, space, offset, type,
+			page[offset + 0 + MAGIC_SZ],
+			page[offset + 1 + MAGIC_SZ],
+			page[offset + 2 + MAGIC_SZ],
+			page[offset + 3 + MAGIC_SZ],
+			page[offset + 4 + MAGIC_SZ],
+			page[offset + 5 + MAGIC_SZ]);
+		ut_error;
+	}
 
 	uint min_key_version = mach_read_from_4
 		(page + offset + MAGIC_SZ + 2 + iv_length);
@@ -327,30 +365,25 @@ fil_space_destroy_crypt_data(
 	fil_space_crypt_t **crypt_data)
 {
 	if (crypt_data != NULL && (*crypt_data) != NULL) {
-		mutex_enter(&fil_crypt_threads_mutex);
 		fil_space_crypt_t* c = *crypt_data;
+		c->~fil_space_crypt_t();
+		mem_free(c);
 		*crypt_data = NULL;
-		mutex_exit(&fil_crypt_threads_mutex);
-		if (c) {
-			c->~fil_space_crypt_t();
-			ut_free(c);
-		}
 	}
 }
 
 /******************************************************************
 Write crypt data to a page (0)
- at param[in]	space	tablespace
- at param[in,out]	page0	first page of the tablespace
- at param[in,out]	mtr	mini-transaction */
+ at param[in,out]	page0		Page 0 where to write
+ at param[in,out]	mtr		Minitransaction */
 UNIV_INTERN
 void
 fil_space_crypt_t::write_page0(
-	const fil_space_t*	space,
 	byte* 			page,
 	mtr_t*			mtr)
 {
-	ut_ad(this == space->crypt_data);
+	ulint space_id = mach_read_from_4(
+		page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID);
 	const uint len = sizeof(iv);
 	const ulint offset = FSP_HEADER_OFFSET
 		+ fsp_header_get_encryption_offset(page_size_t(space->flags));
@@ -380,7 +413,7 @@ fil_space_crypt_t::write_page0(
 			page,
 			MLOG_FILE_WRITE_CRYPT_DATA,
 			log_ptr, mtr);
-		mach_write_to_4(log_ptr, space->id);
+		mach_write_to_4(log_ptr, space_id);
 		log_ptr += 4;
 		mach_write_to_2(log_ptr, offset);
 		log_ptr += 2;
@@ -511,7 +544,8 @@ fil_parse_write_crypt_data(
 	return ptr;
 }
 
-/** Encrypt a buffer.
+/******************************************************************
+Encrypt a buffer
 @param[in,out]		crypt_data	Crypt data
 @param[in]		space		space_id
 @param[in]		offset		Page offset
@@ -535,7 +569,10 @@ fil_encrypt_buf(
 	uint key_version = fil_crypt_get_latest_key_version(crypt_data);
 
 	if (key_version == ENCRYPTION_KEY_VERSION_INVALID) {
-		ib::fatal() << "Unknown encryption key";
+		ib_logf(IB_LOG_LEVEL_FATAL,
+			"Unknown key id %u. Can't continue!\n",
+			crypt_data->key_id);
+		ut_error;
 	}
 
 	ulint orig_page_type = mach_read_from_2(src_frame+FIL_PAGE_TYPE);
@@ -572,6 +609,7 @@ fil_encrypt_buf(
 			    << " src: " << src << " srclen: " << srclen
 			    << " buf: " << dst << " buflen: " << dstlen
 			    << " return-code: "<< rc << " Can't continue!";
+
 		ut_error;
 	}
 
@@ -586,7 +624,8 @@ fil_encrypt_buf(
 			FIL_PAGE_DATA_END);
 	} else {
 		/* Clean up rest of buffer */
-		memset(dst_frame+header_len+srclen, 0, page_size.physical() - (header_len+srclen));
+		memset(dst_frame+header_len+srclen, 0,
+			page_size.physical() - (header_len+srclen));
 	}
 
 	/* handle post encryption checksum */
@@ -597,7 +636,7 @@ fil_encrypt_buf(
 	// store the post-encryption checksum after the key-version
 	mach_write_to_4(dst_frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION + 4, checksum);
 
-	ut_ad(fil_space_verify_crypt_checksum(dst_frame, zip_size, NULL, offset));
+	ut_ad(fil_space_verify_crypt_checksum(dst_frame, page_size, NULL, offset));
 
 	srv_stats.pages_encrypted.inc();
 
@@ -617,18 +656,18 @@ UNIV_INTERN
 byte*
 fil_space_encrypt(
 	const fil_space_t*	space,
-	ulint			offset,
-	lsn_t			lsn,
-	byte*			src_frame,
-	byte*			dst_frame)
+	ulint		offset,
+	lsn_t		lsn,
+	byte*		src_frame,
+	byte*		dst_frame)
 {
-	switch (mach_read_from_2(src_frame+FIL_PAGE_TYPE)) {
-	case FIL_PAGE_TYPE_FSP_HDR:
-	case FIL_PAGE_TYPE_XDES:
-	case FIL_PAGE_RTREE:
-		/* File space header, extent descriptor or spatial index
-		are not encrypted. */
-		return src_frame;
+	ulint orig_page_type = mach_read_from_2(src_frame+FIL_PAGE_TYPE);
+
+	if (orig_page_type==FIL_PAGE_TYPE_FSP_HDR
+		|| orig_page_type==FIL_PAGE_TYPE_XDES) {
+		/* File space header or extent descriptor do not need to be
+		encrypted. */
+		return (src_frame);
 	}
 
 	if (!space->crypt_data || !space->crypt_data->is_encrypted()) {
@@ -636,8 +675,8 @@ fil_space_encrypt(
 	}
 
 	fil_space_crypt_t* crypt_data = space->crypt_data;
-	const page_size_t	page_size(space->flags);
 	ut_ad(space->n_pending_ops);
+	const page_size_t	page_size(space->flags);
 	byte* tmp = fil_encrypt_buf(crypt_data, space->id, offset, lsn,
 				    src_frame, page_size, dst_frame);
 
@@ -678,7 +717,7 @@ fil_space_encrypt(
 			fprintf(stderr, "encrypted_frame\n");
 			buf_page_print(tmp, page_size, BUF_PAGE_PRINT_NO_CRASH);
 			fprintf(stderr, "decrypted_frame\n");
-			buf_page_print(tmp_mem, page_size, 0);
+			buf_page_print(tmp_mem, page_size, BUF_PAGE_PRINT_NO_CRASH);
 		}
 
 		free(tmp_mem);
@@ -691,6 +730,7 @@ fil_space_encrypt(
 			free(uncomp_mem);
 		}
 	}
+
 #endif /* UNIV_DEBUG */
 
 	return tmp;
@@ -709,7 +749,7 @@ bool
 fil_space_decrypt(
 	fil_space_crypt_t*	crypt_data,
 	byte*			tmp_frame,
-	const page_size_t&	page_size,
+	ulint			page_size,
 	byte*			src_frame,
 	dberr_t*		err)
 {
@@ -735,14 +775,13 @@ fil_space_decrypt(
 			data file (ibdata*, not *.ibd), if not
 			clear it. */
 
-			DBUG_LOG("crypt",
-				 ("Page " << page_id_t(space, offset)
-				  << " carries key_version " << key_version
-				  << " (should be undefined)"));
+			DBUG_PRINT("ib_crypt",
+				("Page on space %lu offset %lu has key_version %u"
+				" when it shoud be undefined.",
+				space, offset, key_version));
 
 			mach_write_to_4(src_frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION, 0);
 		}
-
 		return false;
 	}
 
@@ -762,7 +801,7 @@ fil_space_decrypt(
 	const byte* src = src_frame + header_len;
 	byte* dst = tmp_frame + header_len;
 	uint32 dstlen = 0;
-	ulint srclen = page_size.physical() - (header_len + FIL_PAGE_DATA_END);
+	ulint srclen = page_size - (header_len + FIL_PAGE_DATA_END);
 
 	if (page_compressed) {
 		srclen = mach_read_from_2(src_frame + FIL_PAGE_DATA);
@@ -779,11 +818,12 @@ fil_space_decrypt(
 			return false;
 		}
 
-		ib::error() << "Unable to decrypt data-block "
-			    << " src: " << src << "srclen: "
-			    << srclen << " buf: " << dst << "buflen: "
-			    << dstlen << " return-code: " << rc
-			    << " Can't continue!";
+		ib_logf(IB_LOG_LEVEL_FATAL,
+			"Unable to decrypt data-block "
+			" src: %p srclen: %ld buf: %p buflen: %d."
+			" return-code: %d. Can't continue!\n",
+			src, (long)srclen,
+			dst, dstlen, rc);
 		ut_error;
 	}
 
@@ -793,8 +833,8 @@ fil_space_decrypt(
 	to sector boundary is written. */
 	if (!page_compressed) {
 		/* Copy FIL trailer */
-		memcpy(tmp_frame + page_size.physical() - FIL_PAGE_DATA_END,
-		       src_frame + page_size.physical() - FIL_PAGE_DATA_END,
+		memcpy(tmp_frame + page_size - FIL_PAGE_DATA_END,
+		       src_frame + page_size - FIL_PAGE_DATA_END,
 		       FIL_PAGE_DATA_END);
 
 		// clear key-version & crypt-checksum from dst
@@ -806,10 +846,11 @@ fil_space_decrypt(
 	return true; /* page was decrypted */
 }
 
-/**
-Decrypt a page.
+/******************************************************************
+Decrypt a page
 @param[in]	space			Tablespace
 @param[in]	tmp_frame		Temporary buffer used for decrypting
+ at param[in]	page_size		Page size
 @param[in,out]	src_frame		Page to decrypt
 @param[out]	decrypted		true if page was decrypted
 @return decrypted page, or original not encrypted page if decryption is
@@ -861,7 +902,7 @@ UNIV_INTERN
 ulint
 fil_crypt_calculate_checksum(
 	const page_size_t&	page_size,
-	byte*			dst_frame)
+	const byte*	dst_frame)
 {
 	ib_uint32_t checksum = 0;
 	srv_checksum_algorithm_t algorithm =
@@ -893,8 +934,6 @@ fil_crypt_calculate_checksum(
 	return checksum;
 }
 
-<<<<<<< HEAD
-=======
 /*********************************************************************
 Verify that post encryption checksum match calculated checksum.
 This function should be called only if tablespace contains crypt_data
@@ -904,7 +943,7 @@ calculated checksum as if it does page could be valid unencrypted,
 encrypted, or corrupted.
 
 @param[in]	page		Page to verify
- at param[in]	zip_size	zip size
+ at param[in]	page_size	Page size
 @param[in]	space		Tablespace
 @param[in]	pageno		Page no
 @return true if page is encrypted AND OK, false otherwise */
@@ -912,7 +951,7 @@ UNIV_INTERN
 bool
 fil_space_verify_crypt_checksum(
 	byte* 			page,
-	ulint			zip_size,
+	const page_size_t&	page_size,
 	const fil_space_t*	space,
 	ulint			pageno)
 {
@@ -937,7 +976,7 @@ fil_space_verify_crypt_checksum(
 	/* Declare empty pages non-corrupted */
 	if (checksum == 0
 	    && *reinterpret_cast<const ib_uint64_t*>(page + FIL_PAGE_LSN) == 0
-	    && buf_page_is_zeroes(page, zip_size)) {
+	    && buf_page_is_zeroes(page, page_size)) {
 		return(true);
 	}
 
@@ -951,13 +990,13 @@ fil_space_verify_crypt_checksum(
 	/* 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) {
+	if (!page_size.is_compressed()) {
 		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);
 
-		bool valid = page_zip_verify_checksum(page, zip_size);
+		bool valid = page_zip_verify_checksum(page, page_size);
 
 		mach_write_to_4(page + FIL_PAGE_SPACE_OR_CHKSUM, old);
 
@@ -1012,20 +1051,20 @@ fil_space_verify_crypt_checksum(
 		/* If page is encrypted and traditional checksums match,
 		page could be still encrypted, or not encrypted and valid or
 		corrupted. */
-		ib_logf(IB_LOG_LEVEL_ERROR,
-			" Page %lu in space %s (%lu) maybe corrupted."
-			" Post encryption checksum %u stored [%lu:%lu] key_version %u",
-			pageno,
-			space ? space->name : "N/A",
-			mach_read_from_4(page + FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID),
-			checksum, checksum1, checksum2, key_version);
+		ib::error()
+			<< " Page " << pageno << " in space "
+			<< space = space-> name : "N/A" << "(" << space_id
+			<< ") maybe corrupted."
+			" Post encryption checksum " << checksum
+			<< "stored [" << checksum1 << ":" << checksum2
+			<< "] key_version " << key_version;
+
 		encrypted = false;
 	}
 
 	return(encrypted);
 }
 
->>>>>>> origin/10.1
 /***********************************************************************/
 
 /** A copy of global key state */
@@ -1167,21 +1206,24 @@ fil_crypt_start_encrypting_space(
 		/* 2 - get page 0 */
 		dberr_t err = DB_SUCCESS;
 		buf_block_t* block = buf_page_get_gen(
-			page_id_t(space->id, 0), page_size_t(space->flags),
-			RW_X_LATCH, NULL, BUF_GET,
-			__FILE__, __LINE__,
-			&mtr, &err);
+					page_id_t(space->id, 0),
+					page_size_t(space->flags),
+					RW_X_LATCH,
+					NULL,
+					BUF_GET,
+					__FILE__, __LINE__,
+					&mtr, &err);
 
 
 		/* 3 - write crypt data to page 0 */
 		byte* frame = buf_block_get_frame(block);
 		crypt_data->type = CRYPT_SCHEME_1;
-		crypt_data->write_page0(space, frame, &mtr);
+		crypt_data->write_page0(frame, &mtr);
 
 		mtr_commit(&mtr);
 
 		/* record lsn of update */
-		lsn_t end_lsn = mtr.commit_lsn();
+		lsn_t end_lsn = mtr.end_lsn;
 
 		/* 4 - sync tablespace before publishing crypt data */
 
@@ -1190,7 +1232,7 @@ fil_crypt_start_encrypting_space(
 
 		do {
 			ulint n_pages = 0;
-			success = buf_flush_lists(ULINT_MAX, end_lsn, &n_pages);
+			success = buf_flush_list(ULINT_MAX, end_lsn, &n_pages);
 			buf_flush_wait_batch_end(NULL, BUF_FLUSH_LIST);
 			sum_pages += n_pages;
 		} while (!success);
@@ -1242,7 +1284,7 @@ struct rotate_thread_t {
 	uint estimated_max_iops;   /*!< estimation of max iops */
 	uint allocated_iops;	   /*!< allocated iops */
 	uint cnt_waited;	   /*!< #times waited during this slot */
-	uintmax_t sum_waited_us;   /*!< wait time during this slot */
+	uint sum_waited_us;	   /*!< wait time during this slot */
 
 	fil_crypt_stat_t crypt_stat; // statistics
 
@@ -1255,11 +1297,10 @@ struct rotate_thread_t {
 		case SRV_SHUTDOWN_NONE:
 		case SRV_SHUTDOWN_CLEANUP:
 			return thread_no >= srv_n_fil_crypt_threads;
-		case SRV_SHUTDOWN_EXIT_THREADS:
-			/* srv_init_abort() must have been invoked */
 		case SRV_SHUTDOWN_FLUSH_PHASE:
 			return true;
 		case SRV_SHUTDOWN_LAST_PHASE:
+		case SRV_SHUTDOWN_EXIT_THREADS:
 			break;
 		}
 		ut_ad(0);
@@ -1731,61 +1772,24 @@ fil_crypt_find_page_to_rotate(
 /***********************************************************************
 Check if a page is uninitialized (doesn't need to be rotated)
 @param[in]	frame		Page to check
- at param[in]	zip_size	zip_size or 0
+ at param[in]	page_size	Page size
 @return true if page is uninitialized, false if not. */
 static inline
 bool
 fil_crypt_is_page_uninitialized(
-<<<<<<< HEAD
-/*============================*/
-	const byte*		frame, 		/*!< in: Page */
-	const page_size_t&	page_size)	/*!< in: page size */
-{
-	if (fil_page_get_type(frame) == FIL_PAGE_TYPE_ALLOCATED) {
-		/* empty pages aren't encrypted */
-		return true;
-	}
-
-	if (page_size.is_compressed()) {
-		ulint stored_checksum = mach_read_from_4(
-			frame + FIL_PAGE_SPACE_OR_CHKSUM);
-		/* empty pages aren't encrypted */
-		if (stored_checksum == 0) {
-			return true;
-		}
-	} else {
-		ulint size = page_size.logical();
-		ulint checksum_field1 = mach_read_from_4(
-			frame + FIL_PAGE_SPACE_OR_CHKSUM);
-		ulint checksum_field2 = mach_read_from_4(
-			frame + size - FIL_PAGE_END_LSN_OLD_CHKSUM);
-		/* empty pages are not encrypted */
-		if (checksum_field1 == 0 && checksum_field2 == 0
-		    && mach_read_from_4(frame + FIL_PAGE_LSN) == 0) {
-			return true;
-		}
-	}
-	return false;
-}
-
-#define fil_crypt_get_page_throttle(state,offset,mtr,sleeptime_ms) \
-	fil_crypt_get_page_throttle_func(state, offset, mtr, \
-=======
 	const byte	*frame,
-	uint		zip_size)
+	const page_size_t& page_size)
 {
-	return (buf_page_is_zeroes(frame, zip_size));
+	return (buf_page_is_zeroes(frame, page_size));
 }
 
 #define fil_crypt_get_page_throttle(state,offset,mtr,sleeptime_ms) \
 	fil_crypt_get_page_throttle_func(state, offset, mtr, \
->>>>>>> origin/10.1
 					 sleeptime_ms, __FILE__, __LINE__)
 
 /***********************************************************************
 Get a page and compute sleep time
 @param[in,out]		state		Rotation state
- at param[in]		zip_size	compressed size or 0
 @param[in]		offset		Page offset
 @param[in,out]		mtr		Minitransaction
 @param[out]		sleeptime_ms	Sleep time
@@ -1795,25 +1799,6 @@ Get a page and compute sleep time
 static
 buf_block_t*
 fil_crypt_get_page_throttle_func(
-<<<<<<< HEAD
-/*=============================*/
-	rotate_thread_t*	state,		/*!< in/out: Key rotation state */
-	ulint			space,		/*!< in: FIL space id */
-	const page_size_t&	page_size,	/*!< in: page size */
-	ulint 			offset,		/*!< in: page offsett */
-	mtr_t*			mtr,		/*!< in/out: minitransaction */
-	ulint*			sleeptime_ms,	/*!< out: sleep time */
-	const char*		file,		/*!< in: file name */
-	unsigned		line)		/*!< in: file line */
-{
-	const page_id_t&	page_id = page_id_t(space, offset);
-	dberr_t			err = DB_SUCCESS;
-	buf_block_t*		block = NULL;
-
-	// JAN: TODO:
-	// buf_block_t*		block = buf_page_try_get_func(page_id, file, line, mtr);
-
-=======
 	rotate_thread_t*	state,
 	ulint 			offset,
 	mtr_t*			mtr,
@@ -1822,13 +1807,13 @@ fil_crypt_get_page_throttle_func(
 	ulint 			line)
 {
 	fil_space_t* space = state->space;
-	ulint zip_size = fsp_flags_get_zip_size(space->flags);
+	const page_size_t page_size = page_size_t(space->flags);
+	const page_id_t page_id(space->id, offset);
 	ut_ad(space->n_pending_ops > 0);
 
-	buf_block_t* block = buf_page_try_get_func(space->id, offset, RW_X_LATCH,
+	buf_block_t* block = buf_page_try_get_func(page_id, RW_X_LATCH,
 						   true,
 						   file, line, mtr);
->>>>>>> origin/10.1
 	if (block != NULL) {
 		/* page was in buffer pool */
 		state->crypt_stat.pages_read_from_cache++;
@@ -1843,12 +1828,13 @@ fil_crypt_get_page_throttle_func(
 
 	state->crypt_stat.pages_read_from_disk++;
 
-	uintmax_t start = ut_time_us(NULL);
+	ullint start = ut_time_us(NULL);
+	dberr_t err = DB_SUCCESS;
 	block = buf_page_get_gen(page_id, page_size,
 				 RW_X_LATCH,
 				 NULL, BUF_GET_POSSIBLY_FREED,
-				 file, line, mtr, &err);
-	uintmax_t end = ut_time_us(NULL);
+				file, line, mtr, &err);
+	ullint end = ut_time_us(NULL);
 
 	if (end < start) {
 		end = start; // safety...
@@ -1952,6 +1938,7 @@ fil_crypt_rotate_page(
 	ulint offset = state->offset;
 	ulint sleeptime_ms = 0;
 	fil_space_crypt_t *crypt_data = space->crypt_data;
+	const page_size_t page_size = page_size_t(space->flags);
 
 	ut_ad(space->n_pending_ops > 0);
 
@@ -2027,7 +2014,7 @@ fil_crypt_rotate_page(
 		}
 
 		mtr_commit(&mtr);
-		lsn_t end_lsn = mtr.commit_lsn();
+		lsn_t end_lsn = mtr.end_lsn;
 
 		if (needs_scrubbing == BTR_SCRUB_PAGE) {
 			mtr_start(&mtr);
@@ -2035,7 +2022,6 @@ fil_crypt_rotate_page(
 			* refetch page and allocation status
 			*/
 			btr_scrub_page_allocation_status_t allocated;
-
 			block = btr_scrub_get_block_and_allocation_status(
 				state, offset, &mtr,
 				&allocated,
@@ -2163,15 +2149,15 @@ fil_crypt_flush_space(
 		bool success = false;
 		ulint n_pages = 0;
 		ulint sum_pages = 0;
-		uintmax_t start = ut_time_us(NULL);
+		ullint start = ut_time_us(NULL);
 
 		do {
-			success = buf_flush_lists(ULINT_MAX, end_lsn, &n_pages);
+			success = buf_flush_list(ULINT_MAX, end_lsn, &n_pages);
 			buf_flush_wait_batch_end(NULL, BUF_FLUSH_LIST);
 			sum_pages += n_pages;
 		} while (!success && !space->is_stopping());
 
-		uintmax_t end = ut_time_us(NULL);
+		ullint end = ut_time_us(NULL);
 
 		if (sum_pages && end > start) {
 			state->cnt_waited += sum_pages;
@@ -2188,16 +2174,21 @@ fil_crypt_flush_space(
 
 	/* update page 0 */
 	mtr_t mtr;
-	mtr.start();
+	mtr_start(&mtr);
+
+	const page_id_t page_id = page_id_t(space->id, 0);
+	const page_size_t page_size = page_size_t(space->flags);
+	dberr_t err = DB_SUCCESS;
+
+	buf_block_t* block = buf_page_get_gen(page_id, page_size,
+				RW_X_LATCH, NULL, BUF_GET,
+				__FILE__, __LINE__, &mtr, &err);
 
-	buf_block_t* block = buf_page_get_gen(
-		space->id, page_size_t(space->flags), 0,
-		RW_X_LATCH, NULL, BUF_GET, __FILE__, __LINE__, &mtr);
 	byte* frame = buf_block_get_frame(block);
 
-	crypt_data->write_page0(space, frame, &mtr);
+	crypt_data->write_page0(frame, &mtr);
 
-	mtr.commit();
+	mtr_commit(&mtr);
 }
 
 /***********************************************************************
@@ -2402,7 +2393,7 @@ DECLARE_THREAD(fil_crypt_thread)(
 	/* We count the number of threads in os_thread_exit(). A created
 	thread should always use that to exit and not use return() to exit. */
 
-	os_thread_exit();
+	os_thread_exit(NULL);
 
 	OS_THREAD_DUMMY_RETURN;
 }
@@ -2427,10 +2418,10 @@ fil_crypt_set_thread_cnt(
 		for (uint i = 0; i < add; i++) {
 			os_thread_id_t rotation_thread_id;
 			os_thread_create(fil_crypt_thread, NULL, &rotation_thread_id);
-			ib::info() << "Creating "
-				   << i+1 << " encryption thread id "
-				   << os_thread_pf(rotation_thread_id)
-				   << " total threads " << new_cnt << ".";
+
+			ib_logf(IB_LOG_LEVEL_INFO,
+				"Creating #%d thread id %lu total threads %u.",
+				i+1, os_thread_pf(rotation_thread_id), new_cnt);
 		}
 	} else if (new_cnt < srv_n_fil_crypt_threads) {
 		srv_n_fil_crypt_threads = new_cnt;
@@ -2487,11 +2478,12 @@ UNIV_INTERN
 void
 fil_crypt_threads_init()
 {
+	ut_ad(mutex_own(&fil_system->mutex));
 	if (!fil_crypt_threads_inited) {
-		fil_crypt_event = os_event_create(0);
-		fil_crypt_threads_event = os_event_create(0);
-		mutex_create(LATCH_ID_FIL_CRYPT_THREADS_MUTEX,
-		     &fil_crypt_threads_mutex);
+		fil_crypt_event = os_event_create();
+		fil_crypt_threads_event = os_event_create();
+		mutex_create(fil_crypt_threads_mutex_key,
+			&fil_crypt_threads_mutex, SYNC_NO_ORDER_CHECK);
 
 		uint cnt = srv_n_fil_crypt_threads;
 		srv_n_fil_crypt_threads = 0;
@@ -2510,8 +2502,10 @@ fil_crypt_threads_cleanup()
 		return;
 	}
 	ut_a(!srv_n_fil_crypt_threads_started);
-	os_event_destroy(fil_crypt_event);
-	os_event_destroy(fil_crypt_threads_event);
+	os_event_free(fil_crypt_event);
+	fil_crypt_event = NULL;
+	os_event_free(fil_crypt_threads_event);
+	fil_crypt_threads_event = NULL;
 	mutex_free(&fil_crypt_threads_mutex);
 	fil_crypt_threads_inited = false;
 }
@@ -2559,10 +2553,9 @@ fil_space_crypt_close_tablespace(
 		time_t now = time(0);
 
 		if (now >= last + 30) {
-			ib::warn() << "Waited "
-				   << now - start
-				   << " seconds to drop space: "
-				   << space->name << ".";
+			ib_logf(IB_LOG_LEVEL_WARN,
+				"Waited %ld seconds to drop space: %s(" ULINTPF ").",
+				now - start, space->name, space->id);
 			last = now;
 		}
 	}
@@ -2645,7 +2638,7 @@ fil_space_get_scrub_status(
 	status->space = space->id;
 
 	if (crypt_data != NULL) {
-		status->compressed = FSP_FLAGS_GET_ZIP_SSIZE(space->flags) > 0;
+		status->compressed = page_size_t(space->flags).is_compressed();
 		mutex_enter(&crypt_data->mutex);
 		status->last_scrub_completed =
 			crypt_data->rotate_state.scrubbing.last_scrub_completed;
@@ -2665,93 +2658,3 @@ fil_space_get_scrub_status(
 		mutex_exit(&crypt_data->mutex);
 	}
 }
-#endif /* UNIV_INNOCHECKSUM */
-
-/*********************************************************************
-Verify checksum for a page (iff it's encrypted)
-NOTE: currently this function can only be run in single threaded mode
-as it modifies srv_checksum_algorithm (temporarily)
- at param[in]	src_fame	page to verify
- at param[in]	page_size	page_size
- at param[in]	page_no		page number of given read_buf
- at param[in]	strict_check	true if strict-check option is enabled
- at return true if page is encrypted AND OK, false otherwise */
-UNIV_INTERN
-bool
-fil_space_verify_crypt_checksum(
-/*============================*/
-	const byte* 		src_frame,	/*!< in: page the verify */
-	const page_size_t&	page_size	/*!< in: page size */
-#ifdef UNIV_INNOCHECKSUM
-	,uintmax_t 		page_no,
-	bool			strict_check
-#endif /* UNIV_INNOCHECKSUM */
-)
-{
-	// key version
-	uint key_version = mach_read_from_4(
-		src_frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION);
-
-	if (key_version == 0) {
-		return false; // unencrypted page
-	}
-
-	/* "trick" the normal checksum routines by storing the post-encryption
-	* checksum into the normal checksum field allowing for reuse of
-	* the normal routines */
-
-	// post encryption checksum
-	ib_uint32_t stored_post_encryption = mach_read_from_4(
-		src_frame + FIL_PAGE_FILE_FLUSH_LSN_OR_KEY_VERSION + 4);
-
-	// save pre encryption checksum for restore in end of this function
-	ib_uint32_t stored_pre_encryption = mach_read_from_4(
-		src_frame + FIL_PAGE_SPACE_OR_CHKSUM);
-
-	ib_uint32_t checksum_field2 = mach_read_from_4(
-		src_frame + UNIV_PAGE_SIZE - FIL_PAGE_END_LSN_OLD_CHKSUM);
-
-	/** prepare frame for usage of normal checksum routines */
-	mach_write_to_4(const_cast<byte*>(src_frame) + FIL_PAGE_SPACE_OR_CHKSUM,
-			stored_post_encryption);
-
-	/* NOTE: this function is (currently) only run when restoring
-	* dblwr-buffer, server is single threaded so it's safe to modify
-	* srv_checksum_algorithm */
-	srv_checksum_algorithm_t save_checksum_algorithm =
-		(srv_checksum_algorithm_t)srv_checksum_algorithm;
-
-	if (!page_size.is_compressed() &&
-	    (save_checksum_algorithm == SRV_CHECKSUM_ALGORITHM_STRICT_INNODB ||
-	     save_checksum_algorithm == SRV_CHECKSUM_ALGORITHM_INNODB)) {
-		/* handle ALGORITHM_INNODB specially,
-		* "downgrade" to ALGORITHM_INNODB and store BUF_NO_CHECKSUM_MAGIC
-		* checksum_field2 is sort of pointless anyway...
-		*/
-		srv_checksum_algorithm = SRV_CHECKSUM_ALGORITHM_INNODB;
-		mach_write_to_4(const_cast<byte*>(src_frame) +
-				UNIV_PAGE_SIZE - FIL_PAGE_END_LSN_OLD_CHKSUM,
-				BUF_NO_CHECKSUM_MAGIC);
-	}
-
-	/* verify checksums */
-	bool corrupted = buf_page_is_corrupted(false, src_frame,
-		page_size, false
-#ifdef UNIV_INNOCHECKSUM
-		,page_no, strict_check, false, NULL
-#endif /* UNIV_INNOCHECKSUM */
-	);
-
-	/** restore frame & algorithm */
-	srv_checksum_algorithm = save_checksum_algorithm;
-
-	mach_write_to_4(const_cast<byte*>(src_frame) +
-			FIL_PAGE_SPACE_OR_CHKSUM,
-			stored_pre_encryption);
-
-	mach_write_to_4(const_cast<byte*>(src_frame) +
-			UNIV_PAGE_SIZE - FIL_PAGE_END_LSN_OLD_CHKSUM,
-			checksum_field2);
-
-	return (!corrupted);
-}
diff --git a/storage/innobase/fil/fil0fil.cc b/storage/innobase/fil/fil0fil.cc
index 59334622ed7..dc2ed0015ff 100644
--- a/storage/innobase/fil/fil0fil.cc
+++ b/storage/innobase/fil/fil0fil.cc
@@ -1104,6 +1104,20 @@ fil_space_extend_must_retry(
 	we have set the node->being_extended flag. */
 	mutex_exit(&fil_system->mutex);
 
+	ut_ad(size > space->size);
+
+	ulint		start_page_no		= space->size;
+	const ulint	file_start_page_no	= start_page_no - node->size;
+
+	/* Determine correct file block size */
+	if (node->block_size == 0) {
+		node->block_size = os_file_get_block_size(
+			node->handle, node->name);
+	}
+
+	const page_size_t	pageSize(space->flags);
+	const ulint		page_size = pageSize.physical();
+
 #ifdef _WIN32
 	const ulint	io_completion_type = OS_FILE_READ;
 	/* Logically or physically extend the file with zero bytes,
@@ -1147,7 +1161,10 @@ fil_space_extend_must_retry(
 	Because no actual writes were dispatched, a read operation
 	will suffice. */
 
-	if (srv_use_posix_fallocate && !is_sparse) {
+	const ulint	io_completion_type = srv_use_posix_fallocate
+		|| is_sparse ? OS_FILE_READ : OS_FILE_WRITE;
+
+	if (!is_sparse) {
 		const os_offset_t	start_offset
 			= os_offset_t(start_page_no - file_start_page_no)
 			* page_size;
@@ -1166,7 +1183,7 @@ fil_space_extend_must_retry(
 				<< "' from "
 				<< start_offset
 				<< " to " << len + start_offset
-				<< " bytes failed with " << err);
+				<< " bytes failed with " << err;
 		}
 
 		DBUG_EXECUTE_IF("ib_os_aio_func_io_failure_28",
@@ -1178,6 +1195,9 @@ fil_space_extend_must_retry(
 			start_page_no = size;
 		}
 	} else
+#else
+	const ulint io_completion_type = is_sparse
+		?  : OS_FILE_WRITE;
 # endif
 	if (is_sparse) {
 		/* fil_read_first_page() expects UNIV_PAGE_SIZE bytes.
@@ -1193,60 +1213,34 @@ fil_space_extend_must_retry(
 				<< "' from "
 				<< os_offset_t(start_page_no
 					       - file_start_page_no)
-				* page_size << " to " << of_offset_t(s)
-				<< " bytes failed with " << errno);
+				* page_size << " to " << os_offset_t(s)
+				<< " bytes failed with " << errno;
 		} else {
 			start_page_no = size;
 		}
 	} else {
-		/* Extend at most 64 pages at a time */
-		ulint	buf_size = ut_min(64, size - start_page_no)
-			* page_size;
-		byte*	buf2 = static_cast<byte*>(
-			calloc(1, buf_size + page_size));
-		*success = buf2 != NULL;
-		if (!buf2) {
-			ib::error() << "Cannot allocate "
-				<< buf_size + page_size
-				" bytes to extend file";
-		}
-		byte* const	buf = static_cast<byte*>(
-			ut_align(buf2, page_size));
-
-		while (*success && start_page_no < size) {
-			ulint		n_pages
-				= ut_min(buf_size / page_size,
-					 size - start_page_no);
-
-			os_offset_t	offset = static_cast<os_offset_t>(
-				start_page_no - file_start_page_no)
-				* page_size;
-
-			*success = os_aio(OS_FILE_WRITE, 0, OS_AIO_SYNC,
-					  node->name, node->handle, buf,
-					  offset, page_size * n_pages,
-					  page_size, node, NULL, 0);
-
-			DBUG_EXECUTE_IF("ib_os_aio_func_io_failure_28",
-					*success = FALSE;
-					os_has_said_disk_full = TRUE;);
-
-			if (*success) {
-				os_has_said_disk_full = FALSE;
-			}
-			/* Let us measure the size of the file
-			to determine how much we were able to
-			extend it */
-			os_offset_t	fsize = os_file_get_size(node->handle);
-			ut_a(fsize != os_offset_t(-1));
-
-			start_page_no = ulint(fsize / page_size)
-				+ file_start_page_no;
+		if (DB_SUCCESS != fil_write_zeros(
+			    node, page_size, start,
+			    static_cast<ulint>(end - start),
+			    space->purpose == FIL_TYPE_TEMPORARY
+			    && srv_read_only_mode)) {
+			ib::warn()
+				<< "Error while writing " << end - start
+				<< " zeroes to " << node->name
+				<< " starting at offset " << start;
 		}
 
-		free(buf2);
+		/* Check how many pages actually added */
+		os_offset_t	actual_end = os_file_get_size(node->handle);
+		ut_a(actual_end != static_cast<os_offset_t>(-1));
+		ut_a(actual_end >= start);
+
+		*success = end >= actual_end;
+		pages_added = static_cast<ulint>(
+			(std::min(actual_end, end) - start) / page_size);
 	}
 #endif
+
 	mutex_enter(&fil_system->mutex);
 
 	ut_a(node->being_extended);
@@ -1259,8 +1253,7 @@ fil_space_extend_must_retry(
 	const ulint pages_in_MiB = node->size
 		& ~((1 << (20 - UNIV_PAGE_SIZE_SHIFT)) - 1);
 
-	fil_node_complete_io(node, fil_system, is_sparse
-			     ? IORequestRead : IORequestWrite);
+	fil_node_complete_io(node, fil_system, io_completion_type);
 
 	/* Keep the last data file size info up to date, rounded to
 	full megabytes */
@@ -1740,7 +1733,7 @@ fil_space_create(
 		    srv_encrypt_tables)) {
 		/* Key rotation is not enabled, need to inform background
 		encryption threads. */
-		UT_LIST_ADD_LAST(rotation_list, fil_system->rotation_list, space);
+		UT_LIST_ADD_LAST(fil_system->rotation_list, space);
 		space->is_in_rotation_list = true;
 		mutex_exit(&fil_system->mutex);
 		mutex_enter(&fil_crypt_threads_mutex);
@@ -6844,7 +6837,7 @@ fil_space_remove_from_keyrotation(fil_space_t* space)
 	if (space->n_pending_ops == 0 && space->is_in_rotation_list) {
 		space->is_in_rotation_list = false;
 		ut_a(UT_LIST_GET_LEN(fil_system->rotation_list) > 0);
-		UT_LIST_REMOVE(rotation_list, fil_system->rotation_list, space);
+		UT_LIST_REMOVE(fil_system->rotation_list, space);
 	}
 }
 
@@ -7014,26 +7007,6 @@ fil_space_found_by_id(
 	return space;
 }
 
-/****************************************************************//**
-Acquire fil_system mutex */
-void
-fil_system_enter(void)
-/*==================*/
-{
-	ut_ad(!mutex_own(&fil_system->mutex));
-	mutex_enter(&fil_system->mutex);
-}
-
-/****************************************************************//**
-Release fil_system mutex */
-void
-fil_system_exit(void)
-/*=================*/
-{
-	ut_ad(mutex_own(&fil_system->mutex));
-	mutex_exit(&fil_system->mutex);
-}
-
 /**
 Get should we punch hole to tablespace.
 @param[in]	node		File node
diff --git a/storage/innobase/include/fil0fil.h b/storage/innobase/include/fil0fil.h
index 3e47a76fe48..e2f847d0be3 100644
--- a/storage/innobase/include/fil0fil.h
+++ b/storage/innobase/include/fil0fil.h
@@ -43,11 +43,11 @@ struct trx_t;
 class page_id_t;
 class truncate_t;
 
-/* structure containing encryption specification */
-typedef struct fil_space_crypt_struct fil_space_crypt_t;
-
 typedef std::list<char*, ut_allocator<char*> >	space_name_list_t;
 
+/** Structure containing encryption specification */
+struct fil_space_crypt_t;
+
 /** File types */
 enum fil_type_t {
 	/** temporary tablespace (temporary undo log or tables) */
@@ -426,8 +426,6 @@ index */
         fil_page_type_is_index(fil_page_get_type(page))
 
 #ifndef UNIV_INNOCHECKSUM
-/** Structure containing encryption specification */
-struct fil_space_crypt_t;
 
 /** Enum values for encryption table option */
 enum fil_encryption_t {
@@ -601,7 +599,7 @@ fil_space_create(
 	fil_type_t	purpose,	/*!< in: FIL_TABLESPACE, or FIL_LOG if log */
 	fil_space_crypt_t* crypt_data, /*!< in: crypt data */
 	bool		create_table,  /*!< in: true if create table */
-	fil_encryption_t	mode = FIL_ENCRYPTION_DEFAULT);
+	fil_encryption_t	mode = FIL_ENCRYPTION_DEFAULT)
 	MY_ATTRIBUTE((warn_unused_result));
 
 /*******************************************************************//**


More information about the commits mailing list