Newer
Older
mg->requeue_holder = false;
spin_lock_irqsave(&cache->lock, flags);
list_add_tail(&mg->list, &cache->completed_migrations);
spin_unlock_irqrestore(&cache->lock, flags);
wake_worker(cache);
}
static void issue_overwrite(struct dm_cache_migration *mg, struct bio *bio)
{
size_t pb_data_size = get_per_bio_data_size(mg->cache);
struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
dm_hook_bio(&pb->hook_info, bio, overwrite_endio, mg);
remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
generic_make_request(bio);
}
static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
{
return (bio_data_dir(bio) == WRITE) &&
(bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
static void avoid_copy(struct dm_cache_migration *mg)
{
atomic_inc(&mg->cache->stats.copies_avoided);
migration_success_pre_commit(mg);
}
static void issue_copy(struct dm_cache_migration *mg)
{
bool avoid;
struct cache *cache = mg->cache;
if (mg->writeback || mg->demote)
avoid = !is_dirty(cache, mg->cblock) ||
is_discarded_oblock(cache, mg->old_oblock);
else {
struct bio *bio = mg->new_ocell->holder;
avoid = is_discarded_oblock(cache, mg->new_oblock);
if (!avoid && bio_writes_complete_block(cache, bio)) {
issue_overwrite(mg, bio);
return;
}
}
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
avoid ? avoid_copy(mg) : issue_copy_real(mg);
}
static void complete_migration(struct dm_cache_migration *mg)
{
if (mg->err)
migration_failure(mg);
else
migration_success_pre_commit(mg);
}
static void process_migrations(struct cache *cache, struct list_head *head,
void (*fn)(struct dm_cache_migration *))
{
unsigned long flags;
struct list_head list;
struct dm_cache_migration *mg, *tmp;
INIT_LIST_HEAD(&list);
spin_lock_irqsave(&cache->lock, flags);
list_splice_init(head, &list);
spin_unlock_irqrestore(&cache->lock, flags);
list_for_each_entry_safe(mg, tmp, &list, list)
fn(mg);
}
static void __queue_quiesced_migration(struct dm_cache_migration *mg)
{
list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
}
static void queue_quiesced_migration(struct dm_cache_migration *mg)
{
unsigned long flags;
struct cache *cache = mg->cache;
spin_lock_irqsave(&cache->lock, flags);
__queue_quiesced_migration(mg);
spin_unlock_irqrestore(&cache->lock, flags);
wake_worker(cache);
}
static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
{
unsigned long flags;
struct dm_cache_migration *mg, *tmp;
spin_lock_irqsave(&cache->lock, flags);
list_for_each_entry_safe(mg, tmp, work, list)
__queue_quiesced_migration(mg);
spin_unlock_irqrestore(&cache->lock, flags);
wake_worker(cache);
}
static void check_for_quiesced_migrations(struct cache *cache,
struct per_bio_data *pb)
{
struct list_head work;
if (!pb->all_io_entry)
return;
INIT_LIST_HEAD(&work);
if (pb->all_io_entry)
dm_deferred_entry_dec(pb->all_io_entry, &work);
if (!list_empty(&work))
queue_quiesced_migrations(cache, &work);
}
static void quiesce_migration(struct dm_cache_migration *mg)
{
if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
queue_quiesced_migration(mg);
}
static void promote(struct cache *cache, struct prealloc *structs,
dm_oblock_t oblock, dm_cblock_t cblock,
struct dm_bio_prison_cell *cell)
{
struct dm_cache_migration *mg = prealloc_get_migration(structs);
mg->err = false;
mg->writeback = false;
mg->demote = false;
mg->promote = true;
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
mg->cache = cache;
mg->new_oblock = oblock;
mg->cblock = cblock;
mg->old_ocell = NULL;
mg->new_ocell = cell;
mg->start_jiffies = jiffies;
inc_nr_migrations(cache);
quiesce_migration(mg);
}
static void writeback(struct cache *cache, struct prealloc *structs,
dm_oblock_t oblock, dm_cblock_t cblock,
struct dm_bio_prison_cell *cell)
{
struct dm_cache_migration *mg = prealloc_get_migration(structs);
mg->err = false;
mg->writeback = true;
mg->demote = false;
mg->promote = false;
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
mg->cache = cache;
mg->old_oblock = oblock;
mg->cblock = cblock;
mg->old_ocell = cell;
mg->new_ocell = NULL;
mg->start_jiffies = jiffies;
inc_nr_migrations(cache);
quiesce_migration(mg);
}
static void demote_then_promote(struct cache *cache, struct prealloc *structs,
dm_oblock_t old_oblock, dm_oblock_t new_oblock,
dm_cblock_t cblock,
struct dm_bio_prison_cell *old_ocell,
struct dm_bio_prison_cell *new_ocell)
{
struct dm_cache_migration *mg = prealloc_get_migration(structs);
mg->err = false;
mg->writeback = false;
mg->demote = true;
mg->promote = true;
mg->cache = cache;
mg->old_oblock = old_oblock;
mg->new_oblock = new_oblock;
mg->cblock = cblock;
mg->old_ocell = old_ocell;
mg->new_ocell = new_ocell;
mg->start_jiffies = jiffies;
inc_nr_migrations(cache);
quiesce_migration(mg);
}
/*
* Invalidate a cache entry. No writeback occurs; any changes in the cache
* block are thrown away.
*/
static void invalidate(struct cache *cache, struct prealloc *structs,
dm_oblock_t oblock, dm_cblock_t cblock,
struct dm_bio_prison_cell *cell)
{
struct dm_cache_migration *mg = prealloc_get_migration(structs);
mg->err = false;
mg->writeback = false;
mg->demote = true;
mg->promote = false;
mg->requeue_holder = true;
mg->cache = cache;
mg->old_oblock = oblock;
mg->cblock = cblock;
mg->old_ocell = cell;
mg->new_ocell = NULL;
mg->start_jiffies = jiffies;
inc_nr_migrations(cache);
quiesce_migration(mg);
}
/*----------------------------------------------------------------
* bio processing
*--------------------------------------------------------------*/
static void defer_bio(struct cache *cache, struct bio *bio)
{
unsigned long flags;
spin_lock_irqsave(&cache->lock, flags);
bio_list_add(&cache->deferred_bios, bio);
spin_unlock_irqrestore(&cache->lock, flags);
wake_worker(cache);
}
static void process_flush_bio(struct cache *cache, struct bio *bio)
{
size_t pb_data_size = get_per_bio_data_size(cache);
struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
if (!pb->req_nr)
remap_to_origin(cache, bio);
else
remap_to_cache(cache, bio, 0);
issue(cache, bio);
}
/*
* People generally discard large parts of a device, eg, the whole device
* when formatting. Splitting these large discards up into cache block
* sized ios and then quiescing (always neccessary for discard) takes too
* long.
*
* We keep it simple, and allow any size of discard to come in, and just
* mark off blocks on the discard bitset. No passdown occurs!
*
* To implement passdown we need to change the bio_prison such that a cell
* can have a key that spans many blocks.
*/
static void process_discard_bio(struct cache *cache, struct bio *bio)
{
dm_block_t start_block = dm_sector_div_up(bio->bi_iter.bi_sector,
cache->sectors_per_block);
dm_block_t end_block = bio_end_sector(bio);
end_block = block_div(end_block, cache->sectors_per_block);
set_discard(cache, to_oblock(b));
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
bio_endio(bio, 0);
}
static bool spare_migration_bandwidth(struct cache *cache)
{
sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) *
cache->sectors_per_block;
return current_volume < cache->migration_threshold;
}
static void inc_hit_counter(struct cache *cache, struct bio *bio)
{
atomic_inc(bio_data_dir(bio) == READ ?
&cache->stats.read_hit : &cache->stats.write_hit);
}
static void inc_miss_counter(struct cache *cache, struct bio *bio)
{
atomic_inc(bio_data_dir(bio) == READ ?
&cache->stats.read_miss : &cache->stats.write_miss);
}
static void issue_cache_bio(struct cache *cache, struct bio *bio,
struct per_bio_data *pb,
dm_oblock_t oblock, dm_cblock_t cblock)
{
pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
remap_to_cache_dirty(cache, bio, oblock, cblock);
issue(cache, bio);
}
static void process_bio(struct cache *cache, struct prealloc *structs,
struct bio *bio)
{
int r;
bool release_cell = true;
dm_oblock_t block = get_bio_block(cache, bio);
struct dm_bio_prison_cell *cell_prealloc, *old_ocell, *new_ocell;
struct policy_result lookup_result;
size_t pb_data_size = get_per_bio_data_size(cache);
struct per_bio_data *pb = get_per_bio_data(bio, pb_data_size);
bool discarded_block = is_discarded_oblock(cache, block);
bool passthrough = passthrough_mode(&cache->features);
bool can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache));
/*
* Check to see if that block is currently migrating.
*/
cell_prealloc = prealloc_get_cell(structs);
r = bio_detain(cache, block, bio, cell_prealloc,
(cell_free_fn) prealloc_put_cell,
structs, &new_ocell);
if (r > 0)
return;
r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
bio, &lookup_result);
if (r == -EWOULDBLOCK)
/* migration has been denied */
lookup_result.op = POLICY_MISS;
switch (lookup_result.op) {
case POLICY_HIT:
if (passthrough) {
inc_miss_counter(cache, bio);
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
/*
* Passthrough always maps to the origin,
* invalidating any cache blocks that are written
* to.
*/
if (bio_data_dir(bio) == WRITE) {
atomic_inc(&cache->stats.demotion);
invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
release_cell = false;
} else {
/* FIXME: factor out issue_origin() */
pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
remap_to_origin_clear_discard(cache, bio, block);
issue(cache, bio);
}
} else {
inc_hit_counter(cache, bio);
if (bio_data_dir(bio) == WRITE &&
writethrough_mode(&cache->features) &&
!is_dirty(cache, lookup_result.cblock)) {
pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
issue(cache, bio);
} else
issue_cache_bio(cache, bio, pb, block, lookup_result.cblock);
}
break;
case POLICY_MISS:
inc_miss_counter(cache, bio);
pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
remap_to_origin_clear_discard(cache, bio, block);
issue(cache, bio);
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
break;
case POLICY_NEW:
atomic_inc(&cache->stats.promotion);
promote(cache, structs, block, lookup_result.cblock, new_ocell);
release_cell = false;
break;
case POLICY_REPLACE:
cell_prealloc = prealloc_get_cell(structs);
r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
(cell_free_fn) prealloc_put_cell,
structs, &old_ocell);
if (r > 0) {
/*
* We have to be careful to avoid lock inversion of
* the cells. So we back off, and wait for the
* old_ocell to become free.
*/
policy_force_mapping(cache->policy, block,
lookup_result.old_oblock);
atomic_inc(&cache->stats.cache_cell_clash);
break;
}
atomic_inc(&cache->stats.demotion);
atomic_inc(&cache->stats.promotion);
demote_then_promote(cache, structs, lookup_result.old_oblock,
block, lookup_result.cblock,
old_ocell, new_ocell);
release_cell = false;
break;
default:
DMERR_LIMIT("%s: erroring bio, unknown policy op: %u", __func__,
(unsigned) lookup_result.op);
bio_io_error(bio);
}
if (release_cell)
cell_defer(cache, new_ocell, false);
}
static int need_commit_due_to_time(struct cache *cache)
{
return jiffies < cache->last_commit_jiffies ||
jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
}
static int commit_if_needed(struct cache *cache)
{
int r = 0;
if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
dm_cache_changed_this_transaction(cache->cmd)) {
atomic_inc(&cache->stats.commit_count);
cache->commit_requested = false;
r = dm_cache_commit(cache->cmd, false);
cache->last_commit_jiffies = jiffies;
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
}
static void process_deferred_bios(struct cache *cache)
{
unsigned long flags;
struct bio_list bios;
struct bio *bio;
struct prealloc structs;
memset(&structs, 0, sizeof(structs));
bio_list_init(&bios);
spin_lock_irqsave(&cache->lock, flags);
bio_list_merge(&bios, &cache->deferred_bios);
bio_list_init(&cache->deferred_bios);
spin_unlock_irqrestore(&cache->lock, flags);
while (!bio_list_empty(&bios)) {
/*
* If we've got no free migration structs, and processing
* this bio might require one, we pause until there are some
* prepared mappings to process.
*/
if (prealloc_data_structs(cache, &structs)) {
spin_lock_irqsave(&cache->lock, flags);
bio_list_merge(&cache->deferred_bios, &bios);
spin_unlock_irqrestore(&cache->lock, flags);
break;
}
bio = bio_list_pop(&bios);
if (bio->bi_rw & REQ_FLUSH)
process_flush_bio(cache, bio);
else if (bio->bi_rw & REQ_DISCARD)
process_discard_bio(cache, bio);
else
process_bio(cache, &structs, bio);
}
prealloc_free_structs(cache, &structs);
}
static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
{
unsigned long flags;
struct bio_list bios;
struct bio *bio;
bio_list_init(&bios);
spin_lock_irqsave(&cache->lock, flags);
bio_list_merge(&bios, &cache->deferred_flush_bios);
bio_list_init(&cache->deferred_flush_bios);
spin_unlock_irqrestore(&cache->lock, flags);
while ((bio = bio_list_pop(&bios)))
submit_bios ? generic_make_request(bio) : bio_io_error(bio);
}
static void process_deferred_writethrough_bios(struct cache *cache)
{
unsigned long flags;
struct bio_list bios;
struct bio *bio;
bio_list_init(&bios);
spin_lock_irqsave(&cache->lock, flags);
bio_list_merge(&bios, &cache->deferred_writethrough_bios);
bio_list_init(&cache->deferred_writethrough_bios);
spin_unlock_irqrestore(&cache->lock, flags);
while ((bio = bio_list_pop(&bios)))
generic_make_request(bio);
}
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
static void writeback_some_dirty_blocks(struct cache *cache)
{
int r = 0;
dm_oblock_t oblock;
dm_cblock_t cblock;
struct prealloc structs;
struct dm_bio_prison_cell *old_ocell;
memset(&structs, 0, sizeof(structs));
while (spare_migration_bandwidth(cache)) {
if (prealloc_data_structs(cache, &structs))
break;
r = policy_writeback_work(cache->policy, &oblock, &cblock);
if (r)
break;
r = get_cell(cache, oblock, &structs, &old_ocell);
if (r) {
policy_set_dirty(cache->policy, oblock);
break;
}
writeback(cache, &structs, oblock, cblock, old_ocell);
}
prealloc_free_structs(cache, &structs);
}
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
/*----------------------------------------------------------------
* Invalidations.
* Dropping something from the cache *without* writing back.
*--------------------------------------------------------------*/
static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
{
int r = 0;
uint64_t begin = from_cblock(req->cblocks->begin);
uint64_t end = from_cblock(req->cblocks->end);
while (begin != end) {
r = policy_remove_cblock(cache->policy, to_cblock(begin));
if (!r) {
r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
if (r)
break;
} else if (r == -ENODATA) {
/* harmless, already unmapped */
r = 0;
} else {
DMERR("policy_remove_cblock failed");
break;
}
begin++;
}
cache->commit_requested = true;
req->err = r;
atomic_set(&req->complete, 1);
wake_up(&req->result_wait);
}
static void process_invalidation_requests(struct cache *cache)
{
struct list_head list;
struct invalidation_request *req, *tmp;
INIT_LIST_HEAD(&list);
spin_lock(&cache->invalidation_lock);
list_splice_init(&cache->invalidation_requests, &list);
spin_unlock(&cache->invalidation_lock);
list_for_each_entry_safe (req, tmp, &list, list)
process_invalidation_request(cache, req);
}
/*----------------------------------------------------------------
* Main worker loop
*--------------------------------------------------------------*/

