Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
// SPDX-License-Identifier: GPL-2.0
/*
 * Copyright (C) 2018 Red Hat. All rights reserved.
 *
 * This file is released under the GPL.
 */

#include <linux/device-mapper.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/vmalloc.h>
#include <linux/kthread.h>
#include <linux/dm-io.h>
#include <linux/dm-kcopyd.h>
#include <linux/dax.h>
#include <linux/pfn_t.h>
#include <linux/libnvdimm.h>

#define DM_MSG_PREFIX "writecache"

#define HIGH_WATERMARK			50
#define LOW_WATERMARK			45
#define MAX_WRITEBACK_JOBS		0
#define ENDIO_LATENCY			16
#define WRITEBACK_LATENCY		64
#define AUTOCOMMIT_BLOCKS_SSD		65536
#define AUTOCOMMIT_BLOCKS_PMEM		64
#define AUTOCOMMIT_MSEC			1000

#define BITMAP_GRANULARITY	65536
#if BITMAP_GRANULARITY < PAGE_SIZE
#undef BITMAP_GRANULARITY
#define BITMAP_GRANULARITY	PAGE_SIZE
#endif

#if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER)
#define DM_WRITECACHE_HAS_PMEM
#endif

#ifdef DM_WRITECACHE_HAS_PMEM
#define pmem_assign(dest, src)					\
do {								\
	typeof(dest) uniq = (src);				\
	memcpy_flushcache(&(dest), &uniq, sizeof(dest));	\
} while (0)
#else
#define pmem_assign(dest, src)	((dest) = (src))
#endif

#if defined(__HAVE_ARCH_MEMCPY_MCSAFE) && defined(DM_WRITECACHE_HAS_PMEM)
#define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
#endif

#define MEMORY_SUPERBLOCK_MAGIC		0x23489321
#define MEMORY_SUPERBLOCK_VERSION	1

struct wc_memory_entry {
	__le64 original_sector;
	__le64 seq_count;
};

struct wc_memory_superblock {
	union {
		struct {
			__le32 magic;
			__le32 version;
			__le32 block_size;
			__le32 pad;
			__le64 n_blocks;
			__le64 seq_count;
		};
		__le64 padding[8];
	};
	struct wc_memory_entry entries[0];
};

struct wc_entry {
	struct rb_node rb_node;
	struct list_head lru;
	unsigned short wc_list_contiguous;
	bool write_in_progress
#if BITS_PER_LONG == 64
		:1
#endif
	;
	unsigned long index
#if BITS_PER_LONG == 64
		:47
#endif
	;
#ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
	uint64_t original_sector;
	uint64_t seq_count;
#endif
};

#ifdef DM_WRITECACHE_HAS_PMEM
#define WC_MODE_PMEM(wc)			((wc)->pmem_mode)
#define WC_MODE_FUA(wc)				((wc)->writeback_fua)
#else
#define WC_MODE_PMEM(wc)			false
#define WC_MODE_FUA(wc)				false
#endif
#define WC_MODE_SORT_FREELIST(wc)		(!WC_MODE_PMEM(wc))

struct dm_writecache {
	struct mutex lock;
	struct list_head lru;
	union {
		struct list_head freelist;
		struct {
			struct rb_root freetree;
			struct wc_entry *current_free;
		};
	};
	struct rb_root tree;

	size_t freelist_size;
	size_t writeback_size;
	size_t freelist_high_watermark;
	size_t freelist_low_watermark;

	unsigned uncommitted_blocks;
	unsigned autocommit_blocks;
	unsigned max_writeback_jobs;

	int error;

	unsigned long autocommit_jiffies;
	struct timer_list autocommit_timer;
	struct wait_queue_head freelist_wait;

	atomic_t bio_in_progress[2];
	struct wait_queue_head bio_in_progress_wait[2];

	struct dm_target *ti;
	struct dm_dev *dev;
	struct dm_dev *ssd_dev;
	sector_t start_sector;
	void *memory_map;
	uint64_t memory_map_size;
	size_t metadata_sectors;
	size_t n_blocks;
	uint64_t seq_count;
	void *block_start;
	struct wc_entry *entries;
	unsigned block_size;
	unsigned char block_size_bits;

	bool pmem_mode:1;
	bool writeback_fua:1;

	bool overwrote_committed:1;
	bool memory_vmapped:1;

	bool high_wm_percent_set:1;
	bool low_wm_percent_set:1;
	bool max_writeback_jobs_set:1;
	bool autocommit_blocks_set:1;
	bool autocommit_time_set:1;
	bool writeback_fua_set:1;
	bool flush_on_suspend:1;

	unsigned writeback_all;
	struct workqueue_struct *writeback_wq;
	struct work_struct writeback_work;
	struct work_struct flush_work;

	struct dm_io_client *dm_io;

	raw_spinlock_t endio_list_lock;
	struct list_head endio_list;
	struct task_struct *endio_thread;

	struct task_struct *flush_thread;
	struct bio_list flush_list;

	struct dm_kcopyd_client *dm_kcopyd;
	unsigned long *dirty_bitmap;
	unsigned dirty_bitmap_size;

	struct bio_set bio_set;
	mempool_t copy_pool;
};

#define WB_LIST_INLINE		16

struct writeback_struct {
	struct list_head endio_entry;
	struct dm_writecache *wc;
	struct wc_entry **wc_list;
	unsigned wc_list_n;
	struct wc_entry *wc_list_inline[WB_LIST_INLINE];
	struct bio bio;
};

struct copy_struct {
	struct list_head endio_entry;
	struct dm_writecache *wc;
	struct wc_entry *e;
	unsigned n_entries;
	int error;
};

DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
					    "A percentage of time allocated for data copying");

static void wc_lock(struct dm_writecache *wc)
{
	mutex_lock(&wc->lock);
}

static void wc_unlock(struct dm_writecache *wc)
{
	mutex_unlock(&wc->lock);
}

#ifdef DM_WRITECACHE_HAS_PMEM
static int persistent_memory_claim(struct dm_writecache *wc)
{
	int r;
	loff_t s;
	long p, da;
	pfn_t pfn;
	int id;
	struct page **pages;

	wc->memory_vmapped = false;

	if (!wc->ssd_dev->dax_dev) {
		r = -EOPNOTSUPP;
		goto err1;
	}
	s = wc->memory_map_size;
	p = s >> PAGE_SHIFT;
	if (!p) {
		r = -EINVAL;
		goto err1;
	}
	if (p != s >> PAGE_SHIFT) {
		r = -EOVERFLOW;
		goto err1;
	}

	id = dax_read_lock();

	da = dax_direct_access(wc->ssd_dev->dax_dev, 0, p, &wc->memory_map, &pfn);
	if (da < 0) {
		wc->memory_map = NULL;
		r = da;
		goto err2;
	}
	if (!pfn_t_has_page(pfn)) {
		wc->memory_map = NULL;
		r = -EOPNOTSUPP;
		goto err2;
	}
	if (da != p) {
		long i;
		wc->memory_map = NULL;
		pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
		if (!pages) {
			r = -ENOMEM;
			goto err2;
		}
		i = 0;
		do {
			long daa;
			daa = dax_direct_access(wc->ssd_dev->dax_dev, i, p - i,
						NULL, &pfn);
			if (daa <= 0) {
				r = daa ? daa : -EINVAL;
				goto err3;
			}
			if (!pfn_t_has_page(pfn)) {
				r = -EOPNOTSUPP;
				goto err3;
			}
			while (daa-- && i < p) {
				pages[i++] = pfn_t_to_page(pfn);
				pfn.val++;
			}
		} while (i < p);
		wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
		if (!wc->memory_map) {
			r = -ENOMEM;
			goto err3;
		}
		kvfree(pages);
		wc->memory_vmapped = true;
	}

	dax_read_unlock(id);

	wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
	wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;

	return 0;
err3:
	kvfree(pages);
err2:
	dax_read_unlock(id);
err1:
	return r;
}
#else
static int persistent_memory_claim(struct dm_writecache *wc)
{
	BUG();
}
#endif

