File: bm-journal.patch

package info (click to toggle)
thin-provisioning-tools 1.1.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,300 kB
  • sloc: xml: 544; sh: 521; makefile: 133
file content (1505 lines) | stat: -rw-r--r-- 43,292 bytes parent folder | download | duplicates (4)
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
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
commit 6cb3772bdb92399319eb463e658ce62b692c669a
Author: Joe Thornber <ejt@redhat.com>
Date:   Mon Sep 24 14:48:16 2018 +0100

    [bm journal] Journalling version of block manager.
    
    Can be used to confirm we're crash proof with the thin_journal_check tool.

diff --git a/drivers/md/dm-cache-metadata.c b/drivers/md/dm-cache-metadata.c
index 69dddeab124c..f7b11f270846 100644
--- a/drivers/md/dm-cache-metadata.c
+++ b/drivers/md/dm-cache-metadata.c
@@ -106,6 +106,8 @@ struct dm_cache_metadata {
 
 	unsigned version;
 	struct block_device *bdev;
+	struct block_device *journal_dev;
+
 	struct dm_block_manager *bm;
 	struct dm_space_map *metadata_sm;
 	struct dm_transaction_manager *tm;
@@ -281,7 +283,7 @@ static int __superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
 		}
 	}
 
-	dm_bm_unlock(b);
+	dm_bm_unlock(bm, b);
 
 	return 0;
 }
@@ -504,12 +506,12 @@ static int __open_metadata(struct dm_cache_metadata *cmd)
 	dm_disk_bitset_init(cmd->tm, &cmd->discard_info);
 	sb_flags = le32_to_cpu(disk_super->flags);
 	cmd->clean_when_opened = test_bit(CLEAN_SHUTDOWN, &sb_flags);
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(cmd->bm, sblock);
 
 	return 0;
 
 bad:
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(cmd->bm, sblock);
 	return r;
 }
 
@@ -533,8 +535,9 @@ static int __create_persistent_data_objects(struct dm_cache_metadata *cmd,
 					    bool may_format_device)
 {
 	int r;
-	cmd->bm = dm_block_manager_create(cmd->bdev, DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
-					  CACHE_MAX_CONCURRENT_LOCKS);
+	cmd->bm = dm_block_manager_create_with_journal(cmd->bdev,
+                                          DM_CACHE_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
+					  CACHE_MAX_CONCURRENT_LOCKS, cmd->journal_dev);
 	if (IS_ERR(cmd->bm)) {
 		DMERR("could not create block manager");
 		return PTR_ERR(cmd->bm);
@@ -621,9 +624,8 @@ static int __begin_transaction_flags(struct dm_cache_metadata *cmd,
 	disk_super = dm_block_data(sblock);
 	update_flags(disk_super, mutator);
 	read_superblock_fields(cmd, disk_super);
-	dm_bm_unlock(sblock);
 
-	return dm_bm_flush(cmd->bm);
+	return dm_bm_flush_and_unlock(cmd->bm, sblock);
 }
 
 static int __begin_transaction(struct dm_cache_metadata *cmd)
@@ -642,7 +644,7 @@ static int __begin_transaction(struct dm_cache_metadata *cmd)
 
 	disk_super = dm_block_data(sblock);
 	read_superblock_fields(cmd, disk_super);
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(cmd->bm, sblock);
 
 	return 0;
 }
@@ -1775,7 +1777,7 @@ int dm_cache_metadata_set_needs_check(struct dm_cache_metadata *cmd)
 	disk_super = dm_block_data(sblock);
 	disk_super->flags = cpu_to_le32(cmd->flags);
 
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(cmd->bm, sblock);
 
 out:
 	WRITE_UNLOCK(cmd);
diff --git a/drivers/md/dm-era-target.c b/drivers/md/dm-era-target.c
index 8e48920a3ffa..4aad158a58e8 100644
--- a/drivers/md/dm-era-target.c
+++ b/drivers/md/dm-era-target.c
@@ -342,7 +342,7 @@ static int superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
 		}
 	}
 
-	dm_bm_unlock(b);
+	dm_bm_unlock(bm, b);
 
 	return 0;
 }
@@ -583,12 +583,12 @@ static int open_metadata(struct era_metadata *md)
 	md->metadata_snap = le64_to_cpu(disk->metadata_snap);
 	md->archived_writesets = true;
 
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(md->bm, sblock);
 
 	return 0;
 
 bad:
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(md->bm, sblock);
 	return r;
 }
 
diff --git a/drivers/md/dm-thin-metadata.c b/drivers/md/dm-thin-metadata.c
index 72142021b5c9..8420b67b0e51 100644
--- a/drivers/md/dm-thin-metadata.c
+++ b/drivers/md/dm-thin-metadata.c
@@ -146,6 +146,8 @@ struct dm_pool_metadata {
 	struct hlist_node hash;
 
 	struct block_device *bdev;
+	struct block_device *journal_dev;
+
 	struct dm_block_manager *bm;
 	struct dm_space_map *metadata_sm;
 	struct dm_space_map *data_sm;
@@ -399,7 +401,7 @@ static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
 		}
 	}
 
-	dm_bm_unlock(b);
+	dm_bm_unlock(bm, b);
 
 	return 0;
 }
@@ -655,7 +657,7 @@ static int __open_metadata(struct dm_pool_metadata *pmd)
 	}
 
 	__setup_btree_details(pmd);
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(pmd->bm, sblock);
 
 	return 0;
 
@@ -665,7 +667,7 @@ static int __open_metadata(struct dm_pool_metadata *pmd)
 	dm_tm_destroy(pmd->tm);
 	dm_sm_destroy(pmd->metadata_sm);
 bad_unlock_sblock:
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(pmd->bm, sblock);
 
 	return r;
 }
@@ -688,8 +690,18 @@ static int __create_persistent_data_objects(struct dm_pool_metadata *pmd, bool f
 {
 	int r;
 
-	pmd->bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
-					  THIN_MAX_CONCURRENT_LOCKS);
+	pr_alert("pmd->journal_dev = %p\n", pmd->journal_dev);
+	if (pmd->journal_dev)
+		pmd->bm = dm_block_manager_create_with_journal(
+				  pmd->bdev,
+			          THIN_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
+				  THIN_MAX_CONCURRENT_LOCKS,
+				  pmd->journal_dev);
+	else
+		pmd->bm = dm_block_manager_create(pmd->bdev,
+                                                  THIN_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
+                                                  THIN_MAX_CONCURRENT_LOCKS);
+
 	if (IS_ERR(pmd->bm)) {
 		DMERR("could not create block manager");
 		return PTR_ERR(pmd->bm);
@@ -734,7 +746,7 @@ static int __begin_transaction(struct dm_pool_metadata *pmd)
 	pmd->flags = le32_to_cpu(disk_super->flags);
 	pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
 
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(pmd->bm, sblock);
 	return 0;
 }
 
@@ -818,7 +830,8 @@ static int __commit_transaction(struct dm_pool_metadata *pmd)
 
 struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
 					       sector_t data_block_size,
-					       bool format_device)
+					       bool format_device,
+                                               struct block_device *journal)
 {
 	int r;
 	struct dm_pool_metadata *pmd;
@@ -834,6 +847,7 @@ struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
 	INIT_LIST_HEAD(&pmd->thin_devices);
 	pmd->fail_io = false;
 	pmd->bdev = bdev;
+	pmd->journal_dev = journal;
 	pmd->data_block_size = data_block_size;
 
 	r = __create_persistent_data_objects(pmd, format_device);
@@ -1253,7 +1267,7 @@ static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
 
 	disk_super = dm_block_data(sblock);
 	disk_super->held_root = cpu_to_le64(held_root);
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(pmd->bm, sblock);
 	return 0;
 }
 
