Skip to content

Commit a4c8dd9

Browse files
lostjefflesnitm
authored andcommitted
dm table: fix iterate_devices based device capability checks
According to the definition of dm_iterate_devices_fn: * This function must iterate through each section of device used by the * target until it encounters a non-zero return code, which it then returns. * Returns zero if no callout returned non-zero. For some target type (e.g. dm-stripe), one call of iterate_devices() may iterate multiple underlying devices internally, in which case a non-zero return code returned by iterate_devices_callout_fn will stop the iteration in advance. No iterate_devices_callout_fn should return non-zero unless device iteration should stop. Rename dm_table_requires_stable_pages() to dm_table_any_dev_attr() and elevate it for reuse to stop iterating (and return non-zero) on the first device that causes iterate_devices_callout_fn to return non-zero. Use dm_table_any_dev_attr() to properly iterate through devices. Rename device_is_nonrot() to device_is_rotational() and invert logic accordingly to fix improper disposition. Fixes: c3c4555 ("dm table: clear add_random unless all devices have it set") Fixes: 4693c96 ("dm table: propagate non rotational flag") Cc: [email protected] Signed-off-by: Jeffle Xu <[email protected]> Signed-off-by: Mike Snitzer <[email protected]>
1 parent 054bee1 commit a4c8dd9

1 file changed

Lines changed: 51 additions & 46 deletions

File tree

drivers/md/dm-table.c

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,6 +1295,46 @@ struct dm_target *dm_table_find_target(struct dm_table *t, sector_t sector)
12951295
return &t->targets[(KEYS_PER_NODE * n) + k];
12961296
}
12971297

1298+
/*
1299+
* type->iterate_devices() should be called when the sanity check needs to
1300+
* iterate and check all underlying data devices. iterate_devices() will
1301+
* iterate all underlying data devices until it encounters a non-zero return
1302+
* code, returned by whether the input iterate_devices_callout_fn, or
1303+
* iterate_devices() itself internally.
1304+
*
1305+
* For some target type (e.g. dm-stripe), one call of iterate_devices() may
1306+
* iterate multiple underlying devices internally, in which case a non-zero
1307+
* return code returned by iterate_devices_callout_fn will stop the iteration
1308+
* in advance.
1309+
*
1310+
* Cases requiring _any_ underlying device supporting some kind of attribute,
1311+
* should use the iteration structure like dm_table_any_dev_attr(), or call
1312+
* it directly. @func should handle semantics of positive examples, e.g.
1313+
* capable of something.
1314+
*
1315+
* Cases requiring _all_ underlying devices supporting some kind of attribute,
1316+
* should use the iteration structure like dm_table_supports_nowait() or
1317+
* dm_table_supports_discards(). Or introduce dm_table_all_devs_attr() that
1318+
* uses an @anti_func that handle semantics of counter examples, e.g. not
1319+
* capable of something. So: return !dm_table_any_dev_attr(t, anti_func);
1320+
*/
1321+
static bool dm_table_any_dev_attr(struct dm_table *t,
1322+
iterate_devices_callout_fn func)
1323+
{
1324+
struct dm_target *ti;
1325+
unsigned int i;
1326+
1327+
for (i = 0; i < dm_table_get_num_targets(t); i++) {
1328+
ti = dm_table_get_target(t, i);
1329+
1330+
if (ti->type->iterate_devices &&
1331+
ti->type->iterate_devices(ti, func, NULL))
1332+
return true;
1333+
}
1334+
1335+
return false;
1336+
}
1337+
12981338
static int count_device(struct dm_target *ti, struct dm_dev *dev,
12991339
sector_t start, sector_t len, void *data)
13001340
{
@@ -1595,12 +1635,12 @@ static int dm_table_supports_dax_write_cache(struct dm_table *t)
15951635
return false;
15961636
}
15971637

1598-
static int device_is_nonrot(struct dm_target *ti, struct dm_dev *dev,
1599-
sector_t start, sector_t len, void *data)
1638+
static int device_is_rotational(struct dm_target *ti, struct dm_dev *dev,
1639+
sector_t start, sector_t len, void *data)
16001640
{
16011641
struct request_queue *q = bdev_get_queue(dev->bdev);
16021642

1603-
return q && blk_queue_nonrot(q);
1643+
return q && !blk_queue_nonrot(q);
16041644
}
16051645