static void persistent_memory_release(struct dm_writecache *wc)
{
	if (wc->memory_vmapped)
		vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
}

static struct page *persistent_memory_page(void *addr)
{
	if (is_vmalloc_addr(addr))
		return vmalloc_to_page(addr);
	else
		return virt_to_page(addr);
}

static unsigned persistent_memory_page_offset(void *addr)
{
	return (unsigned long)addr & (PAGE_SIZE - 1);
}

static void persistent_memory_flush_cache(void *ptr, size_t size)
{
	if (is_vmalloc_addr(ptr))
		flush_kernel_vmap_range(ptr, size);
}

static void persistent_memory_invalidate_cache(void *ptr, size_t size)
{
	if (is_vmalloc_addr(ptr))
		invalidate_kernel_vmap_range(ptr, size);
}

static struct wc_memory_superblock *sb(struct dm_writecache *wc)
{
	return wc->memory_map;
}

static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
{
	return &sb(wc)->entries[e->index];
}

static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
{
	return (char *)wc->block_start + (e->index << wc->block_size_bits);
}

static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
{
	return wc->start_sector + wc->metadata_sectors +
		((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
}

static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
{
#ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
	return e->original_sector;
#else
	return le64_to_cpu(memory_entry(wc, e)->original_sector);
#endif
}

static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
{
#ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
	return e->seq_count;
#else
	return le64_to_cpu(memory_entry(wc, e)->seq_count);
#endif
}

static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
{
#ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
	e->seq_count = -1;
#endif
	pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
}

static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
					    uint64_t original_sector, uint64_t seq_count)
{
	struct wc_memory_entry me;
#ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
	e->original_sector = original_sector;
	e->seq_count = seq_count;
#endif
	me.original_sector = cpu_to_le64(original_sector);
	me.seq_count = cpu_to_le64(seq_count);
	pmem_assign(*memory_entry(wc, e), me);
}

#define writecache_error(wc, err, msg, arg...)				\
do {									\
	if (!cmpxchg(&(wc)->error, 0, err))				\
		DMERR(msg, ##arg);					\
	wake_up(&(wc)->freelist_wait);					\
} while (0)

#define writecache_has_error(wc)	(unlikely(READ_ONCE((wc)->error)))

static void writecache_flush_all_metadata(struct dm_writecache *wc)
{
	if (!WC_MODE_PMEM(wc))
		memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
}

static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
{
	if (!WC_MODE_PMEM(wc))
		__set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
			  wc->dirty_bitmap);
}

static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);

struct io_notify {
	struct dm_writecache *wc;
	struct completion c;
	atomic_t count;
};

static void writecache_notify_io(unsigned long error, void *context)
{
	struct io_notify *endio = context;

	if (unlikely(error != 0))
		writecache_error(endio->wc, -EIO, "error writing metadata");
	BUG_ON(atomic_read(&endio->count) <= 0);
	if (atomic_dec_and_test(&endio->count))
		complete(&endio->c);
}

static void ssd_commit_flushed(struct dm_writecache *wc)
{
	struct dm_io_region region;
	struct dm_io_request req;
	struct io_notify endio = {
		wc,
		COMPLETION_INITIALIZER_ONSTACK(endio.c),
		ATOMIC_INIT(1),
	};
	unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
	unsigned i = 0;

	while (1) {
		unsigned j;
		i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
		if (unlikely(i == bitmap_bits))
			break;
		j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);

		region.bdev = wc->ssd_dev->bdev;
		region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
		region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);

		if (unlikely(region.sector >= wc->metadata_sectors))
			break;
		if (unlikely(region.sector + region.count > wc->metadata_sectors))
			region.count = wc->metadata_sectors - region.sector;

		region.sector += wc->start_sector;
		atomic_inc(&endio.count);
		req.bi_op = REQ_OP_WRITE;
		req.bi_op_flags = REQ_SYNC;
		req.mem.type = DM_IO_VMA;
		req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
		req.client = wc->dm_io;
		req.notify.fn = writecache_notify_io;
		req.notify.context = &endio;

		/* writing via async dm-io (implied by notify.fn above) won't return an error */
	        (void) dm_io(&req, 1, &region, NULL);
		i = j;
	}

	writecache_notify_io(0, &endio);
	wait_for_completion_io(&endio.c);

	writecache_disk_flush(wc, wc->ssd_dev);

	memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
}

static void writecache_commit_flushed(struct dm_writecache *wc)
{
	if (WC_MODE_PMEM(wc))
		wmb();
	else
		ssd_commit_flushed(wc);
}

static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
{
	int r;
	struct dm_io_region region;
	struct dm_io_request req;

	region.bdev = dev->bdev;
	region.sector = 0;
	region.count = 0;
	req.bi_op = REQ_OP_WRITE;
	req.bi_op_flags = REQ_PREFLUSH;
	req.mem.type = DM_IO_KMEM;
	req.mem.ptr.addr = NULL;
	req.client = wc->dm_io;
	req.notify.fn = NULL;

	r = dm_io(&req, 1, &region, NULL);
	if (unlikely(r))
		writecache_error(wc, r, "error flushing metadata: %d", r);
}

static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
{
	wait_event(wc->bio_in_progress_wait[direction],
		   !atomic_read(&wc->bio_in_progress[direction]));
}

#define WFE_RETURN_FOLLOWING	1
#define WFE_LOWEST_SEQ		2

static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
					      uint64_t block, int flags)
{
	struct wc_entry *e;
	struct rb_node *node = wc->tree.rb_node;

	if (unlikely(!node))
		return NULL;

	while (1) {
		e = container_of(node, struct wc_entry, rb_node);
		if (read_original_sector(wc, e) == block)
			break;

		node = (read_original_sector(wc, e) >= block ?
			e->rb_node.rb_left : e->rb_node.rb_right);
		if (unlikely(!node)) {
			if (!(flags & WFE_RETURN_FOLLOWING))
				return NULL;
			if (read_original_sector(wc, e) >= block) {
				return e;
			} else {
				node = rb_next(&e->rb_node);
				if (unlikely(!node))
					return NULL;
				e = container_of(node, struct wc_entry, rb_node);
				return e;
			}
		}
	}

	while (1) {
		struct wc_entry *e2;
		if (flags & WFE_LOWEST_SEQ)
			node = rb_prev(&e->rb_node);
		else
			node = rb_next(&e->rb_node);
		if (unlikely(!node))
			return e;
		e2 = container_of(node, struct wc_entry, rb_node);
		if (read_original_sector(wc, e2) != block)
			return e;
		e = e2;
	}
}

static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
{
	struct wc_entry *e;
	struct rb_node **node = &wc->tree.rb_node, *parent = NULL;

	while (*node) {
		e = container_of(*node, struct wc_entry, rb_node);
		parent = &e->rb_node;
		if (read_original_sector(wc, e) > read_original_sector(wc, ins))
			node = &parent->rb_left;
		else
			node = &parent->rb_right;
	}
	rb_link_node(&ins->rb_node, parent, node);
	rb_insert_color(&ins->rb_node, &wc->tree);
	list_add(&ins->lru, &wc->lru);
}

static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
{
	list_del(&e->lru);
	rb_erase(&e->rb_node, &wc->tree);
}

