Skip to content

Commit 5ae89a8

Browse files
snitmkergon
authored andcommitted
dm: linear support discard
Allow discards to be passed through to linear mappings if at least one underlying device supports it. Discards will be forwarded only to devices that support them. A target that supports discards should set num_discard_requests to indicate how many times each discard request must be submitted to it. Verify table's underlying devices support discards prior to setting the associated DM device as capable of discards (via QUEUE_FLAG_DISCARD). Signed-off-by: Mike Snitzer <snitzer@redhat.com> Signed-off-by: Mikulas Patocka <mpatocka@redhat.com> Reviewed-by: Joe Thornber <thornber@redhat.com> Signed-off-by: Alasdair G Kergon <agk@redhat.com>
1 parent 5ebaee6 commit 5ae89a8

5 files changed

Lines changed: 105 additions & 12 deletions

File tree

‎drivers/md/dm-linear.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ static int linear_ctr(struct dm_target *ti, unsigned int argc, char **argv)
5353
}
5454

5555
ti->num_flush_requests = 1;
56+
ti->num_discard_requests = 1;
5657
ti->private = lc;
5758
return 0;
5859

‎drivers/md/dm-table.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ struct dm_table {
5454
sector_t *highs;
5555
struct dm_target *targets;
5656

57+
unsigned discards_supported:1;
58+
5759
/*
5860
* Indicates the rw permissions for the new logical
5961
* device. This should be a combination of FMODE_READ
@@ -203,6 +205,7 @@ int dm_table_create(struct dm_table **result, fmode_t mode,
203205

204206
INIT_LIST_HEAD(&t->devices);
205207
atomic_set(&t->holders, 0);
208+
t->discards_supported = 1;
206209

207210
if (!num_targets)
208211
num_targets = KEYS_PER_NODE;
@@ -770,6 +773,9 @@ int dm_table_add_target(struct dm_table *t, const char *type,
770773

771774
t->highs[t->num_targets++] = tgt->begin + tgt->len - 1;
772775

776+
if (!tgt->num_discard_requests)
777+
t->discards_supported = 0;
778+
773779
return 0;
774780

775781
bad:
@@ -1135,6 +1141,11 @@ void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
11351141
else
11361142
queue_flag_set_unlocked(QUEUE_FLAG_CLUSTER, q);
11371143

1144+
if (!dm_table_supports_discards(t))
1145+
queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, q);
1146+
else
1147+
queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
1148+
11381149
dm_table_set_integrity(t);
11391150

11401151
/*
@@ -1281,6 +1292,39 @@ struct mapped_device *dm_table_get_md(struct dm_table *t)
12811292
return t->md;
12821293
}
12831294

1295+
static int device_discard_capable(struct dm_target *ti, struct dm_dev *dev,
1296+
sector_t start, sector_t len, void *data)
1297+
{
1298+
struct request_queue *q = bdev_get_queue(dev->bdev);
1299+
1300+
return q && blk_queue_discard(q);
1301+
}
1302+
1303+
bool dm_table_supports_discards(struct dm_table *t)
1304+
{
1305+
struct dm_target *ti;
1306+
unsigned i = 0;
1307+
1308+
if (!t->discards_supported)
1309+
return 0;
1310+
1311+
/*
1312+
* Ensure that at least one underlying device supports discards.
1313+
* t->devices includes internal dm devices such as mirror logs
1314+
* so we need to use iterate_devices here, which targets
1315+
* supporting discard must provide.
1316+
*/
1317+
while (i < dm_table_get_num_targets(t)) {
1318+
ti = dm_table_get_target(t, i++);
1319+
1320+
if (ti->type->iterate_devices &&
1321+
ti->type->iterate_devices(ti, device_discard_capable, NULL))
1322+
return 1;
1323+
}
1324+
1325+
return 0;
1326+
}
1327+
12841328
EXPORT_SYMBOL(dm_vcalloc);
12851329
EXPORT_SYMBOL(dm_get_device);
12861330
EXPORT_SYMBOL(dm_put_device);

‎drivers/md/dm.c‎

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,53 @@ static int __clone_and_map_empty_barrier(struct clone_info *ci)
12121212
return 0;
12131213
}
12141214

