#include <linux/init.h>
#include <linux/module.h>
-#include <linux/mutex.h>
#include <linux/moduleparam.h>
#include <linux/blkpg.h>
#include <linux/bio.h>
#include <linux/mempool.h>
#include <linux/slab.h>
#include <linux/idr.h>
-#include <linux/hdreg.h>
-#include <linux/blktrace_api.h>
static const char *_name = DM_NAME;
static unsigned int major = 0;
static unsigned int _major = 0;
-static DEFINE_SPINLOCK(_minor_lock);
/*
* One of these is allocated per bio.
*/
return NULL;
}
-#define MINOR_ALLOCED ((void *)-1)
-
/*
* Bits for the md->flags field.
*/
#define DMF_BLOCK_IO 0
#define DMF_SUSPENDED 1
#define DMF_FROZEN 2
-#define DMF_FREEING 3
struct mapped_device {
struct rw_semaphore io_lock;
request_queue_t *queue;
struct gendisk *disk;
- char name[16];
void *interface_ptr;
*/
struct super_block *frozen_sb;
struct block_device *suspended_bdev;
-
- /* forced geometry settings */
- struct hd_geometry geometry;
};
#define MIN_IOS 256
{
struct mapped_device *md;
- spin_lock(&_minor_lock);
-
md = inode->i_bdev->bd_disk->private_data;
- if (!md)
- goto out;
-
- if (test_bit(DMF_FREEING, &md->flags)) {
- md = NULL;
- goto out;
- }
-
dm_get(md);
-
-out:
- spin_unlock(&_minor_lock);
-
- return md ? 0 : -ENXIO;
+ return 0;
}
static int dm_blk_close(struct inode *inode, struct file *file)
return 0;
}
-static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
-{
- struct mapped_device *md = bdev->bd_disk->private_data;
-
- return dm_get_geometry(md, geo);
-}
-
static inline struct dm_io *alloc_io(struct mapped_device *md)
{
return mempool_alloc(md->io_pool, GFP_NOIO);
return t;
}
-/*
- * Get the geometry associated with a dm device
- */
-int dm_get_geometry(struct mapped_device *md, struct hd_geometry *geo)
-{
- *geo = md->geometry;
-
- return 0;
-}
-
-/*
- * Set the geometry of a device.
- */
-int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
-{
- sector_t sz = (sector_t)geo->cylinders * geo->heads * geo->sectors;
-
- if (geo->start > sz) {
- DMWARN("Start sector is beyond the geometry limits.");
- return -EINVAL;
- }
-
- md->geometry = *geo;
-
- return 0;
-}
-
/*-----------------------------------------------------------------
* CRUD START:
* A more elegant soln is in the works that uses the queue
/* nudge anyone waiting on suspend queue */
wake_up(&io->md->wait);
- blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
-
bio_endio(io->bio, io->bio->bi_size, io->error);
free_io(io->md, io);
}
struct target_io *tio)
{
int r;
- sector_t sector;
/*
* Sanity checks.
* this io.
*/
atomic_inc(&tio->io->io_count);
- sector = clone->bi_sector;
r = ti->type->map(ti, clone, &tio->info);
- if (r > 0) {
+ if (r > 0)
/* the bio has been remapped so dispatch it */
-
- blk_add_trace_remap(bdev_get_queue(clone->bi_bdev), clone,
- tio->io->bio->bi_bdev->bd_dev, sector,
- clone->bi_sector);
-
generic_make_request(clone);
- }
else if (r < 0) {
/* error the io and bail out */
/*-----------------------------------------------------------------
* An IDR is used to keep track of allocated minor numbers.
*---------------------------------------------------------------*/
+static DECLARE_MUTEX(_minor_lock);
static DEFINE_IDR(_minor_idr);
static void free_minor(unsigned int minor)
{
- spin_lock(&_minor_lock);
+ down(&_minor_lock);
idr_remove(&_minor_idr, minor);
- spin_unlock(&_minor_lock);
+ up(&_minor_lock);
}
/*
if (minor >= (1 << MINORBITS))
return -EINVAL;
- r = idr_pre_get(&_minor_idr, GFP_KERNEL);
- if (!r)
- return -ENOMEM;
-
- spin_lock(&_minor_lock);
+ down(&_minor_lock);
if (idr_find(&_minor_idr, minor)) {
r = -EBUSY;
goto out;
}
- r = idr_get_new_above(&_minor_idr, MINOR_ALLOCED, minor, &m);
- if (r)
+ r = idr_pre_get(&_minor_idr, GFP_KERNEL);
+ if (!r) {
+ r = -ENOMEM;
goto out;
+ }
+
+ r = idr_get_new_above(&_minor_idr, md, minor, &m);
+ if (r) {
+ goto out;
+ }
if (m != minor) {
idr_remove(&_minor_idr, m);
}
out:
- spin_unlock(&_minor_lock);
+ up(&_minor_lock);
return r;
}
int r;
unsigned int m;
- r = idr_pre_get(&_minor_idr, GFP_KERNEL);
- if (!r)
- return -ENOMEM;
+ down(&_minor_lock);
- spin_lock(&_minor_lock);
+ r = idr_pre_get(&_minor_idr, GFP_KERNEL);
+ if (!r) {
+ r = -ENOMEM;
+ goto out;
+ }
- r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
+ r = idr_get_new(&_minor_idr, md, &m);
if (r) {
goto out;
}
*minor = m;
out:
- spin_unlock(&_minor_lock);
+ up(&_minor_lock);
return r;
}
{
int r;
struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
- void *old_md;
if (!md) {
DMWARN("unable to allocate device, out of memory.");
md->queue->unplug_fn = dm_unplug_all;
md->queue->issue_flush_fn = dm_flush_all;
- md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
+ md->io_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
+ mempool_free_slab, _io_cache);
if (!md->io_pool)
goto bad2;
- md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
+ md->tio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
+ mempool_free_slab, _tio_cache);
if (!md->tio_pool)
goto bad3;
if (!md->disk)
goto bad4;
- atomic_set(&md->pending, 0);
- init_waitqueue_head(&md->wait);
- init_waitqueue_head(&md->eventq);
-
md->disk->major = _major;
md->disk->first_minor = minor;
md->disk->fops = &dm_blk_dops;
md->disk->private_data = md;
sprintf(md->disk->disk_name, "dm-%d", minor);
add_disk(md->disk);
- format_dev_t(md->name, MKDEV(_major, minor));
- /* Populate the mapping, nobody knows we exist yet */
- spin_lock(&_minor_lock);
- old_md = idr_replace(&_minor_idr, md, minor);
- spin_unlock(&_minor_lock);
-
- BUG_ON(old_md != MINOR_ALLOCED);
+ atomic_set(&md->pending, 0);
+ init_waitqueue_head(&md->wait);
+ init_waitqueue_head(&md->eventq);
return md;
bad3:
mempool_destroy(md->io_pool);
bad2:
- blk_cleanup_queue(md->queue);
+ blk_put_queue(md->queue);
free_minor(minor);
bad1:
module_put(THIS_MODULE);
mempool_destroy(md->io_pool);
del_gendisk(md->disk);
free_minor(minor);
-
- spin_lock(&_minor_lock);
- md->disk->private_data = NULL;
- spin_unlock(&_minor_lock);
-
put_disk(md->disk);
- blk_cleanup_queue(md->queue);
+ blk_put_queue(md->queue);
module_put(THIS_MODULE);
kfree(md);
}
sector_t size;
size = dm_table_get_size(t);
-
- /*
- * Wipe any geometry if the size of the table changed.
- */
- if (size != get_capacity(md->disk))
- memset(&md->geometry, 0, sizeof(md->geometry));
-
__set_size(md, size);
if (size == 0)
return 0;
if (MAJOR(dev) != _major || minor >= (1 << MINORBITS))
return NULL;
- spin_lock(&_minor_lock);
+ down(&_minor_lock);
md = idr_find(&_minor_idr, minor);
- if (md && (md == MINOR_ALLOCED ||
- (dm_disk(md)->first_minor != minor) ||
- test_bit(DMF_FREEING, &md->flags))) {
+ if (!md || (dm_disk(md)->first_minor != minor))
md = NULL;
- goto out;
- }
-out:
- spin_unlock(&_minor_lock);
+ up(&_minor_lock);
return md;
}
return md;
}
-void *dm_get_mdptr(struct mapped_device *md)
+void *dm_get_mdptr(dev_t dev)
{
- return md->interface_ptr;
+ struct mapped_device *md;
+ void *mdptr = NULL;
+
+ md = dm_find_md(dev);
+ if (md)
+ mdptr = md->interface_ptr;
+ return mdptr;
}
void dm_set_mdptr(struct mapped_device *md, void *ptr)
void dm_put(struct mapped_device *md)
{
- struct dm_table *map;
-
- BUG_ON(test_bit(DMF_FREEING, &md->flags));
+ struct dm_table *map = dm_get_table(md);
- if (atomic_dec_and_lock(&md->holders, &_minor_lock)) {
- map = dm_get_table(md);
- idr_replace(&_minor_idr, MINOR_ALLOCED, dm_disk(md)->first_minor);
- set_bit(DMF_FREEING, &md->flags);
- spin_unlock(&_minor_lock);
+ if (atomic_dec_and_test(&md->holders)) {
if (!dm_suspended(md)) {
dm_table_presuspend_targets(map);
dm_table_postsuspend_targets(map);
}
__unbind(md);
- dm_table_put(map);
free_dev(md);
}
+
+ dm_table_put(map);
}
/*
static struct block_device_operations dm_blk_dops = {
.open = dm_blk_open,
.release = dm_blk_close,
- .getgeo = dm_blk_getgeo,
.owner = THIS_MODULE
};