static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
{
	if (WC_MODE_SORT_FREELIST(wc)) {
		struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
		if (unlikely(!*node))
			wc->current_free = e;
		while (*node) {
			parent = *node;
			if (&e->rb_node < *node)
				node = &parent->rb_left;
			else
				node = &parent->rb_right;
		}
		rb_link_node(&e->rb_node, parent, node);
		rb_insert_color(&e->rb_node, &wc->freetree);
	} else {
		list_add_tail(&e->lru, &wc->freelist);
	}
	wc->freelist_size++;
}

static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc)
{
	struct wc_entry *e;

	if (WC_MODE_SORT_FREELIST(wc)) {
		struct rb_node *next;
		if (unlikely(!wc->current_free))
			return NULL;
		e = wc->current_free;
		next = rb_next(&e->rb_node);
		rb_erase(&e->rb_node, &wc->freetree);
		if (unlikely(!next))
			next = rb_first(&wc->freetree);
		wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
	} else {
		if (unlikely(list_empty(&wc->freelist)))
			return NULL;
		e = container_of(wc->freelist.next, struct wc_entry, lru);
		list_del(&e->lru);
	}
	wc->freelist_size--;
	if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
		queue_work(wc->writeback_wq, &wc->writeback_work);

	return e;
}

static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
{
	writecache_unlink(wc, e);
	writecache_add_to_freelist(wc, e);
	clear_seq_count(wc, e);
	writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
	if (unlikely(waitqueue_active(&wc->freelist_wait)))
		wake_up(&wc->freelist_wait);
}

static void writecache_wait_on_freelist(struct dm_writecache *wc)
{
	DEFINE_WAIT(wait);

	prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
	wc_unlock(wc);
	io_schedule();
	finish_wait(&wc->freelist_wait, &wait);
	wc_lock(wc);
}

static void writecache_poison_lists(struct dm_writecache *wc)
{
	/*
	 * Catch incorrect access to these values while the device is suspended.
	 */
	memset(&wc->tree, -1, sizeof wc->tree);
	wc->lru.next = LIST_POISON1;
	wc->lru.prev = LIST_POISON2;
	wc->freelist.next = LIST_POISON1;
	wc->freelist.prev = LIST_POISON2;
}

static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
{
	writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
	if (WC_MODE_PMEM(wc))
		writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
}

static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
{
	return read_seq_count(wc, e) < wc->seq_count;
}

static void writecache_flush(struct dm_writecache *wc)
{
	struct wc_entry *e, *e2;
	bool need_flush_after_free;

	wc->uncommitted_blocks = 0;
	del_timer(&wc->autocommit_timer);

	if (list_empty(&wc->lru))
		return;

	e = container_of(wc->lru.next, struct wc_entry, lru);
	if (writecache_entry_is_committed(wc, e)) {
		if (wc->overwrote_committed) {
			writecache_wait_for_ios(wc, WRITE);
			writecache_disk_flush(wc, wc->ssd_dev);
			wc->overwrote_committed = false;
		}
		return;
	}
	while (1) {
		writecache_flush_entry(wc, e);
		if (unlikely(e->lru.next == &wc->lru))
			break;
		e2 = container_of(e->lru.next, struct wc_entry, lru);
		if (writecache_entry_is_committed(wc, e2))
			break;
		e = e2;
		cond_resched();
	}
	writecache_commit_flushed(wc);

	if (!WC_MODE_PMEM(wc))
		writecache_wait_for_ios(wc, WRITE);

	wc->seq_count++;
	pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
	writecache_flush_region(wc, &sb(wc)->seq_count, sizeof sb(wc)->seq_count);
	writecache_commit_flushed(wc);

	wc->overwrote_committed = false;

	need_flush_after_free = false;
	while (1) {
		/* Free another committed entry with lower seq-count */
		struct rb_node *rb_node = rb_prev(&e->rb_node);

		if (rb_node) {
			e2 = container_of(rb_node, struct wc_entry, rb_node);
			if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
			    likely(!e2->write_in_progress)) {
				writecache_free_entry(wc, e2);
				need_flush_after_free = true;
			}
		}
		if (unlikely(e->lru.prev == &wc->lru))
			break;
		e = container_of(e->lru.prev, struct wc_entry, lru);
		cond_resched();
	}

	if (need_flush_after_free)
		writecache_commit_flushed(wc);
}

static void writecache_flush_work(struct work_struct *work)
{
	struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);

	wc_lock(wc);
	writecache_flush(wc);
	wc_unlock(wc);
}

static void writecache_autocommit_timer(struct timer_list *t)
{
	struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
	if (!writecache_has_error(wc))
		queue_work(wc->writeback_wq, &wc->flush_work);
}

static void writecache_schedule_autocommit(struct dm_writecache *wc)
{
	if (!timer_pending(&wc->autocommit_timer))
		mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
}

static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
{
	struct wc_entry *e;
	bool discarded_something = false;

	e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
	if (unlikely(!e))
		return;

	while (read_original_sector(wc, e) < end) {
		struct rb_node *node = rb_next(&e->rb_node);

		if (likely(!e->write_in_progress)) {
			if (!discarded_something) {
				writecache_wait_for_ios(wc, READ);
				writecache_wait_for_ios(wc, WRITE);
				discarded_something = true;
			}
			writecache_free_entry(wc, e);
		}

		if (unlikely(!node))
			break;

		e = container_of(node, struct wc_entry, rb_node);
	}

	if (discarded_something)
		writecache_commit_flushed(wc);
}

static bool writecache_wait_for_writeback(struct dm_writecache *wc)
{
	if (wc->writeback_size) {
		writecache_wait_on_freelist(wc);
		return true;
	}
	return false;
}

static void writecache_suspend(struct dm_target *ti)
{
	struct dm_writecache *wc = ti->private;
	bool flush_on_suspend;

	del_timer_sync(&wc->autocommit_timer);

	wc_lock(wc);
	writecache_flush(wc);
	flush_on_suspend = wc->flush_on_suspend;
	if (flush_on_suspend) {
		wc->flush_on_suspend = false;
		wc->writeback_all++;
		queue_work(wc->writeback_wq, &wc->writeback_work);
	}
	wc_unlock(wc);

	flush_workqueue(wc->writeback_wq);

	wc_lock(wc);
	if (flush_on_suspend)
		wc->writeback_all--;
	while (writecache_wait_for_writeback(wc));

	if (WC_MODE_PMEM(wc))
		persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);

	writecache_poison_lists(wc);

	wc_unlock(wc);
}

static int writecache_alloc_entries(struct dm_writecache *wc)
{
	size_t b;

	if (wc->entries)
		return 0;
	wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
	if (!wc->entries)
		return -ENOMEM;
	for (b = 0; b < wc->n_blocks; b++) {
		struct wc_entry *e = &wc->entries[b];
		e->index = b;
		e->write_in_progress = false;
	}

	return 0;
}

static void writecache_resume(struct dm_target *ti)
{
	struct dm_writecache *wc = ti->private;
	size_t b;
	bool need_flush = false;
	__le64 sb_seq_count;
	int r;

	wc_lock(wc);

	if (WC_MODE_PMEM(wc))
		persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);

	wc->tree = RB_ROOT;
	INIT_LIST_HEAD(&wc->lru);
	if (WC_MODE_SORT_FREELIST(wc)) {
		wc->freetree = RB_ROOT;
		wc->current_free = NULL;
	} else {
		INIT_LIST_HEAD(&wc->freelist);
	}
	wc->freelist_size = 0;

	r = memcpy_mcsafe(&sb_seq_count, &sb(wc)->seq_count, sizeof(uint64_t));
	if (r) {
		writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
		sb_seq_count = cpu_to_le64(0);
	}
	wc->seq_count = le64_to_cpu(sb_seq_count);

#ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
	for (b = 0; b < wc->n_blocks; b++) {
		struct wc_entry *e = &wc->entries[b];
		struct wc_memory_entry wme;
		if (writecache_has_error(wc)) {
			e->original_sector = -1;
			e->seq_count = -1;
			continue;
		}
		r = memcpy_mcsafe(&wme, memory_entry(wc, e), sizeof(struct wc_memory_entry));
		if (r) {
			writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
					 (unsigned long)b, r);
			e->original_sector = -1;
			e->seq_count = -1;
		} else {
			e->original_sector = le64_to_cpu(wme.original_sector);
			e->seq_count = le64_to_cpu(wme.seq_count);
		}
	}
#endif
	for (b = 0; b < wc->n_blocks; b++) {
		struct wc_entry *e = &wc->entries[b];
		if (!writecache_entry_is_committed(wc, e)) {
			if (read_seq_count(wc, e) != -1) {
erase_this:
				clear_seq_count(wc, e);
				need_flush = true;
			}
			writecache_add_to_freelist(wc, e);
		} else {
			struct wc_entry *old;

			old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
			if (!old) {
				writecache_insert_entry(wc, e);
			} else {
				if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
					writecache_error(wc, -EINVAL,
						 "two identical entries, position %llu, sector %llu, sequence %llu",
						 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
						 (unsigned long long)read_seq_count(wc, e));
				}
				if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
					goto erase_this;
				} else {
					writecache_free_entry(wc, old);
					writecache_insert_entry(wc, e);
					need_flush = true;
				}
			}
		}
		cond_resched();
	}

	if (need_flush) {
		writecache_flush_all_metadata(wc);
		writecache_commit_flushed(wc);
	}

	wc_unlock(wc);
}

static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
{
	if (argc != 1)
		return -EINVAL;

	wc_lock(wc);
	if (dm_suspended(wc->ti)) {
		wc_unlock(wc);
		return -EBUSY;
	}
	if (writecache_has_error(wc)) {
		wc_unlock(wc);
		return -EIO;
	}

	writecache_flush(wc);
	wc->writeback_all++;
	queue_work(wc->writeback_wq, &wc->writeback_work);
	wc_unlock(wc);

	flush_workqueue(wc->writeback_wq);

	wc_lock(wc);
	wc->writeback_all--;
	if (writecache_has_error(wc)) {
		wc_unlock(wc);
		return -EIO;
	}
	wc_unlock(wc);

	return 0;
}

static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
{
	if (argc != 1)
		return -EINVAL;

	wc_lock(wc);
	wc->flush_on_suspend = true;
	wc_unlock(wc);

	return 0;
}

static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
			      char *result, unsigned maxlen)
{
	int r = -EINVAL;
	struct dm_writecache *wc = ti->private;

	if (!strcasecmp(argv[0], "flush"))
		r = process_flush_mesg(argc, argv, wc);
	else if (!strcasecmp(argv[0], "flush_on_suspend"))
		r = process_flush_on_suspend_mesg(argc, argv, wc);
	else
		DMERR("unrecognised message received: %s", argv[0]);

	return r;
}

static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
{
	void *buf;
	unsigned long flags;
	unsigned size;
	int rw = bio_data_dir(bio);
	unsigned remaining_size = wc->block_size;

	do {
		struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
		buf = bvec_kmap_irq(&bv, &flags);
		size = bv.bv_len;
		if (unlikely(size > remaining_size))
			size = remaining_size;

		if (rw == READ) {
			int r;
			r = memcpy_mcsafe(buf, data, size);
			flush_dcache_page(bio_page(bio));
			if (unlikely(r)) {
				writecache_error(wc, r, "hardware memory error when reading data: %d", r);
				bio->bi_status = BLK_STS_IOERR;
			}
		} else {
			flush_dcache_page(bio_page(bio));
			memcpy_flushcache(data, buf, size);
		}

		bvec_kunmap_irq(buf, &flags);

		data = (char *)data + size;
		remaining_size -= size;
		bio_advance(bio, size);
	} while (unlikely(remaining_size));
}

static int writecache_flush_thread(void *data)
{
	struct dm_writecache *wc = data;

	while (1) {
		struct bio *bio;

		wc_lock(wc);
		bio = bio_list_pop(&wc->flush_list);
		if (!bio) {
			set_current_state(TASK_INTERRUPTIBLE);
			wc_unlock(wc);

			if (unlikely(kthread_should_stop())) {
				set_current_state(TASK_RUNNING);
				break;
			}

			schedule();
			continue;
		}

		if (bio_op(bio) == REQ_OP_DISCARD) {
			writecache_discard(wc, bio->bi_iter.bi_sector,
					   bio_end_sector(bio));
			wc_unlock(wc);
			bio_set_dev(bio, wc->dev->bdev);
			generic_make_request(bio);
		} else {
			writecache_flush(wc);
			wc_unlock(wc);
			if (writecache_has_error(wc))
				bio->bi_status = BLK_STS_IOERR;
			bio_endio(bio);
		}
	}

	return 0;
}

static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
{
	if (bio_list_empty(&wc->flush_list))
		wake_up_process(wc->flush_thread);
	bio_list_add(&wc->flush_list, bio);
}

static int writecache_map(struct dm_target *ti, struct bio *bio)
{
	struct wc_entry *e;
	struct dm_writecache *wc = ti->private;

	bio->bi_private = NULL;

	wc_lock(wc);

	if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
		if (writecache_has_error(wc))
			goto unlock_error;
		if (WC_MODE_PMEM(wc)) {
			writecache_flush(wc);
			if (writecache_has_error(wc))
				goto unlock_error;
			goto unlock_submit;
		} else {
			writecache_offload_bio(wc, bio);
			goto unlock_return;
		}
	}

	bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);

	if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
				(wc->block_size / 512 - 1)) != 0)) {
		DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
		      (unsigned long long)bio->bi_iter.bi_sector,
		      bio->bi_iter.bi_size, wc->block_size);
		goto unlock_error;
	}

	if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
		if (writecache_has_error(wc))
			goto unlock_error;
		if (WC_MODE_PMEM(wc)) {
			writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
			goto unlock_remap_origin;
		} else {
			writecache_offload_bio(wc, bio);
			goto unlock_return;
		}
	}

	if (bio_data_dir(bio) == READ) {
read_next_block:
		e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
		if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
			if (WC_MODE_PMEM(wc)) {
				bio_copy_block(wc, bio, memory_data(wc, e));
				if (bio->bi_iter.bi_size)
					goto read_next_block;
				goto unlock_submit;
			} else {
				dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
				bio_set_dev(bio, wc->ssd_dev->bdev);
				bio->bi_iter.bi_sector = cache_sector(wc, e);
				if (!writecache_entry_is_committed(wc, e))
					writecache_wait_for_ios(wc, WRITE);
				goto unlock_remap;
			}
		} else {
			if (e) {
				sector_t next_boundary =
					read_original_sector(wc, e) - bio->bi_iter.bi_sector;
				if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
					dm_accept_partial_bio(bio, next_boundary);
				}
			}
			goto unlock_remap_origin;
		}
	} else {
		do {
			if (writecache_has_error(wc))
				goto unlock_error;
			e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
			if (e) {
				if (!writecache_entry_is_committed(wc, e))
					goto bio_copy;
				if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
					wc->overwrote_committed = true;
					goto bio_copy;
				}
			}
			e = writecache_pop_from_freelist(wc);
			if (unlikely(!e)) {
				writecache_wait_on_freelist(wc);
				continue;
			}
			write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
			writecache_insert_entry(wc, e);
			wc->uncommitted_blocks++;
bio_copy:
			if (WC_MODE_PMEM(wc)) {
				bio_copy_block(wc, bio, memory_data(wc, e));
			} else {
				dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
				bio_set_dev(bio, wc->ssd_dev->bdev);
				bio->bi_iter.bi_sector = cache_sector(wc, e);
				if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
					wc->uncommitted_blocks = 0;
					queue_work(wc->writeback_wq, &wc->flush_work);
				} else {
					writecache_schedule_autocommit(wc);
				}
				goto unlock_remap;
			}
		} while (bio->bi_iter.bi_size);

		if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks))
			writecache_flush(wc);
		else
			writecache_schedule_autocommit(wc);
		goto unlock_submit;
	}