1215+
/*
1216+
* Perform all io with a single clone.
1217+
*/
1218+
static void __clone_and_map_simple(struct clone_info *ci, struct dm_target *ti)
1219+
{
1220+
struct bio *clone, *bio = ci->bio;
1221+
struct dm_target_io *tio;
1222+
1223+
tio = alloc_tio(ci, ti);
1224+
clone = clone_bio(bio, ci->sector, ci->idx,
1225+
bio->bi_vcnt - ci->idx, ci->sector_count,
1226+
ci->md->bs);
1227+
__map_bio(ti, clone, tio);
1228+
ci->sector_count = 0;
1229+
}
1230+
1231+
static int __clone_and_map_discard(struct clone_info *ci)
1232+
{
1233+
struct dm_target *ti;
1234+
sector_t max;
1235+
1236+
ti = dm_table_find_target(ci->map, ci->sector);
1237+
if (!dm_target_is_valid(ti))
1238+
return -EIO;
1239+
1240+
/*
1241+
* Even though the device advertised discard support,
1242+
* reconfiguration might have changed that since the
1243+
* check was performed.
1244+
*/
1245+
1246+
if (!ti->num_discard_requests)
1247+
return -EOPNOTSUPP;
1248+
1249+
max = max_io_len(ci->md, ci->sector, ti);
1250+
1251+
if (ci->sector_count > max)
1252+
/*
1253+
* FIXME: Handle a discard that spans two or more targets.
1254+
*/
1255+
return -EOPNOTSUPP;
1256+
1257+
__clone_and_map_simple(ci, ti);
1258+
1259+
return 0;
1260+
}
1261+
12151262
static int __clone_and_map(struct clone_info *ci)
12161263
{
12171264
struct bio *clone, *bio = ci->bio;
@@ -1222,27 +1269,21 @@ static int __clone_and_map(struct clone_info *ci)
12221269
if (unlikely(bio_empty_barrier(bio)))
12231270
return __clone_and_map_empty_barrier(ci);
12241271

1272+
if (unlikely(bio->bi_rw & REQ_DISCARD))
1273+
return __clone_and_map_discard(ci);
1274+
12251275
ti = dm_table_find_target(ci->map, ci->sector);
12261276
if (!dm_target_is_valid(ti))
12271277
return -EIO;
12281278

12291279
max = max_io_len(ci->md, ci->sector, ti);
12301280

1231-
/*
1232-
* Allocate a target io object.
1233-
*/
1234-
tio = alloc_tio(ci, ti);
1235-
12361281
if (ci->sector_count <= max) {
12371282
/*
12381283
* Optimise for the simple case where we can do all of
12391284
* the remaining io with a single clone.
12401285
*/
1241-
clone = clone_bio(bio, ci->sector, ci->idx,
1242-
bio->bi_vcnt - ci->idx, ci->sector_count,
1243-
ci->md->bs);
1244-
__map_bio(ti, clone, tio);
1245-
ci->sector_count = 0;
1286+
__clone_and_map_simple(ci, ti);
12461287

12471288
} else if (to_sector(bio->bi_io_vec[ci->idx].bv_len) <= max) {
12481289
/*
@@ -1263,6 +1304,7 @@ static int __clone_and_map(struct clone_info *ci)
12631304
len += bv_len;
12641305
}
12651306

1307+
tio = alloc_tio(ci, ti);
12661308
clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
12671309
ci->md->bs);
12681310
__map_bio(ti, clone, tio);
@@ -1286,12 +1328,11 @@ static int __clone_and_map(struct clone_info *ci)
12861328
return -EIO;
12871329

12881330
max = max_io_len(ci->md, ci->sector, ti);
1289-
1290-
tio = alloc_tio(ci, ti);
12911331
}
12921332

12931333
len = min(remaining, max);
12941334

1335+
tio = alloc_tio(ci, ti);
12951336
clone = split_bvec(bio, ci->sector, ci->idx,
12961337
bv->bv_offset + offset, len,
12971338
ci->md->bs);

‎drivers/md/dm.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ int dm_table_any_congested(struct dm_table *t, int bdi_bits);
6161
int dm_table_any_busy_target(struct dm_table *t);
6262
unsigned dm_table_get_type(struct dm_table *t);
6363
bool dm_table_request_based(struct dm_table *t);
64+
bool dm_table_supports_discards(struct dm_table *t);
6465
int dm_table_alloc_md_mempools(struct dm_table *t);
6566
void dm_table_free_md_mempools(struct dm_table *t);
6667
struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t);

‎include/linux/device-mapper.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ struct dm_target {
180180
*/
181181
unsigned num_flush_requests;
182182

183+
/*
184+
* The number of discard requests that will be submitted to the
185+
* target. map_info->request_nr is used just like num_flush_requests.
186+
*/
187+
unsigned num_discard_requests;
188+
183189
/* target specific data */
184190
void *private;
185191

0 commit comments

Comments
 (0)