@@ -1284,7 +1298,7 @@ static int __release_metadata_snap(struct dm_pool_metadata *pmd)
 	held_root = le64_to_cpu(disk_super->held_root);
 	disk_super->held_root = cpu_to_le64(0);
 
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(pmd->bm, sblock);
 
 	if (!held_root) {
 		DMWARN("No pool metadata snapshot found: nothing to release.");
@@ -1332,7 +1346,7 @@ static int __get_metadata_snap(struct dm_pool_metadata *pmd,
 	disk_super = dm_block_data(sblock);
 	*result = le64_to_cpu(disk_super->held_root);
 
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(pmd->bm, sblock);
 
 	return 0;
 }
@@ -1790,6 +1804,10 @@ int dm_pool_abort_metadata(struct dm_pool_metadata *pmd)
 
 	__set_abort_with_changes_flags(pmd);
 	__destroy_persistent_data_objects(pmd);
+
+	// FIXME: hack to avoid writing code for reopening the journal
+	BUG();
+
 	r = __create_persistent_data_objects(pmd, false);
 	if (r)
 		pmd->fail_io = true;
@@ -1985,7 +2003,7 @@ int dm_pool_metadata_set_needs_check(struct dm_pool_metadata *pmd)
 	disk_super = dm_block_data(sblock);
 	disk_super->flags = cpu_to_le32(pmd->flags);
 
-	dm_bm_unlock(sblock);
+	dm_bm_unlock(pmd->bm, sblock);
 out:
 	up_write(&pmd->root_lock);
 	return r;
diff --git a/drivers/md/dm-thin-metadata.h b/drivers/md/dm-thin-metadata.h
index 35e954ea20a9..6bd01c74e925 100644
--- a/drivers/md/dm-thin-metadata.h
+++ b/drivers/md/dm-thin-metadata.h
@@ -43,7 +43,8 @@ typedef uint64_t dm_thin_id;
  */
 struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
 					       sector_t data_block_size,
-					       bool format_device);
+					       bool format_device,
+                                               struct block_device *journal);
 
 int dm_pool_metadata_close(struct dm_pool_metadata *pmd);
 
diff --git a/drivers/md/dm-thin.c b/drivers/md/dm-thin.c
index 7bd60a150f8f..66f03447a05e 100644
--- a/drivers/md/dm-thin.c
+++ b/drivers/md/dm-thin.c
@@ -8,6 +8,7 @@
 #include "dm-bio-prison-v1.h"
 #include "dm.h"
 
+#include <linux/ctype.h>
 #include <linux/device-mapper.h>
 #include <linux/dm-io.h>
 #include <linux/dm-kcopyd.h>
@@ -34,6 +35,10 @@
 
 static unsigned no_space_timeout_secs = NO_SPACE_TIMEOUT_SECS;
 
+static char *journal_name = NULL;
+module_param_named(block_manager_journal, journal_name, charp, S_IRUGO | S_IWUSR);
+MODULE_PARM_DESC(block_manager_journal, "Device to recieve the block manager journal (used for debugging)");
+
 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(snapshot_copy_throttle,
 		"A percentage of time allocated for copy on write");
 
