Lines Matching refs:cache

10 #include "dm-cache-metadata.h"
20 #define DM_MSG_PREFIX "cache"
23 "A percentage of time allocated for copying to and/or from cache");
31 * cblock: index of a cache block
32 * promotion: movement of a block from origin to cache
33 * demotion: movement of a block from cache to origin
34 * migration: movement of a block between the origin and cache device,
104 * The block size of the device holding cache data must be
111 * FIXME: the cache is read/write for the time being.
121 * dirty. If you lose the cache device you will lose data.
127 * Data is written to both cache and origin. Blocks are never
133 * A degraded mode useful for various cache coherency situations
135 * origin. If a write goes to a cached oblock, then the cache
178 struct cache {
206 * Size of the cache device in blocks.
295 * structure and the 'cache' member must be the first as it
298 struct cache *cache;
305 struct cache *cache;
334 static void wake_worker(struct cache *cache)
336 queue_work(cache->wq, &cache->worker);
341 static struct dm_bio_prison_cell *alloc_prison_cell(struct cache *cache)
344 return dm_bio_prison_alloc_cell(cache->prison, GFP_NOWAIT);
347 static void free_prison_cell(struct cache *cache, struct dm_bio_prison_cell *cell)
349 dm_bio_prison_free_cell(cache->prison, cell);
352 static int prealloc_data_structs(struct cache *cache, struct prealloc *p)
355 p->mg = mempool_alloc(cache->migration_pool, GFP_NOWAIT);
361 p->cell1 = alloc_prison_cell(cache);
367 p->cell2 = alloc_prison_cell(cache);
375 static void prealloc_free_structs(struct cache *cache, struct prealloc *p)
378 free_prison_cell(cache, p->cell2);
381 free_prison_cell(cache, p->cell1);
384 mempool_free(p->mg, cache->migration_pool);
450 static int bio_detain(struct cache *cache, dm_oblock_t oblock,
459 r = dm_bio_detain(cache->prison, &key, bio, cell_prealloc, cell_result);
466 static int get_cell(struct cache *cache,
478 r = dm_get_cell(cache->prison, &key, cell_prealloc, cell_result);
487 static bool is_dirty(struct cache *cache, dm_cblock_t b)
489 return test_bit(from_cblock(b), cache->dirty_bitset);
492 static void set_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
494 if (!test_and_set_bit(from_cblock(cblock), cache->dirty_bitset)) {
495 atomic_inc(&cache->nr_dirty);
496 policy_set_dirty(cache->policy, oblock);
500 static void clear_dirty(struct cache *cache, dm_oblock_t oblock, dm_cblock_t cblock)
502 if (test_and_clear_bit(from_cblock(cblock), cache->dirty_bitset)) {
503 policy_clear_dirty(cache->policy, oblock);
504 if (atomic_dec_return(&cache->nr_dirty) == 0)
505 dm_table_event(cache->ti->table);
511 static bool block_size_is_power_of_two(struct cache *cache)
513 return cache->sectors_per_block_shift >= 0;
527 static void set_discard(struct cache *cache, dm_oblock_t b)
531 atomic_inc(&cache->stats.discard_count);
533 spin_lock_irqsave(&cache->lock, flags);
534 set_bit(from_oblock(b), cache->discard_bitset);
535 spin_unlock_irqrestore(&cache->lock, flags);
538 static void clear_discard(struct cache *cache, dm_oblock_t b)
542 spin_lock_irqsave(&cache->lock, flags);
543 clear_bit(from_oblock(b), cache->discard_bitset);
544 spin_unlock_irqrestore(&cache->lock, flags);
547 static bool is_discarded(struct cache *cache, dm_oblock_t b)
552 spin_lock_irqsave(&cache->lock, flags);
553 r = test_bit(from_oblock(b), cache->discard_bitset);
554 spin_unlock_irqrestore(&cache->lock, flags);
559 static bool is_discarded_oblock(struct cache *cache, dm_oblock_t b)
564 spin_lock_irqsave(&cache->lock, flags);
565 r = test_bit(from_oblock(b), cache->discard_bitset);
566 spin_unlock_irqrestore(&cache->lock, flags);
573 static void load_stats(struct cache *cache)
577 dm_cache_metadata_get_stats(cache->cmd, &stats);
578 atomic_set(&cache->stats.read_hit, stats.read_hits);
579 atomic_set(&cache->stats.read_miss, stats.read_misses);
580 atomic_set(&cache->stats.write_hit, stats.write_hits);
581 atomic_set(&cache->stats.write_miss, stats.write_misses);
584 static void save_stats(struct cache *cache)
588 stats.read_hits = atomic_read(&cache->stats.read_hit);
589 stats.read_misses = atomic_read(&cache->stats.read_miss);
590 stats.write_hits = atomic_read(&cache->stats.write_hit);
591 stats.write_misses = atomic_read(&cache->stats.write_miss);
593 dm_cache_metadata_set_stats(cache->cmd, &stats);
603 #define PB_DATA_SIZE_WB (offsetof(struct per_bio_data, cache))
621 static size_t get_per_bio_data_size(struct cache *cache)
623 return writethrough_mode(&cache->features) ? PB_DATA_SIZE_WT : PB_DATA_SIZE_WB;
647 static void remap_to_origin(struct cache *cache, struct bio *bio)
649 bio->bi_bdev = cache->origin_dev->bdev;
652 static void remap_to_cache(struct cache *cache, struct bio *bio,
658 bio->bi_bdev = cache->cache_dev->bdev;
659 if (!block_size_is_power_of_two(cache))
661 (block * cache->sectors_per_block) +
662 sector_div(bi_sector, cache->sectors_per_block);
665 (block << cache->sectors_per_block_shift) |
666 (bi_sector & (cache->sectors_per_block - 1));
669 static void check_if_tick_bio_needed(struct cache *cache, struct bio *bio)
672 size_t pb_data_size = get_per_bio_data_size(cache);
675 spin_lock_irqsave(&cache->lock, flags);
676 if (cache->need_tick_bio &&
679 cache->need_tick_bio = false;
681 spin_unlock_irqrestore(&cache->lock, flags);
684 static void remap_to_origin_clear_discard(struct cache *cache, struct bio *bio,
687 check_if_tick_bio_needed(cache, bio);
688 remap_to_origin(cache, bio);
690 clear_discard(cache, oblock);
693 static void remap_to_cache_dirty(struct cache *cache, struct bio *bio,
696 check_if_tick_bio_needed(cache, bio);
697 remap_to_cache(cache, bio, cblock);
699 set_dirty(cache, oblock, cblock);
700 clear_discard(cache, oblock);
704 static dm_oblock_t get_bio_block(struct cache *cache, struct bio *bio)
708 if (!block_size_is_power_of_two(cache))
709 (void) sector_div(block_nr, cache->sectors_per_block);
711 block_nr >>= cache->sectors_per_block_shift;
716 static int bio_triggers_commit(struct cache *cache, struct bio *bio)
725 static void inc_ds(struct cache *cache, struct bio *bio,
728 size_t pb_data_size = get_per_bio_data_size(cache);
734 pb->all_io_entry = dm_deferred_entry_inc(cache->all_io_ds);
737 static void issue(struct cache *cache, struct bio *bio)
741 if (!bio_triggers_commit(cache, bio)) {
750 spin_lock_irqsave(&cache->lock, flags);
751 cache->commit_requested = true;
752 bio_list_add(&cache->deferred_flush_bios, bio);
753 spin_unlock_irqrestore(&cache->lock, flags);
756 static void inc_and_issue(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell *cell)
758 inc_ds(cache, bio, cell);
759 issue(cache, bio);
762 static void defer_writethrough_bio(struct cache *cache, struct bio *bio)
766 spin_lock_irqsave(&cache->lock, flags);
767 bio_list_add(&cache->deferred_writethrough_bios, bio);
768 spin_unlock_irqrestore(&cache->lock, flags);
770 wake_worker(cache);
785 remap_to_cache(pb->cache, bio, pb->cblock);
792 defer_writethrough_bio(pb->cache, bio);
797 * to both the cache and origin devices. In future we'd like to clone the
801 static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
806 pb->cache = cache;
811 remap_to_origin_clear_discard(pb->cache, bio, oblock);
817 * Migration covers moving data from the origin device to the cache, or
822 mempool_free(mg, mg->cache->migration_pool);
825 static void inc_nr_migrations(struct cache *cache)
827 atomic_inc(&cache->nr_migrations);
830 static void dec_nr_migrations(struct cache *cache)
832 atomic_dec(&cache->nr_migrations);
837 wake_up(&cache->migration_wait);
840 static void __cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
844 (cache->prison, cell, &cache->deferred_bios);
845 free_prison_cell(cache, cell);
848 static void cell_defer(struct cache *cache, struct dm_bio_prison_cell *cell,
853 spin_lock_irqsave(&cache->lock, flags);
854 __cell_defer(cache, cell, holder);
855 spin_unlock_irqrestore(&cache->lock, flags);
857 wake_worker(cache);
862 struct cache *cache = mg->cache;
864 dec_nr_migrations(cache);
869 struct cache *cache = mg->cache;
873 set_dirty(cache, mg->old_oblock, mg->cblock);
874 cell_defer(cache, mg->old_ocell, false);
878 policy_force_mapping(cache->policy, mg->new_oblock, mg->old_oblock);
880 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
882 cell_defer(cache, mg->new_ocell, true);
885 policy_remove_mapping(cache->policy, mg->new_oblock);
886 cell_defer(cache, mg->new_ocell, true);
895 struct cache *cache = mg->cache;
898 clear_dirty(cache, mg->old_oblock, mg->cblock);
899 cell_defer(cache, mg->old_ocell, false);
904 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) {
906 policy_force_mapping(cache->policy, mg->new_oblock,
909 cell_defer(cache, mg->new_ocell, true);
914 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) {
916 policy_remove_mapping(cache->policy, mg->new_oblock);
922 spin_lock_irqsave(&cache->lock, flags);
923 list_add_tail(&mg->list, &cache->need_commit_migrations);
924 cache->commit_requested = true;
925 spin_unlock_irqrestore(&cache->lock, flags);
931 struct cache *cache = mg->cache;
938 cell_defer(cache, mg->old_ocell, mg->promote ? false : true);
943 spin_lock_irqsave(&cache->lock, flags);
944 list_add_tail(&mg->list, &cache->quiesced_migrations);
945 spin_unlock_irqrestore(&cache->lock, flags);
949 policy_remove_mapping(cache->policy, mg->old_oblock);
954 clear_dirty(cache, mg->new_oblock, mg->cblock);
956 cell_defer(cache, mg->new_ocell, true);
959 cell_defer(cache, mg->new_ocell, false);
969 struct cache *cache = mg->cache;
974 spin_lock_irqsave(&cache->lock, flags);
975 list_add_tail(&mg->list, &cache->completed_migrations);
976 spin_unlock_irqrestore(&cache->lock, flags);
978 wake_worker(cache);
985 struct cache *cache = mg->cache;
988 o_region.bdev = cache->origin_dev->bdev;
989 o_region.count = cache->sectors_per_block;
991 c_region.bdev = cache->cache_dev->bdev;
992 c_region.sector = cblock * cache->sectors_per_block;
993 c_region.count = cache->sectors_per_block;
997 o_region.sector = from_oblock(mg->old_oblock) * cache->sectors_per_block;
998 r = dm_kcopyd_copy(cache->copier, &c_region, 1, &o_region, 0, copy_complete, mg);
1001 o_region.sector = from_oblock(mg->new_oblock) * cache->sectors_per_block;
1002 r = dm_kcopyd_copy(cache->copier, &o_region, 1, &c_region, 0, copy_complete, mg);
1014 struct cache *cache = mg->cache;
1015 size_t pb_data_size = get_per_bio_data_size(cache);
1026 spin_lock_irqsave(&cache->lock, flags);
1027 list_add_tail(&mg->list, &cache->completed_migrations);
1028 spin_unlock_irqrestore(&cache->lock, flags);
1030 wake_worker(cache);
1035 size_t pb_data_size = get_per_bio_data_size(mg->cache);
1039 remap_to_cache_dirty(mg->cache, bio, mg->new_oblock, mg->cblock);
1048 static bool bio_writes_complete_block(struct cache *cache, struct bio *bio)
1051 (bio->bi_iter.bi_size == (cache->sectors_per_block << SECTOR_SHIFT));
1056 atomic_inc(&mg->cache->stats.copies_avoided);
1063 struct cache *cache = mg->cache;
1066 avoid = !is_dirty(cache, mg->cblock) ||
1067 is_discarded_oblock(cache, mg->old_oblock);
1071 avoid = is_discarded_oblock(cache, mg->new_oblock);
1073 if (!avoid && bio_writes_complete_block(cache, bio)) {
1090 static void process_migrations(struct cache *cache, struct list_head *head,
1098 spin_lock_irqsave(&cache->lock, flags);
1100 spin_unlock_irqrestore(&cache->lock, flags);
1108 list_add_tail(&mg->list, &mg->cache->quiesced_migrations);
1114 struct cache *cache = mg->cache;
1116 spin_lock_irqsave(&cache->lock, flags);
1118 spin_unlock_irqrestore(&cache->lock, flags);
1120 wake_worker(cache);
1123 static void queue_quiesced_migrations(struct cache *cache, struct list_head *work)
1128 spin_lock_irqsave(&cache->lock, flags);
1131 spin_unlock_irqrestore(&cache->lock, flags);
1133 wake_worker(cache);
1136 static void check_for_quiesced_migrations(struct cache *cache,
1148 queue_quiesced_migrations(cache, &work);
1153 if (!dm_deferred_set_add_work(mg->cache->all_io_ds, &mg->list))
1157 static void promote(struct cache *cache, struct prealloc *structs,
1169 mg->cache = cache;
1176 inc_nr_migrations(cache);
1180 static void writeback(struct cache *cache, struct prealloc *structs,
1192 mg->cache = cache;
1199 inc_nr_migrations(cache);
1203 static void demote_then_promote(struct cache *cache, struct prealloc *structs,
1217 mg->cache = cache;
1225 inc_nr_migrations(cache);
1230 * Invalidate a cache entry. No writeback occurs; any changes in the cache
1233 static void invalidate(struct cache *cache, struct prealloc *structs,
1245 mg->cache = cache;
1252 inc_nr_migrations(cache);
1259 static void defer_bio(struct cache *cache, struct bio *bio)
1263 spin_lock_irqsave(&cache->lock, flags);
1264 bio_list_add(&cache->deferred_bios, bio);
1265 spin_unlock_irqrestore(&cache->lock, flags);
1267 wake_worker(cache);
1270 static void process_flush_bio(struct cache *cache, struct bio *bio)
1272 size_t pb_data_size = get_per_bio_data_size(cache);
1277 remap_to_origin(cache, bio);
1279 remap_to_cache(cache, bio, 0);
1286 issue(cache, bio);
1291 * when formatting. Splitting these large discards up into cache block
1301 static void process_discard_bio(struct cache *cache, struct bio *bio)
1304 cache->sectors_per_block);
1308 end_block = block_div(end_block, cache->sectors_per_block);
1311 set_discard(cache, to_oblock(b));
1316 static bool spare_migration_bandwidth(struct cache *cache)
1318 sector_t current_volume = (atomic_read(&cache->nr_migrations) + 1) *
1319 cache->sectors_per_block;
1320 return current_volume < cache->migration_threshold;
1323 static void inc_hit_counter(struct cache *cache, struct bio *bio)
1326 &cache->stats.read_hit : &cache->stats.write_hit);
1329 static void inc_miss_counter(struct cache *cache, struct bio *bio)
1332 &cache->stats.read_miss : &cache->stats.write_miss);
1335 static void process_bio(struct cache *cache, struct prealloc *structs,
1340 dm_oblock_t block = get_bio_block(cache, bio);
1343 bool discarded_block = is_discarded_oblock(cache, block);
1344 bool passthrough = passthrough_mode(&cache->features);
1345 bool can_migrate = !passthrough && (discarded_block || spare_migration_bandwidth(cache));
1351 r = bio_detain(cache, block, bio, cell_prealloc,
1357 r = policy_map(cache->policy, block, true, can_migrate, discarded_block,
1367 inc_miss_counter(cache, bio);
1371 * invalidating any cache blocks that are written
1376 atomic_inc(&cache->stats.demotion);
1377 invalidate(cache, structs, block, lookup_result.cblock, new_ocell);
1382 remap_to_origin_clear_discard(cache, bio, block);
1383 inc_and_issue(cache, bio, new_ocell);
1386 inc_hit_counter(cache, bio);
1389 writethrough_mode(&cache->features) &&
1390 !is_dirty(cache, lookup_result.cblock)) {
1391 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
1392 inc_and_issue(cache, bio, new_ocell);
1395 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
1396 inc_and_issue(cache, bio, new_ocell);
1403 inc_miss_counter(cache, bio);
1404 remap_to_origin_clear_discard(cache, bio, block);
1405 inc_and_issue(cache, bio, new_ocell);
1409 atomic_inc(&cache->stats.promotion);
1410 promote(cache, structs, block, lookup_result.cblock, new_ocell);
1416 r = bio_detain(cache, lookup_result.old_oblock, bio, cell_prealloc,
1425 policy_force_mapping(cache->policy, block,
1427 atomic_inc(&cache->stats.cache_cell_clash);
1430 atomic_inc(&cache->stats.demotion);
1431 atomic_inc(&cache->stats.promotion);
1433 demote_then_promote(cache, structs, lookup_result.old_oblock,
1446 cell_defer(cache, new_ocell, false);
1449 static int need_commit_due_to_time(struct cache *cache)
1451 return jiffies < cache->last_commit_jiffies ||
1452 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
1455 static int commit_if_needed(struct cache *cache)
1459 if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
1460 dm_cache_changed_this_transaction(cache->cmd)) {
1461 atomic_inc(&cache->stats.commit_count);
1462 cache->commit_requested = false;
1463 r = dm_cache_commit(cache->cmd, false);
1464 cache->last_commit_jiffies = jiffies;
1470 static void process_deferred_bios(struct cache *cache)
1480 spin_lock_irqsave(&cache->lock, flags);
1481 bio_list_merge(&bios, &cache->deferred_bios);
1482 bio_list_init(&cache->deferred_bios);
1483 spin_unlock_irqrestore(&cache->lock, flags);
1491 if (prealloc_data_structs(cache, &structs)) {
1492 spin_lock_irqsave(&cache->lock, flags);
1493 bio_list_merge(&cache->deferred_bios, &bios);
1494 spin_unlock_irqrestore(&cache->lock, flags);
1501 process_flush_bio(cache, bio);
1503 process_discard_bio(cache, bio);
1505 process_bio(cache, &structs, bio);
1508 prealloc_free_structs(cache, &structs);
1511 static void process_deferred_flush_bios(struct cache *cache, bool submit_bios)
1519 spin_lock_irqsave(&cache->lock, flags);
1520 bio_list_merge(&bios, &cache->deferred_flush_bios);
1521 bio_list_init(&cache->deferred_flush_bios);
1522 spin_unlock_irqrestore(&cache->lock, flags);
1531 static void process_deferred_writethrough_bios(struct cache *cache)
1539 spin_lock_irqsave(&cache->lock, flags);
1540 bio_list_merge(&bios, &cache->deferred_writethrough_bios);
1541 bio_list_init(&cache->deferred_writethrough_bios);
1542 spin_unlock_irqrestore(&cache->lock, flags);
1551 static void writeback_some_dirty_blocks(struct cache *cache)
1561 while (spare_migration_bandwidth(cache)) {
1562 if (prealloc_data_structs(cache, &structs))
1565 r = policy_writeback_work(cache->policy, &oblock, &cblock);
1569 r = get_cell(cache, oblock, &structs, &old_ocell);
1571 policy_set_dirty(cache->policy, oblock);
1575 writeback(cache, &structs, oblock, cblock, old_ocell);
1578 prealloc_free_structs(cache, &structs);
1583 * Dropping something from the cache *without* writing back.
1586 static void process_invalidation_request(struct cache *cache, struct invalidation_request *req)
1593 r = policy_remove_cblock(cache->policy, to_cblock(begin));
1595 r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
1611 cache->commit_requested = true;
1619 static void process_invalidation_requests(struct cache *cache)
1625 spin_lock(&cache->invalidation_lock);
1626 list_splice_init(&cache->invalidation_requests, &list);
1627 spin_unlock(&cache->invalidation_lock);
1630 process_invalidation_request(cache, req);
1636 static bool is_quiescing(struct cache *cache)
1638 return atomic_read(&cache->quiescing);
1641 static void ack_quiescing(struct cache *cache)
1643 if (is_quiescing(cache)) {
1644 atomic_inc(&cache->quiescing_ack);
1645 wake_up(&cache->quiescing_wait);
1649 static void wait_for_quiescing_ack(struct cache *cache)
1651 wait_event(cache->quiescing_wait, atomic_read(&cache->quiescing_ack));
1654 static void start_quiescing(struct cache *cache)
1656 atomic_inc(&cache->quiescing);
1657 wait_for_quiescing_ack(cache);
1660 static void stop_quiescing(struct cache *cache)
1662 atomic_set(&cache->quiescing, 0);
1663 atomic_set(&cache->quiescing_ack, 0);
1666 static void wait_for_migrations(struct cache *cache)
1668 wait_event(cache->migration_wait, !atomic_read(&cache->nr_migrations));
1671 static void stop_worker(struct cache *cache)
1673 cancel_delayed_work(&cache->waker);
1674 flush_workqueue(cache->wq);
1677 static void requeue_deferred_io(struct cache *cache)
1683 bio_list_merge(&bios, &cache->deferred_bios);
1684 bio_list_init(&cache->deferred_bios);
1690 static int more_work(struct cache *cache)
1692 if (is_quiescing(cache))
1693 return !list_empty(&cache->quiesced_migrations) ||
1694 !list_empty(&cache->completed_migrations) ||
1695 !list_empty(&cache->need_commit_migrations);
1697 return !bio_list_empty(&cache->deferred_bios) ||
1698 !bio_list_empty(&cache->deferred_flush_bios) ||
1699 !bio_list_empty(&cache->deferred_writethrough_bios) ||
1700 !list_empty(&cache->quiesced_migrations) ||
1701 !list_empty(&cache->completed_migrations) ||
1702 !list_empty(&cache->need_commit_migrations) ||
1703 cache->invalidate;
1708 struct cache *cache = container_of(ws, struct cache, worker);
1711 if (!is_quiescing(cache)) {
1712 writeback_some_dirty_blocks(cache);
1713 process_deferred_writethrough_bios(cache);
1714 process_deferred_bios(cache);
1715 process_invalidation_requests(cache);
1718 process_migrations(cache, &cache->quiesced_migrations, issue_copy);
1719 process_migrations(cache, &cache->completed_migrations, complete_migration);
1721 if (commit_if_needed(cache)) {
1722 process_deferred_flush_bios(cache, false);
1723 process_migrations(cache, &cache->need_commit_migrations, migration_failure);
1730 process_deferred_flush_bios(cache, true);
1731 process_migrations(cache, &cache->need_commit_migrations,
1735 ack_quiescing(cache);
1737 } while (more_work(cache));
1746 struct cache *cache = container_of(to_delayed_work(ws), struct cache, waker);
1747 policy_tick(cache->policy);
1748 wake_worker(cache);
1749 queue_delayed_work(cache->wq, &cache->waker, COMMIT_PERIOD);
1762 struct cache *cache = container_of(cb, struct cache, callbacks);
1764 return is_congested(cache->origin_dev, bdi_bits) ||
1765 is_congested(cache->cache_dev, bdi_bits);
1776 static void destroy(struct cache *cache)
1780 if (cache->next_migration)
1781 mempool_free(cache->next_migration, cache->migration_pool);
1783 if (cache->migration_pool)
1784 mempool_destroy(cache->migration_pool);
1786 if (cache->all_io_ds)
1787 dm_deferred_set_destroy(cache->all_io_ds);
1789 if (cache->prison)
1790 dm_bio_prison_destroy(cache->prison);
1792 if (cache->wq)
1793 destroy_workqueue(cache->wq);
1795 if (cache->dirty_bitset)
1796 free_bitset(cache->dirty_bitset);
1798 if (cache->discard_bitset)
1799 free_bitset(cache->discard_bitset);
1801 if (cache->copier)
1802 dm_kcopyd_client_destroy(cache->copier);
1804 if (cache->cmd)
1805 dm_cache_metadata_close(cache->cmd);
1807 if (cache->metadata_dev)
1808 dm_put_device(cache->ti, cache->metadata_dev);
1810 if (cache->origin_dev)
1811 dm_put_device(cache->ti, cache->origin_dev);
1813 if (cache->cache_dev)
1814 dm_put_device(cache->ti, cache->cache_dev);
1816 if (cache->policy)
1817 dm_cache_policy_destroy(cache->policy);
1819 for (i = 0; i < cache->nr_ctr_args ; i++)
1820 kfree(cache->ctr_args[i]);
1821 kfree(cache->ctr_args);
1823 kfree(cache);
1828 struct cache *cache = ti->private;
1830 destroy(cache);
1841 * Construct a cache device mapping.
1843 * cache <metadata dev> <cache dev> <origin dev> <block size>
1848 * cache dev : fast device holding cached data blocks
1850 * block size : cache unit size in sectors
1860 * See cache-policies.txt for details.
1863 * writethrough : write through caching that prohibits cache block
1866 * back cache block contents later for performance reasons,
1949 *error = "Error opening cache device";
1998 *error = "Data block size is larger than the cache device";
2017 {0, 1, "Invalid number of cache feature arguments"},
2044 *error = "Unrecognised cache feature requested";
2118 static int process_config_option(struct cache *cache, const char *key, const char *value)
2126 cache->migration_threshold = tmp;
2133 static int set_config_value(struct cache *cache, const char *key, const char *value)
2135 int r = process_config_option(cache, key, value);
2138 r = policy_set_config_value(cache->policy, key, value);
2146 static int set_config_values(struct cache *cache, int argc, const char **argv)
2156 r = set_config_value(cache, argv[0], argv[1]);
2167 static int create_cache_policy(struct cache *cache, struct cache_args *ca,
2171 cache->cache_size,
2172 cache->origin_sectors,
2173 cache->sectors_per_block);
2175 *error = "Error creating cache's policy";
2178 cache->policy = p;
2185 static int cache_create(struct cache_args *ca, struct cache **result)
2189 struct cache *cache;
2195 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
2196 if (!cache)
2199 cache->ti = ca->ti;
2200 ti->private = cache;
2210 cache->features = ca->features;
2211 ti->per_bio_data_size = get_per_bio_data_size(cache);
2213 cache->callbacks.congested_fn = cache_is_congested;
2214 dm_table_add_target_callbacks(ti->table, &cache->callbacks);
2216 cache->metadata_dev = ca->metadata_dev;
2217 cache->origin_dev = ca->origin_dev;
2218 cache->cache_dev = ca->cache_dev;
2223 origin_blocks = cache->origin_sectors = ca->origin_sectors;
2225 cache->origin_blocks = to_oblock(origin_blocks);
2227 cache->sectors_per_block = ca->block_size;
2228 if (dm_set_target_max_io_len(ti, cache->sectors_per_block)) {
2236 cache->sectors_per_block_shift = -1;
2238 cache->cache_size = to_cblock(cache_size);
2240 cache->sectors_per_block_shift = __ffs(ca->block_size);
2241 cache->cache_size = to_cblock(ca->cache_sectors >> cache->sectors_per_block_shift);
2244 r = create_cache_policy(cache, ca, error);
2248 cache->policy_nr_args = ca->policy_argc;
2249 cache->migration_threshold = DEFAULT_MIGRATION_THRESHOLD;
2251 r = set_config_values(cache, ca->policy_argc, ca->policy_argv);
2253 *error = "Error setting cache policy's config values";
2257 cmd = dm_cache_metadata_open(cache->metadata_dev->bdev,
2259 dm_cache_policy_get_hint_size(cache->policy));
2265 cache->cmd = cmd;
2267 if (passthrough_mode(&cache->features)) {
2270 r = dm_cache_metadata_all_clean(cache->cmd, &all_clean);
2283 spin_lock_init(&cache->lock);
2284 bio_list_init(&cache->deferred_bios);
2285 bio_list_init(&cache->deferred_flush_bios);
2286 bio_list_init(&cache->deferred_writethrough_bios);
2287 INIT_LIST_HEAD(&cache->quiesced_migrations);
2288 INIT_LIST_HEAD(&cache->completed_migrations);
2289 INIT_LIST_HEAD(&cache->need_commit_migrations);
2290 atomic_set(&cache->nr_migrations, 0);
2291 init_waitqueue_head(&cache->migration_wait);
2293 init_waitqueue_head(&cache->quiescing_wait);
2294 atomic_set(&cache->quiescing, 0);
2295 atomic_set(&cache->quiescing_ack, 0);
2298 atomic_set(&cache->nr_dirty, 0);
2299 cache->dirty_bitset = alloc_bitset(from_cblock(cache->cache_size));
2300 if (!cache->dirty_bitset) {
2304 clear_bitset(cache->dirty_bitset, from_cblock(cache->cache_size));
2306 cache->discard_nr_blocks = cache->origin_blocks;
2307 cache->discard_bitset = alloc_bitset(from_oblock(cache->discard_nr_blocks));
2308 if (!cache->discard_bitset) {
2312 clear_bitset(cache->discard_bitset, from_oblock(cache->discard_nr_blocks));
2314 cache->copier = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2315 if (IS_ERR(cache->copier)) {
2317 r = PTR_ERR(cache->copier);
2321 cache->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
2322 if (!cache->wq) {
2326 INIT_WORK(&cache->worker, do_worker);
2327 INIT_DELAYED_WORK(&cache->waker, do_waker);
2328 cache->last_commit_jiffies = jiffies;
2330 cache->prison = dm_bio_prison_create(PRISON_CELLS);
2331 if (!cache->prison) {
2336 cache->all_io_ds = dm_deferred_set_create();
2337 if (!cache->all_io_ds) {
2342 cache->migration_pool = mempool_create_slab_pool(MIGRATION_POOL_SIZE,
2344 if (!cache->migration_pool) {
2345 *error = "Error creating cache's migration mempool";
2349 cache->next_migration = NULL;
2351 cache->need_tick_bio = true;
2352 cache->sized = false;
2353 cache->invalidate = false;
2354 cache->commit_requested = false;
2355 cache->loaded_mappings = false;
2356 cache->loaded_discards = false;
2358 load_stats(cache);
2360 atomic_set(&cache->stats.demotion, 0);
2361 atomic_set(&cache->stats.promotion, 0);
2362 atomic_set(&cache->stats.copies_avoided, 0);
2363 atomic_set(&cache->stats.cache_cell_clash, 0);
2364 atomic_set(&cache->stats.commit_count, 0);
2365 atomic_set(&cache->stats.discard_count, 0);
2367 spin_lock_init(&cache->invalidation_lock);
2368 INIT_LIST_HEAD(&cache->invalidation_requests);
2370 *result = cache;
2374 destroy(cache);
2378 static int copy_ctr_args(struct cache *cache, int argc, const char **argv)
2396 cache->nr_ctr_args = argc;
2397 cache->ctr_args = copy;
2406 struct cache *cache = NULL;
2410 ti->error = "Error allocating memory for cache";
2419 r = cache_create(ca, &cache);
2423 r = copy_ctr_args(cache, argc - 3, (const char **)argv + 3);
2425 destroy(cache);
2429 ti->private = cache;
2436 static int __cache_map(struct cache *cache, struct bio *bio, struct dm_bio_prison_cell **cell)
2439 dm_oblock_t block = get_bio_block(cache, bio);
2440 size_t pb_data_size = get_per_bio_data_size(cache);
2446 if (unlikely(from_oblock(block) >= from_oblock(cache->origin_blocks))) {
2449 * the end of the origin device. We don't cache these.
2452 remap_to_origin(cache, bio);
2457 defer_bio(cache, bio);
2464 *cell = alloc_prison_cell(cache);
2466 defer_bio(cache, bio);
2470 r = bio_detain(cache, block, bio, *cell,
2472 cache, cell);
2475 defer_bio(cache, bio);
2480 discarded_block = is_discarded_oblock(cache, block);
2482 r = policy_map(cache->policy, block, false, can_migrate, discarded_block,
2485 cell_defer(cache, *cell, true);
2489 DMERR_LIMIT("Unexpected return from cache replacement policy: %d", r);
2490 cell_defer(cache, *cell, false);
2498 if (passthrough_mode(&cache->features)) {
2504 cell_defer(cache, *cell, true);
2508 inc_miss_counter(cache, bio);
2509 remap_to_origin_clear_discard(cache, bio, block);
2513 inc_hit_counter(cache, bio);
2514 if (bio_data_dir(bio) == WRITE && writethrough_mode(&cache->features) &&
2515 !is_dirty(cache, lookup_result.cblock))
2516 remap_to_origin_then_cache(cache, bio, block, lookup_result.cblock);
2518 remap_to_cache_dirty(cache, bio, block, lookup_result.cblock);
2523 inc_miss_counter(cache, bio);
2530 cell_defer(cache, *cell, false);
2534 remap_to_origin_clear_discard(cache, bio, block);
2541 cell_defer(cache, *cell, false);
2553 struct cache *cache = ti->private;
2555 r = __cache_map(cache, bio, &cell);
2557 inc_ds(cache, bio, cell);
2558 cell_defer(cache, cell, false);
2566 struct cache *cache = ti->private;
2568 size_t pb_data_size = get_per_bio_data_size(cache);
2572 policy_tick(cache->policy);
2574 spin_lock_irqsave(&cache->lock, flags);
2575 cache->need_tick_bio = true;
2576 spin_unlock_irqrestore(&cache->lock, flags);
2579 check_for_quiesced_migrations(cache, pb);
2584 static int write_dirty_bitset(struct cache *cache)
2588 for (i = 0; i < from_cblock(cache->cache_size); i++) {
2589 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
2590 is_dirty(cache, to_cblock(i)));
2598 static int write_discard_bitset(struct cache *cache)
2602 r = dm_cache_discard_bitset_resize(cache->cmd, cache->sectors_per_block,
2603 cache->origin_blocks);
2609 for (i = 0; i < from_oblock(cache->discard_nr_blocks); i++) {
2610 r = dm_cache_set_discard(cache->cmd, to_oblock(i),
2611 is_discarded(cache, to_oblock(i)));
2622 static bool sync_metadata(struct cache *cache)
2626 r1 = write_dirty_bitset(cache);
2630 r2 = write_discard_bitset(cache);
2634 save_stats(cache);
2636 r3 = dm_cache_write_hints(cache->cmd, cache->policy);
2645 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3);
2647 DMERR("could not write cache metadata. Data loss may occur.");
2654 struct cache *cache = ti->private;
2656 start_quiescing(cache);
2657 wait_for_migrations(cache);
2658 stop_worker(cache);
2659 requeue_deferred_io(cache);
2660 stop_quiescing(cache);
2662 (void) sync_metadata(cache);
2669 struct cache *cache = context;
2671 r = policy_load_mapping(cache->policy, oblock, cblock, hint, hint_valid);
2676 set_dirty(cache, oblock, cblock);
2678 clear_dirty(cache, oblock, cblock);
2686 struct cache *cache = context;
2689 set_discard(cache, oblock);
2691 clear_discard(cache, oblock);
2696 static dm_cblock_t get_cache_dev_size(struct cache *cache)
2698 sector_t size = get_dev_size(cache->cache_dev);
2699 (void) sector_div(size, cache->sectors_per_block);
2703 static bool can_resize(struct cache *cache, dm_cblock_t new_size)
2705 if (from_cblock(new_size) > from_cblock(cache->cache_size))
2709 * We can't drop a dirty block when shrinking the cache.
2711 while (from_cblock(new_size) < from_cblock(cache->cache_size)) {
2713 if (is_dirty(cache, new_size)) {
2714 DMERR("unable to shrink cache; cache block %llu is dirty",
2723 static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
2727 r = dm_cache_resize(cache->cmd, new_size);
2729 DMERR("could not resize cache metadata");
2733 cache->cache_size = new_size;
2741 struct cache *cache = ti->private;
2742 dm_cblock_t csize = get_cache_dev_size(cache);
2745 * Check to see if the cache has resized.
2747 if (!cache->sized) {
2748 r = resize_cache_dev(cache, csize);
2752 cache->sized = true;
2754 } else if (csize != cache->cache_size) {
2755 if (!can_resize(cache, csize))
2758 r = resize_cache_dev(cache, csize);
2763 if (!cache->loaded_mappings) {
2764 r = dm_cache_load_mappings(cache->cmd, cache->policy,
2765 load_mapping, cache);
2767 DMERR("could not load cache mappings");
2771 cache->loaded_mappings = true;
2774 if (!cache->loaded_discards) {
2775 r = dm_cache_load_discards(cache->cmd, load_discard, cache);
2781 cache->loaded_discards = true;
2789 struct cache *cache = ti->private;
2791 cache->need_tick_bio = true;
2792 do_waker(&cache->waker.work);
2799 * <cache block size> <#used cache blocks>/<#total cache blocks>
2815 struct cache *cache = ti->private;
2822 r = dm_cache_commit(cache->cmd, false);
2827 r = dm_cache_get_free_metadata_block_count(cache->cmd,
2834 r = dm_cache_get_metadata_dev_size(cache->cmd, &nr_blocks_metadata);
2840 residency = policy_residency(cache->policy);
2846 cache->sectors_per_block,
2848 (unsigned long long) from_cblock(cache->cache_size),
2849 (unsigned) atomic_read(&cache->stats.read_hit),
2850 (unsigned) atomic_read(&cache->stats.read_miss),
2851 (unsigned) atomic_read(&cache->stats.write_hit),
2852 (unsigned) atomic_read(&cache->stats.write_miss),
2853 (unsigned) atomic_read(&cache->stats.demotion),
2854 (unsigned) atomic_read(&cache->stats.promotion),
2855 (unsigned long) atomic_read(&cache->nr_dirty));
2857 if (writethrough_mode(&cache->features))
2860 else if (passthrough_mode(&cache->features))
2863 else if (writeback_mode(&cache->features))
2867 DMERR("internal error: unknown io mode: %d", (int) cache->features.io_mode);
2871 DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
2873 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
2875 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz);
2883 format_dev_t(buf, cache->metadata_dev->bdev->bd_dev);
2885 format_dev_t(buf, cache->cache_dev->bdev->bd_dev);
2887 format_dev_t(buf, cache->origin_dev->bdev->bd_dev);
2890 for (i = 0; i < cache->nr_ctr_args - 1; i++)
2891 DMEMIT(" %s", cache->ctr_args[i]);
2892 if (cache->nr_ctr_args)
2893 DMEMIT(" %s", cache->ctr_args[cache->nr_ctr_args - 1]);
2903 * A cache block range can take two forms:
2908 static int parse_cblock_range(struct cache *cache, const char *str,
2945 static int validate_cblock_range(struct cache *cache, struct cblock_range *range)
2949 uint64_t n = from_cblock(cache->cache_size);
2969 static int request_invalidation(struct cache *cache, struct cblock_range *range)
2979 spin_lock(&cache->invalidation_lock);
2980 list_add(&req.list, &cache->invalidation_requests);
2981 spin_unlock(&cache->invalidation_lock);
2982 wake_worker(cache);
2988 static int process_invalidate_cblocks_message(struct cache *cache, unsigned count,
2995 if (!passthrough_mode(&cache->features)) {
2996 DMERR("cache has to be in passthrough mode for invalidation");
3001 r = parse_cblock_range(cache, cblock_ranges[i], &range);
3005 r = validate_cblock_range(cache, &range);
3012 r = request_invalidation(cache, &range);
3026 * The key migration_threshold is supported by the cache target core.
3030 struct cache *cache = ti->private;
3036 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
3041 return set_config_value(cache, argv[0], argv[1]);
3048 struct cache *cache = ti->private;
3050 r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
3052 r = fn(ti, cache->origin_dev, 0, ti->len, data);
3067 struct cache *cache = ti->private;
3068 struct request_queue *q = bdev_get_queue(cache->origin_dev->bdev);
3073 bvm->bi_bdev = cache->origin_dev->bdev;
3077 static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
3080 * FIXME: these limits may be incompatible with the cache device
3082 limits->max_discard_sectors = cache->sectors_per_block;
3083 limits->discard_granularity = cache->sectors_per_block << SECTOR_SHIFT;
3088 struct cache *cache = ti->private;
3093 * cache's blocksize (io_opt is a factor) do not override them.
3095 if (io_opt_sectors < cache->sectors_per_block ||
3096 do_div(io_opt_sectors, cache->sectors_per_block)) {
3097 blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
3098 blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
3100 set_discard_limits(cache, limits);
3106 .name = "cache",
3129 DMERR("cache target registration failed: %d", r);
3151 MODULE_DESCRIPTION(DM_NAME " cache target");