Joe Thornber
committed
static bool is_quiescing(struct cache *cache)
return atomic_read(&cache->quiescing);

Joe Thornber
committed
static void ack_quiescing(struct cache *cache)
{
if (is_quiescing(cache)) {
atomic_inc(&cache->quiescing_ack);
wake_up(&cache->quiescing_wait);
}
}
static void wait_for_quiescing_ack(struct cache *cache)
{
wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
}
static void start_quiescing(struct cache *cache)
atomic_inc(&cache->quiescing);

Joe Thornber
committed
wait_for_quiescing_ack(cache);

Joe Thornber
committed
static void stop_quiescing(struct cache *cache)
atomic_set(&cache->quiescing, 0);

Joe Thornber
committed
atomic_set(&cache->quiescing_ack, 0);
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
}
static void wait_for_migrations(struct cache *cache)
{
wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations));
}
static void stop_worker(struct cache *cache)
{
cancel_delayed_work(&cache->waker);
flush_workqueue(cache->wq);
}
static void requeue_deferred_io(struct cache *cache)
{
struct bio *bio;
struct bio_list bios;
bio_list_init(&bios);
bio_list_merge(&bios, &cache->deferred_bios);
bio_list_init(&cache->deferred_bios);
while ((bio = bio_list_pop(&bios)))
bio_endio(bio, DM_ENDIO_REQUEUE);
}
static int more_work(struct cache *cache)
{
if (is_quiescing(cache))
return !list_empty(&cache->quiesced_migrations) ||
!list_empty(&cache->completed_migrations) ||
!list_empty(&cache->need_commit_migrations);
else
return !bio_list_empty(&cache->deferred_bios) ||
!bio_list_empty(&cache->deferred_flush_bios) ||
!bio_list_empty(&cache->deferred_writethrough_bios) ||
!list_empty(&cache->quiesced_migrations) ||
!list_empty(&cache->completed_migrations) ||
!list_empty(&cache->need_commit_migrations) ||
cache->invalidate;
}
static void do_worker(struct work_struct *ws)
{
struct cache *cache = container_of(ws, struct cache, worker);
do {

Joe Thornber
committed
if (!is_quiescing(cache)) {
writeback_some_dirty_blocks(cache);
process_deferred_writethrough_bios(cache);
process_invalidation_requests(cache);

Joe Thornber
committed
}
process_migrations(cache, &cache->quiesced_migrations, issue_copy);
process_migrations(cache, &cache->completed_migrations, complete_migration);
if (commit_if_needed(cache)) {
process_deferred_flush_bios(cache, false);
/*
* FIXME: rollback metadata or just go into a
* failure mode and error everything
*/
} else {
process_deferred_flush_bios(cache, true);
process_migrations(cache, &cache->need_commit_migrations,
migration_success_post_commit);
}

Joe Thornber
committed
ack_quiescing(cache);
} while (more_work(cache));
}
/*
* We want to commit periodically so that not too much
* unwritten metadata builds up.
*/
static void do_waker(struct work_struct *ws)
{
struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
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
wake_worker(cache);
queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
}
/*----------------------------------------------------------------*/
static int is_congested(struct dm_dev *dev, int bdi_bits)
{
struct request_queue *q = bdev_get_queue(dev->bdev);
return bdi_congested(&q->backing_dev_info, bdi_bits);
}
static int cache_is_congested(struct dm_target_callbacks *cb, int bdi_bits)
{
struct cache *cache = container_of(cb, struct cache, callbacks);
return is_congested(cache->origin_dev, bdi_bits) ||
is_congested(cache->cache_dev, bdi_bits);
}
/*----------------------------------------------------------------
* Target methods
*--------------------------------------------------------------*/
/*
* This function gets called on the error paths of the constructor, so we
* have to cope with a partially initialised struct.
*/
static void destroy(struct cache *cache)
{
unsigned i;
if (cache->next_migration)
mempool_free(cache->next_migration, cache->migration_pool);
if (cache->migration_pool)
mempool_destroy(cache->migration_pool);
if (cache->all_io_ds)
dm_deferred_set_destroy(cache->all_io_ds);
if (cache->prison)
dm_bio_prison_destroy(cache->prison);
if (cache->wq)
destroy_workqueue(cache->wq);
if (cache->dirty_bitset)
free_bitset(cache->dirty_bitset);
if (cache->discard_bitset)
free_bitset(cache->discard_bitset);
if (cache->copier)
dm_kcopyd_client_destroy(cache->copier);
if (cache->cmd)
dm_cache_metadata_close(cache->cmd);
if (cache->metadata_dev)
dm_put_device(cache->ti, cache->metadata_dev);
if (cache->origin_dev)
dm_put_device(cache->ti, cache->origin_dev);
if (cache->cache_dev)
dm_put_device(cache->ti, cache->cache_dev);
if (cache->policy)
dm_cache_policy_destroy(cache->policy);
for (i = 0; i < cache->nr_ctr_args ; i++)
kfree(cache->ctr_args[i]);
kfree(cache->ctr_args);
kfree(cache);
}
static void cache_dtr(struct dm_target *ti)
{
struct cache *cache = ti->private;
destroy(cache);
}
static sector_t get_dev_size(struct dm_dev *dev)
{
return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
}
/*----------------------------------------------------------------*/
/*
* Construct a cache device mapping.
*
* cache <metadata dev> <cache dev> <origin dev> <block size>
* <#feature args> [<feature arg>]*
* <policy> <#policy args> [<policy arg>]*
*
* metadata dev : fast device holding the persistent metadata
* cache dev : fast device holding cached data blocks
* origin dev : slow device holding original data blocks
* block size : cache unit size in sectors
*
* #feature args : number of feature arguments passed
* feature args : writethrough. (The default is writeback.)
*
* policy : the replacement policy to use
* #policy args : an even number of policy arguments corresponding
* to key/value pairs passed to the policy
* policy args : key/value pairs passed to the policy
* E.g. 'sequential_threshold 1024'
* See cache-policies.txt for details.
*
* Optional feature arguments are:
* writethrough : write through caching that prohibits cache block
* content from being different from origin block content.
* Without this argument, the default behaviour is to write
* back cache block contents later for performance reasons,
* so they may differ from the corresponding origin blocks.
*/
struct cache_args {
struct dm_target *ti;
struct dm_dev *metadata_dev;
struct dm_dev *cache_dev;
sector_t cache_sectors;
struct dm_dev *origin_dev;
sector_t origin_sectors;
uint32_t block_size;
const char *policy_name;
int policy_argc;
const char **policy_argv;
struct cache_features features;
};
static void destroy_cache_args(struct cache_args *ca)
{
if (ca->metadata_dev)
dm_put_device(ca->ti, ca->metadata_dev);
if (ca->cache_dev)
dm_put_device(ca->ti, ca->cache_dev);
if (ca->origin_dev)
dm_put_device(ca->ti, ca->origin_dev);
kfree(ca);
}
static bool at_least_one_arg(struct dm_arg_set *as, char **error)
{
if (!as->argc) {
*error = "Insufficient args";
return false;
}
return true;
}
static int parse_metadata_dev(struct cache_args *ca, struct dm_arg_set *as,
char **error)
{
int r;
sector_t metadata_dev_size;
char b[BDEVNAME_SIZE];
if (!at_least_one_arg(as, error))
return -EINVAL;
r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
&ca->metadata_dev);
if (r) {
*error = "Error opening metadata device";
return r;
}
metadata_dev_size = get_dev_size(ca->metadata_dev);
if (metadata_dev_size > DM_CACHE_METADATA_MAX_SECTORS_WARNING)
DMWARN("Metadata device %s is larger than %u sectors: excess space will not be used.",
bdevname(ca->metadata_dev->bdev, b), THIN_METADATA_MAX_SECTORS);
return 0;
}
static int parse_cache_dev(struct cache_args *ca, struct dm_arg_set *as,
char **error)
{
int r;
if (!at_least_one_arg(as, error))
return -EINVAL;
r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
&ca->cache_dev);
if (r) {
*error = "Error opening cache device";
return r;
}
ca->cache_sectors = get_dev_size(ca->cache_dev);
return 0;
}
static int parse_origin_dev(struct cache_args *ca, struct dm_arg_set *as,
char **error)
{
int r;
if (!at_least_one_arg(as, error))
return -EINVAL;
r = dm_get_device(ca->ti, dm_shift_arg(as), FMODE_READ | FMODE_WRITE,
&ca->origin_dev);
if (r) {
*error = "Error opening origin device";
return r;
}
ca->origin_sectors = get_dev_size(ca->origin_dev);
if (ca->ti->len > ca->origin_sectors) {
*error = "Device size larger than cached device";
return -EINVAL;
}
return 0;
}
static int parse_block_size(struct cache_args *ca, struct dm_arg_set *as,
char **error)
{
unsigned long block_size;
if (!at_least_one_arg(as, error))
return -EINVAL;
if (kstrtoul(dm_shift_arg(as), 10, &block_size) || !block_size ||
block_size < DATA_DEV_BLOCK_SIZE_MIN_SECTORS ||
block_size > DATA_DEV_BLOCK_SIZE_MAX_SECTORS ||
block_size & (DATA_DEV_BLOCK_SIZE_MIN_SECTORS - 1)) {
*error = "Invalid data block size";
return -EINVAL;
}
if (block_size > ca->cache_sectors) {
*error = "Data block size is larger than the cache device";
return -EINVAL;
}
ca->block_size = block_size;
return 0;
}
static void init_features(struct cache_features *cf)
{
cf->mode = CM_WRITE;
}
static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
char **error)
{
static struct dm_arg _args[] = {
{0, 1, "Invalid number of cache feature arguments"},
};
int r;
unsigned argc;
const char *arg;
struct cache_features *cf = &ca->features;
init_features(cf);