unlock_remap_origin:
	bio_set_dev(bio, wc->dev->bdev);
	wc_unlock(wc);
	return DM_MAPIO_REMAPPED;

unlock_remap:
	/* make sure that writecache_end_io decrements bio_in_progress: */
	bio->bi_private = (void *)1;
	atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
	wc_unlock(wc);
	return DM_MAPIO_REMAPPED;

unlock_submit:
	wc_unlock(wc);
	bio_endio(bio);
	return DM_MAPIO_SUBMITTED;

unlock_return:
	wc_unlock(wc);
	return DM_MAPIO_SUBMITTED;

unlock_error:
	wc_unlock(wc);
	bio_io_error(bio);
	return DM_MAPIO_SUBMITTED;
}

static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
{
	struct dm_writecache *wc = ti->private;

	if (bio->bi_private != NULL) {
		int dir = bio_data_dir(bio);
		if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
			if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
				wake_up(&wc->bio_in_progress_wait[dir]);
	}
	return 0;
}

static int writecache_iterate_devices(struct dm_target *ti,
				      iterate_devices_callout_fn fn, void *data)
{
	struct dm_writecache *wc = ti->private;

	return fn(ti, wc->dev, 0, ti->len, data);
}

static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
{
	struct dm_writecache *wc = ti->private;

	if (limits->logical_block_size < wc->block_size)
		limits->logical_block_size = wc->block_size;

	if (limits->physical_block_size < wc->block_size)
		limits->physical_block_size = wc->block_size;

	if (limits->io_min < wc->block_size)
		limits->io_min = wc->block_size;
}


static void writecache_writeback_endio(struct bio *bio)
{
	struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
	struct dm_writecache *wc = wb->wc;
	unsigned long flags;

	raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
	if (unlikely(list_empty(&wc->endio_list)))
		wake_up_process(wc->endio_thread);
	list_add_tail(&wb->endio_entry, &wc->endio_list);
	raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
}

static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
{
	struct copy_struct *c = ptr;
	struct dm_writecache *wc = c->wc;

	c->error = likely(!(read_err | write_err)) ? 0 : -EIO;

	raw_spin_lock_irq(&wc->endio_list_lock);
	if (unlikely(list_empty(&wc->endio_list)))
		wake_up_process(wc->endio_thread);
	list_add_tail(&c->endio_entry, &wc->endio_list);
	raw_spin_unlock_irq(&wc->endio_list_lock);
}

static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
{
	unsigned i;
	struct writeback_struct *wb;
	struct wc_entry *e;
	unsigned long n_walked = 0;

	do {
		wb = list_entry(list->next, struct writeback_struct, endio_entry);
		list_del(&wb->endio_entry);

		if (unlikely(wb->bio.bi_status != BLK_STS_OK))
			writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
					"write error %d", wb->bio.bi_status);
		i = 0;
		do {
			e = wb->wc_list[i];
			BUG_ON(!e->write_in_progress);
			e->write_in_progress = false;
			INIT_LIST_HEAD(&e->lru);
			if (!writecache_has_error(wc))
				writecache_free_entry(wc, e);
			BUG_ON(!wc->writeback_size);
			wc->writeback_size--;
			n_walked++;
			if (unlikely(n_walked >= ENDIO_LATENCY)) {
				writecache_commit_flushed(wc);
				wc_unlock(wc);
				wc_lock(wc);
				n_walked = 0;
			}
		} while (++i < wb->wc_list_n);

		if (wb->wc_list != wb->wc_list_inline)
			kfree(wb->wc_list);
		bio_put(&wb->bio);
	} while (!list_empty(list));
}

static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
{
	struct copy_struct *c;
	struct wc_entry *e;

	do {
		c = list_entry(list->next, struct copy_struct, endio_entry);
		list_del(&c->endio_entry);

		if (unlikely(c->error))
			writecache_error(wc, c->error, "copy error");

		e = c->e;
		do {
			BUG_ON(!e->write_in_progress);
			e->write_in_progress = false;
			INIT_LIST_HEAD(&e->lru);
			if (!writecache_has_error(wc))
				writecache_free_entry(wc, e);

			BUG_ON(!wc->writeback_size);
			wc->writeback_size--;
			e++;
		} while (--c->n_entries);
		mempool_free(c, &wc->copy_pool);
	} while (!list_empty(list));
}

static int writecache_endio_thread(void *data)
{
	struct dm_writecache *wc = data;

	while (1) {
		struct list_head list;

		raw_spin_lock_irq(&wc->endio_list_lock);
		if (!list_empty(&wc->endio_list))
			goto pop_from_list;
		set_current_state(TASK_INTERRUPTIBLE);
		raw_spin_unlock_irq(&wc->endio_list_lock);

		if (unlikely(kthread_should_stop())) {
			set_current_state(TASK_RUNNING);
			break;
		}

		schedule();

		continue;

pop_from_list:
		list = wc->endio_list;
		list.next->prev = list.prev->next = &list;
		INIT_LIST_HEAD(&wc->endio_list);
		raw_spin_unlock_irq(&wc->endio_list_lock);

		if (!WC_MODE_FUA(wc))
			writecache_disk_flush(wc, wc->dev);

		wc_lock(wc);

		if (WC_MODE_PMEM(wc)) {
			__writecache_endio_pmem(wc, &list);
		} else {
			__writecache_endio_ssd(wc, &list);
			writecache_wait_for_ios(wc, READ);
		}

		writecache_commit_flushed(wc);

		wc_unlock(wc);
	}

	return 0;
}

static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e, gfp_t gfp)
{
	struct dm_writecache *wc = wb->wc;
	unsigned block_size = wc->block_size;
	void *address = memory_data(wc, e);

	persistent_memory_flush_cache(address, block_size);
	return bio_add_page(&wb->bio, persistent_memory_page(address),
			    block_size, persistent_memory_page_offset(address)) != 0;
}

struct writeback_list {
	struct list_head list;
	size_t size;
};

static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
{
	if (unlikely(wc->max_writeback_jobs)) {
		if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
			wc_lock(wc);
			while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
				writecache_wait_on_freelist(wc);
			wc_unlock(wc);
		}
	}
	cond_resched();
}

