aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/md/dm-cache-target.c
diff options
context:
space:
mode:
authorJoe Thornber <ejt@redhat.com>2015-04-22 16:42:35 -0400
committerMike Snitzer <snitzer@redhat.com>2015-06-11 17:13:00 -0400
commit028ae9f76f2935e8cf9974bff9a4587e3a995ff3 (patch)
tree1bd3984690e112d03cf1e56d43b32c000e420ab4 /drivers/md/dm-cache-target.c
parent88bf5184fa5861e766e39fd34fc6d21557ac7be8 (diff)
dm cache: add fail io mode and needs_check flag
If a cache metadata operation fails (e.g. transaction commit) the cache's metadata device will abort the current transaction, set a new needs_check flag, and the cache will transition to "read-only" mode. If aborting the transaction or setting the needs_check flag fails the cache will transition to "fail-io" mode. Once needs_check is set the cache device will not be allowed to activate. Activation requires write access to metadata. Future work is needed to add proper support for running the cache in read-only mode. Once in fail-io mode the cache will report a status of "Fail". Also, add commit() wrapper that will disallow commits if in read_only or fail mode. Signed-off-by: Joe Thornber <ejt@redhat.com> Signed-off-by: Mike Snitzer <snitzer@redhat.com>
Diffstat (limited to 'drivers/md/dm-cache-target.c')
-rw-r--r--drivers/md/dm-cache-target.c204
1 files changed, 176 insertions, 28 deletions
diff --git a/drivers/md/dm-cache-target.c b/drivers/md/dm-cache-target.c
index 6d36ed3c46a0..dae0321ebfa9 100644
--- a/drivers/md/dm-cache-target.c
+++ b/drivers/md/dm-cache-target.c
@@ -150,12 +150,10 @@ static void dm_unhook_bio(struct dm_hook_info *h, struct bio *bio)
150#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT) 150#define DATA_DEV_BLOCK_SIZE_MIN_SECTORS (32 * 1024 >> SECTOR_SHIFT)
151#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT) 151#define DATA_DEV_BLOCK_SIZE_MAX_SECTORS (1024 * 1024 * 1024 >> SECTOR_SHIFT)
152 152
153/*
154 * FIXME: the cache is read/write for the time being.
155 */
156enum cache_metadata_mode { 153enum cache_metadata_mode {
157 CM_WRITE, /* metadata may be changed */ 154 CM_WRITE, /* metadata may be changed */
158 CM_READ_ONLY, /* metadata may not be changed */ 155 CM_READ_ONLY, /* metadata may not be changed */
156 CM_FAIL
159}; 157};
160 158
161enum cache_io_mode { 159enum cache_io_mode {
@@ -385,6 +383,8 @@ struct prealloc {
385 struct dm_bio_prison_cell *cell2; 383 struct dm_bio_prison_cell *cell2;
386}; 384};
387 385
386static enum cache_metadata_mode get_cache_mode(struct cache *cache);
387
388static void wake_worker(struct cache *cache) 388static void wake_worker(struct cache *cache)
389{ 389{
390 queue_work(cache->wq, &cache->worker); 390 queue_work(cache->wq, &cache->worker);
@@ -699,6 +699,9 @@ static void save_stats(struct cache *cache)
699{ 699{
700 struct dm_cache_statistics stats; 700 struct dm_cache_statistics stats;
701 701
702 if (get_cache_mode(cache) >= CM_READ_ONLY)
703 return;
704
702 stats.read_hits = atomic_read(&cache->stats.read_hit); 705 stats.read_hits = atomic_read(&cache->stats.read_hit);
703 stats.read_misses = atomic_read(&cache->stats.read_miss); 706 stats.read_misses = atomic_read(&cache->stats.read_miss);
704 stats.write_hits = atomic_read(&cache->stats.write_hit); 707 stats.write_hits = atomic_read(&cache->stats.write_hit);
@@ -958,6 +961,84 @@ static void remap_to_origin_then_cache(struct cache *cache, struct bio *bio,
958} 961}
959 962
960/*---------------------------------------------------------------- 963/*----------------------------------------------------------------
964 * Failure modes
965 *--------------------------------------------------------------*/
966static enum cache_metadata_mode get_cache_mode(struct cache *cache)
967{
968 return cache->features.mode;
969}
970
971static void notify_mode_switch(struct cache *cache, enum cache_metadata_mode mode)
972{
973 const char *descs[] = {
974 "write",
975 "read-only",
976 "fail"
977 };
978
979 dm_table_event(cache->ti->table);
980 DMINFO("switching cache to %s mode", descs[(int)mode]);
981}
982
983static void set_cache_mode(struct cache *cache, enum cache_metadata_mode new_mode)
984{
985 bool needs_check = dm_cache_metadata_needs_check(cache->cmd);
986 enum cache_metadata_mode old_mode = get_cache_mode(cache);
987
988 if (new_mode == CM_WRITE && needs_check) {
989 DMERR("unable to switch cache to write mode until repaired.");
990 if (old_mode != new_mode)
991 new_mode = old_mode;
992 else
993 new_mode = CM_READ_ONLY;
994 }
995
996 /* Never move out of fail mode */
997 if (old_mode == CM_FAIL)
998 new_mode = CM_FAIL;
999
1000 switch (new_mode) {
1001 case CM_FAIL:
1002 case CM_READ_ONLY:
1003 dm_cache_metadata_set_read_only(cache->cmd);
1004 break;
1005
1006 case CM_WRITE:
1007 dm_cache_metadata_set_read_write(cache->cmd);
1008 break;
1009 }
1010
1011 cache->features.mode = new_mode;
1012
1013 if (new_mode != old_mode)
1014 notify_mode_switch(cache, new_mode);
1015}
1016
1017static void abort_transaction(struct cache *cache)
1018{
1019 if (get_cache_mode(cache) >= CM_READ_ONLY)
1020 return;
1021
1022 if (dm_cache_metadata_set_needs_check(cache->cmd)) {
1023 DMERR("failed to set 'needs_check' flag in metadata");
1024 set_cache_mode(cache, CM_FAIL);
1025 }
1026
1027 DMERR_LIMIT("aborting current metadata transaction");
1028 if (dm_cache_metadata_abort(cache->cmd)) {
1029 DMERR("failed to abort metadata transaction");
1030 set_cache_mode(cache, CM_FAIL);
1031 }
1032}
1033
1034static void metadata_operation_failed(struct cache *cache, const char *op, int r)
1035{
1036 DMERR_LIMIT("metadata operation '%s' failed: error = %d", op, r);
1037 abort_transaction(cache);
1038 set_cache_mode(cache, CM_READ_ONLY);
1039}
1040
1041/*----------------------------------------------------------------
961 * Migration processing 1042 * Migration processing
962 * 1043 *
963 * Migration covers moving data from the origin device to the cache, or 1044 * Migration covers moving data from the origin device to the cache, or
@@ -1063,6 +1144,7 @@ static void migration_failure(struct dm_cache_migration *mg)
1063 1144
1064static void migration_success_pre_commit(struct dm_cache_migration *mg) 1145static void migration_success_pre_commit(struct dm_cache_migration *mg)
1065{ 1146{
1147 int r;
1066 unsigned long flags; 1148 unsigned long flags;
1067 struct cache *cache = mg->cache; 1149 struct cache *cache = mg->cache;
1068 1150
@@ -1073,8 +1155,10 @@ static void migration_success_pre_commit(struct dm_cache_migration *mg)
1073 return; 1155 return;
1074 1156
1075 } else if (mg->demote) { 1157 } else if (mg->demote) {
1076 if (dm_cache_remove_mapping(cache->cmd, mg->cblock)) { 1158 r = dm_cache_remove_mapping(cache->cmd, mg->cblock);
1159 if (r) {
1077 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata"); 1160 DMWARN_LIMIT("demotion failed; couldn't update on disk metadata");
1161 metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
1078 policy_force_mapping(cache->policy, mg->new_oblock, 1162 policy_force_mapping(cache->policy, mg->new_oblock,
1079 mg->old_oblock); 1163 mg->old_oblock);
1080 if (mg->promote) 1164 if (mg->promote)
@@ -1083,8 +1167,10 @@ static void migration_success_pre_commit(struct dm_cache_migration *mg)
1083 return; 1167 return;
1084 } 1168 }
1085 } else { 1169 } else {
1086 if (dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock)) { 1170 r = dm_cache_insert_mapping(cache->cmd, mg->cblock, mg->new_oblock);
1171 if (r) {
1087 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata"); 1172 DMWARN_LIMIT("promotion failed; couldn't update on disk metadata");
1173 metadata_operation_failed(cache, "dm_cache_insert_mapping", r);
1088 policy_remove_mapping(cache->policy, mg->new_oblock); 1174 policy_remove_mapping(cache->policy, mg->new_oblock);
1089 free_io_migration(mg); 1175 free_io_migration(mg);
1090 return; 1176 return;
@@ -1812,15 +1898,32 @@ static int need_commit_due_to_time(struct cache *cache)
1812 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD; 1898 jiffies > cache->last_commit_jiffies + COMMIT_PERIOD;
1813} 1899}
1814 1900
1901/*
1902 * A non-zero return indicates read_only or fail_io mode.
1903 */
1904static int commit(struct cache *cache, bool clean_shutdown)
1905{
1906 int r;
1907
1908 if (get_cache_mode(cache) >= CM_READ_ONLY)
1909 return -EINVAL;
1910
1911 atomic_inc(&cache->stats.commit_count);
1912 r = dm_cache_commit(cache->cmd, clean_shutdown);
1913 if (r)
1914 metadata_operation_failed(cache, "dm_cache_commit", r);
1915
1916 return r;
1917}
1918
1815static int commit_if_needed(struct cache *cache) 1919static int commit_if_needed(struct cache *cache)
1816{ 1920{
1817 int r = 0; 1921 int r = 0;
1818 1922
1819 if ((cache->commit_requested || need_commit_due_to_time(cache)) && 1923 if ((cache->commit_requested || need_commit_due_to_time(cache)) &&
1820 dm_cache_changed_this_transaction(cache->cmd)) { 1924 dm_cache_changed_this_transaction(cache->cmd)) {
1821 atomic_inc(&cache->stats.commit_count); 1925 r = commit(cache, false);
1822 cache->commit_requested = false; 1926 cache->commit_requested = false;
1823 r = dm_cache_commit(cache->cmd, false);
1824 cache->last_commit_jiffies = jiffies; 1927 cache->last_commit_jiffies = jiffies;
1825 } 1928 }
1826 1929
@@ -1988,8 +2091,10 @@ static void process_invalidation_request(struct cache *cache, struct invalidatio
1988 r = policy_remove_cblock(cache->policy, to_cblock(begin)); 2091 r = policy_remove_cblock(cache->policy, to_cblock(begin));
1989 if (!r) { 2092 if (!r) {
1990 r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin)); 2093 r = dm_cache_remove_mapping(cache->cmd, to_cblock(begin));
1991 if (r) 2094 if (r) {
2095 metadata_operation_failed(cache, "dm_cache_remove_mapping", r);
1992 break; 2096 break;
2097 }
1993 2098
1994 } else if (r == -ENODATA) { 2099 } else if (r == -ENODATA) {
1995 /* harmless, already unmapped */ 2100 /* harmless, already unmapped */
@@ -2133,12 +2238,6 @@ static void do_worker(struct work_struct *ws)
2133 if (commit_if_needed(cache)) { 2238 if (commit_if_needed(cache)) {
2134 process_deferred_flush_bios(cache, false); 2239 process_deferred_flush_bios(cache, false);
2135 process_migrations(cache, &cache->need_commit_migrations, migration_failure); 2240 process_migrations(cache, &cache->need_commit_migrations, migration_failure);
2136
2137 /*
2138 * FIXME: rollback metadata or just go into a
2139 * failure mode and error everything
2140 */
2141
2142 } else { 2241 } else {
2143 process_deferred_flush_bios(cache, true); 2242 process_deferred_flush_bios(cache, true);
2144 process_migrations(cache, &cache->need_commit_migrations, 2243 process_migrations(cache, &cache->need_commit_migrations,
@@ -2711,6 +2810,12 @@ static int cache_create(struct cache_args *ca, struct cache **result)
2711 goto bad; 2810 goto bad;
2712 } 2811 }
2713 cache->cmd = cmd; 2812 cache->cmd = cmd;
2813 set_cache_mode(cache, CM_WRITE);
2814 if (get_cache_mode(cache) != CM_WRITE) {
2815 *error = "Unable to get write access to metadata, please check/repair metadata.";
2816 r = -EINVAL;
2817 goto bad;
2818 }
2714 2819
2715 if (passthrough_mode(&cache->features)) { 2820 if (passthrough_mode(&cache->features)) {
2716 bool all_clean; 2821 bool all_clean;
@@ -3043,11 +3148,16 @@ static int write_dirty_bitset(struct cache *cache)
3043{ 3148{
3044 unsigned i, r; 3149 unsigned i, r;
3045 3150
3151 if (get_cache_mode(cache) >= CM_READ_ONLY)
3152 return -EINVAL;
3153
3046 for (i = 0; i < from_cblock(cache->cache_size); i++) { 3154 for (i = 0; i < from_cblock(cache->cache_size); i++) {
3047 r = dm_cache_set_dirty(cache->cmd, to_cblock(i), 3155 r = dm_cache_set_dirty(cache->cmd, to_cblock(i),
3048 is_dirty(cache, to_cblock(i))); 3156 is_dirty(cache, to_cblock(i)));
3049 if (r) 3157 if (r) {
3158 metadata_operation_failed(cache, "dm_cache_set_dirty", r);
3050 return r; 3159 return r;
3160 }
3051 } 3161 }
3052 3162
3053 return 0; 3163 return 0;
@@ -3057,18 +3167,40 @@ static int write_discard_bitset(struct cache *cache)
3057{ 3167{
3058 unsigned i, r; 3168 unsigned i, r;
3059 3169
3170 if (get_cache_mode(cache) >= CM_READ_ONLY)
3171 return -EINVAL;
3172
3060 r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size, 3173 r = dm_cache_discard_bitset_resize(cache->cmd, cache->discard_block_size,
3061 cache->discard_nr_blocks); 3174 cache->discard_nr_blocks);
3062 if (r) { 3175 if (r) {
3063 DMERR("could not resize on-disk discard bitset"); 3176 DMERR("could not resize on-disk discard bitset");
3177 metadata_operation_failed(cache, "dm_cache_discard_bitset_resize", r);
3064 return r; 3178 return r;
3065 } 3179 }
3066 3180
3067 for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) { 3181 for (i = 0; i < from_dblock(cache->discard_nr_blocks); i++) {
3068 r = dm_cache_set_discard(cache->cmd, to_dblock(i), 3182 r = dm_cache_set_discard(cache->cmd, to_dblock(i),
3069 is_discarded(cache, to_dblock(i))); 3183 is_discarded(cache, to_dblock(i)));
3070 if (r) 3184 if (r) {
3185 metadata_operation_failed(cache, "dm_cache_set_discard", r);
3071 return r; 3186 return r;
3187 }
3188 }
3189
3190 return 0;
3191}
3192
3193static int write_hints(struct cache *cache)
3194{
3195 int r;
3196
3197 if (get_cache_mode(cache) >= CM_READ_ONLY)
3198 return -EINVAL;
3199
3200 r = dm_cache_write_hints(cache->cmd, cache->policy);
3201 if (r) {
3202 metadata_operation_failed(cache, "dm_cache_write_hints", r);
3203 return r;
3072 } 3204 }
3073 3205
3074 return 0; 3206 return 0;
@@ -3091,7 +3223,7 @@ static bool sync_metadata(struct cache *cache)
3091 3223
3092 save_stats(cache); 3224 save_stats(cache);
3093 3225
3094 r3 = dm_cache_write_hints(cache->cmd, cache->policy); 3226 r3 = write_hints(cache);
3095 if (r3) 3227 if (r3)
3096 DMERR("could not write hints"); 3228 DMERR("could not write hints");
3097 3229
@@ -3100,9 +3232,9 @@ static bool sync_metadata(struct cache *cache)
3100 * set the clean shutdown flag. This will effectively force every 3232 * set the clean shutdown flag. This will effectively force every
3101 * dirty bit to be set on reload. 3233 * dirty bit to be set on reload.
3102 */ 3234 */
3103 r4 = dm_cache_commit(cache->cmd, !r1 && !r2 && !r3); 3235 r4 = commit(cache, !r1 && !r2 && !r3);
3104 if (r4) 3236 if (r4)
3105 DMERR("could not write cache metadata. Data loss may occur."); 3237 DMERR("could not write cache metadata.");
3106 3238
3107 return !r1 && !r2 && !r3 && !r4; 3239 return !r1 && !r2 && !r3 && !r4;
3108} 3240}
@@ -3118,7 +3250,8 @@ static void cache_postsuspend(struct dm_target *ti)
3118 requeue_deferred_cells(cache); 3250 requeue_deferred_cells(cache);
3119 stop_quiescing(cache); 3251 stop_quiescing(cache);
3120 3252
3121 (void) sync_metadata(cache); 3253 if (get_cache_mode(cache) == CM_WRITE)
3254 (void) sync_metadata(cache);
3122} 3255}
3123 3256
3124static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock, 3257static int load_mapping(void *context, dm_oblock_t oblock, dm_cblock_t cblock,
@@ -3257,6 +3390,7 @@ static int resize_cache_dev(struct cache *cache, dm_cblock_t new_size)
3257 r = dm_cache_resize(cache->cmd, new_size); 3390 r = dm_cache_resize(cache->cmd, new_size);
3258 if (r) { 3391 if (r) {
3259 DMERR("could not resize cache metadata"); 3392 DMERR("could not resize cache metadata");
3393 metadata_operation_failed(cache, "dm_cache_resize", r);
3260 return r; 3394 return r;
3261 } 3395 }
3262 3396
@@ -3295,6 +3429,7 @@ static int cache_preresume(struct dm_target *ti)
3295 load_mapping, cache); 3429 load_mapping, cache);
3296 if (r) { 3430 if (r) {
3297 DMERR("could not load cache mappings"); 3431 DMERR("could not load cache mappings");
3432 metadata_operation_failed(cache, "dm_cache_load_mappings", r);
3298 return r; 3433 return r;
3299 } 3434 }
3300 3435
@@ -3315,6 +3450,7 @@ static int cache_preresume(struct dm_target *ti)
3315 r = dm_cache_load_discards(cache->cmd, load_discard, &li); 3450 r = dm_cache_load_discards(cache->cmd, load_discard, &li);
3316 if (r) { 3451 if (r) {
3317 DMERR("could not load origin discards"); 3452 DMERR("could not load origin discards");
3453 metadata_operation_failed(cache, "dm_cache_load_discards", r);
3318 return r; 3454 return r;
3319 } 3455 }
3320 set_discard_range(&li); 3456 set_discard_range(&li);
@@ -3342,7 +3478,7 @@ static void cache_resume(struct dm_target *ti)
3342 * <#demotions> <#promotions> <#dirty> 3478 * <#demotions> <#promotions> <#dirty>
3343 * <#features> <features>* 3479 * <#features> <features>*
3344 * <#core args> <core args> 3480 * <#core args> <core args>
3345 * <policy name> <#policy args> <policy args>* 3481 * <policy name> <#policy args> <policy args>* <cache metadata mode>
3346 */ 3482 */
3347static void cache_status(struct dm_target *ti, status_type_t type, 3483static void cache_status(struct dm_target *ti, status_type_t type,
3348 unsigned status_flags, char *result, unsigned maxlen) 3484 unsigned status_flags, char *result, unsigned maxlen)
@@ -3358,13 +3494,15 @@ static void cache_status(struct dm_target *ti, status_type_t type,
3358 3494
3359 switch (type) { 3495 switch (type) {
3360 case STATUSTYPE_INFO: 3496 case STATUSTYPE_INFO:
3361 /* Commit to ensure statistics aren't out-of-date */ 3497 if (get_cache_mode(cache) == CM_FAIL) {
3362 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti)) { 3498 DMEMIT("Fail");
3363 r = dm_cache_commit(cache->cmd, false); 3499 break;
3364 if (r)
3365 DMERR("could not commit metadata for accurate status");
3366 } 3500 }
3367 3501
3502 /* Commit to ensure statistics aren't out-of-date */
3503 if (!(status_flags & DM_STATUS_NOFLUSH_FLAG) && !dm_suspended(ti))
3504 (void) commit(cache, false);
3505
3368 r = dm_cache_get_free_metadata_block_count(cache->cmd, 3506 r = dm_cache_get_free_metadata_block_count(cache->cmd,
3369 &nr_free_blocks_metadata); 3507 &nr_free_blocks_metadata);
3370 if (r) { 3508 if (r) {
@@ -3413,11 +3551,16 @@ static void cache_status(struct dm_target *ti, status_type_t type,
3413 3551
3414 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy)); 3552 DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
3415 if (sz < maxlen) { 3553 if (sz < maxlen) {
3416 r = policy_emit_config_values(cache->policy, result + sz, maxlen - sz); 3554 r = policy_emit_config_values(cache->policy, result, maxlen, &sz);
3417 if (r) 3555 if (r)
3418 DMERR("policy_emit_config_values returned %d", r); 3556 DMERR("policy_emit_config_values returned %d", r);
3419 } 3557 }
3420 3558
3559 if (get_cache_mode(cache) == CM_READ_ONLY)
3560 DMEMIT("ro ");
3561 else
3562 DMEMIT("rw ");
3563
3421 break; 3564 break;
3422 3565
3423 case STATUSTYPE_TABLE: 3566 case STATUSTYPE_TABLE:
@@ -3573,6 +3716,11 @@ static int cache_message(struct dm_target *ti, unsigned argc, char **argv)
3573 if (!argc) 3716 if (!argc)
3574 return -EINVAL; 3717 return -EINVAL;
3575 3718
3719 if (get_cache_mode(cache) >= CM_READ_ONLY) {
3720 DMERR("unable to service cache target messages in READ_ONLY or FAIL mode");
3721 return -EOPNOTSUPP;
3722 }
3723
3576 if (!strcasecmp(argv[0], "invalidate_cblocks")) 3724 if (!strcasecmp(argv[0], "invalidate_cblocks"))
3577 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1); 3725 return process_invalidate_cblocks_message(cache, argc - 1, (const char **) argv + 1);
3578 3726
@@ -3646,7 +3794,7 @@ static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
3646 3794
3647static struct target_type cache_target = { 3795static struct target_type cache_target = {
3648 .name = "cache", 3796 .name = "cache",
3649 .version = {1, 6, 0}, 3797 .version = {1, 7, 0},
3650 .module = THIS_MODULE, 3798 .module = THIS_MODULE,
3651 .ctr = cache_ctr, 3799 .ctr = cache_ctr,
3652 .dtr = cache_dtr, 3800 .dtr = cache_dtr,