struct mapped_device {
struct rw_semaphore lock;
+ rwlock_t map_lock;
atomic_t holders;
unsigned long flags;
return 0; /* deferred successfully */
}
+/*
+ * Everyone (including functions in this file), should use this
+ * function to access the md->map field, and make sure they call
+ * dm_table_put() when finished.
+ */
+struct dm_table *dm_get_table(struct mapped_device *md)
+{
+ struct dm_table *t;
+
+ read_lock(&md->map_lock);
+ t = md->map;
+ if (t)
+ dm_table_get(t);
+ read_unlock(&md->map_lock);
+
+ return t;
+}
+
/*-----------------------------------------------------------------
* CRUD START:
* A more elegant soln is in the works that uses the queue
struct clone_info {
struct mapped_device *md;
+ struct dm_table *map;
struct bio *bio;
struct dm_io *io;
sector_t sector;
static void __clone_and_map(struct clone_info *ci)
{
struct bio *clone, *bio = ci->bio;
- struct dm_target *ti = dm_table_find_target(ci->md->map, ci->sector);
+ struct dm_target *ti = dm_table_find_target(ci->map, ci->sector);
sector_t len = 0, max = max_io_len(ci->md, ci->sector, ti);
struct target_io *tio;
ci->sector += max;
ci->sector_count -= max;
- ti = dm_table_find_target(ci->md->map, ci->sector);
+ ti = dm_table_find_target(ci->map, ci->sector);
len = to_sector(bv->bv_len) - max;
clone = split_bvec(bio, ci->sector, ci->idx,
struct clone_info ci;
ci.md = md;
+ ci.map = dm_get_table(md);
ci.bio = bio;
ci.io = alloc_io(md);
ci.io->error = 0;
/* drop the extra reference count */
dec_pending(ci.io, 0);
+ dm_table_put(ci.map);
}
/*-----------------------------------------------------------------
* CRUD END
return 0;
}
+static int dm_any_congested(void *congested_data, int bdi_bits)
+{
+ int r;
+ struct mapped_device *md = (struct mapped_device *) congested_data;
+ struct dm_table *map = dm_get_table(md);
+
+ if (!map || test_bit(DMF_BLOCK_IO, &md->flags))
+ /* FIXME: shouldn't suspended count a congested ? */
+ r = bdi_bits;
+ else
+ r = dm_table_any_congested(map, bdi_bits);
+
+ dm_table_put(map);
+ return r;
+}
+
/*-----------------------------------------------------------------
* A bitset is used to keep track of allocated minor numbers.
*---------------------------------------------------------------*/
memset(md, 0, sizeof(*md));
init_rwsem(&md->lock);
+ rwlock_init(&md->map_lock);
atomic_set(&md->holders, 1);
md->queue = blk_alloc_queue(GFP_KERNEL);
goto bad1;
md->queue->queuedata = md;
+ md->queue->backing_dev_info.congested_fn = dm_any_congested;
+ md->queue->backing_dev_info.congested_data = md;
blk_queue_make_request(md->queue, dm_request);
md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
if (size == 0)
return 0;
+ write_lock(&md->map_lock);
md->map = t;
- dm_table_event_callback(md->map, event_callback, md);
+ write_unlock(&md->map_lock);
dm_table_get(t);
+ dm_table_event_callback(md->map, event_callback, md);
dm_table_set_restrictions(t, q);
return 0;
}
static void __unbind(struct mapped_device *md)
{
- if (!md->map)
+ struct dm_table *map = md->map;
+
+ if (!map)
return;
- dm_table_event_callback(md->map, NULL, NULL);
- dm_table_put(md->map);
+ dm_table_event_callback(map, NULL, NULL);
+ write_lock(&md->map_lock);
md->map = NULL;
+ write_unlock(&md->map_lock);
+ dm_table_put(map);
}
/*
void dm_put(struct mapped_device *md)
{
+ struct dm_table *map = dm_get_table(md);
+
if (atomic_dec_and_test(&md->holders)) {
- if (!test_bit(DMF_SUSPENDED, &md->flags) && md->map)
- dm_table_suspend_targets(md->map);
+ if (!test_bit(DMF_SUSPENDED, &md->flags) && map)
+ dm_table_suspend_targets(map);
__unbind(md);
free_dev(md);
}
+
+ dm_table_put(map);
}
/*
*/
int dm_suspend(struct mapped_device *md)
{
+ struct dm_table *map;
DECLARE_WAITQUEUE(wait, current);
down_write(&md->lock);
down_write(&md->lock);
remove_wait_queue(&md->wait, &wait);
set_bit(DMF_SUSPENDED, &md->flags);
- if (md->map)
- dm_table_suspend_targets(md->map);
+
+ map = dm_get_table(md);
+ if (map)
+ dm_table_suspend_targets(map);
+ dm_table_put(map);
up_write(&md->lock);
return 0;
int dm_resume(struct mapped_device *md)
{
struct bio *def;
+ struct dm_table *map = dm_get_table(md);
down_write(&md->lock);
- if (!md->map ||
+ if (!map ||
!test_bit(DMF_SUSPENDED, &md->flags) ||
- !dm_table_get_size(md->map)) {
+ !dm_table_get_size(map)) {
up_write(&md->lock);
+ dm_table_put(map);
return -EINVAL;
}
- dm_table_resume_targets(md->map);
+ dm_table_resume_targets(map);
clear_bit(DMF_SUSPENDED, &md->flags);
clear_bit(DMF_BLOCK_IO, &md->flags);
def = bio_list_get(&md->deferred);
__flush_deferred_io(md, def);
up_write(&md->lock);
+ dm_table_put(map);
blk_run_queues();
return md->disk;
}
-struct dm_table *dm_get_table(struct mapped_device *md)
-{
- struct dm_table *t;
-
- down_read(&md->lock);
- t = md->map;
- if (t)
- dm_table_get(t);
- up_read(&md->lock);
-
- return t;
-}
-
int dm_suspended(struct mapped_device *md)
{
return test_bit(DMF_SUSPENDED, &md->flags);