static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
{
	struct wc_entry *e, *f;
	struct bio *bio;
	struct writeback_struct *wb;
	unsigned max_pages;

	while (wbl->size) {
		wbl->size--;
		e = container_of(wbl->list.prev, struct wc_entry, lru);
		list_del(&e->lru);

		max_pages = e->wc_list_contiguous;

		bio = bio_alloc_bioset(GFP_NOIO, max_pages, &wc->bio_set);
		wb = container_of(bio, struct writeback_struct, bio);
		wb->wc = wc;
		bio->bi_end_io = writecache_writeback_endio;
		bio_set_dev(bio, wc->dev->bdev);
		bio->bi_iter.bi_sector = read_original_sector(wc, e);
		if (max_pages <= WB_LIST_INLINE ||
		    unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
							   GFP_NOIO | __GFP_NORETRY |
							   __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
			wb->wc_list = wb->wc_list_inline;
			max_pages = WB_LIST_INLINE;
		}

		BUG_ON(!wc_add_block(wb, e, GFP_NOIO));

		wb->wc_list[0] = e;
		wb->wc_list_n = 1;

		while (wbl->size && wb->wc_list_n < max_pages) {
			f = container_of(wbl->list.prev, struct wc_entry, lru);
			if (read_original_sector(wc, f) !=
			    read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
				break;
			if (!wc_add_block(wb, f, GFP_NOWAIT | __GFP_NOWARN))
				break;
			wbl->size--;
			list_del(&f->lru);
			wb->wc_list[wb->wc_list_n++] = f;
			e = f;
		}
		bio_set_op_attrs(bio, REQ_OP_WRITE, WC_MODE_FUA(wc) * REQ_FUA);
		if (writecache_has_error(wc)) {
			bio->bi_status = BLK_STS_IOERR;
			bio_endio(bio);
		} else {
			submit_bio(bio);
		}

		__writeback_throttle(wc, wbl);
	}
}

static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
{
	struct wc_entry *e, *f;
	struct dm_io_region from, to;
	struct copy_struct *c;

	while (wbl->size) {
		unsigned n_sectors;

		wbl->size--;
		e = container_of(wbl->list.prev, struct wc_entry, lru);
		list_del(&e->lru);

		n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);

		from.bdev = wc->ssd_dev->bdev;
		from.sector = cache_sector(wc, e);
		from.count = n_sectors;
		to.bdev = wc->dev->bdev;
		to.sector = read_original_sector(wc, e);
		to.count = n_sectors;

		c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
		c->wc = wc;
		c->e = e;
		c->n_entries = e->wc_list_contiguous;

		while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
			wbl->size--;
			f = container_of(wbl->list.prev, struct wc_entry, lru);
			BUG_ON(f != e + 1);
			list_del(&f->lru);
			e = f;
		}

		dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);

		__writeback_throttle(wc, wbl);
	}
}

static void writecache_writeback(struct work_struct *work)
{
	struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
	struct blk_plug plug;
	struct wc_entry *f, *g, *e = NULL;
	struct rb_node *node, *next_node;
	struct list_head skipped;
	struct writeback_list wbl;
	unsigned long n_walked;

	wc_lock(wc);
restart:
	if (writecache_has_error(wc)) {
		wc_unlock(wc);
		return;
	}

	if (unlikely(wc->writeback_all)) {
		if (writecache_wait_for_writeback(wc))
			goto restart;
	}

	if (wc->overwrote_committed) {
		writecache_wait_for_ios(wc, WRITE);
	}

	n_walked = 0;
	INIT_LIST_HEAD(&skipped);
	INIT_LIST_HEAD(&wbl.list);
	wbl.size = 0;
	while (!list_empty(&wc->lru) &&
	       (wc->writeback_all ||
		wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark)) {

		n_walked++;
		if (unlikely(n_walked > WRITEBACK_LATENCY) &&
		    likely(!wc->writeback_all) && likely(!dm_suspended(wc->ti))) {
			queue_work(wc->writeback_wq, &wc->writeback_work);
			break;
		}

		if (unlikely(wc->writeback_all)) {
			if (unlikely(!e)) {
				writecache_flush(wc);
				e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
			} else
				e = g;
		} else
			e = container_of(wc->lru.prev, struct wc_entry, lru);
		BUG_ON(e->write_in_progress);
		if (unlikely(!writecache_entry_is_committed(wc, e))) {
			writecache_flush(wc);
		}
		node = rb_prev(&e->rb_node);
		if (node) {
			f = container_of(node, struct wc_entry, rb_node);
			if (unlikely(read_original_sector(wc, f) ==
				     read_original_sector(wc, e))) {
				BUG_ON(!f->write_in_progress);
				list_del(&e->lru);
				list_add(&e->lru, &skipped);
				cond_resched();
				continue;
			}
		}
		wc->writeback_size++;
		list_del(&e->lru);
		list_add(&e->lru, &wbl.list);
		wbl.size++;
		e->write_in_progress = true;
		e->wc_list_contiguous = 1;

		f = e;

		while (1) {
			next_node = rb_next(&f->rb_node);
			if (unlikely(!next_node))
				break;
			g = container_of(next_node, struct wc_entry, rb_node);
			if (unlikely(read_original_sector(wc, g) ==
			    read_original_sector(wc, f))) {
				f = g;
				continue;
			}
			if (read_original_sector(wc, g) !=
			    read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
				break;
			if (unlikely(g->write_in_progress))
				break;
			if (unlikely(!writecache_entry_is_committed(wc, g)))
				break;

			if (!WC_MODE_PMEM(wc)) {
				if (g != f + 1)
					break;
			}

			n_walked++;
			//if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
			//	break;

			wc->writeback_size++;
			list_del(&g->lru);
			list_add(&g->lru, &wbl.list);
			wbl.size++;
			g->write_in_progress = true;
			g->wc_list_contiguous = BIO_MAX_PAGES;
			f = g;
			e->wc_list_contiguous++;
			if (unlikely(e->wc_list_contiguous == BIO_MAX_PAGES)) {
				if (unlikely(wc->writeback_all)) {
					next_node = rb_next(&f->rb_node);
					if (likely(next_node))
						g = container_of(next_node, struct wc_entry, rb_node);
				}
				break;
			}
		}
		cond_resched();
	}

	if (!list_empty(&skipped)) {
		list_splice_tail(&skipped, &wc->lru);
		/*
		 * If we didn't do any progress, we must wait until some
		 * writeback finishes to avoid burning CPU in a loop
		 */
		if (unlikely(!wbl.size))
			writecache_wait_for_writeback(wc);
	}

	wc_unlock(wc);

	blk_start_plug(&plug);

	if (WC_MODE_PMEM(wc))
		__writecache_writeback_pmem(wc, &wbl);
	else
		__writecache_writeback_ssd(wc, &wbl);

	blk_finish_plug(&plug);

	if (unlikely(wc->writeback_all)) {
		wc_lock(wc);
		while (writecache_wait_for_writeback(wc));
		wc_unlock(wc);
	}
}

static int calculate_memory_size(uint64_t device_size, unsigned block_size,
				 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
{
	uint64_t n_blocks, offset;
	struct wc_entry e;

	n_blocks = device_size;
	do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));

	while (1) {
		if (!n_blocks)
			return -ENOSPC;
		/* Verify the following entries[n_blocks] won't overflow */
		if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
				 sizeof(struct wc_memory_entry)))
			return -EFBIG;
		offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
		offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
		if (offset + n_blocks * block_size <= device_size)
			break;
		n_blocks--;
	}

	/* check if the bit field overflows */
	e.index = n_blocks;
	if (e.index != n_blocks)
		return -EFBIG;

	if (n_blocks_p)
		*n_blocks_p = n_blocks;
	if (n_metadata_blocks_p)
		*n_metadata_blocks_p = offset >> __ffs(block_size);
	return 0;
}

static int init_memory(struct dm_writecache *wc)
{
	size_t b;
	int r;

	r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
	if (r)
		return r;

	r = writecache_alloc_entries(wc);
	if (r)
		return r;

	for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
		pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
	pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
	pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
	pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
	pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));

	for (b = 0; b < wc->n_blocks; b++)
		write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);

	writecache_flush_all_metadata(wc);
	writecache_commit_flushed(wc);
	pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
	writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
	writecache_commit_flushed(wc);

	return 0;
}