16061646
static int device_is_not_random(struct dm_target *ti, struct dm_dev *dev,
@@ -1611,23 +1651,6 @@ static int device_is_not_random(struct dm_target *ti, struct dm_dev *dev,
16111651
return q && !blk_queue_add_random(q);
16121652
}
16131653

1614-
static bool dm_table_all_devices_attribute(struct dm_table *t,
1615-
iterate_devices_callout_fn func)
1616-
{
1617-
struct dm_target *ti;
1618-
unsigned i;
1619-
1620-
for (i = 0; i < dm_table_get_num_targets(t); i++) {
1621-
ti = dm_table_get_target(t, i);
1622-
1623-
if (!ti->type->iterate_devices ||
1624-
!ti->type->iterate_devices(ti, func, NULL))
1625-
return false;
1626-
}
1627-
1628-
return true;
1629-
}
1630-
16311654
static int device_not_write_same_capable(struct dm_target *ti, struct dm_dev *dev,
16321655
sector_t start, sector_t len, void *data)
16331656
{
@@ -1779,27 +1802,6 @@ static int device_requires_stable_pages(struct dm_target *ti,
17791802
return q && blk_queue_stable_writes(q);
17801803
}
17811804

1782-
/*
1783-
* If any underlying device requires stable pages, a table must require
1784-
* them as well. Only targets that support iterate_devices are considered:
1785-
* don't want error, zero, etc to require stable pages.
1786-
*/
1787-
static bool dm_table_requires_stable_pages(struct dm_table *t)
1788-
{
1789-
struct dm_target *ti;
1790-
unsigned i;
1791-
1792-
for (i = 0; i < dm_table_get_num_targets(t); i++) {
1793-
ti = dm_table_get_target(t, i);
1794-
1795-
if (ti->type->iterate_devices &&
1796-
ti->type->iterate_devices(ti, device_requires_stable_pages, NULL))
1797-
return true;
1798-
}
1799-
1800-
return false;
1801-
}
1802-
18031805
void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
18041806
struct queue_limits *limits)
18051807
{
@@ -1849,10 +1851,10 @@ void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
18491851
dax_write_cache(t->md->dax_dev, true);
18501852

18511853
/* Ensure that all underlying devices are non-rotational. */
1852-
if (dm_table_all_devices_attribute(t, device_is_nonrot))
1853-
blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
1854-
else
1854+
if (dm_table_any_dev_attr(t, device_is_rotational))
18551855
blk_queue_flag_clear(QUEUE_FLAG_NONROT, q);
1856+
else
1857+
blk_queue_flag_set(QUEUE_FLAG_NONROT, q);
18561858

18571859
if (!dm_table_supports_write_same(t))
18581860
q->limits.max_write_same_sectors = 0;
@@ -1864,8 +1866,11 @@ void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
18641866
/*
18651867
* Some devices don't use blk_integrity but still want stable pages
18661868
* because they do their own checksumming.
1869+
* If any underlying device requires stable pages, a table must require
1870+
* them as well. Only targets that support iterate_devices are considered:
1871+
* don't want error, zero, etc to require stable pages.
18671872
*/
1868-
if (dm_table_requires_stable_pages(t))
1873+
if (dm_table_any_dev_attr(t, device_requires_stable_pages))
18691874
blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, q);
18701875
else
18711876
blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, q);
@@ -1876,7 +1881,7 @@ void dm_table_set_restrictions(struct dm_table *t, struct request_queue *q,
18761881
* Clear QUEUE_FLAG_ADD_RANDOM if any underlying device does not
18771882
* have it set.
18781883
*/
1879-
if (blk_queue_add_random(q) && dm_table_all_devices_attribute(t, device_is_not_random))
1884+
if (blk_queue_add_random(q) && dm_table_any_dev_attr(t, device_is_not_random))
18801885
blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, q);
18811886

18821887
/*

0 commit comments

Comments
 (0)