~hrvojem/percona-xtrabackup/bug1065761-2.1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
--- a/storage/innodb_plugin/btr/btr0btr.c
+++ b/storage/innodb_plugin/btr/btr0btr.c
@@ -674,7 +674,7 @@
 /**************************************************************//**
 Gets the root node of a tree and x-latches it.
 @return	root page, x-latched */
-static
+//static
 buf_block_t*
 btr_root_block_get(
 /*===============*/
@@ -1134,7 +1134,7 @@
 /************************************************************//**
 Returns the child page of a node pointer and x-latches it.
 @return	child page, x-latched */
-static
+//static
 buf_block_t*
 btr_node_ptr_get_child(
 /*===================*/
--- a/storage/innodb_plugin/buf/buf0buf.c
+++ b/storage/innodb_plugin/buf/buf0buf.c
@@ -412,7 +412,7 @@
 		return(TRUE);
 	}
 
-#ifndef UNIV_HOTBACKUP
+#ifdef UNDEFINED
 	if (recv_lsn_checks_on) {
 		ib_uint64_t	current_lsn;
 
@@ -1888,7 +1888,9 @@
 	ut_ad(zip_size == fil_space_get_zip_size(space));
 	ut_ad(ut_is_2pow(zip_size));
 #ifndef UNIV_LOG_DEBUG
-	ut_ad(!ibuf_inside() || ibuf_page(space, zip_size, offset, NULL));
+	/* ibuf_page() always returns FALSE with srv_fake_write=1 */
+	ut_ad(srv_fake_write ||
+	      !ibuf_inside() || ibuf_page(space, zip_size, offset, NULL));
 #endif
 	if (innobase_get_slow_log()) {
 		trx = innobase_get_trx();
@@ -3302,7 +3304,7 @@
 			recv_recover_page(TRUE, (buf_block_t*) bpage);
 		}
 
-		if (uncompressed && !recv_no_ibuf_operations) {
+		if (uncompressed && !recv_no_ibuf_operations && !srv_fake_write) {
 			ibuf_merge_or_delete_for_page(
 				/* Delete possible entries, if bpage is_corrupt */
 				(srv_pass_corrupt_table && bpage->is_corrupt) ? NULL :
--- a/storage/innodb_plugin/fil/fil0fil.c
+++ b/storage/innodb_plugin/fil/fil0fil.c
@@ -297,7 +297,7 @@
 
 /** The tablespace memory cache. This variable is NULL before the module is
 initialized. */
-static fil_system_t*	fil_system	= NULL;
+fil_system_t*	fil_system	= NULL;
 
 
 /********************************************************************//**
@@ -308,7 +308,7 @@
 off the LRU list if it is in the LRU list. The caller must hold the fil_sys
 mutex. */
 static
-void
+ulint
 fil_node_prepare_for_io(
 /*====================*/
 	fil_node_t*	node,	/*!< in: file node */
@@ -633,7 +633,7 @@
 Opens a the file of a node of a tablespace. The caller must own the fil_system
 mutex. */
 static
-void
+ulint
 fil_node_open_file(
 /*===============*/
 	fil_node_t*	node,	/*!< in: file node */
@@ -667,7 +667,17 @@
 			node->name, OS_FILE_OPEN, OS_FILE_READ_ONLY, &success);
 		if (!success) {
 			/* The following call prints an error message */
-			os_file_get_last_error(TRUE);
+			if (os_file_get_last_error(TRUE) == OS_FILE_NOT_FOUND)
+			{
+				ut_print_timestamp(stderr);
+				fprintf(stderr,
+					" InnoDB: Warning: cannot open %s\n"
+					"InnoDB: this can happen if the table "
+					"was removed or renamed during an \n"
+					"InnoDB: xtrabackup run and is not dangerous.\n",
+					node->name);
+				return(OS_FILE_NOT_FOUND);
+			}
 
 			ut_print_timestamp(stderr);
 
@@ -728,12 +738,14 @@
 
 		if (UNIV_UNLIKELY(space_id != space->id)) {
 			fprintf(stderr,
-				"InnoDB: Error: tablespace id is %lu"
+				"InnoDB: Warning: tablespace id is %lu"
 				" in the data dictionary\n"
-				"InnoDB: but in file %s it is %lu!\n",
+				"InnoDB: but in file %s it is %lu!\n"
+				"InnoDB: this can happen if the table metadata "
+				"was modified during an xtrabackup run\n"
+				"InnoDB: and is not dangerous.\n",
 				space->id, node->name, space_id);
-
-			ut_error;
+			return(OS_FILE_NOT_FOUND);
 		}
 
 		if (UNIV_UNLIKELY(space_id == ULINT_UNDEFINED
@@ -757,8 +769,8 @@
 		}
 
 		if (size_bytes >= 1024 * 1024) {
-			/* Truncate the size to whole megabytes. */
-			size_bytes = ut_2pow_round(size_bytes, 1024 * 1024);
+			/* The size should be exact for after applying .delta */
+			//size_bytes = ut_2pow_round(size_bytes, 1024 * 1024);
 		}
 
 		if (!(flags & DICT_TF_ZSSIZE_MASK)) {
@@ -803,6 +815,8 @@
 		/* Put the node to the LRU list */
 		UT_LIST_ADD_FIRST(LRU, system->LRU, node);
 	}
+
+	return(0);
 }
 
 /**********************************************************************//**
@@ -1431,7 +1445,12 @@
 		the file yet; the following calls will open it and update the
 		size fields */
 
-		fil_node_prepare_for_io(node, fil_system, space);
+		if (fil_node_prepare_for_io(node, fil_system, space))
+		{
+			mutex_exit(&fil_system->mutex);
+
+			return(0);
+		}
 		fil_node_complete_io(node, fil_system, OS_FILE_READ);
 	}
 
@@ -1483,7 +1502,12 @@
 		the file yet; the following calls will open it and update the
 		size fields */
 
-		fil_node_prepare_for_io(node, fil_system, space);
+		if (fil_node_prepare_for_io(node, fil_system, space))
+		{
+			mutex_exit(&fil_system->mutex);
+
+			return(ULINT_UNDEFINED);
+		}
 		fil_node_complete_io(node, fil_system, OS_FILE_READ);
 	}
 
@@ -1933,7 +1957,7 @@
 	mem_free(path);
 }
 
-#ifndef UNIV_HOTBACKUP
+#if 0
 /********************************************************//**
 Writes a log record about an .ibd file create/rename/delete. */
 static
@@ -2157,7 +2181,7 @@
 			if (fil_create_new_single_table_tablespace(
 				    space_id, name, FALSE, flags,
 				    FIL_IBD_FILE_INITIAL_SIZE) != DB_SUCCESS) {
-				ut_error;
+				//ut_error;
 			}
 		}
 
@@ -2324,7 +2348,7 @@
 	}
 
 	if (success) {
-#ifndef UNIV_HOTBACKUP
+#ifdef UNDEFINED
 		/* Write a log record about the deletion of the .ibd
 		file, so that ibbackup can replay it in the
 		--apply-log phase. We use a dummy mtr and the familiar
@@ -2625,7 +2649,7 @@
 
 	mutex_exit(&fil_system->mutex);
 
-#ifndef UNIV_HOTBACKUP
+#ifdef UNDEFINED
 	if (success) {
 		mtr_t		mtr;
 
@@ -2815,7 +2839,7 @@
 
 	fil_node_create(path, size, space_id, FALSE);
 
-#ifndef UNIV_HOTBACKUP
+#ifdef UNDEFINED
 	{
 		mtr_t		mtr;
 
@@ -3159,19 +3183,97 @@
 		      "InnoDB: open the tablespace file ", stderr);
 		ut_print_filename(stderr, filepath);
 		fputs("!\n"
-		      "InnoDB: Have you moved InnoDB .ibd files around"
-		      " without using the\n"
-		      "InnoDB: commands DISCARD TABLESPACE and"
-		      " IMPORT TABLESPACE?\n"
-		      "InnoDB: It is also possible that this is"
-		      " a temporary table #sql...,\n"
-		      "InnoDB: and MySQL removed the .ibd file for this.\n"
-		      "InnoDB: Please refer to\n"
-		      "InnoDB: " REFMAN "innodb-troubleshooting-datadict.html\n"
-		      "InnoDB: for how to resolve the issue.\n", stderr);
+		      "InnoDB: It will be removed from data dictionary.\n"
+		      , stderr);
 
 		mem_free(filepath);
 
+		/* removing from data dictionary */
+		{
+			trx_t*		trx;
+			pars_info_t*	info = NULL;
+
+			trx = trx_allocate_for_mysql();
+
+			trx->op_info = "removing invalid table from data dictionary";
+
+			info = pars_info_create();
+
+			pars_info_add_str_literal(info, "table_name", name);
+
+			que_eval_sql(info,
+			   "PROCEDURE DROP_TABLE_PROC () IS\n"
+			   "sys_foreign_id CHAR;\n"
+			   "table_id CHAR;\n"
+			   "index_id CHAR;\n"
+			   "foreign_id CHAR;\n"
+			   "found INT;\n"
+			   "BEGIN\n"
+			   "SELECT ID INTO table_id\n"
+			   "FROM SYS_TABLES\n"
+			   "WHERE NAME = :table_name\n"
+			   "LOCK IN SHARE MODE;\n"
+			   "IF (SQL % NOTFOUND) THEN\n"
+			   "       RETURN;\n"
+			   "END IF;\n"
+			   "found := 1;\n"
+			   "SELECT ID INTO sys_foreign_id\n"
+			   "FROM SYS_TABLES\n"
+			   "WHERE NAME = 'SYS_FOREIGN'\n"
+			   "LOCK IN SHARE MODE;\n"
+			   "IF (SQL % NOTFOUND) THEN\n"
+			   "       found := 0;\n"
+			   "END IF;\n"
+			   "IF (:table_name = 'SYS_FOREIGN') THEN\n"
+			   "       found := 0;\n"
+			   "END IF;\n"
+			   "IF (:table_name = 'SYS_FOREIGN_COLS') THEN\n"
+			   "       found := 0;\n"
+			   "END IF;\n"
+			   "WHILE found = 1 LOOP\n"
+			   "       SELECT ID INTO foreign_id\n"
+			   "       FROM SYS_FOREIGN\n"
+			   "       WHERE FOR_NAME = :table_name\n"
+			   "               AND TO_BINARY(FOR_NAME)\n"
+			   "                 = TO_BINARY(:table_name)\n"
+			   "               LOCK IN SHARE MODE;\n"
+			   "       IF (SQL % NOTFOUND) THEN\n"
+			   "               found := 0;\n"
+			   "       ELSE\n"
+			   "               DELETE FROM SYS_FOREIGN_COLS\n"
+			   "               WHERE ID = foreign_id;\n"
+			   "               DELETE FROM SYS_FOREIGN\n"
+			   "               WHERE ID = foreign_id;\n"
+			   "       END IF;\n"
+			   "END LOOP;\n"
+			   "found := 1;\n"
+			   "WHILE found = 1 LOOP\n"
+			   "       SELECT ID INTO index_id\n"
+			   "       FROM SYS_INDEXES\n"
+			   "       WHERE TABLE_ID = table_id\n"
+			   "       LOCK IN SHARE MODE;\n"
+			   "       IF (SQL % NOTFOUND) THEN\n"
+			   "               found := 0;\n"
+			   "       ELSE\n"
+			   "               DELETE FROM SYS_FIELDS\n"
+			   "               WHERE INDEX_ID = index_id;\n"
+			   "               DELETE FROM SYS_INDEXES\n"
+			   "               WHERE ID = index_id\n"
+			   "               AND TABLE_ID = table_id;\n"
+			   "       END IF;\n"
+			   "END LOOP;\n"
+			   "DELETE FROM SYS_COLUMNS\n"
+			   "WHERE TABLE_ID = table_id;\n"
+			   "DELETE FROM SYS_TABLES\n"
+			   "WHERE ID = table_id;\n"
+			   "END;\n"
+			   , FALSE, trx);
+
+			trx_commit_for_mysql(trx);
+
+			trx_free_for_mysql(trx);
+		}
+
 		return(FALSE);
 	}
 
@@ -4106,7 +4208,7 @@
 	cannot be ok. */
 
 	size = (((ib_int64_t)size_high) << 32) + (ib_int64_t)size_low;
-#ifndef UNIV_HOTBACKUP
+#ifdef UNDEFINED
 	if (size < FIL_IBD_FILE_INITIAL_SIZE * (lint)UNIV_PAGE_SIZE) {
 		fprintf(stderr,
 			"InnoDB: Error: the size of single-table tablespace"
@@ -4247,7 +4349,7 @@
 idea is to read as much good data as we can and jump over bad data.
 @return 0 if ok, -1 if error even after the retries, 1 if at the end
 of the directory */
-static
+//static
 int
 fil_file_readdir_next_file(
 /*=======================*/
@@ -4546,15 +4648,97 @@
 				"InnoDB: in InnoDB data dictionary"
 				" has tablespace id %lu,\n"
 				"InnoDB: but tablespace with that id"
-				" or name does not exist. Have\n"
-				"InnoDB: you deleted or moved .ibd files?\n"
-				"InnoDB: This may also be a table created with"
-				" CREATE TEMPORARY TABLE\n"
-				"InnoDB: whose .ibd and .frm files"
-				" MySQL automatically removed, but the\n"
-				"InnoDB: table still exists in the"
-				" InnoDB internal data dictionary.\n",
+				" or name does not exist. It will be removed from data dictionary.\n"
+				,
 				(ulong) id);
+			mem_free(path);
+			mutex_exit(&fil_system->mutex);
+			/* removing from data dictionary */
+			{
+				trx_t*		trx;
+				pars_info_t*	info = NULL;
+
+				trx = trx_allocate_for_mysql();
+
+				trx->op_info = "removing invalid table from data dictionary";
+
+				info = pars_info_create();
+
+				pars_info_add_str_literal(info, "table_name", name);
+
+				que_eval_sql(info,
+				   "PROCEDURE DROP_TABLE_PROC () IS\n"
+				   "sys_foreign_id CHAR;\n"
+				   "table_id CHAR;\n"
+				   "index_id CHAR;\n"
+				   "foreign_id CHAR;\n"
+				   "found INT;\n"
+				   "BEGIN\n"
+				   "SELECT ID INTO table_id\n"
+				   "FROM SYS_TABLES\n"
+				   "WHERE NAME = :table_name\n"
+				   "LOCK IN SHARE MODE;\n"
+				   "IF (SQL % NOTFOUND) THEN\n"
+				   "       RETURN;\n"
+				   "END IF;\n"
+				   "found := 1;\n"
+				   "SELECT ID INTO sys_foreign_id\n"
+				   "FROM SYS_TABLES\n"
+				   "WHERE NAME = 'SYS_FOREIGN'\n"
+				   "LOCK IN SHARE MODE;\n"
+				   "IF (SQL % NOTFOUND) THEN\n"
+				   "       found := 0;\n"
+				   "END IF;\n"
+				   "IF (:table_name = 'SYS_FOREIGN') THEN\n"
+				   "       found := 0;\n"
+				   "END IF;\n"
+				   "IF (:table_name = 'SYS_FOREIGN_COLS') THEN\n"
+				   "       found := 0;\n"
+				   "END IF;\n"
+				   "WHILE found = 1 LOOP\n"
+				   "       SELECT ID INTO foreign_id\n"
+				   "       FROM SYS_FOREIGN\n"
+				   "       WHERE FOR_NAME = :table_name\n"
+				   "               AND TO_BINARY(FOR_NAME)\n"
+				   "                 = TO_BINARY(:table_name)\n"
+				   "               LOCK IN SHARE MODE;\n"
+				   "       IF (SQL % NOTFOUND) THEN\n"
+				   "               found := 0;\n"
+				   "       ELSE\n"
+				   "               DELETE FROM SYS_FOREIGN_COLS\n"
+				   "               WHERE ID = foreign_id;\n"
+				   "               DELETE FROM SYS_FOREIGN\n"
+				   "               WHERE ID = foreign_id;\n"
+				   "       END IF;\n"
+				   "END LOOP;\n"
+				   "found := 1;\n"
+				   "WHILE found = 1 LOOP\n"
+				   "       SELECT ID INTO index_id\n"
+				   "       FROM SYS_INDEXES\n"
+				   "       WHERE TABLE_ID = table_id\n"
+				   "       LOCK IN SHARE MODE;\n"
+				   "       IF (SQL % NOTFOUND) THEN\n"
+				   "               found := 0;\n"
+				   "       ELSE\n"
+				   "               DELETE FROM SYS_FIELDS\n"
+				   "               WHERE INDEX_ID = index_id;\n"
+				   "               DELETE FROM SYS_INDEXES\n"
+				   "               WHERE ID = index_id\n"
+				   "               AND TABLE_ID = table_id;\n"
+				   "       END IF;\n"
+				   "END LOOP;\n"
+				   "DELETE FROM SYS_COLUMNS\n"
+				   "WHERE TABLE_ID = table_id;\n"
+				   "DELETE FROM SYS_TABLES\n"
+				   "WHERE ID = table_id;\n"
+				   "END;\n"
+				   , FALSE, trx);
+
+				trx_commit_for_mysql(trx);
+
+				trx_free_for_mysql(trx);
+			}
+			return(FALSE);
 		} else {
 			ut_print_timestamp(stderr);
 			fputs("  InnoDB: Error: table ", stderr);
@@ -4953,7 +5137,7 @@
 off the LRU list if it is in the LRU list. The caller must hold the fil_sys
 mutex. */
 static
-void
+ulint
 fil_node_prepare_for_io(
 /*====================*/
 	fil_node_t*	node,	/*!< in: file node */
@@ -4973,10 +5157,13 @@
 	}
 
 	if (node->open == FALSE) {
+		ulint	err;
 		/* File is closed: open it */
 		ut_a(node->n_pending == 0);
 
-		fil_node_open_file(node, system, space);
+		err = fil_node_open_file(node, system, space);
+		if (err)
+			return(err);
 	}
 
 	if (node->n_pending == 0 && space->purpose == FIL_TABLESPACE
@@ -4989,6 +5176,8 @@
 	}
 
 	node->n_pending++;
+
+	return(0);
 }
 
 /********************************************************************//**
@@ -5125,7 +5314,9 @@
 	ut_ad(recv_no_ibuf_operations || (type == OS_FILE_WRITE)
 	      || !ibuf_bitmap_page(zip_size, block_offset)
 	      || sync || is_log);
-	ut_ad(!ibuf_inside() || is_log || (type == OS_FILE_WRITE)
+	/* ibuf_page() always returns FALSE with srv_fake_write=1 */
+	ut_ad(srv_fake_write ||
+	      !ibuf_inside() || is_log || (type == OS_FILE_WRITE)
 	      || ibuf_page(space_id, zip_size, block_offset, NULL));
 # endif /* UNIV_LOG_DEBUG */
 	if (sync) {
@@ -5190,6 +5381,16 @@
 
 	ut_ad((mode != OS_AIO_IBUF) || (space->purpose == FIL_TABLESPACE));
 
+	if (space->size <= block_offset) {
+		ulint	actual_size;
+
+		mutex_exit(&fil_system->mutex);
+		fil_extend_space_to_desired_size(&actual_size, space->id,
+						 ((block_offset + 1) / 64 + 1) * 64);
+		mutex_enter(&fil_system->mutex);
+		/* should retry? but it may safe for xtrabackup for now. */
+	}
+
 	node = UT_LIST_GET_FIRST(space->chain);
 
 	for (;;) {
--- a/storage/innodb_plugin/handler/ha_innodb.cc
+++ b/storage/innodb_plugin/handler/ha_innodb.cc
@@ -365,12 +365,6 @@
   "Timeout in seconds an InnoDB transaction may wait for a lock before being rolled back. Values above 100000000 disable the timeout.",
   NULL, NULL, 50, 1, 1024 * 1024 * 1024, 0);
 
-static MYSQL_THDVAR_ULONG(flush_log_at_trx_commit_session, PLUGIN_VAR_RQCMDARG,
-  "Control innodb_flush_log_at_trx_commit for each sessions. "
-  "The value 0~2 are same meanings to innodb_flush_log_at_trx_commit. "
-  "The value 3 regards innodb_flush_log_at_trx_commit (default).",
-  NULL, NULL, 3, 0, 3, 0);
-
 static MYSQL_THDVAR_BOOL(fake_changes, PLUGIN_VAR_OPCMDARG,
   "In the transaction after enabled, UPDATE, INSERT and DELETE only move the cursor to the records "
   "and do nothing other operations (no changes, no ibuf, no undo, no transaction log) in the transaction. "
@@ -760,17 +754,6 @@
 }
 
 /******************************************************************//**
-*/
-extern "C" UNIV_INTERN
-ulong
-thd_flush_log_at_trx_commit_session(
-/*================================*/
-	void*	thd)
-{
-	return(THDVAR((THD*) thd, flush_log_at_trx_commit_session));
-}
-
-/******************************************************************//**
 Returns true if expand_fast_index_creation is enabled for the current
 session.
 @return	the value of the server's expand_fast_index_creation variable */
@@ -11966,7 +11949,6 @@
   MYSQL_SYSVAR(flush_neighbor_pages),
   MYSQL_SYSVAR(read_ahead),
   MYSQL_SYSVAR(adaptive_checkpoint),
-  MYSQL_SYSVAR(flush_log_at_trx_commit_session),
   MYSQL_SYSVAR(enable_unsafe_group_commit),
   MYSQL_SYSVAR(expand_import),
   MYSQL_SYSVAR(extra_rsegments),
--- a/storage/innodb_plugin/ibuf/ibuf0ibuf.c
+++ b/storage/innodb_plugin/ibuf/ibuf0ibuf.c
@@ -1064,6 +1064,9 @@
 
 	ut_ad(!recv_no_ibuf_operations);
 
+	if (srv_fake_write)
+		return(FALSE);
+
 	if (ibuf_fixed_addr_page(space, zip_size, page_no)) {
 
 		return(TRUE);
@@ -2280,6 +2283,9 @@
 	ulint	n_bytes;
 	ulint	n_pag2;
 
+	if (srv_fake_write)
+		return(0);
+
 	while (sum_pages < n_pages) {
 		n_bytes = ibuf_contract_ext(&n_pag2, sync);
 
--- a/storage/innodb_plugin/include/ha_prototypes.h
+++ b/storage/innodb_plugin/include/ha_prototypes.h
@@ -269,14 +269,6 @@
 			the global innodb_lock_wait_timeout */
 
 /******************************************************************//**
-*/
-
-ulong
-thd_flush_log_at_trx_commit_session(
-/*================================*/
-	void*	thd);
-
-/******************************************************************//**
 Returns true if innodb_expand_fast_index_creation is enabled for the current
 session.
 @return	the value of the server's innodb_expand_fast_index_creation variable */
--- a/storage/innodb_plugin/include/log0recv.h
+++ b/storage/innodb_plugin/include/log0recv.h
@@ -444,7 +444,7 @@
 /* defined in os0file.c */
 #define OS_AIO_MERGE_N_CONSECUTIVE	64
 /* defined in log0recv.c */
-#define RECV_READ_AHEAD_AREA	32
+#define RECV_READ_AHEAD_AREA	128
 	time_t		stats_recv_start_time;
 	ulint		stats_recv_turns;
 
--- a/storage/innodb_plugin/include/mem0mem.ic
+++ b/storage/innodb_plugin/include/mem0mem.ic
@@ -367,7 +367,7 @@
 	buf = (byte*)buf + MEM_FIELD_HEADER_SIZE;
 
 	/* Check that the field lengths agree */
-	ut_ad(n == (ulint)mem_field_header_get_len(buf));
+	ut_ad(n == (ulint)mem_field_header_get_len((byte *)buf));
 #endif
 
 	return(buf);
--- a/storage/innodb_plugin/include/srv0srv.h
+++ b/storage/innodb_plugin/include/srv0srv.h
@@ -226,6 +226,10 @@
 
 extern ulong	srv_replication_delay;
 
+extern ibool	srv_read_only;
+extern ibool	srv_fake_write;
+extern ibool	srv_apply_log_only;
+
 extern long long	srv_ibuf_max_size;
 extern ulint	srv_ibuf_active_contract;
 extern ulint	srv_ibuf_accel_rate;
--- a/storage/innodb_plugin/include/srv0start.h
+++ b/storage/innodb_plugin/include/srv0start.h
@@ -91,6 +91,8 @@
 /** Log sequence number immediately after startup */
 extern	ib_uint64_t	srv_start_lsn;
 
+extern	ib_uint64_t	srv_oldest_lsn;
+
 #ifdef __NETWARE__
 void set_panic_flag_for_netware(void);
 #endif
--- a/storage/innodb_plugin/include/trx0trx.h
+++ b/storage/innodb_plugin/include/trx0trx.h
@@ -508,7 +508,6 @@
 					FALSE, one can save CPU time and about
 					150 bytes in the undo log size as then
 					we skip XA steps */
-	ulint		flush_log_at_trx_commit_session;
 	ulint		fake_changes;
 	ulint		flush_log_later;/* In 2PC, we hold the
 					prepare_commit mutex across
--- a/storage/innodb_plugin/include/ut0byte.ic
+++ b/storage/innodb_plugin/include/ut0byte.ic
@@ -168,6 +168,16 @@
 	dulint	a,	/*!< in: dulint */
 	ulint	b)	/*!< in: ulint */
 {
+#if SIZEOF_LONG != 4
+	{
+		ulint	b_h;
+
+		b_h = b >> 32;
+		b &= 0xFFFFFFFFUL;
+		a.high += b_h;
+	}
+#endif
+
 	if (0xFFFFFFFFUL - b >= a.low) {
 		a.low += b;
 
@@ -191,6 +201,16 @@
 	dulint	a,	/*!< in: dulint */
 	ulint	b)	/*!< in: ulint, b <= a */
 {
+#if SIZEOF_LONG != 4
+	{
+		ulint	b_h;
+
+		b_h = b >> 32;
+		b &= 0xFFFFFFFFUL;
+		a.high -= b_h;
+	}
+#endif
+
 	if (a.low >= b) {
 		a.low -= b;
 
@@ -235,6 +255,10 @@
 
 	ut_ad(diff > a.low);
 
+#if SIZEOF_LONG != 4
+		diff += (a.high - b.high - 1) << 32;
+#endif
+
 	return(diff);
 }
 
--- a/storage/innodb_plugin/log/log0log.c
+++ b/storage/innodb_plugin/log/log0log.c
@@ -1370,7 +1370,7 @@
 #endif /* UNIV_DEBUG */
 	ulint		unlock;
 
-	if (recv_no_ibuf_operations) {
+	if (recv_no_ibuf_operations || srv_fake_write) {
 		/* Recovery is running and no operations on the log files are
 		allowed yet (the variable name .._no_ibuf_.. is misleading) */
 
@@ -2032,7 +2032,7 @@
 		return(TRUE);
 	}
 
-	ut_ad(log_sys->flushed_to_disk_lsn >= oldest_lsn);
+	ut_ad(srv_fake_write || log_sys->flushed_to_disk_lsn >= oldest_lsn);
 
 	if (log_sys->n_pending_checkpoint_writes > 0) {
 		/* A checkpoint write is running */
@@ -3127,6 +3127,7 @@
 	shutdown, because the InnoDB layer may have committed or
 	prepared transactions and we don't want to lose them. */
 
+	if (!srv_apply_log_only) {
 	if (trx_n_mysql_transactions > 0
 	    || UT_LIST_GET_LEN(trx_sys->trx_list) > trx_n_prepared) {
 
@@ -3134,6 +3135,7 @@
 
 		goto loop;
 	}
+	}
 
 	if (srv_fast_shutdown == 2) {
 		/* In this fastest shutdown we do not flush the buffer pool:
--- a/storage/innodb_plugin/log/log0recv.c
+++ b/storage/innodb_plugin/log/log0recv.c
@@ -42,27 +42,27 @@
 #include "trx0undo.h"
 #include "trx0rec.h"
 #include "fil0fil.h"
-#ifndef UNIV_HOTBACKUP
+//#ifndef UNIV_HOTBACKUP
 # include "buf0rea.h"
 # include "srv0srv.h"
 # include "srv0start.h"
 # include "trx0roll.h"
 # include "row0merge.h"
 # include "sync0sync.h"
-#else /* !UNIV_HOTBACKUP */
+//#else /* !UNIV_HOTBACKUP */
 
 /** This is set to FALSE if the backup was originally taken with the
 ibbackup --include regexp option: then we do not want to create tables in
 directories which were not included */
 UNIV_INTERN ibool	recv_replay_file_ops	= TRUE;
-#endif /* !UNIV_HOTBACKUP */
+//#endif /* !UNIV_HOTBACKUP */
 
 /** Log records are stored in the hash table in chunks at most of this size;
 this must be less than UNIV_PAGE_SIZE as it is stored in the buffer pool */
 #define RECV_DATA_BLOCK_SIZE	(MEM_MAX_ALLOC_IN_BUF - sizeof(recv_data_t))
 
 /** Read-ahead area in applying log records to file pages */
-#define RECV_READ_AHEAD_AREA	32
+#define RECV_READ_AHEAD_AREA	128
 
 /** The recovery system */
 UNIV_INTERN recv_sys_t*	recv_sys = NULL;
@@ -621,7 +621,7 @@
 /***********************************************************************//**
 Checks the consistency of the checkpoint info
 @return	TRUE if ok */
-static
+//static
 ibool
 recv_check_cp_is_consistent(
 /*========================*/
@@ -651,7 +651,7 @@
 /********************************************************//**
 Looks for the maximum consistent checkpoint from the log groups.
 @return	error code or DB_SUCCESS */
-static
+//static
 ulint
 recv_find_max_checkpoint(
 /*=====================*/
@@ -840,7 +840,7 @@
 InnoDB-3.23.52 where the checksum field contains the log block number.
 @return TRUE if ok, or if the log block may be in the format of InnoDB
 version predating 3.23.52 */
-static
+//static
 ibool
 log_block_checksum_is_ok_or_old_format(
 /*===================================*/
@@ -2369,7 +2369,7 @@
 			   || type == MLOG_FILE_RENAME
 			   || type == MLOG_FILE_DELETE) {
 			ut_a(space);
-#ifdef UNIV_HOTBACKUP
+//#ifdef UNIV_HOTBACKUP
 			if (recv_replay_file_ops) {
 
 				/* In ibbackup --apply-log, replay an .ibd file
@@ -2392,7 +2392,7 @@
 					ut_error;
 				}
 			}
-#endif
+//#endif
 			/* In normal mysqld crash recovery we do not try to
 			replay file operations */
 #ifdef UNIV_LOG_LSN_DEBUG
@@ -2809,8 +2809,11 @@
 
 			fprintf(stderr,
 				"InnoDB: Doing recovery: scanned up to"
-				" log sequence number %llu\n",
-				*group_scanned_lsn);
+				" log sequence number %llu (%lu %%)\n",
+				*group_scanned_lsn,
+				(ulong) (*group_scanned_lsn - srv_oldest_lsn)
+				/ (8 * log_group_get_capacity(UT_LIST_GET_FIRST(log_sys->log_groups))/900)
+			);
 		}
 	}
 
@@ -2922,12 +2925,14 @@
 
 	if (srv_force_recovery < SRV_FORCE_NO_LOG_REDO) {
 
+#ifdef UNDEFINED
 		fprintf(stderr,
 			"InnoDB: Restoring possible"
 			" half-written data pages from"
 			" the doublewrite\n"
 			"InnoDB: buffer...\n");
-		trx_sys_doublewrite_init_or_restore_pages(TRUE);
+#endif
+		trx_sys_doublewrite_init_or_restore_pages(FALSE);
 	}
 }
 
@@ -3095,6 +3100,7 @@
 		recv_sys->recovered_lsn = checkpoint_lsn;
 
 		srv_start_lsn = checkpoint_lsn;
+		srv_oldest_lsn = checkpoint_lsn;
 	}
 
 	contiguous_lsn = ut_uint64_align_down(recv_sys->scanned_lsn,
@@ -3453,6 +3459,7 @@
 	that the data dictionary tables will be free of any locks.
 	The data dictionary latch should guarantee that there is at
 	most one data dictionary transaction active at a time. */
+	if (!srv_apply_log_only)
 	trx_rollback_or_clean_recovered(FALSE);
 }
 
--- a/storage/innodb_plugin/os/os0file.c
+++ b/storage/innodb_plugin/os/os0file.c
@@ -555,7 +555,7 @@
 }
 
 #undef USE_FILE_LOCK
-#define USE_FILE_LOCK
+//#define USE_FILE_LOCK
 #if defined(UNIV_HOTBACKUP) || defined(__WIN__) || defined(__NETWARE__)
 /* InnoDB Hot Backup does not lock the data files.
  * On Windows, mandatory locking is used.
@@ -1357,8 +1357,9 @@
 		attributes = 0;
 		ut_error;
 	}
-
+	share_mode |= FILE_SHARE_WRITE; /* Why? */
 	file = CreateFile((LPCTSTR) name,
+			  (srv_read_only && create_flag == OPEN_EXISTING) ? GENERIC_READ :
 			  GENERIC_READ | GENERIC_WRITE, /* read and write
 							access */
 			  share_mode,	/* File can be read also by other
@@ -1417,7 +1418,11 @@
 	if (create_mode == OS_FILE_OPEN || create_mode == OS_FILE_OPEN_RAW
 	    || create_mode == OS_FILE_OPEN_RETRY) {
 		mode_str = "OPEN";
-		create_flag = O_RDWR;
+		if (srv_read_only) {
+			create_flag = O_RDONLY;
+		} else {
+			create_flag = O_RDWR;
+		}
 	} else if (create_mode == OS_FILE_CREATE) {
 		mode_str = "CREATE";
 		create_flag = O_RDWR | O_CREAT | O_EXCL;
@@ -2682,6 +2687,9 @@
 	ut_a((offset & 0xFFFFFFFFUL) == offset);
 	ut_a((n & 0xFFFFFFFFUL) == n);
 
+	if (srv_fake_write)
+		return(TRUE);
+
 	os_n_file_writes++;
 
 	ut_ad(file);
@@ -2806,6 +2814,9 @@
 #else
 	ssize_t	ret;
 
+	if (srv_fake_write)
+		return(TRUE);
+
 	ret = os_file_pwrite(file, buf, n, offset, offset_high);
 
 	if ((ulint)ret == n) {
--- a/storage/innodb_plugin/os/os0thread.c
+++ b/storage/innodb_plugin/os/os0thread.c
@@ -287,12 +287,17 @@
 #elif defined(__NETWARE__)
 	delay(tm / 1000);
 #else
+	/* select() simetimes hang up from xtrabackup */
+	/* change to use usleep() for now */
+	usleep(tm);
+/*
 	struct timeval	t;
 
 	t.tv_sec = tm / 1000000;
 	t.tv_usec = tm % 1000000;
 
 	select(0, NULL, NULL, NULL, &t);
+*/
 #endif
 }
 
--- a/storage/innodb_plugin/row/row0merge.c
+++ b/storage/innodb_plugin/row/row0merge.c
@@ -454,7 +454,9 @@
 	rec = rec_convert_dtuple_to_rec(*buf, index, tuple, n_ext);
 	offsets = rec_get_offsets(rec, index, NULL, ULINT_UNDEFINED, &heap);
 
-	innobase_rec_to_mysql(dup->table, rec, index, offsets);
+	//innobase_rec_to_mysql(dup->table, rec, index, offsets);
+	fprintf(stderr, "InnoDB: Error: row_merge_dup_report() is called.\n");
+	ut_error;
 
 	mem_heap_free(heap);
 }
@@ -1465,8 +1467,11 @@
 		case 0:
 			if (UNIV_UNLIKELY
 			    (dict_index_is_unique(index) && !null_eq)) {
-				innobase_rec_to_mysql(table, mrec0,
-						      index, offsets0);
+				//innobase_rec_to_mysql(table, mrec0,
+				//		      index, offsets0);
+				(void) table;
+				fprintf(stderr, "InnoDB: Error: row_merge_blocks() is called.\n");
+				ut_error;
 				mem_heap_free(heap);
 				return(DB_DUPLICATE_KEY);
 			}
@@ -2594,7 +2599,9 @@
 
 	/* Reset the MySQL row buffer that is used when reporting
 	duplicate keys. */
-	innobase_rec_reset(table);
+	//innobase_rec_reset(table);
+	fprintf(stderr, "InnoDB: Error: row_merge_build_indexes() is called.\n");
+	ut_error;
 
 	/* Read clustered index of the table and create files for
 	secondary index entries for merge sort */
--- a/storage/innodb_plugin/srv/srv0srv.c
+++ b/storage/innodb_plugin/srv/srv0srv.c
@@ -86,11 +86,6 @@
 #include "trx0i_s.h"
 #include "os0sync.h" /* for HAVE_ATOMIC_BUILTINS */
 
-/* prototypes of new functions added to ha_innodb.cc for kill_idle_transaction */
-ibool		innobase_thd_is_idle(const void* thd);
-ib_int64_t	innobase_thd_get_start_time(const void* thd);
-void		innobase_thd_kill(void* thd);
-
 /* prototypes for new functions added to ha_innodb.cc */
 ibool	innobase_get_slow_log();
 
@@ -415,6 +410,10 @@
 
 UNIV_INTERN ulong	srv_replication_delay		= 0;
 
+UNIV_INTERN ibool	srv_read_only	= FALSE;
+UNIV_INTERN ibool	srv_fake_write	= FALSE;
+UNIV_INTERN ibool	srv_apply_log_only = FALSE;
+
 UNIV_INTERN long long	srv_ibuf_max_size = 0;
 UNIV_INTERN ulint	srv_ibuf_active_contract = 0; /* 0:disable 1:enable */
 UNIV_INTERN ulint	srv_ibuf_accel_rate = 100;
@@ -1065,7 +1064,7 @@
 	}
 
 	/* Initialize some INFORMATION SCHEMA internal structures */
-	trx_i_s_cache_init(trx_i_s_cache);
+	//trx_i_s_cache_init(trx_i_s_cache);
 }
 
 /*********************************************************************//**
@@ -1076,6 +1075,7 @@
 /*==========*/
 {
 	os_fast_mutex_free(&srv_conc_mutex);
+#ifdef UNDEFINED
 	mem_free(srv_conc_slots);
 	srv_conc_slots = NULL;
 
@@ -1089,6 +1089,7 @@
 	srv_mysql_table = NULL;
 
 	trx_i_s_cache_free(trx_i_s_cache);
+#endif
 }
 
 /*********************************************************************//**
@@ -2570,36 +2571,6 @@
 		old_sema = sema;
 	}
 
-	if (srv_kill_idle_transaction && trx_sys) {
-		trx_t*	trx;
-		time_t	now;
-rescan_idle:
-		now = time(NULL);
-		mutex_enter(&kernel_mutex);
-		trx = UT_LIST_GET_FIRST(trx_sys->mysql_trx_list);
-		while (trx) {
-			if (trx->conc_state == TRX_ACTIVE
-			    && trx->mysql_thd
-			    && innobase_thd_is_idle(trx->mysql_thd)) {
-				ib_int64_t	start_time; /* as stmt ID */
-
-				start_time = innobase_thd_get_start_time(trx->mysql_thd);
-				if (trx->last_stmt_start != start_time) {
-					trx->idle_start = now;
-					trx->last_stmt_start = start_time;
-				} else if (difftime(now, trx->idle_start)
-					   > srv_kill_idle_transaction) {
-					/* kill the session */
-					mutex_exit(&kernel_mutex);
-					innobase_thd_kill(trx->mysql_thd);
-					goto rescan_idle;
-				}
-			}
-			trx = UT_LIST_GET_NEXT(mysql_trx_list, trx);
-		}
-		mutex_exit(&kernel_mutex);
-	}
-
 	/* Flush stderr so that a database user gets the output
 	to possible MySQL error file */
 
--- a/storage/innodb_plugin/srv/srv0start.c
+++ b/storage/innodb_plugin/srv/srv0start.c
@@ -95,6 +95,8 @@
 /** Log sequence number at shutdown */
 UNIV_INTERN ib_uint64_t	srv_shutdown_lsn;
 
+UNIV_INTERN ib_uint64_t srv_oldest_lsn;
+
 #ifdef HAVE_DARWIN_THREADS
 # include <sys/utsname.h>
 /** TRUE if the F_FULLFSYNC option is available */
@@ -545,7 +547,7 @@
 /*********************************************************************//**
 Creates or opens the log files and closes them.
 @return	DB_SUCCESS or error code */
-static
+//static
 ulint
 open_or_create_log_file(
 /*====================*/
@@ -703,7 +705,7 @@
 /*********************************************************************//**
 Creates or opens database data files and closes them.
 @return	DB_SUCCESS or error code */
-static
+//static
 ulint
 open_or_create_data_files(
 /*======================*/
@@ -1782,6 +1784,10 @@
 		are initialized in trx_sys_init_at_db_start(). */
 
 		recv_recovery_from_checkpoint_finish();
+
+		if (srv_apply_log_only)
+			goto skip_processes;
+
 		if (srv_force_recovery < SRV_FORCE_NO_IBUF_MERGE) {
 			/* The following call is necessary for the insert
 			buffer to work with multiple tablespaces. We must
@@ -1962,7 +1968,18 @@
 
 	if (srv_auto_extend_last_data_file
 	    && sum_of_data_file_sizes < tablespace_size_in_header) {
+		/* extend table space size aligning with header */
+		ulint	actual_size;
+		fil_extend_space_to_desired_size(&actual_size, 0, tablespace_size_in_header);
+		if (actual_size < tablespace_size_in_header) {
+			fprintf(stderr,
+"InnoDB: Warning: To extend tablespace size aligning with header seems to be failed.\n"
+"InnoDB: The acutual size %lu must be larger than %lu.\n",
+				(ulong) actual_size,
+				(ulong) tablespace_size_in_header);
+		}
 
+#ifdef UNDEFINED
 		fprintf(stderr,
 			"InnoDB: Error: tablespace size stored in header"
 			" is %lu pages, but\n"
@@ -1987,6 +2004,7 @@
 
 			return(DB_ERROR);
 		}
+#endif
 	}
 
 	/* Check that os_fast_mutexes work as expected */
@@ -2089,6 +2107,7 @@
 		ibuf_update_max_tablespace_id();
 	}
 
+skip_processes:
 	srv_file_per_table = srv_file_per_table_original_value;
 
 	srv_was_started = TRUE;
--- a/storage/innodb_plugin/trx/trx0purge.c
+++ b/storage/innodb_plugin/trx/trx0purge.c
@@ -1113,6 +1113,9 @@
 	/*	que_thr_t*	thr2; */
 	ulint		old_pages_handled;
 
+	if (srv_fake_write)
+		return(0);
+
 	mutex_enter(&(purge_sys->mutex));
 
 	if (purge_sys->trx->n_active_thrs > 0) {
--- a/storage/innodb_plugin/trx/trx0rseg.c
+++ b/storage/innodb_plugin/trx/trx0rseg.c
@@ -143,9 +143,11 @@
 
 	mutex_free(&rseg->mutex);
 
+	if (!srv_apply_log_only) {
 	/* There can't be any active transactions. */
 	ut_a(UT_LIST_GET_LEN(rseg->update_undo_list) == 0);
 	ut_a(UT_LIST_GET_LEN(rseg->insert_undo_list) == 0);
+	}
 
 	undo = UT_LIST_GET_FIRST(rseg->update_undo_cached);
 
--- a/storage/innodb_plugin/trx/trx0sys.c
+++ b/storage/innodb_plugin/trx/trx0sys.c
@@ -1910,12 +1910,14 @@
 	mem_free(trx_doublewrite);
 	trx_doublewrite = NULL;
 
+	if (!srv_apply_log_only) {
 	/* Only prepared transactions may be left in the system. Free them. */
 	ut_a(UT_LIST_GET_LEN(trx_sys->trx_list) == trx_n_prepared);
 
 	while ((trx = UT_LIST_GET_FIRST(trx_sys->trx_list)) != NULL) {
 		trx_free_prepared(trx);
 	}
+	}
 
 	/* There can't be any active transactions. */
 	rseg = UT_LIST_GET_FIRST(trx_sys->rseg_list);
@@ -1941,10 +1943,12 @@
 		UT_LIST_REMOVE(view_list, trx_sys->view_list, prev_view);
 	}
 
+	if (!srv_apply_log_only) {
 	ut_a(UT_LIST_GET_LEN(trx_sys->trx_list) == 0);
 	ut_a(UT_LIST_GET_LEN(trx_sys->rseg_list) == 0);
 	ut_a(UT_LIST_GET_LEN(trx_sys->view_list) == 0);
 	ut_a(UT_LIST_GET_LEN(trx_sys->mysql_trx_list) == 0);
+	}
 
 	mem_free(trx_sys);
 
--- a/storage/innodb_plugin/trx/trx0trx.c
+++ b/storage/innodb_plugin/trx/trx0trx.c
@@ -112,8 +112,6 @@
 
 	trx->support_xa = TRUE;
 
-	trx->flush_log_at_trx_commit_session = 3; /* means to use innodb_flush_log_at_trx_commit value */
-
 	trx->fake_changes = FALSE;
 
 	trx->check_foreigns = TRUE;
@@ -556,8 +554,8 @@
 
 					if (srv_force_recovery == 0) {
 
-						trx->conc_state = TRX_PREPARED;
-						trx_n_prepared++;
+						/* xtrabackup should rollback it */
+						trx->conc_state = TRX_ACTIVE;
 					} else {
 						fprintf(stderr,
 							"InnoDB: Since"
@@ -633,10 +631,9 @@
 								trx->id));
 
 						if (srv_force_recovery == 0) {
-
+							/* xtrabackup should rollback it */
 							trx->conc_state
-								= TRX_PREPARED;
-							trx_n_prepared++;
+								= TRX_ACTIVE;
 						} else {
 							fprintf(stderr,
 								"InnoDB: Since"
@@ -801,9 +798,6 @@
 	generated by the same transaction, doesn't. */
 	trx->support_xa = thd_supports_xa(trx->mysql_thd);
 
-	trx->flush_log_at_trx_commit_session =
-		thd_flush_log_at_trx_commit_session(trx->mysql_thd);
-
 	mutex_enter(&kernel_mutex);
 
 	ret = trx_start_low(trx, rseg_id);
@@ -990,7 +984,6 @@
 	trx->read_view = NULL;
 
 	if (lsn) {
-		ulint	flush_log_at_trx_commit;
 
 		mutex_exit(&kernel_mutex);
 
@@ -999,11 +992,6 @@
 			trx_undo_insert_cleanup(trx);
 		}
 
-		if (trx->flush_log_at_trx_commit_session == 3) {
-			flush_log_at_trx_commit = srv_flush_log_at_trx_commit;
-		} else {
-			flush_log_at_trx_commit = trx->flush_log_at_trx_commit_session;
-		}
 
 		/* NOTE that we could possibly make a group commit more
 		efficient here: call os_thread_yield here to allow also other
@@ -1036,9 +1024,9 @@
 		if (trx->flush_log_later) {
 			/* Do nothing yet */
 			trx->must_flush_log_later = TRUE;
-		} else if (flush_log_at_trx_commit == 0) {
+		} else if (srv_flush_log_at_trx_commit == 0) {
 			/* Do nothing */
-		} else if (flush_log_at_trx_commit == 1) {
+		} else if (srv_flush_log_at_trx_commit == 1) {
 			if (srv_unix_file_flush_method == SRV_UNIX_NOSYNC) {
 				/* Write the log but do not flush it to disk */
 
@@ -1050,7 +1038,7 @@
 
 				log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, TRUE);
 			}
-		} else if (flush_log_at_trx_commit == 2) {
+		} else if (srv_flush_log_at_trx_commit == 2) {
 
 			/* Write the log but do not flush it to disk */
 
@@ -1730,23 +1718,16 @@
 	trx_t*	trx)	/*!< in: trx handle */
 {
 	ib_uint64_t	lsn	= trx->commit_lsn;
-	ulint		flush_log_at_trx_commit;
 
 	ut_a(trx);
 
 	trx->op_info = "flushing log";
 
-	if (trx->flush_log_at_trx_commit_session == 3) {
-		flush_log_at_trx_commit = srv_flush_log_at_trx_commit;
-	} else {
-		flush_log_at_trx_commit = trx->flush_log_at_trx_commit_session;
-	}
-
 	if (!trx->must_flush_log_later) {
 		/* Do nothing */
-	} else if (flush_log_at_trx_commit == 0) {
+	} else if (srv_flush_log_at_trx_commit == 0) {
 		/* Do nothing */
-	} else if (flush_log_at_trx_commit == 1) {
+	} else if (srv_flush_log_at_trx_commit == 1) {
 		if (srv_unix_file_flush_method == SRV_UNIX_NOSYNC) {
 			/* Write the log but do not flush it to disk */
 
@@ -1757,7 +1738,7 @@
 
 			log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, TRUE);
 		}
-	} else if (flush_log_at_trx_commit == 2) {
+	} else if (srv_flush_log_at_trx_commit == 2) {
 
 		/* Write the log but do not flush it to disk */
 
@@ -2018,8 +1999,6 @@
 	/*--------------------------------------*/
 
 	if (lsn) {
-		ulint	flush_log_at_trx_commit;
-
 		/* Depending on the my.cnf options, we may now write the log
 		buffer to the log files, making the prepared state of the
 		transaction durable if the OS does not crash. We may also
@@ -2039,15 +2018,9 @@
 
 		mutex_exit(&kernel_mutex);
 
-		if (trx->flush_log_at_trx_commit_session == 3) {
-			flush_log_at_trx_commit = srv_flush_log_at_trx_commit;
-		} else {
-			flush_log_at_trx_commit = trx->flush_log_at_trx_commit_session;
-		}
-
-		if (flush_log_at_trx_commit == 0) {
+		if (srv_flush_log_at_trx_commit == 0) {
 			/* Do nothing */
-		} else if (flush_log_at_trx_commit == 1) {
+		} else if (srv_flush_log_at_trx_commit == 1) {
 			if (srv_unix_file_flush_method == SRV_UNIX_NOSYNC) {
 				/* Write the log but do not flush it to disk */
 
@@ -2059,7 +2032,7 @@
 
 				log_write_up_to(lsn, LOG_WAIT_ONE_GROUP, TRUE);
 			}
-		} else if (flush_log_at_trx_commit == 2) {
+		} else if (srv_flush_log_at_trx_commit == 2) {
 
 			/* Write the log but do not flush it to disk */