static void writecache_dtr(struct dm_target *ti)
{
	struct dm_writecache *wc = ti->private;

	if (!wc)
		return;

	if (wc->endio_thread)
		kthread_stop(wc->endio_thread);

	if (wc->flush_thread)
		kthread_stop(wc->flush_thread);

	bioset_exit(&wc->bio_set);

	mempool_exit(&wc->copy_pool);

	if (wc->writeback_wq)
		destroy_workqueue(wc->writeback_wq);

	if (wc->dev)
		dm_put_device(ti, wc->dev);

	if (wc->ssd_dev)
		dm_put_device(ti, wc->ssd_dev);

	if (wc->entries)
		vfree(wc->entries);

	if (wc->memory_map) {
		if (WC_MODE_PMEM(wc))
			persistent_memory_release(wc);
		else
			vfree(wc->memory_map);
	}

	if (wc->dm_kcopyd)
		dm_kcopyd_client_destroy(wc->dm_kcopyd);

	if (wc->dm_io)
		dm_io_client_destroy(wc->dm_io);

	if (wc->dirty_bitmap)
		vfree(wc->dirty_bitmap);

	kfree(wc);
}

static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
{
	struct dm_writecache *wc;
	struct dm_arg_set as;
	const char *string;
	unsigned opt_params;
	size_t offset, data_size;
	int i, r;
	char dummy;
	int high_wm_percent = HIGH_WATERMARK;
	int low_wm_percent = LOW_WATERMARK;
	uint64_t x;
	struct wc_memory_superblock s;

	static struct dm_arg _args[] = {
		{0, 10, "Invalid number of feature args"},
	};

	as.argc = argc;
	as.argv = argv;

	wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
	if (!wc) {
		ti->error = "Cannot allocate writecache structure";
		r = -ENOMEM;
		goto bad;
	}
	ti->private = wc;
	wc->ti = ti;

	mutex_init(&wc->lock);
	writecache_poison_lists(wc);
	init_waitqueue_head(&wc->freelist_wait);
	timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);

	for (i = 0; i < 2; i++) {
		atomic_set(&wc->bio_in_progress[i], 0);
		init_waitqueue_head(&wc->bio_in_progress_wait[i]);
	}

	wc->dm_io = dm_io_client_create();
	if (IS_ERR(wc->dm_io)) {
		r = PTR_ERR(wc->dm_io);
		ti->error = "Unable to allocate dm-io client";
		wc->dm_io = NULL;
		goto bad;
	}

	wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
	if (!wc->writeback_wq) {
		r = -ENOMEM;
		ti->error = "Could not allocate writeback workqueue";
		goto bad;
	}
	INIT_WORK(&wc->writeback_work, writecache_writeback);
	INIT_WORK(&wc->flush_work, writecache_flush_work);

	raw_spin_lock_init(&wc->endio_list_lock);
	INIT_LIST_HEAD(&wc->endio_list);
	wc->endio_thread = kthread_create(writecache_endio_thread, wc, "writecache_endio");
	if (IS_ERR(wc->endio_thread)) {
		r = PTR_ERR(wc->endio_thread);
		wc->endio_thread = NULL;
		ti->error = "Couldn't spawn endio thread";
		goto bad;
	}
	wake_up_process(wc->endio_thread);

	/*
	 * Parse the mode (pmem or ssd)
	 */
	string = dm_shift_arg(&as);
	if (!string)
		goto bad_arguments;

	if (!strcasecmp(string, "s")) {
		wc->pmem_mode = false;
	} else if (!strcasecmp(string, "p")) {
#ifdef DM_WRITECACHE_HAS_PMEM
		wc->pmem_mode = true;
		wc->writeback_fua = true;
#else
		/*
		 * If the architecture doesn't support persistent memory or
		 * the kernel doesn't support any DAX drivers, this driver can
		 * only be used in SSD-only mode.
		 */
		r = -EOPNOTSUPP;
		ti->error = "Persistent memory or DAX not supported on this system";
		goto bad;
#endif
	} else {
		goto bad_arguments;
	}

	if (WC_MODE_PMEM(wc)) {
		r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
				offsetof(struct writeback_struct, bio),
				BIOSET_NEED_BVECS);
		if (r) {
			ti->error = "Could not allocate bio set";
			goto bad;
		}
	} else {
		r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
		if (r) {
			ti->error = "Could not allocate mempool";
			goto bad;
		}
	}

	/*
	 * Parse the origin data device
	 */
	string = dm_shift_arg(&as);
	if (!string)
		goto bad_arguments;
	r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
	if (r) {
		ti->error = "Origin data device lookup failed";
		goto bad;
	}

	/*
	 * Parse cache data device (be it pmem or ssd)
	 */
	string = dm_shift_arg(&as);
	if (!string)
		goto bad_arguments;

	r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
	if (r) {
		ti->error = "Cache data device lookup failed";
		goto bad;
	}
	wc->memory_map_size = i_size_read(wc->ssd_dev->bdev->bd_inode);

	/*
	 * Parse the cache block size
	 */
	string = dm_shift_arg(&as);
	if (!string)
		goto bad_arguments;
	if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
	    wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
	    (wc->block_size & (wc->block_size - 1))) {
		r = -EINVAL;
		ti->error = "Invalid block size";
		goto bad;
	}
	wc->block_size_bits = __ffs(wc->block_size);

	wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
	wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
	wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);

	/*
	 * Parse optional arguments
	 */
	r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
	if (r)
		goto bad;

	while (opt_params) {
		string = dm_shift_arg(&as), opt_params--;
		if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
			unsigned long long start_sector;
			string = dm_shift_arg(&as), opt_params--;
			if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
				goto invalid_optional;
			wc->start_sector = start_sector;
			if (wc->start_sector != start_sector ||
			    wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
				goto invalid_optional;
		} else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
			string = dm_shift_arg(&as), opt_params--;
			if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
				goto invalid_optional;
			if (high_wm_percent < 0 || high_wm_percent > 100)
				goto invalid_optional;
			wc->high_wm_percent_set = true;
		} else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
			string = dm_shift_arg(&as), opt_params--;
			if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
				goto invalid_optional;
			if (low_wm_percent < 0 || low_wm_percent > 100)
				goto invalid_optional;
			wc->low_wm_percent_set = true;
		} else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
			string = dm_shift_arg(&as), opt_params--;
			if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
				goto invalid_optional;
			wc->max_writeback_jobs_set = true;
		} else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
			string = dm_shift_arg(&as), opt_params--;
			if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
				goto invalid_optional;
			wc->autocommit_blocks_set = true;
		} else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
			unsigned autocommit_msecs;
			string = dm_shift_arg(&as), opt_params--;
			if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
				goto invalid_optional;
			if (autocommit_msecs > 3600000)
				goto invalid_optional;
			wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
			wc->autocommit_time_set = true;
		} else if (!strcasecmp(string, "fua")) {
			if (WC_MODE_PMEM(wc)) {
				wc->writeback_fua = true;
				wc->writeback_fua_set = true;
			} else goto invalid_optional;
		} else if (!strcasecmp(string, "nofua")) {
			if (WC_MODE_PMEM(wc)) {
				wc->writeback_fua = false;
				wc->writeback_fua_set = true;
			} else goto invalid_optional;
		} else {
invalid_optional:
			r = -EINVAL;
			ti->error = "Invalid optional argument";
			goto bad;
		}
	}

	if (high_wm_percent < low_wm_percent) {
		r = -EINVAL;
		ti->error = "High watermark must be greater than or equal to low watermark";
		goto bad;
	}

	if (WC_MODE_PMEM(wc)) {
		r = persistent_memory_claim(wc);
		if (r) {
			ti->error = "Unable to map persistent memory for cache";
			goto bad;
		}
	} else {
		struct dm_io_region region;
		struct dm_io_request req;
		size_t n_blocks, n_metadata_blocks;
		uint64_t n_bitmap_bits;

		wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;

		bio_list_init(&wc->flush_list);
		wc->flush_thread = kthread_create(writecache_flush_thread, wc, "dm_writecache_flush");
		if (IS_ERR(wc->flush_thread)) {
			r = PTR_ERR(wc->flush_thread);
			wc->flush_thread = NULL;
			ti->error = "Couldn't spawn flush thread";
			goto bad;
		}
		wake_up_process(wc->flush_thread);

		r = calculate_memory_size(wc->memory_map_size, wc->block_size,
					  &n_blocks, &n_metadata_blocks);
		if (r) {
			ti->error = "Invalid device size";
			goto bad;
		}

		n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
				 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
		/* this is limitation of test_bit functions */
		if (n_bitmap_bits > 1U << 31) {
			r = -EFBIG;
			ti->error = "Invalid device size";
			goto bad;
		}

		wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
		if (!wc->memory_map) {
			r = -ENOMEM;
			ti->error = "Unable to allocate memory for metadata";
			goto bad;
		}

		wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
		if (IS_ERR(wc->dm_kcopyd)) {
			r = PTR_ERR(wc->dm_kcopyd);
			ti->error = "Unable to allocate dm-kcopyd client";
			wc->dm_kcopyd = NULL;
			goto bad;
		}

		wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
		wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
			BITS_PER_LONG * sizeof(unsigned long);
		wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
		if (!wc->dirty_bitmap) {
			r = -ENOMEM;
			ti->error = "Unable to allocate dirty bitmap";
			goto bad;
		}

		region.bdev = wc->ssd_dev->bdev;
		region.sector = wc->start_sector;
		region.count = wc->metadata_sectors;
		req.bi_op = REQ_OP_READ;
		req.bi_op_flags = REQ_SYNC;
		req.mem.type = DM_IO_VMA;
		req.mem.ptr.vma = (char *)wc->memory_map;
		req.client = wc->dm_io;
		req.notify.fn = NULL;

		r = dm_io(&req, 1, &region, NULL);
		if (r) {
			ti->error = "Unable to read metadata";
			goto bad;
		}
	}

	r = memcpy_mcsafe(&s, sb(wc), sizeof(struct wc_memory_superblock));
	if (r) {
		ti->error = "Hardware memory error when reading superblock";
		goto bad;
	}
	if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
		r = init_memory(wc);
		if (r) {
			ti->error = "Unable to initialize device";
			goto bad;
		}
		r = memcpy_mcsafe(&s, sb(wc), sizeof(struct wc_memory_superblock));
		if (r) {
			ti->error = "Hardware memory error when reading superblock";
			goto bad;
		}
	}

	if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
		ti->error = "Invalid magic in the superblock";
		r = -EINVAL;
		goto bad;
	}

	if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
		ti->error = "Invalid version in the superblock";
		r = -EINVAL;
		goto bad;
	}

	if (le32_to_cpu(s.block_size) != wc->block_size) {
		ti->error = "Block size does not match superblock";
		r = -EINVAL;
		goto bad;
	}

	wc->n_blocks = le64_to_cpu(s.n_blocks);

	offset = wc->n_blocks * sizeof(struct wc_memory_entry);
	if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
