Source
346
346
* origin. If a write goes to a cached oblock, then the cache
347
347
* block is invalidated.
348
348
*/
349
349
CM_IO_PASSTHROUGH
350
350
};
351
351
352
352
struct cache_features {
353
353
enum cache_metadata_mode mode;
354
354
enum cache_io_mode io_mode;
355
355
unsigned metadata_version;
356
+
bool discard_passdown:1;
356
357
};
357
358
358
359
struct cache_stats {
359
360
atomic_t read_hit;
360
361
atomic_t read_miss;
361
362
atomic_t write_hit;
362
363
atomic_t write_miss;
363
364
atomic_t demotion;
364
365
atomic_t promotion;
365
366
atomic_t writeback;
1892
1893
1893
1894
// FIXME: do we need to lock the region? Or can we just assume the
1894
1895
// user wont be so foolish as to issue discard concurrently with
1895
1896
// other IO?
1896
1897
calc_discard_block_range(cache, bio, &b, &e);
1897
1898
while (b != e) {
1898
1899
set_discard(cache, b);
1899
1900
b = to_dblock(from_dblock(b) + 1);
1900
1901
}
1901
1902
1902
-
bio_endio(bio);
1903
+
if (cache->features.discard_passdown) {
1904
+
remap_to_origin(cache, bio);
1905
+
generic_make_request(bio);
1906
+
} else
1907
+
bio_endio(bio);
1903
1908
1904
1909
return false;
1905
1910
}
1906
1911
1907
1912
static void process_deferred_bios(struct work_struct *ws)
1908
1913
{
1909
1914
struct cache *cache = container_of(ws, struct cache, deferred_bio_worker);
1910
1915
1911
1916
unsigned long flags;
1912
1917
bool commit_needed = false;
2226
2231
ca->block_size = block_size;
2227
2232
2228
2233
return 0;
2229
2234
}
2230
2235
2231
2236
static void init_features(struct cache_features *cf)
2232
2237
{
2233
2238
cf->mode = CM_WRITE;
2234
2239
cf->io_mode = CM_IO_WRITEBACK;
2235
2240
cf->metadata_version = 1;
2241
+
cf->discard_passdown = true;
2236
2242
}
2237
2243
2238
2244
static int parse_features(struct cache_args *ca, struct dm_arg_set *as,
2239
2245
char **error)
2240
2246
{
2241
2247
static const struct dm_arg _args[] = {
2242
-
{0, 2, "Invalid number of cache feature arguments"},
2248
+
{0, 3, "Invalid number of cache feature arguments"},
2243
2249
};
2244
2250
2245
2251
int r, mode_ctr = 0;
2246
2252
unsigned argc;
2247
2253
const char *arg;
2248
2254
struct cache_features *cf = &ca->features;
2249
2255
2250
2256
init_features(cf);
2251
2257
2252
2258
r = dm_read_arg_group(_args, as, &argc, error);
2267
2273
}
2268
2274
2269
2275
else if (!strcasecmp(arg, "passthrough")) {
2270
2276
cf->io_mode = CM_IO_PASSTHROUGH;
2271
2277
mode_ctr++;
2272
2278
}
2273
2279
2274
2280
else if (!strcasecmp(arg, "metadata2"))
2275
2281
cf->metadata_version = 2;
2276
2282
2283
+
else if (!strcasecmp(arg, "no_discard_passdown"))
2284
+
cf->discard_passdown = false;
2285
+
2277
2286
else {
2278
2287
*error = "Unrecognised cache feature requested";
2279
2288
return -EINVAL;
2280
2289
}
2281
2290
}
2282
2291
2283
2292
if (mode_ctr > 1) {
2284
2293
*error = "Duplicate cache io_mode features requested";
2285
2294
return -EINVAL;
2286
2295
}
3112
3121
3113
3122
static void cache_resume(struct dm_target *ti)
3114
3123
{
3115
3124
struct cache *cache = ti->private;
3116
3125
3117
3126
cache->need_tick_bio = true;
3118
3127
allow_background_work(cache);
3119
3128
do_waker(&cache->waker.work);
3120
3129
}
3121
3130
3131
+
static void emit_flags(struct cache *cache, char *result,
3132
+
unsigned maxlen, ssize_t *sz_ptr)
3133
+
{
3134
+
ssize_t sz = *sz_ptr;
3135
+
struct cache_features *cf = &cache->features;
3136
+
unsigned count = (cf->metadata_version == 2) + !cf->discard_passdown + 1;
3137
+
3138
+
DMEMIT("%u ", count);
3139
+
3140
+
if (cf->metadata_version == 2)
3141
+
DMEMIT("metadata2 ");
3142
+
3143
+
if (writethrough_mode(cache))
3144
+
DMEMIT("writethrough ");
3145
+
3146
+
else if (passthrough_mode(cache))
3147
+
DMEMIT("passthrough ");
3148
+
3149
+
else if (writeback_mode(cache))
3150
+
DMEMIT("writeback ");
3151
+
3152
+
else {
3153
+
DMEMIT("unknown ");
3154
+
DMERR("%s: internal error: unknown io mode: %d",
3155
+
cache_device_name(cache), (int) cf->io_mode);
3156
+
}
3157
+
3158
+
if (!cf->discard_passdown)
3159
+
DMEMIT("no_discard_passdown ");
3160
+
3161
+
*sz_ptr = sz;
3162
+
}
3163
+
3122
3164
/*
3123
3165
* Status format:
3124
3166
*
3125
3167
* <metadata block size> <#used metadata blocks>/<#total metadata blocks>
3126
3168
* <cache block size> <#used cache blocks>/<#total cache blocks>
3127
3169
* <#read hits> <#read misses> <#write hits> <#write misses>
3128
3170
* <#demotions> <#promotions> <#dirty>
3129
3171
* <#features> <features>*
3130
3172
* <#core args> <core args>
3131
3173
* <policy name> <#policy args> <policy args>* <cache metadata mode> <needs_check>
3178
3220
(unsigned long long) from_cblock(residency),
3179
3221
(unsigned long long) from_cblock(cache->cache_size),
3180
3222
(unsigned) atomic_read(&cache->stats.read_hit),
3181
3223
(unsigned) atomic_read(&cache->stats.read_miss),
3182
3224
(unsigned) atomic_read(&cache->stats.write_hit),
3183
3225
(unsigned) atomic_read(&cache->stats.write_miss),
3184
3226
(unsigned) atomic_read(&cache->stats.demotion),
3185
3227
(unsigned) atomic_read(&cache->stats.promotion),
3186
3228
(unsigned long) atomic_read(&cache->nr_dirty));
3187
3229
3188
-
if (cache->features.metadata_version == 2)
3189
-
DMEMIT("2 metadata2 ");
3190
-
else
3191
-
DMEMIT("1 ");
3192
-
3193
-
if (writethrough_mode(cache))
3194
-
DMEMIT("writethrough ");
3195
-
3196
-
else if (passthrough_mode(cache))
3197
-
DMEMIT("passthrough ");
3198
-
3199
-
else if (writeback_mode(cache))
3200
-
DMEMIT("writeback ");
3201
-
3202
-
else {
3203
-
DMERR("%s: internal error: unknown io mode: %d",
3204
-
cache_device_name(cache), (int) cache->features.io_mode);
3205
-
goto err;
3206
-
}
3230
+
emit_flags(cache, result, maxlen, &sz);
3207
3231
3208
3232
DMEMIT("2 migration_threshold %llu ", (unsigned long long) cache->migration_threshold);
3209
3233
3210
3234
DMEMIT("%s ", dm_cache_policy_get_name(cache->policy));
3211
3235
if (sz < maxlen) {
3212
3236
r = policy_emit_config_values(cache->policy, result, maxlen, &sz);
3213
3237
if (r)
3214
3238
DMERR("%s: policy_emit_config_values returned %d",
3215
3239
cache_device_name(cache), r);
3216
3240
}
3425
3449
int r = 0;
3426
3450
struct cache *cache = ti->private;
3427
3451
3428
3452
r = fn(ti, cache->cache_dev, 0, get_dev_size(cache->cache_dev), data);
3429
3453
if (!r)
3430
3454
r = fn(ti, cache->origin_dev, 0, ti->len, data);
3431
3455
3432
3456
return r;
3433
3457
}
3434
3458
3459
+
static bool origin_dev_supports_discard(struct block_device *origin_bdev)
3460
+
{
3461
+
struct request_queue *q = bdev_get_queue(origin_bdev);
3462
+
3463
+
return q && blk_queue_discard(q);
3464
+
}
3465
+
3466
+
/*
3467
+
* If discard_passdown was enabled verify that the origin device
3468
+
* supports discards. Disable discard_passdown if not.
3469
+
*/
3470
+
static void disable_passdown_if_not_supported(struct cache *cache)
3471
+
{
3472
+
struct block_device *origin_bdev = cache->origin_dev->bdev;
3473
+
struct queue_limits *origin_limits = &bdev_get_queue(origin_bdev)->limits;
3474
+
const char *reason = NULL;
3475
+
char buf[BDEVNAME_SIZE];
3476
+
3477
+
if (!cache->features.discard_passdown)
3478
+
return;
3479
+
3480
+
if (!origin_dev_supports_discard(origin_bdev))
3481
+
reason = "discard unsupported";
3482
+
3483
+
else if (origin_limits->max_discard_sectors < cache->sectors_per_block)
3484
+
reason = "max discard sectors smaller than a block";
3485
+
3486
+
if (reason) {
3487
+
DMWARN("Origin device (%s) %s: Disabling discard passdown.",
3488
+
bdevname(origin_bdev, buf), reason);
3489
+
cache->features.discard_passdown = false;
3490
+
}
3491
+
}
3492
+
3435
3493
static void set_discard_limits(struct cache *cache, struct queue_limits *limits)
3436
3494
{
3495
+
struct block_device *origin_bdev = cache->origin_dev->bdev;
3496
+
struct queue_limits *origin_limits = &bdev_get_queue(origin_bdev)->limits;
3497
+
3498
+
if (!cache->features.discard_passdown) {
3499
+
/* No passdown is done so setting own virtual limits */
3500
+
limits->max_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024,
3501
+
cache->origin_sectors);
3502
+
limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
3503
+
return;
3504
+
}
3505
+
3437
3506
/*
3438
-
* FIXME: these limits may be incompatible with the cache device
3507
+
* cache_iterate_devices() is stacking both origin and fast device limits
3508
+
* but discards aren't passed to fast device, so inherit origin's limits.
3439
3509
*/
3440
-
limits->max_discard_sectors = min_t(sector_t, cache->discard_block_size * 1024,
3441
-
cache->origin_sectors);
3442
-
limits->discard_granularity = cache->discard_block_size << SECTOR_SHIFT;
3510
+
limits->max_discard_sectors = origin_limits->max_discard_sectors;
3511
+
limits->max_hw_discard_sectors = origin_limits->max_hw_discard_sectors;
3512
+
limits->discard_granularity = origin_limits->discard_granularity;
3513
+
limits->discard_alignment = origin_limits->discard_alignment;
3514
+
limits->discard_misaligned = origin_limits->discard_misaligned;
3443
3515
}
3444
3516
3445
3517
static void cache_io_hints(struct dm_target *ti, struct queue_limits *limits)
3446
3518
{
3447
3519
struct cache *cache = ti->private;
3448
3520
uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
3449
3521
3450
3522
/*
3451
3523
* If the system-determined stacked limits are compatible with the
3452
3524
* cache's blocksize (io_opt is a factor) do not override them.
3453
3525
*/
3454
3526
if (io_opt_sectors < cache->sectors_per_block ||
3455
3527
do_div(io_opt_sectors, cache->sectors_per_block)) {
3456
3528
blk_limits_io_min(limits, cache->sectors_per_block << SECTOR_SHIFT);
3457
3529
blk_limits_io_opt(limits, cache->sectors_per_block << SECTOR_SHIFT);
3458
3530
}
3531
+
3532
+
disable_passdown_if_not_supported(cache);
3459
3533
set_discard_limits(cache, limits);
3460
3534
}
3461
3535
3462
3536
/*----------------------------------------------------------------*/
3463
3537
3464
3538
static struct target_type cache_target = {
3465
3539
.name = "cache",
3466
-
.version = {2, 0, 0},
3540
+
.version = {2, 1, 0},
3467
3541
.module = THIS_MODULE,
3468
3542
.ctr = cache_ctr,
3469
3543
.dtr = cache_dtr,
3470
3544
.map = cache_map,
3471
3545
.end_io = cache_end_io,
3472
3546
.postsuspend = cache_postsuspend,
3473
3547
.preresume = cache_preresume,
3474
3548
.resume = cache_resume,
3475
3549
.status = cache_status,
3476
3550
.message = cache_message,