@@ -287,6 +292,7 @@ struct pool_c {
 	struct pool *pool;
 	struct dm_dev *data_dev;
 	struct dm_dev *metadata_dev;
+	struct dm_dev *journal_dev;
 	struct dm_target_callbacks callbacks;
 
 	dm_block_t low_water_blocks;
@@ -2839,6 +2845,7 @@ static struct kmem_cache *_new_mapping_cache;
 
 static struct pool *pool_create(struct mapped_device *pool_md,
 				struct block_device *metadata_dev,
+                                struct block_device *journal_dev,
 				unsigned long block_size,
 				int read_only, char **error)
 {
@@ -2848,7 +2855,8 @@ static struct pool *pool_create(struct mapped_device *pool_md,
 	struct dm_pool_metadata *pmd;
 	bool format_device = read_only ? false : true;
 
-	pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device);
+	pr_alert("passing journal_dev = %p\n", journal_dev);
+	pmd = dm_pool_metadata_open(metadata_dev, block_size, format_device, journal_dev);
 	if (IS_ERR(pmd)) {
 		*error = "Error creating metadata object";
 		return (struct pool *)pmd;
@@ -2986,6 +2994,7 @@ static void __pool_dec(struct pool *pool)
 
 static struct pool *__pool_find(struct mapped_device *pool_md,
 				struct block_device *metadata_dev,
+                                struct block_device *journal_dev,
 				unsigned long block_size, int read_only,
 				char **error, int *created)
 {
@@ -3008,7 +3017,7 @@ static struct pool *__pool_find(struct mapped_device *pool_md,
 			__pool_inc(pool);
 
 		} else {
-			pool = pool_create(pool_md, metadata_dev, block_size, read_only, error);
+			pool = pool_create(pool_md, metadata_dev, journal_dev, block_size, read_only, error);
 			*created = 1;
 		}
 	}
@@ -3029,6 +3038,7 @@ static void pool_dtr(struct dm_target *ti)
 	__pool_dec(pt->pool);
 	dm_put_device(ti, pt->metadata_dev);
 	dm_put_device(ti, pt->data_dev);
+	dm_put_device(ti, pt->journal_dev);
 	kfree(pt);
 
 	mutex_unlock(&dm_thin_pool_table.mutex);
@@ -3145,6 +3155,14 @@ static dm_block_t calc_metadata_threshold(struct pool_c *pt)
 	return min((dm_block_t)1024ULL /* 4M */, quarter);
 }
 
+static void normalise_journal_name_(const char *name, char *buffer, size_t len)
+{
+	while (*name && !isspace(*name) && --len)
+		*buffer++ = *name++;
+
+	*buffer = '\0';
+}
+
 /*
  * thin-pool <metadata dev> <data dev>
  *	     <data block size (sectors)>
@@ -3169,6 +3187,7 @@ static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
 	unsigned long block_size;
 	dm_block_t low_water_blocks;
 	struct dm_dev *metadata_dev;
+	struct dm_dev *journal_dev = NULL;
 	fmode_t metadata_mode;
 
 	/*
@@ -3230,7 +3249,21 @@ static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
 		goto out;
 	}
 
-	pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev,
+	if (journal_name) {
+		char buffer[64];
+		normalise_journal_name_(journal_name, buffer, sizeof(buffer));
+		if (buffer[0]) {
+			r = dm_get_device(ti, buffer, FMODE_READ | FMODE_WRITE, &journal_dev);
+			if (r) {
+				pr_alert("couldn't open journal device '%s'", buffer);
+				journal_dev = NULL;
+			} else {
+				pr_alert("opened journal device '%s'", buffer);
+			}
+		}
+	}
+
+	pool = __pool_find(dm_table_get_md(ti->table), metadata_dev->bdev, journal_dev ? journal_dev->bdev : NULL,
 			   block_size, pf.mode == PM_READ_ONLY, &ti->error, &pool_created);
 	if (IS_ERR(pool)) {
 		r = PTR_ERR(pool);
@@ -3253,6 +3286,7 @@ static int pool_ctr(struct dm_target *ti, unsigned argc, char **argv)
 	pt->ti = ti;
 	pt->metadata_dev = metadata_dev;
 	pt->data_dev = data_dev;
+	pt->journal_dev = journal_dev;
 	pt->low_water_blocks = low_water_blocks;
 	pt->adjusted_pf = pt->requested_pf = pf;
 	ti->num_flush_bios = 1;
@@ -4400,6 +4434,7 @@ module_exit(dm_thin_exit);
 module_param_named(no_space_timeout, no_space_timeout_secs, uint, S_IRUGO | S_IWUSR);
 MODULE_PARM_DESC(no_space_timeout, "Out of data space queue IO timeout in seconds");
 
+
 MODULE_DESCRIPTION(DM_NAME " thin provisioning target");
 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
 MODULE_LICENSE("GPL");
diff --git a/drivers/md/persistent-data/dm-block-manager.c b/drivers/md/persistent-data/dm-block-manager.c
index 492a3f8ac119..b1f773cb037f 100644
--- a/drivers/md/persistent-data/dm-block-manager.c
+++ b/drivers/md/persistent-data/dm-block-manager.c
@@ -291,6 +291,7 @@ static int bl_down_write(struct block_lock *lock)
 static void bl_up_write(struct block_lock *lock)
 {
 	spin_lock(&lock->lock);
+	BUG_ON(lock->count != -1);
 	__del_holder(lock, current);
 	lock->count = 0;
 	if (!list_empty(&lock->waiters))
@@ -343,13 +344,16 @@ void *dm_block_data(struct dm_block *b)
 }
 EXPORT_SYMBOL_GPL(dm_block_data);
 
+// FIXME: test to see if it's worth reducing this
+#define CHECKSUM_SIZE 32
+#define NR_CHECKSUMS (4096 / CHECKSUM_SIZE)
+
 struct buffer_aux {
 	struct dm_block_validator *validator;
+	struct block_lock lock;
 	int write_locked;
 
-#ifdef CONFIG_DM_DEBUG_BLOCK_MANAGER_LOCKING
-	struct block_lock lock;
-#endif
+	uint32_t checksums[NR_CHECKSUMS];
 };
 
 static void dm_block_manager_alloc_callback(struct dm_buffer *buf)
@@ -368,69 +372,43 @@ static void dm_block_manager_write_callback(struct dm_buffer *buf)
 	}
 }
 
-/*----------------------------------------------------------------
- * Public interface
- *--------------------------------------------------------------*/
-struct dm_block_manager {
+/*--------------------------------------------------------------*/
+
+struct block_manager {
+	struct dm_block_manager bm;
 	struct dm_bufio_client *bufio;
 	bool read_only:1;
 };
 
-struct dm_block_manager *dm_block_manager_create(struct block_device *bdev,
-						 unsigned block_size,
-						 unsigned max_held_per_thread)
-{
-	int r;
-	struct dm_block_manager *bm;
-
-	bm = kmalloc(sizeof(*bm), GFP_KERNEL);
-	if (!bm) {
-		r = -ENOMEM;
-		goto bad;
-	}
-
-	bm->bufio = dm_bufio_client_create(bdev, block_size, max_held_per_thread,
-					   sizeof(struct buffer_aux),
-					   dm_block_manager_alloc_callback,
-					   dm_block_manager_write_callback);
-	if (IS_ERR(bm->bufio)) {
-		r = PTR_ERR(bm->bufio);
-		kfree(bm);
-		goto bad;
-	}
-
-	bm->read_only = false;
-
-	return bm;
+#define DECLARE_BM struct block_manager *bm = container_of(dbm, struct block_manager, bm)
 
-bad:
-	return ERR_PTR(r);
-}
-EXPORT_SYMBOL_GPL(dm_block_manager_create);
-
-void dm_block_manager_destroy(struct dm_block_manager *bm)
+static void _destroy(struct dm_block_manager *dbm)
 {
+	DECLARE_BM;
+
 	dm_bufio_client_destroy(bm->bufio);
 	kfree(bm);
 }
-EXPORT_SYMBOL_GPL(dm_block_manager_destroy);
 
-unsigned dm_bm_block_size(struct dm_block_manager *bm)
+static unsigned _block_size(struct dm_block_manager *dbm)
 {
+	DECLARE_BM;
 	return dm_bufio_get_block_size(bm->bufio);
 }
-EXPORT_SYMBOL_GPL(dm_bm_block_size);
 
-dm_block_t dm_bm_nr_blocks(struct dm_block_manager *bm)
+static dm_block_t _nr_blocks(struct dm_block_manager *dbm)
 {
+	DECLARE_BM;
 	return dm_bufio_get_device_size(bm->bufio);
 }
 
-static int dm_bm_validate_buffer(struct dm_block_manager *bm,
-				 struct dm_buffer *buf,
-				 struct buffer_aux *aux,
-				 struct dm_block_validator *v)
+static int _validate_buffer(struct dm_block_manager *dbm,
+			    struct dm_buffer *buf,
+			    struct buffer_aux *aux,
+			    struct dm_block_validator *v)
 {
+	DECLARE_BM;
+
 	if (unlikely(!aux->validator)) {
 		int r;
 		if (!v)
@@ -453,10 +431,18 @@ static int dm_bm_validate_buffer(struct dm_block_manager *bm,
 
 	return 0;
 }
-int dm_bm_read_lock(struct dm_block_manager *bm, dm_block_t b,
-		    struct dm_block_validator *v,
-		    struct dm_block **result)
+
+static void _prefetch(struct dm_block_manager *dbm, dm_block_t b)
 {
+	DECLARE_BM;
+	dm_bufio_prefetch(bm->bufio, b, 1);
+}
+
+static int _read_lock(struct dm_block_manager *dbm, dm_block_t b,
+		      struct dm_block_validator *v,
+		      struct dm_block **result)
+{
+	DECLARE_BM;
 	struct buffer_aux *aux;
 	void *p;
 	int r;
@@ -475,7 +461,7 @@ int dm_bm_read_lock(struct dm_block_manager *bm, dm_block_t b,
 
 	aux->write_locked = 0;
 
-	r = dm_bm_validate_buffer(bm, to_buffer(*result), aux, v);
+	r = dm_bm_validate_buffer(dbm, to_buffer(*result), aux, v);
 	if (unlikely(r)) {
 		bl_up_read(&aux->lock);
 		dm_bufio_release(to_buffer(*result));
@@ -484,12 +470,12 @@ int dm_bm_read_lock(struct dm_block_manager *bm, dm_block_t b,
 
 	return 0;
 }
-EXPORT_SYMBOL_GPL(dm_bm_read_lock);
 
-int dm_bm_write_lock(struct dm_block_manager *bm,
-		     dm_block_t b, struct dm_block_validator *v,
-		     struct dm_block **result)
+static int _write_lock(struct dm_block_manager *dbm,
+		       dm_block_t b, struct dm_block_validator *v,
+		       struct dm_block **result)
 {
+	DECLARE_BM;
 	struct buffer_aux *aux;
 	void *p;
 	int r;
@@ -511,7 +497,7 @@ int dm_bm_write_lock(struct dm_block_manager *bm,
 
 	aux->write_locked = 1;
 
-	r = dm_bm_validate_buffer(bm, to_buffer(*result), aux, v);
+	r = dm_bm_validate_buffer(dbm, to_buffer(*result), aux, v);
 	if (unlikely(r)) {
 		bl_up_write(&aux->lock);
 		dm_bufio_release(to_buffer(*result));
@@ -520,12 +506,12 @@ int dm_bm_write_lock(struct dm_block_manager *bm,
 
 	return 0;
 }
-EXPORT_SYMBOL_GPL(dm_bm_write_lock);
 
-int dm_bm_read_try_lock(struct dm_block_manager *bm,
-			dm_block_t b, struct dm_block_validator *v,
-			struct dm_block **result)
+static int _read_try_lock(struct dm_block_manager *dbm,
+			  dm_block_t b, struct dm_block_validator *v,
+			  struct dm_block **result)
 {
+	DECLARE_BM;
 	struct buffer_aux *aux;
 	void *p;
 	int r;
@@ -545,7 +531,7 @@ int dm_bm_read_try_lock(struct dm_block_manager *bm,
 	}
 	aux->write_locked = 0;
 
-	r = dm_bm_validate_buffer(bm, to_buffer(*result), aux, v);
+	r = dm_bm_validate_buffer(dbm, to_buffer(*result), aux, v);
 	if (unlikely(r)) {
 		bl_up_read(&aux->lock);
 		dm_bufio_release(to_buffer(*result));
@@ -555,10 +541,11 @@ int dm_bm_read_try_lock(struct dm_block_manager *bm,
 	return 0;
 }
 
-int dm_bm_write_lock_zero(struct dm_block_manager *bm,
-			  dm_block_t b, struct dm_block_validator *v,
-			  struct dm_block **result)
+static int _write_lock_zero(struct dm_block_manager *dbm,
+			    dm_block_t b, struct dm_block_validator *v,
+			    struct dm_block **result)
 {
+	DECLARE_BM;
 	int r;
 	struct buffer_aux *aux;
 	void *p;
@@ -570,7 +557,7 @@ int dm_bm_write_lock_zero(struct dm_block_manager *bm,
 	if (unlikely(IS_ERR(p)))
 		return PTR_ERR(p);
 
-	memset(p, 0, dm_bm_block_size(bm));
+	memset(p, 0, dm_bm_block_size(dbm));
 
 	aux = dm_bufio_get_aux_data(to_buffer(*result));
 	r = bl_down_write(&aux->lock);
@@ -584,9 +571,8 @@ int dm_bm_write_lock_zero(struct dm_block_manager *bm,
 
 	return 0;
 }
-EXPORT_SYMBOL_GPL(dm_bm_write_lock_zero);
 
-void dm_bm_unlock(struct dm_block *b)
+static void _unlock(struct dm_block_manager *bm, struct dm_block *b)
 {
 	struct buffer_aux *aux;
 	aux = dm_bufio_get_aux_data(to_buffer(b));
@@ -599,39 +585,579 @@ void dm_bm_unlock(struct dm_block *b)
 
 	dm_bufio_release(to_buffer(b));
 }
-EXPORT_SYMBOL_GPL(dm_bm_unlock);
 
-int dm_bm_flush(struct dm_block_manager *bm)
+static int _flush(struct dm_block_manager *dbm)
 {
+	DECLARE_BM;
+
 	if (bm->read_only)
 		return -EPERM;
 
 	return dm_bufio_write_dirty_buffers(bm->bufio);
 }
-EXPORT_SYMBOL_GPL(dm_bm_flush);
 
-void dm_bm_prefetch(struct dm_block_manager *bm, dm_block_t b)
+static int _flush_and_unlock(struct dm_block_manager *dbm,
+			     struct dm_block *superblock)
 {
-	dm_bufio_prefetch(bm->bufio, b, 1);
+	DECLARE_BM;
+	int r;
+
+	if (bm->read_only)
+		return -EPERM;
+
+	r = dm_bufio_write_dirty_buffers(bm->bufio);
+	if (unlikely(r)) {
+		dm_bm_unlock(dbm, superblock);
+		return r;
+	}
+
+	dm_bm_unlock(dbm, superblock);
+
+	return dm_bufio_write_dirty_buffers(bm->bufio);
 }
 
-bool dm_bm_is_read_only(struct dm_block_manager *bm)
+static bool _is_read_only(struct dm_block_manager *dbm)
 {
+	DECLARE_BM;
 	return bm->read_only;
 }
-EXPORT_SYMBOL_GPL(dm_bm_is_read_only);
 
-void dm_bm_set_read_only(struct dm_block_manager *bm)
+static void _set_read_only(struct dm_block_manager *dbm)
 {
+	DECLARE_BM;
 	bm->read_only = true;
 }
-EXPORT_SYMBOL_GPL(dm_bm_set_read_only);
 
-void dm_bm_set_read_write(struct dm_block_manager *bm)
+static void _set_read_write(struct dm_block_manager *dbm)
 {
+	DECLARE_BM;
 	bm->read_only = false;
 }
-EXPORT_SYMBOL_GPL(dm_bm_set_read_write);
+#undef DECLARE_BM
+
+static void _check_bm_filled_out(struct dm_block_manager *dbm)
+{
+	BUG_ON(!dbm->destroy);
+	BUG_ON(!dbm->block_size);
+	BUG_ON(!dbm->nr_blocks);
+	BUG_ON(!dbm->validate_buffer);
+	BUG_ON(!dbm->prefetch);
+	BUG_ON(!dbm->read_lock_);
+	BUG_ON(!dbm->write_lock_);
+	BUG_ON(!dbm->read_try_lock_);
+	BUG_ON(!dbm->write_lock_zero);
+	BUG_ON(!dbm->unlock);
+	BUG_ON(!dbm->flush);
+	BUG_ON(!dbm->flush_and_unlock);
+	BUG_ON(!dbm->is_read_only);
+	BUG_ON(!dbm->set_read_only);
+	BUG_ON(!dbm->set_read_write);
+}
+
+struct dm_block_manager *dm_block_manager_create(struct block_device *bdev,
+						 unsigned block_size,
+						 unsigned max_held_per_thread)
+{
+	int r;
+	struct block_manager *bm;
+
+	bm = kmalloc(sizeof(*bm), GFP_KERNEL);
+	if (!bm) {
+		r = -ENOMEM;
+		goto bad;
+	}
+
+	bm->bm.destroy = _destroy;
+	bm->bm.block_size = _block_size;
+	bm->bm.nr_blocks = _nr_blocks;
+	bm->bm.validate_buffer = _validate_buffer;
+	bm->bm.prefetch = _prefetch;
+	bm->bm.read_lock_ = _read_lock;
+	bm->bm.write_lock_ = _write_lock;
+	bm->bm.read_try_lock_ = _read_try_lock;
+	bm->bm.write_lock_zero = _write_lock_zero;
+	bm->bm.unlock = _unlock;
+	bm->bm.flush = _flush;
+	bm->bm.flush_and_unlock = _flush_and_unlock;
+	bm->bm.is_read_only = _is_read_only;
+	bm->bm.set_read_only = _set_read_only;
+	bm->bm.set_read_write = _set_read_write;
+
+        bm->bufio = dm_bufio_client_create(bdev, block_size, max_held_per_thread,
+                                           sizeof(struct buffer_aux),
+                                           dm_block_manager_alloc_callback,
+                                           dm_block_manager_write_callback);
+
+	if (IS_ERR(bm->bufio)) {
+		r = PTR_ERR(bm->bufio);
+		kfree(bm);
+		goto bad;
+	}
+
+	bm->read_only = false;
+
+	_check_bm_filled_out(&bm->bm);
+
+	pr_alert("created real at %p\n", &bm->bm);
+	return &bm->bm;
+
+bad:
+	return ERR_PTR(r);
+}
+EXPORT_SYMBOL_GPL(dm_block_manager_create);
+
+/*----------------------------------------------------------------*/
+
+enum msg_type {
+	MT_OPEN_JOURNAL,
+	MT_CLOSE_JOURNAL,
+
+	MT_READ_LOCK,
+	MT_WRITE_LOCK,
+	MT_ZERO_LOCK,
+	MT_TRY_READ_LOCK,
+	MT_UNLOCK,
+	MT_VERIFY,
+	MT_PREPARE,
+	MT_FLUSH,
+	MT_FLUSH_AND_UNLOCK,
+	MT_PREFETCH,
+	MT_SET_READ_ONLY,
+	MT_SET_READ_WRITE,
+};
+
+struct byte_stream {
+	spinlock_t lock;
+	struct block_device *dev;
+	struct dm_bufio_client *cache;
+
+	uint64_t block_index;
+	struct dm_buffer *current_buffer;
+	void *current_data;
+	uint8_t *out_begin;
+	uint8_t *out_end;
+};
+
+#define JOURNAL_BLOCK_SIZE (1024 * 1024 * 1024)
+
+// We just BUG if there's an error; this is developement code.
+static void _prep_block(struct byte_stream *bs, uint64_t block)
+{
+	bs->current_data = dm_bufio_new(bs->cache, block, &bs->current_buffer);
+	BUG_ON(!bs->current_data);
+	bs->out_begin = bs->current_data;
+	bs->out_end = bs->current_data + JOURNAL_BLOCK_SIZE;
+}
+
+static void _commit_block(struct byte_stream *bs)
+{
+	dm_bufio_mark_buffer_dirty(bs->current_buffer);
+	dm_bufio_release(bs->current_buffer);
+}
+
+static struct byte_stream *_bs_open(struct block_device *dev)
+{
+	struct byte_stream *bs = kzalloc(sizeof(*bs), GFP_KERNEL);
+
+	if (!bs)
+		return NULL;
+
+	spin_lock_init(&bs->lock);
+	bs->dev = dev;
+	bs->cache = dm_bufio_client_create(dev, JOURNAL_BLOCK_SIZE,
+		       			   2, 0, NULL, NULL);
+	if (!bs->cache) {
+		kfree(bs);
+		return NULL;
+	}
+
+	_prep_block(bs, 0);
+	
+	return bs;
+}
+
+static void _bs_close(struct byte_stream *bs)
+{
+	_commit_block(bs);
+	dm_bufio_client_destroy(bs->cache);
+	kfree(bs);
+}
+
+static size_t _cpy_bytes(struct byte_stream *bs, uint8_t *b, uint8_t *e)
+{
+	size_t len = min(e - b, bs->out_end - bs->out_begin);
+	memcpy(bs->out_begin, b, len);
+	bs->out_begin += len;
+	return len;
+}
+
+static bool _no_space(struct byte_stream *bs)
+{
+	return bs->out_begin == bs->out_end;
+}
+
+static void _push_bytes(struct byte_stream *bs, uint8_t *b, uint8_t *e)
+{
+	while (b != e) {
+		if (_no_space(bs)) {
+			pr_alert("push_bytes: out of space\n");
+			_commit_block(bs);
+			_prep_block(bs, bs->block_index + 1);
+			pr_alert("done");
+		}
+
+		b += _cpy_bytes(bs, b, e);
+	}
+}
+
+static void _push_u8(struct byte_stream *bs, uint8_t v)
+{
+	return _push_bytes(bs, &v, &v + 1);
+}
+
+static void _push_u16(struct byte_stream *bs, uint16_t v)
+{
+	return _push_bytes(bs, (uint8_t *) &v, (uint8_t *) (&v + 1));
+}
+
+static void _push_u64(struct byte_stream *bs, uint64_t v)
+{
+	return _push_bytes(bs, (uint8_t *) &v, (uint8_t *) (&v + 1));
+}
+
+static void _push_msg(struct byte_stream *bs, enum msg_type t, int err)
+{
+	uint8_t b = t << 1;
+	b |= err ? 0 : 1;
+	_push_u8(bs, b);
+}
+
+/*----------------------------------------------------------------*/
+
+static u32 _cs_chunk(const void *data, unsigned chunk)
+{
+	return crc32c(0, data + (chunk * CHECKSUM_SIZE), CHECKSUM_SIZE);
+}
+
+static void _calc_checksums(struct dm_block *b)
+{
+	unsigned i;
+	const void *data = dm_block_data(b);
+	struct buffer_aux *aux = dm_bufio_get_aux_data((struct dm_buffer *) b);
+
+	for (i = 0; i < NR_CHECKSUMS; i++)
+		aux->checksums[i] = _cs_chunk(data, i);
+}
+
+static void _write_delta(struct byte_stream *bs, struct dm_block *b, unsigned chunk)
+{
+	uint8_t *begin = dm_block_data(b) + (chunk * CHECKSUM_SIZE);
+	uint8_t *end = begin + CHECKSUM_SIZE;
+
+	_push_u16(bs, chunk);
+	_push_bytes(bs, begin, end);
+}
+
+static void _terminate_deltas(struct byte_stream *bs)
+{
+	BUG_ON(NR_CHECKSUMS > 0xff);
+	_push_u16(bs, 0xffff);
+}
+
+static void _push_deltas(struct byte_stream *bs, struct dm_block *b)
+{
+	unsigned i;
+	uint32_t sum;
+	const void *data = dm_block_data(b);
+	struct buffer_aux *aux = dm_bufio_get_aux_data((struct dm_buffer *) b);
+
+	if (aux->write_locked)
+		for (i = 0; i < NR_CHECKSUMS; i++) {
+			sum = _cs_chunk(data, i);
+			if (sum != aux->checksums[i])
+				_write_delta(bs, b, i);
+		}
+
+	_terminate_deltas(bs);
+}
+
+/*----------------------------------------------------------------*/
+
+struct journal_bm {
+	struct dm_block_manager bm;
+	struct dm_block_manager *inner;
+	struct byte_stream *out;
+};
+
+#define DECLARE_BM struct journal_bm *bm = container_of(dbm, struct journal_bm, bm)
+
+static void _j_destroy(struct dm_block_manager *dbm)
+{
+	DECLARE_BM;
+	_push_msg(bm->out, MT_CLOSE_JOURNAL, true);
+	_bs_close(bm->out);
+	bm->inner->destroy(bm->inner);
+	kfree(bm);
+}
+
+static unsigned _j_block_size(struct dm_block_manager *dbm)
+{
+	DECLARE_BM;
+	return bm->inner->block_size(bm->inner);
+}
+
+static dm_block_t _j_nr_blocks(struct dm_block_manager *dbm)
+{
+	DECLARE_BM;
+	return bm->inner->nr_blocks(bm->inner);
+}
+
+static int _j_validate_buffer(struct dm_block_manager *dbm,
+			    struct dm_buffer *buf,
+			    struct buffer_aux *aux,
+			    struct dm_block_validator *v)
+{
+	int r; 
+	DECLARE_BM;
+	unsigned long flags;
+
+	r = bm->inner->validate_buffer(bm->inner, buf, aux, v);
+
+	spin_lock_irqsave(&bm->out->lock, flags);
+	_push_msg(bm->out, MT_VERIFY, r);
+	_push_u64(bm->out, dm_bufio_get_block_number(buf));
+	spin_unlock_irqrestore(&bm->out->lock, flags);
+
+	return r;
+}
+
+static void _j_prefetch(struct dm_block_manager *dbm, dm_block_t b)
+{
+	DECLARE_BM;
+	bm->inner->prefetch(bm->inner, b);
+}
+
+static int _j_read_lock(struct dm_block_manager *dbm, dm_block_t b,
+		        struct dm_block_validator *v,
+		        struct dm_block **result)
+{
+	int r;
+	DECLARE_BM;
+	unsigned long flags;
+
+	r = bm->inner->read_lock_(bm->inner, b, v, result);
+
+	// No need to calculate checksums for a read lock
+	spin_lock_irqsave(&bm->out->lock, flags);
+	_push_msg(bm->out, MT_READ_LOCK, r);
+	_push_u64(bm->out, b);
+	spin_unlock_irqrestore(&bm->out->lock, flags);
+
+	return r;
+}
+
+static int _j_write_lock(struct dm_block_manager *dbm,
+		         dm_block_t b, struct dm_block_validator *v,
+		         struct dm_block **result)
+{
+	int r;
+	DECLARE_BM;
+	unsigned long flags;
+
+	r = bm->inner->write_lock_(bm->inner, b, v, result);
+	if (!r)
+		_calc_checksums(*result);
+
+	spin_lock_irqsave(&bm->out->lock, flags);
+	_push_msg(bm->out, MT_WRITE_LOCK, r);
+	_push_u64(bm->out, b);
+	spin_unlock_irqrestore(&bm->out->lock, flags);
+
+	return r;
+}
+
+static int _j_read_try_lock(struct dm_block_manager *dbm,
+			    dm_block_t b, struct dm_block_validator *v,
+			    struct dm_block **result)
+{
+	int r;
+	DECLARE_BM;
+	unsigned long flags;
+
+	r = bm->inner->read_try_lock_(bm->inner, b, v, result);
+
+	// try_read_lock is called from request context, so we mustn't trigger io.
+	// FIXME: work out a way to journal this!
+	spin_lock_irqsave(&bm->out->lock, flags);
+	_push_msg(bm->out, MT_TRY_READ_LOCK, r);
+	_push_u64(bm->out, b);
+	spin_unlock_irqrestore(&bm->out->lock, flags);
+
+	return r;
+}
+
+static int _j_write_lock_zero(struct dm_block_manager *dbm,
+			      dm_block_t b, struct dm_block_validator *v,
+			      struct dm_block **result)
+{
+	int r;
+	DECLARE_BM;
+	unsigned long flags;
+
+	r = bm->inner->write_lock_zero(bm->inner, b, v, result);
+	if (!r)
+		_calc_checksums(*result);
+
+	spin_lock_irqsave(&bm->out->lock, flags);
+	_push_msg(bm->out, MT_ZERO_LOCK, r);
+	_push_u64(bm->out, b);
+	spin_unlock_irqrestore(&bm->out->lock, flags);
+
+	return r;
+}
+
+static void _j_unlock(struct dm_block_manager *dbm, struct dm_block *b)
+{
+	DECLARE_BM;
+	unsigned long flags;
+
+	spin_lock_irqsave(&bm->out->lock, flags);
+	_push_msg(bm->out, MT_UNLOCK, 0);
+	_push_u64(bm->out, dm_block_location(b));
+	_push_deltas(bm->out, b);
+	spin_unlock_irqrestore(&bm->out->lock, flags);
+
+	bm->inner->unlock(bm->inner, b);
+}
+
+static int _j_flush(struct dm_block_manager *dbm)
+{
+	int r;
+	DECLARE_BM;
+	unsigned long flags;
+
+	r = bm->inner->flush(bm->inner);
+	spin_lock_irqsave(&bm->out->lock, flags);
+	_push_msg(bm->out, MT_FLUSH, r);
+	spin_unlock_irqrestore(&bm->out->lock, flags);
+	return r;
+}
+
+static int _j_flush_and_unlock(struct dm_block_manager *dbm,
+			       struct dm_block *superblock)
+{
+	DECLARE_BM;
+	unsigned long flags;
+
+	pr_alert("flush_and_unlock\n");
+	spin_lock_irqsave(&bm->out->lock, flags);
+	_push_msg(bm->out, MT_FLUSH_AND_UNLOCK, 0);
+	_push_u64(bm->out, dm_block_location(superblock));
+	_push_deltas(bm->out, superblock);
+	spin_unlock_irqrestore(&bm->out->lock, flags);
+
+	return bm->inner->flush_and_unlock(bm->inner, superblock);
+}
+
+static bool _j_is_read_only(struct dm_block_manager *dbm)
+{
+	DECLARE_BM;
+
+	return bm->inner->is_read_only(bm->inner);
+}
+
+static void _j_set_read_only(struct dm_block_manager *dbm)
+{
+	DECLARE_BM;
+	unsigned long flags;
+
+	bm->inner->set_read_only(bm->inner);
+
+	spin_lock_irqsave(&bm->out->lock, flags);
+	_push_msg(bm->out, MT_SET_READ_ONLY, true);
+	spin_unlock_irqrestore(&bm->out->lock, flags);
+}
+
+static void _j_set_read_write(struct dm_block_manager *dbm)
+{
+	DECLARE_BM;
+	unsigned long flags;
+
+	bm->inner->set_read_write(bm->inner);
+
+	spin_lock_irqsave(&bm->out->lock, flags);
+	_push_msg(bm->out, MT_SET_READ_WRITE, true);
+	spin_unlock_irqrestore(&bm->out->lock, flags);
+}
+
+#undef DECLARE_BM
+
+static bool _unformatted_journal(struct byte_stream *bs)
+{
+	// The journal is unformatted if the first sector (512 bytes) is zeroed.
+	uint8_t buffer[64];
+
+	for (i = 0; i < 8; i++) {
+		_bs_
+	}
+
+	_bs_rewrind(bs);
+}
+
+struct dm_block_manager *dm_block_manager_create_with_journal(struct block_device *bdev,
+                                                              unsigned block_size,
+							      unsigned max_held_per_thread,
+							      struct block_device *jdev)
+{
+	struct journal_bm *jbm;
+	struct dm_block_manager *inner = dm_block_manager_create(bdev, block_size, max_held_per_thread);
+
+	if (IS_ERR(inner))
+		return inner;
+
+	jbm = kmalloc(sizeof(*jbm), GFP_KERNEL);
+	if (!jbm) {
+		inner->destroy(inner);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	jbm->out = _bs_open(jdev);
+	if (!jbm->out) {
+		inner->destroy(inner);
+		kfree(jbm);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	jbm->bm.destroy = _j_destroy;
+	jbm->bm.block_size = _j_block_size;
+	jbm->bm.nr_blocks = _j_nr_blocks;
+	jbm->bm.validate_buffer = _j_validate_buffer;
+	jbm->bm.prefetch = _j_prefetch;
+	jbm->bm.read_lock_ = _j_read_lock;
+	jbm->bm.write_lock_ = _j_write_lock;
+	jbm->bm.read_try_lock_ = _j_read_try_lock;
+	jbm->bm.write_lock_zero = _j_write_lock_zero;
+	jbm->bm.unlock = _j_unlock;
+	jbm->bm.flush = _j_flush;
+	jbm->bm.flush_and_unlock = _j_flush_and_unlock;
+	jbm->bm.is_read_only = _j_is_read_only;
+	jbm->bm.set_read_only = _j_set_read_only;
+	jbm->bm.set_read_write = _j_set_read_write;
+
+	_check_bm_filled_out(&jbm->bm);
+
+	jbm->inner = inner;
+
+	pr_alert("journalling block manager created\n");
+
+	_push_msg(jbm->out, MT_OPEN_JOURNAL, 0);
+	_push_u64(jbm->out, dm_bm_nr_blocks(inner));
+
+	return &jbm->bm;
+}
+EXPORT_SYMBOL_GPL(dm_block_manager_create_with_journal);
+
+/*----------------------------------------------------------------*/
 
 u32 dm_bm_checksum(const void *data, size_t len, u32 init_xor)
 {
@@ -645,4 +1171,5 @@ MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Joe Thornber <dm-devel@redhat.com>");
 MODULE_DESCRIPTION("Immutable metadata library for dm");
 
+
 /*----------------------------------------------------------------*/
diff --git a/drivers/md/persistent-data/dm-block-manager.h b/drivers/md/persistent-data/dm-block-manager.h
index e728937f376a..adb55f6aceac 100644
--- a/drivers/md/persistent-data/dm-block-manager.h
+++ b/drivers/md/persistent-data/dm-block-manager.h
@@ -23,23 +23,8 @@ void *dm_block_data(struct dm_block *b);
 
 /*----------------------------------------------------------------*/
 
-/*
- * @name should be a unique identifier for the block manager, no longer
- * than 32 chars.
- *
- * @max_held_per_thread should be the maximum number of locks, read or
- * write, that an individual thread holds at any one time.
- */
-struct dm_block_manager;
-struct dm_block_manager *dm_block_manager_create(
-	struct block_device *bdev, unsigned block_size,
-	unsigned max_held_per_thread);
-void dm_block_manager_destroy(struct dm_block_manager *bm);
-
-unsigned dm_bm_block_size(struct dm_block_manager *bm);
-dm_block_t dm_bm_nr_blocks(struct dm_block_manager *bm);
-
-/*----------------------------------------------------------------*/
+struct dm_buffer;
+struct buffer_aux;
 
 /*
  * The validator allows the caller to verify newly-read data and modify
@@ -57,44 +42,141 @@ struct dm_block_validator {
 	int (*check)(struct dm_block_validator *v, struct dm_block *b, size_t block_size);
 };
 
+struct dm_block_manager {
+	void (*destroy)(struct dm_block_manager *bm);
+	unsigned (*block_size)(struct dm_block_manager *bm);
+	dm_block_t (*nr_blocks)(struct dm_block_manager *bm);
+	int (*validate_buffer)(struct dm_block_manager *bm,
+			       struct dm_buffer *buf,
+			       struct buffer_aux *aux,
+			       struct dm_block_validator *v);
+	void (*prefetch)(struct dm_block_manager *bm, dm_block_t b);
+	int (*read_lock_)(struct dm_block_manager *bm, dm_block_t b,
+			 struct dm_block_validator *v,
+			 struct dm_block **result);
+	int (*write_lock_)(struct dm_block_manager *bm,
+			  dm_block_t b, struct dm_block_validator *v,
+			  struct dm_block **result);
+	int (*read_try_lock_)(struct dm_block_manager *bm,
+			     dm_block_t b, struct dm_block_validator *v,
+			     struct dm_block **result);
+	int (*write_lock_zero)(struct dm_block_manager *bm,
+			       dm_block_t b, struct dm_block_validator *v,
+			       struct dm_block **result);
+	void (*unlock)(struct dm_block_manager *bm, struct dm_block *b);
+	int (*flush)(struct dm_block_manager *bm);
+	int (*flush_and_unlock)(struct dm_block_manager *bm,
+				struct dm_block *superblock);
+	bool (*is_read_only)(struct dm_block_manager *bm);
+	void (*set_read_only)(struct dm_block_manager *bm);
+	void (*set_read_write)(struct dm_block_manager *bm);
+};
+
+/*
+ * @name should be a unique identifier for the block manager, no longer
+ * than 32 chars.
+ *
+ * @max_held_per_thread should be the maximum number of locks, read or
+ * write, that an individual thread holds at any one time.
+ */
+
+struct dm_block_manager *dm_block_manager_create(
+	struct block_device *bdev, unsigned block_size,
+	unsigned max_held_per_thread);
+
+struct dm_block_manager *dm_block_manager_create_with_journal(
+	struct block_device *bdev, unsigned block_size,
+	unsigned max_held_per_thread,
+	struct block_device *jdev);
+
 /*----------------------------------------------------------------*/
 
+static inline void dm_block_manager_destroy(struct dm_block_manager *bm)
+{
+	bm->destroy(bm);
+}
+
+static inline unsigned dm_bm_block_size(struct dm_block_manager *bm)
+{
+	return bm->block_size(bm);
+}
+
+static inline dm_block_t dm_bm_nr_blocks(struct dm_block_manager *bm)
+{
+	return bm->nr_blocks(bm);
+}
+
+/*----------------------------------------------------------------*/
+
+static inline int dm_bm_validate_buffer(struct dm_block_manager *bm,
+				        struct dm_buffer *buf,
+				        struct buffer_aux *aux,
+				        struct dm_block_validator *v)
+{
+	return bm->validate_buffer(bm, buf, aux, v);
+}
+
 /*
  * You can have multiple concurrent readers or a single writer holding a
  * block lock.
  */
 
+static inline void dm_bm_prefetch(struct dm_block_manager *bm, dm_block_t b)
+{
+	bm->prefetch(bm, b);
+}
+
 /*
  * dm_bm_lock() locks a block and returns through @result a pointer to
  * memory that holds a copy of that block.  If you have write-locked the
  * block then any changes you make to memory pointed to by @result will be
  * written back to the disk sometime after dm_bm_unlock is called.
  */
-int dm_bm_read_lock(struct dm_block_manager *bm, dm_block_t b,
-		    struct dm_block_validator *v,
-		    struct dm_block **result);
-
-int dm_bm_write_lock(struct dm_block_manager *bm, dm_block_t b,
-		     struct dm_block_validator *v,
-		     struct dm_block **result);
+static inline int dm_bm_read_lock(struct dm_block_manager *bm, dm_block_t b,
+		    		  struct dm_block_validator *v,
+		    		  struct dm_block **result)
+{
+	return bm->read_lock_(bm, b, v, result);
+}
+
+static inline int dm_bm_write_lock(struct dm_block_manager *bm, dm_block_t b,
+		     		   struct dm_block_validator *v,
+		     		   struct dm_block **result)
+{
+	return bm->write_lock_(bm, b, v, result);
+}
 
 /*
  * The *_try_lock variants return -EWOULDBLOCK if the block isn't
  * available immediately.
  */
-int dm_bm_read_try_lock(struct dm_block_manager *bm, dm_block_t b,
-			struct dm_block_validator *v,
-			struct dm_block **result);
+static inline int dm_bm_read_try_lock(struct dm_block_manager *bm, dm_block_t b,
+				      struct dm_block_validator *v,
+				      struct dm_block **result)
+{
+	return bm->read_try_lock_(bm, b, v, result);
+}
 
 /*
  * Use dm_bm_write_lock_zero() when you know you're going to
  * overwrite the block completely.  It saves a disk read.
  */
-int dm_bm_write_lock_zero(struct dm_block_manager *bm, dm_block_t b,
-			  struct dm_block_validator *v,
-			  struct dm_block **result);
-
-void dm_bm_unlock(struct dm_block *b);
+static inline int dm_bm_write_lock_zero(struct dm_block_manager *bm, dm_block_t b,
+			  		struct dm_block_validator *v,
+			  		struct dm_block **result)
+{
+	return bm->write_lock_zero(bm, b, v, result);
+}
+
+static inline void dm_bm_unlock(struct dm_block_manager *bm, struct dm_block *b)
+{
+	bm->unlock(bm, b);
+}
+
+static inline int dm_bm_flush(struct dm_block_manager *bm)
+{
+	return bm->flush(bm);
+}
 
 /*
  * It's a common idiom to have a superblock that should be committed last.
@@ -105,12 +187,11 @@ void dm_bm_unlock(struct dm_block *b);
  *
  * This method always blocks.
  */
-int dm_bm_flush(struct dm_block_manager *bm);
-
-/*
- * Request data is prefetched into the cache.
- */
-void dm_bm_prefetch(struct dm_block_manager *bm, dm_block_t b);
+static inline int dm_bm_flush_and_unlock(struct dm_block_manager *bm,
+			   		 struct dm_block *superblock)
+{
+	return bm->flush_and_unlock(bm, superblock);
+}
 
 /*
  * Switches the bm to a read only mode.  Once read-only mode
@@ -123,9 +204,20 @@ void dm_bm_prefetch(struct dm_block_manager *bm, dm_block_t b);
  * Additionally you should not use dm_bm_unlock_move, however no error will
  * be returned if you do.
  */
-bool dm_bm_is_read_only(struct dm_block_manager *bm);
-void dm_bm_set_read_only(struct dm_block_manager *bm);
-void dm_bm_set_read_write(struct dm_block_manager *bm);
+static inline void dm_bm_set_read_only(struct dm_block_manager *bm)
+{
+	return bm->set_read_only(bm);
+}
+
+static inline bool dm_bm_is_read_only(struct dm_block_manager *bm)
+{
+	return bm->is_read_only(bm);
+}
+
+static inline void dm_bm_set_read_write(struct dm_block_manager *bm)
+{
+	bm->set_read_write(bm);
+}
 
 u32 dm_bm_checksum(const void *data, size_t len, u32 init_xor);
 
diff --git a/drivers/md/persistent-data/dm-transaction-manager.c b/drivers/md/persistent-data/dm-transaction-manager.c
index abe2c5dd0993..5b447efdc2fb 100644
--- a/drivers/md/persistent-data/dm-transaction-manager.c
+++ b/drivers/md/persistent-data/dm-transaction-manager.c
@@ -225,7 +225,7 @@ int dm_tm_commit(struct dm_transaction_manager *tm, struct dm_block *root)
 		return -EWOULDBLOCK;
 
 	wipe_shadow_table(tm);
-	dm_bm_unlock(root);
+	dm_bm_unlock(tm->bm, root);
 
 	return dm_bm_flush(tm->bm);
 }
@@ -289,14 +289,14 @@ static int __shadow_block(struct dm_transaction_manager *tm, dm_block_t orig,
 	 */
 	r = dm_bm_write_lock_zero(tm->bm, new, v, result);
 	if (r) {
-		dm_bm_unlock(orig_block);
+		dm_bm_unlock(tm->bm, orig_block);
 		return r;
 	}
 
 	memcpy(dm_block_data(*result), dm_block_data(orig_block),
 	       dm_bm_block_size(tm->bm));
 
-	dm_bm_unlock(orig_block);
+	dm_bm_unlock(tm->bm, orig_block);
 	return r;
 }
 
@@ -344,7 +344,10 @@ EXPORT_SYMBOL_GPL(dm_tm_read_lock);
 
 void dm_tm_unlock(struct dm_transaction_manager *tm, struct dm_block *b)
 {
-	dm_bm_unlock(b);
+	if (tm->is_clone)
+		tm = tm->real;
+
+	dm_bm_unlock(tm->bm, b);
 }
 EXPORT_SYMBOL_GPL(dm_tm_unlock);