overflow:
		ti->error = "Overflow in size calculation";
		r = -EINVAL;
		goto bad;
	}
	offset += sizeof(struct wc_memory_superblock);
	if (offset < sizeof(struct wc_memory_superblock))
		goto overflow;
	offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
	data_size = wc->n_blocks * (size_t)wc->block_size;
	if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
	    (offset + data_size < offset))
		goto overflow;
	if (offset + data_size > wc->memory_map_size) {
		ti->error = "Memory area is too small";
		r = -EINVAL;
		goto bad;
	}

	wc->metadata_sectors = offset >> SECTOR_SHIFT;
	wc->block_start = (char *)sb(wc) + offset;

	x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
	x += 50;
	do_div(x, 100);
	wc->freelist_high_watermark = x;
	x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
	x += 50;
	do_div(x, 100);
	wc->freelist_low_watermark = x;

	r = writecache_alloc_entries(wc);
	if (r) {
		ti->error = "Cannot allocate memory";
		goto bad;
	}

	ti->num_flush_bios = 1;
	ti->flush_supported = true;
	ti->num_discard_bios = 1;

	if (WC_MODE_PMEM(wc))
		persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);

	return 0;

bad_arguments:
	r = -EINVAL;
	ti->error = "Bad arguments";
bad:
	writecache_dtr(ti);
	return r;
}

static void writecache_status(struct dm_target *ti, status_type_t type,
			      unsigned status_flags, char *result, unsigned maxlen)
{
	struct dm_writecache *wc = ti->private;
	unsigned extra_args;
	unsigned sz = 0;
	uint64_t x;

	switch (type) {
	case STATUSTYPE_INFO:
		DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),
		       (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
		       (unsigned long long)wc->writeback_size);
		break;
	case STATUSTYPE_TABLE:
		DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
				wc->dev->name, wc->ssd_dev->name, wc->block_size);
		extra_args = 0;
		if (wc->start_sector)
			extra_args += 2;
		if (wc->high_wm_percent_set)
			extra_args += 2;
		if (wc->low_wm_percent_set)
			extra_args += 2;
		if (wc->max_writeback_jobs_set)
			extra_args += 2;
		if (wc->autocommit_blocks_set)
			extra_args += 2;
		if (wc->autocommit_time_set)
			extra_args += 2;
		if (wc->writeback_fua_set)
			extra_args++;

		DMEMIT("%u", extra_args);
		if (wc->start_sector)
			DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
		if (wc->high_wm_percent_set) {
			x = (uint64_t)wc->freelist_high_watermark * 100;
			x += wc->n_blocks / 2;
			do_div(x, (size_t)wc->n_blocks);
			DMEMIT(" high_watermark %u", 100 - (unsigned)x);
		}
		if (wc->low_wm_percent_set) {
			x = (uint64_t)wc->freelist_low_watermark * 100;
			x += wc->n_blocks / 2;
			do_div(x, (size_t)wc->n_blocks);
			DMEMIT(" low_watermark %u", 100 - (unsigned)x);
		}
		if (wc->max_writeback_jobs_set)
			DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
		if (wc->autocommit_blocks_set)
			DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
		if (wc->autocommit_time_set)
			DMEMIT(" autocommit_time %u", jiffies_to_msecs(wc->autocommit_jiffies));
		if (wc->writeback_fua_set)
			DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
		break;
	}
}

static struct target_type writecache_target = {
	.name			= "writecache",
	.version		= {1, 1, 1},
	.module			= THIS_MODULE,
	.ctr			= writecache_ctr,
	.dtr			= writecache_dtr,
	.status			= writecache_status,
	.postsuspend		= writecache_suspend,
	.resume			= writecache_resume,
	.message		= writecache_message,
	.map			= writecache_map,
	.end_io			= writecache_end_io,
	.iterate_devices	= writecache_iterate_devices,
	.io_hints		= writecache_io_hints,
};

static int __init dm_writecache_init(void)
{
	int r;

	r = dm_register_target(&writecache_target);
	if (r < 0) {
		DMERR("register failed %d", r);
		return r;
	}

	return 0;
}

static void __exit dm_writecache_exit(void)
{
	dm_unregister_target(&writecache_target);
}

module_init(dm_writecache_init);
module_exit(dm_writecache_exit);

MODULE_DESCRIPTION(DM_NAME " writecache target");
MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
MODULE_LICENSE("GPL");