Skip to content

Commit a5664da

Browse files
snitmkergon
authored andcommitted
dm ioctl: make bio or request based device type immutable
Determine whether a mapped device is bio-based or request-based when loading its first (inactive) table and don't allow that to be changed later. This patch performs different device initialisation in each of the two cases. (We don't think it's necessary to add code to support changing between the two types.) Allowed md->type transitions: DM_TYPE_NONE to DM_TYPE_BIO_BASED DM_TYPE_NONE to DM_TYPE_REQUEST_BASED We now prevent table_load from replacing the inactive table with a conflicting type of table even after an explicit table_clear. Introduce 'type_lock' into the struct mapped_device to protect md->type and to prepare for the next patch that will change the queue initialization and allocate memory while md->type_lock is held. Signed-off-by: Mike Snitzer <snitzer@redhat.com> Acked-by: Kiyoshi Ueda <k-ueda@ct.jp.nec.com> Signed-off-by: Alasdair G Kergon <agk@redhat.com> drivers/md/dm-ioctl.c | 15 +++++++++++++++ drivers/md/dm.c | 37 ++++++++++++++++++++++++++++++------- drivers/md/dm.h | 5 +++++ include/linux/dm-ioctl.h | 4 ++-- 4 files changed, 52 insertions(+), 9 deletions(-)
1 parent 708e929 commit a5664da

4 files changed

Lines changed: 52 additions & 9 deletions

File tree

‎drivers/md/dm-ioctl.c‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,6 +1189,21 @@ static int table_load(struct dm_ioctl *param, size_t param_size)
11891189
goto out;
11901190
}
11911191

1192+
/* Protect md->type against concurrent table loads. */
1193+
dm_lock_md_type(md);
1194+
if (dm_get_md_type(md) == DM_TYPE_NONE)
1195+
/* Initial table load: acquire type of table. */
1196+
dm_set_md_type(md, dm_table_get_type(t));
1197+
else if (dm_get_md_type(md) != dm_table_get_type(t)) {
1198+
DMWARN("can't change device type after initial table load.");
1199+
dm_table_destroy(t);
1200+
dm_unlock_md_type(md);
1201+
r = -EINVAL;
1202+
goto out;
1203+
}
1204+
dm_unlock_md_type(md);
1205+
1206+
/* stage inactive table */
11921207
down_write(&_hash_lock);
11931208
hc = dm_get_mdptr(md);
11941209
if (!hc || hc->md != md) {

‎drivers/md/dm.c‎

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ struct mapped_device {
125125
unsigned long flags;
126126

127127
struct request_queue *queue;
128+
unsigned type;
129+
/* Protect type against concurrent access. */
130+
struct mutex type_lock;
131+
128132
struct gendisk *disk;
129133
char name[16];
130134

@@ -1877,8 +1881,10 @@ static struct mapped_device *alloc_dev(int minor)
18771881
if (r < 0)
18781882
goto bad_minor;
18791883

1884+
md->type = DM_TYPE_NONE;
18801885
init_rwsem(&md->io_lock);
18811886
mutex_init(&md->suspend_lock);
1887+
mutex_init(&md->type_lock);
18821888
spin_lock_init(&md->deferred_lock);
18831889
spin_lock_init(&md->barrier_error_lock);
18841890
rwlock_init(&md->map_lock);
@@ -2130,6 +2136,30 @@ int dm_create(int minor, struct mapped_device **result)
21302136
return 0;
21312137
}
21322138

2139+
/*
2140+
* Functions to manage md->type.
2141+
* All are required to hold md->type_lock.
2142+
*/
2143+
void dm_lock_md_type(struct mapped_device *md)
2144+
{
2145+
mutex_lock(&md->type_lock);
2146+
}
2147+
2148+
void dm_unlock_md_type(struct mapped_device *md)
2149+
{
2150+
mutex_unlock(&md->type_lock);
2151+
}
2152+
2153+
void dm_set_md_type(struct mapped_device *md, unsigned type)
2154+
{
2155+
md->type = type;
2156+
}
2157+
2158+
unsigned dm_get_md_type(struct mapped_device *md)
2159+
{
2160+
return md->type;
2161+
}
2162+
21332163
static struct mapped_device *dm_find_md(dev_t dev)
21342164
{
21352165
struct mapped_device *md;
@@ -2440,13 +2470,6 @@ struct dm_table *dm_swap_table(struct mapped_device *md, struct dm_table *table)
24402470
goto out;
24412471
}
24422472

2443-
/* cannot change the device type, once a table is bound */
2444-
if (md->map &&
2445-
(dm_table_get_type(md->map) != dm_table_get_type(table))) {
2446-
DMWARN("can't change the device type after a table is bound");
2447-
goto out;
2448-
}
2449-
24502473
map = __bind(md, table, &limits);
24512474

24522475
out:

‎drivers/md/dm.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ int dm_table_alloc_md_mempools(struct dm_table *t);
6666
void dm_table_free_md_mempools(struct dm_table *t);
6767
struct dm_md_mempools *dm_table_get_md_mempools(struct dm_table *t);
6868

69+
void dm_lock_md_type(struct mapped_device *md);
70+
void dm_unlock_md_type(struct mapped_device *md);
71+
void dm_set_md_type(struct mapped_device *md, unsigned type);
72+
unsigned dm_get_md_type(struct mapped_device *md);
73+
6974
/*
7075
* To check the return value from dm_table_find_target().
7176
*/

‎include/linux/dm-ioctl.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,9 @@ enum {
266266
#define DM_DEV_SET_GEOMETRY _IOWR(DM_IOCTL, DM_DEV_SET_GEOMETRY_CMD, struct dm_ioctl)
267267

268268
#define DM_VERSION_MAJOR 4
269-
#define DM_VERSION_MINOR 17
269+
#define DM_VERSION_MINOR 18
270270
#define DM_VERSION_PATCHLEVEL 0
271-
#define DM_VERSION_EXTRA "-ioctl (2010-03-05)"
271+
#define DM_VERSION_EXTRA "-ioctl (2010-06-29)"
272272

273273
/* Status bits */
274274
#define DM_READONLY_FLAG (1 << 0) /* In/Out */

0 commit comments

Comments
 (0)