vfs: export filemap_fdatawrite_range()
[safe/jmp/linux-2.6] / drivers / md / dm.c
index 6982f86..372369b 100644 (file)
@@ -7,6 +7,7 @@
 
 #include "dm.h"
 #include "dm-bio-list.h"
+#include "dm-uevent.h"
 
 #include <linux/init.h>
 #include <linux/module.h>
@@ -20,6 +21,9 @@
 #include <linux/idr.h>
 #include <linux/hdreg.h>
 #include <linux/blktrace_api.h>
+#include <linux/smp_lock.h>
+
+#define DM_MSG_PREFIX "core"
 
 static const char *_name = DM_NAME;
 
@@ -42,7 +46,7 @@ struct dm_io {
  * One of these is allocated per target within a bio.  Hopefully
  * this will be simplified out one day.
  */
-struct target_io {
+struct dm_target_io {
        struct dm_io *io;
        struct dm_target *ti;
        union map_info info;
@@ -51,7 +55,7 @@ struct target_io {
 union map_info *dm_get_mapinfo(struct bio *bio)
 {
        if (bio && bio->bi_private)
-               return &((struct target_io *)bio->bi_private)->info;
+               return &((struct dm_target_io *)bio->bi_private)->info;
        return NULL;
 }
 
@@ -65,17 +69,32 @@ union map_info *dm_get_mapinfo(struct bio *bio)
 #define DMF_FROZEN 2
 #define DMF_FREEING 3
 #define DMF_DELETING 4
+#define DMF_NOFLUSH_SUSPENDING 5
+
+/*
+ * Work processed by per-device workqueue.
+ */
+struct dm_wq_req {
+       enum {
+               DM_WQ_FLUSH_ALL,
+               DM_WQ_FLUSH_DEFERRED,
+       } type;
+       struct work_struct work;
+       struct mapped_device *md;
+       void *context;
+};
 
 struct mapped_device {
        struct rw_semaphore io_lock;
-       struct semaphore suspend_lock;
+       struct mutex suspend_lock;
+       spinlock_t pushback_lock;
        rwlock_t map_lock;
        atomic_t holders;
        atomic_t open_count;
 
        unsigned long flags;
 
-       request_queue_t *queue;
+       struct request_queue *queue;
        struct gendisk *disk;
        char name[16];
 
@@ -86,7 +105,13 @@ struct mapped_device {
         */
        atomic_t pending;
        wait_queue_head_t wait;
-       struct bio_list deferred;
+       struct bio_list deferred;
+       struct bio_list pushback;
+
+       /*
+        * Processing queue (flush/barriers)
+        */
+       struct workqueue_struct *wq;
 
        /*
         * The current mapping.
@@ -99,11 +124,16 @@ struct mapped_device {
        mempool_t *io_pool;
        mempool_t *tio_pool;
 
+       struct bio_set *bs;
+
        /*
         * Event handling.
         */
        atomic_t event_nr;
        wait_queue_head_t eventq;
+       atomic_t uevent_seq;
+       struct list_head uevent_list;
+       spinlock_t uevent_lock; /* Protect access to uevent_list */
 
        /*
         * freeze/thaw support require holding onto a super block
@@ -116,38 +146,38 @@ struct mapped_device {
 };
 
 #define MIN_IOS 256
-static kmem_cache_t *_io_cache;
-static kmem_cache_t *_tio_cache;
-
-static struct bio_set *dm_set;
+static struct kmem_cache *_io_cache;
+static struct kmem_cache *_tio_cache;
 
 static int __init local_init(void)
 {
        int r;
 
-       dm_set = bioset_create(16, 16, 4);
-       if (!dm_set)
-               return -ENOMEM;
-
        /* allocate a slab for the dm_ios */
-       _io_cache = kmem_cache_create("dm_io",
-                                     sizeof(struct dm_io), 0, 0, NULL, NULL);
+       _io_cache = KMEM_CACHE(dm_io, 0);
        if (!_io_cache)
                return -ENOMEM;
 
        /* allocate a slab for the target ios */
-       _tio_cache = kmem_cache_create("dm_tio", sizeof(struct target_io),
-                                      0, 0, NULL, NULL);
+       _tio_cache = KMEM_CACHE(dm_target_io, 0);
        if (!_tio_cache) {
                kmem_cache_destroy(_io_cache);
                return -ENOMEM;
        }
 
+       r = dm_uevent_init();
+       if (r) {
+               kmem_cache_destroy(_tio_cache);
+               kmem_cache_destroy(_io_cache);
+               return r;
+       }
+
        _major = major;
        r = register_blkdev(_major, _name);
        if (r < 0) {
                kmem_cache_destroy(_tio_cache);
                kmem_cache_destroy(_io_cache);
+               dm_uevent_exit();
                return r;
        }
 
@@ -161,30 +191,29 @@ static void local_exit(void)
 {
        kmem_cache_destroy(_tio_cache);
        kmem_cache_destroy(_io_cache);
-
-       bioset_free(dm_set);
-
-       if (unregister_blkdev(_major, _name) < 0)
-               DMERR("devfs_unregister_blkdev failed");
+       unregister_blkdev(_major, _name);
+       dm_uevent_exit();
 
        _major = 0;
 
        DMINFO("cleaned up");
 }
 
-int (*_inits[])(void) __initdata = {
+static int (*_inits[])(void) __initdata = {
        local_init,
        dm_target_init,
        dm_linear_init,
        dm_stripe_init,
+       dm_kcopyd_init,
        dm_interface_init,
 };
 
-void (*_exits[])(void) = {
+static void (*_exits[])(void) = {
        local_exit,
        dm_target_exit,
        dm_linear_exit,
        dm_stripe_exit,
+       dm_kcopyd_exit,
        dm_interface_exit,
 };
 
@@ -286,22 +315,61 @@ static int dm_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
        return dm_get_geometry(md, geo);
 }
 
-static inline struct dm_io *alloc_io(struct mapped_device *md)
+static int dm_blk_ioctl(struct inode *inode, struct file *file,
+                       unsigned int cmd, unsigned long arg)
+{
+       struct mapped_device *md;
+       struct dm_table *map;
+       struct dm_target *tgt;
+       int r = -ENOTTY;
+
+       /* We don't really need this lock, but we do need 'inode'. */
+       unlock_kernel();
+
+       md = inode->i_bdev->bd_disk->private_data;
+
+       map = dm_get_table(md);
+
+       if (!map || !dm_table_get_size(map))
+               goto out;
+
+       /* We only support devices that have a single target */
+       if (dm_table_get_num_targets(map) != 1)
+               goto out;
+
+       tgt = dm_table_get_target(map, 0);
+
+       if (dm_suspended(md)) {
+               r = -EAGAIN;
+               goto out;
+       }
+
+       if (tgt->type->ioctl)
+               r = tgt->type->ioctl(tgt, inode, file, cmd, arg);
+
+out:
+       dm_table_put(map);
+
+       lock_kernel();
+       return r;
+}
+
+static struct dm_io *alloc_io(struct mapped_device *md)
 {
        return mempool_alloc(md->io_pool, GFP_NOIO);
 }
 
-static inline void free_io(struct mapped_device *md, struct dm_io *io)
+static void free_io(struct mapped_device *md, struct dm_io *io)
 {
        mempool_free(io, md->io_pool);
 }
 
-static inline struct target_io *alloc_tio(struct mapped_device *md)
+static struct dm_target_io *alloc_tio(struct mapped_device *md)
 {
        return mempool_alloc(md->tio_pool, GFP_NOIO);
 }
 
-static inline void free_tio(struct mapped_device *md, struct target_io *tio)
+static void free_tio(struct mapped_device *md, struct dm_target_io *tio)
 {
        mempool_free(tio, md->tio_pool);
 }
@@ -408,54 +476,90 @@ int dm_set_geometry(struct mapped_device *md, struct hd_geometry *geo)
  *   you this clearly demarcated crap.
  *---------------------------------------------------------------*/
 
+static int __noflush_suspending(struct mapped_device *md)
+{
+       return test_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
+}
+
 /*
  * Decrements the number of outstanding ios that a bio has been
  * cloned into, completing the original io if necc.
  */
 static void dec_pending(struct dm_io *io, int error)
 {
-       if (error)
+       unsigned long flags;
+
+       /* Push-back supersedes any I/O errors */
+       if (error && !(io->error > 0 && __noflush_suspending(io->md)))
                io->error = error;
 
        if (atomic_dec_and_test(&io->io_count)) {
+               if (io->error == DM_ENDIO_REQUEUE) {
+                       /*
+                        * Target requested pushing back the I/O.
+                        * This must be handled before the sleeper on
+                        * suspend queue merges the pushback list.
+                        */
+                       spin_lock_irqsave(&io->md->pushback_lock, flags);
+                       if (__noflush_suspending(io->md))
+                               bio_list_add(&io->md->pushback, io->bio);
+                       else
+                               /* noflush suspend was interrupted. */
+                               io->error = -EIO;
+                       spin_unlock_irqrestore(&io->md->pushback_lock, flags);
+               }
+
                if (end_io_acct(io))
                        /* nudge anyone waiting on suspend queue */
                        wake_up(&io->md->wait);
 
-               blk_add_trace_bio(io->md->queue, io->bio, BLK_TA_COMPLETE);
+               if (io->error != DM_ENDIO_REQUEUE) {
+                       blk_add_trace_bio(io->md->queue, io->bio,
+                                         BLK_TA_COMPLETE);
+
+                       bio_endio(io->bio, io->error);
+               }
 
-               bio_endio(io->bio, io->bio->bi_size, io->error);
                free_io(io->md, io);
        }
 }
 
-static int clone_endio(struct bio *bio, unsigned int done, int error)
+static void clone_endio(struct bio *bio, int error)
 {
        int r = 0;
-       struct target_io *tio = bio->bi_private;
-       struct dm_io *io = tio->io;
+       struct dm_target_io *tio = bio->bi_private;
+       struct mapped_device *md = tio->io->md;
        dm_endio_fn endio = tio->ti->type->end_io;
 
-       if (bio->bi_size)
-               return 1;
-
        if (!bio_flagged(bio, BIO_UPTODATE) && !error)
                error = -EIO;
 
        if (endio) {
                r = endio(tio->ti, bio, error, &tio->info);
-               if (r < 0)
+               if (r < 0 || r == DM_ENDIO_REQUEUE)
+                       /*
+                        * error and requeue request are handled
+                        * in dec_pending().
+                        */
                        error = r;
-
-               else if (r > 0)
-                       /* the target wants another shot at the io */
-                       return 1;
+               else if (r == DM_ENDIO_INCOMPLETE)
+                       /* The target will handle the io */
+                       return;
+               else if (r) {
+                       DMWARN("unimplemented target endio return value: %d", r);
+                       BUG();
+               }
        }
 
-       free_tio(io->md, tio);
-       dec_pending(io, error);
+       dec_pending(tio->io, error);
+
+       /*
+        * Store md for cleanup instead of tio which is about to get freed.
+        */
+       bio->bi_private = md->bs;
+
        bio_put(bio);
-       return r;
+       free_tio(md, tio);
 }
 
 static sector_t max_io_len(struct mapped_device *md,
@@ -479,10 +583,11 @@ static sector_t max_io_len(struct mapped_device *md,
 }
 
 static void __map_bio(struct dm_target *ti, struct bio *clone,
-                     struct target_io *tio)
+                     struct dm_target_io *tio)
 {
        int r;
        sector_t sector;
+       struct mapped_device *md;
 
        /*
         * Sanity checks.
@@ -500,22 +605,27 @@ static void __map_bio(struct dm_target *ti, struct bio *clone,
        atomic_inc(&tio->io->io_count);
        sector = clone->bi_sector;
        r = ti->type->map(ti, clone, &tio->info);
-       if (r > 0) {
+       if (r == DM_MAPIO_REMAPPED) {
                /* 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);
+                                   tio->io->bio->bi_bdev->bd_dev,
+                                   clone->bi_sector, sector);
 
                generic_make_request(clone);
-       }
-
-       else if (r < 0) {
-               /* error the io and bail out */
-               struct dm_io *io = tio->io;
-               free_tio(tio->io->md, tio);
-               dec_pending(io, r);
+       } else if (r < 0 || r == DM_MAPIO_REQUEUE) {
+               /* error the io and bail out, or requeue it if needed */
+               md = tio->io->md;
+               dec_pending(tio->io, r);
+               /*
+                * Store bio_set for cleanup.
+                */
+               clone->bi_private = md->bs;
                bio_put(clone);
+               free_tio(md, tio);
+       } else if (r) {
+               DMWARN("unimplemented target map return value: %d", r);
+               BUG();
        }
 }
 
@@ -531,7 +641,9 @@ struct clone_info {
 
 static void dm_bio_destructor(struct bio *bio)
 {
-       bio_free(bio, dm_set);
+       struct bio_set *bs = bio->bi_private;
+
+       bio_free(bio, bs);
 }
 
 /*
@@ -539,12 +651,12 @@ static void dm_bio_destructor(struct bio *bio)
  */
 static struct bio *split_bvec(struct bio *bio, sector_t sector,
                              unsigned short idx, unsigned int offset,
-                             unsigned int len)
+                             unsigned int len, struct bio_set *bs)
 {
        struct bio *clone;
        struct bio_vec *bv = bio->bi_io_vec + idx;
 
-       clone = bio_alloc_bioset(GFP_NOIO, 1, dm_set);
+       clone = bio_alloc_bioset(GFP_NOIO, 1, bs);
        clone->bi_destructor = dm_bio_destructor;
        *clone->bi_io_vec = *bv;
 
@@ -564,11 +676,13 @@ static struct bio *split_bvec(struct bio *bio, sector_t sector,
  */
 static struct bio *clone_bio(struct bio *bio, sector_t sector,
                             unsigned short idx, unsigned short bv_count,
-                            unsigned int len)
+                            unsigned int len, struct bio_set *bs)
 {
        struct bio *clone;
 
-       clone = bio_clone(bio, GFP_NOIO);
+       clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
+       __bio_clone(clone, bio);
+       clone->bi_destructor = dm_bio_destructor;
        clone->bi_sector = sector;
        clone->bi_idx = idx;
        clone->bi_vcnt = idx + bv_count;
@@ -578,12 +692,18 @@ static struct bio *clone_bio(struct bio *bio, sector_t sector,
        return clone;
 }
 
-static void __clone_and_map(struct clone_info *ci)
+static int __clone_and_map(struct clone_info *ci)
 {
        struct bio *clone, *bio = ci->bio;
-       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;
+       struct dm_target *ti;
+       sector_t len = 0, max;
+       struct dm_target_io *tio;
+
+       ti = dm_table_find_target(ci->map, ci->sector);
+       if (!dm_target_is_valid(ti))
+               return -EIO;
+
+       max = max_io_len(ci->md, ci->sector, ti);
 
        /*
         * Allocate a target io object.
@@ -599,7 +719,8 @@ static void __clone_and_map(struct clone_info *ci)
                 * the remaining io with a single clone.
                 */
                clone = clone_bio(bio, ci->sector, ci->idx,
-                                 bio->bi_vcnt - ci->idx, ci->sector_count);
+                                 bio->bi_vcnt - ci->idx, ci->sector_count,
+                                 ci->md->bs);
                __map_bio(ti, clone, tio);
                ci->sector_count = 0;
 
@@ -622,7 +743,8 @@ static void __clone_and_map(struct clone_info *ci)
                        len += bv_len;
                }
 
-               clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len);
+               clone = clone_bio(bio, ci->sector, ci->idx, i - ci->idx, len,
+                                 ci->md->bs);
                __map_bio(ti, clone, tio);
 
                ci->sector += len;
@@ -640,6 +762,9 @@ static void __clone_and_map(struct clone_info *ci)
                do {
                        if (offset) {
                                ti = dm_table_find_target(ci->map, ci->sector);
+                               if (!dm_target_is_valid(ti))
+                                       return -EIO;
+
                                max = max_io_len(ci->md, ci->sector, ti);
 
                                tio = alloc_tio(ci->md);
@@ -651,7 +776,8 @@ static void __clone_and_map(struct clone_info *ci)
                        len = min(remaining, max);
 
                        clone = split_bvec(bio, ci->sector, ci->idx,
-                                          bv->bv_offset + offset, len);
+                                          bv->bv_offset + offset, len,
+                                          ci->md->bs);
 
                        __map_bio(ti, clone, tio);
 
@@ -662,20 +788,21 @@ static void __clone_and_map(struct clone_info *ci)
 
                ci->idx++;
        }
+
+       return 0;
 }
 
 /*
  * Split the bio into several clones.
  */
-static void __split_bio(struct mapped_device *md, struct bio *bio)
+static int __split_bio(struct mapped_device *md, struct bio *bio)
 {
        struct clone_info ci;
+       int error = 0;
 
        ci.map = dm_get_table(md);
-       if (!ci.map) {
-               bio_io_error(bio, bio->bi_size);
-               return;
-       }
+       if (unlikely(!ci.map))
+               return -EIO;
 
        ci.md = md;
        ci.bio = bio;
@@ -689,12 +816,14 @@ static void __split_bio(struct mapped_device *md, struct bio *bio)
        ci.idx = bio->bi_idx;
 
        start_io_acct(ci.io);
-       while (ci.sector_count)
-               __clone_and_map(&ci);
+       while (ci.sector_count && !error)
+               error = __clone_and_map(&ci);
 
        /* drop the extra reference count */
-       dec_pending(ci.io, 0);
+       dec_pending(ci.io, error);
        dm_table_put(ci.map);
+
+       return 0;
 }
 /*-----------------------------------------------------------------
  * CRUD END
@@ -704,12 +833,21 @@ static void __split_bio(struct mapped_device *md, struct bio *bio)
  * The request function that just remaps the bio built up by
  * dm_merge_bvec.
  */
-static int dm_request(request_queue_t *q, struct bio *bio)
+static int dm_request(struct request_queue *q, struct bio *bio)
 {
-       int r;
+       int r = -EIO;
        int rw = bio_data_dir(bio);
        struct mapped_device *md = q->queuedata;
 
+       /*
+        * There is no use in forwarding any barrier request since we can't
+        * guarantee it is (or can be) handled by the targets correctly.
+        */
+       if (unlikely(bio_barrier(bio))) {
+               bio_endio(bio, -EOPNOTSUPP);
+               return 0;
+       }
+
        down_read(&md->io_lock);
 
        disk_stat_inc(dm_disk(md), ios[rw]);
@@ -722,18 +860,11 @@ static int dm_request(request_queue_t *q, struct bio *bio)
        while (test_bit(DMF_BLOCK_IO, &md->flags)) {
                up_read(&md->io_lock);
 
-               if (bio_rw(bio) == READA) {
-                       bio_io_error(bio, bio->bi_size);
-                       return 0;
-               }
-
-               r = queue_io(md, bio);
-               if (r < 0) {
-                       bio_io_error(bio, bio->bi_size);
-                       return 0;
+               if (bio_rw(bio) != READA)
+                       r = queue_io(md, bio);
 
-               } else if (r == 0)
-                       return 0;       /* deferred successfully */
+               if (r <= 0)
+                       goto out_req;
 
                /*
                 * We're in a while loop, because someone could suspend
@@ -742,27 +873,17 @@ static int dm_request(request_queue_t *q, struct bio *bio)
                down_read(&md->io_lock);
        }
 
-       __split_bio(md, bio);
+       r = __split_bio(md, bio);
        up_read(&md->io_lock);
-       return 0;
-}
 
-static int dm_flush_all(request_queue_t *q, struct gendisk *disk,
-                       sector_t *error_sector)
-{
-       struct mapped_device *md = q->queuedata;
-       struct dm_table *map = dm_get_table(md);
-       int ret = -ENXIO;
-
-       if (map) {
-               ret = dm_table_flush_all(map);
-               dm_table_put(map);
-       }
+out_req:
+       if (r < 0)
+               bio_io_error(bio);
 
-       return ret;
+       return 0;
 }
 
-static void dm_unplug_all(request_queue_t *q)
+static void dm_unplug_all(struct request_queue *q)
 {
        struct mapped_device *md = q->queuedata;
        struct dm_table *map = dm_get_table(md);
@@ -803,7 +924,7 @@ static void free_minor(int minor)
 /*
  * See if the device with a specific minor # is free.
  */
-static int specific_minor(struct mapped_device *md, int minor)
+static int specific_minor(int minor)
 {
        int r, m;
 
@@ -836,7 +957,7 @@ out:
        return r;
 }
 
-static int next_free_minor(struct mapped_device *md, int *minor)
+static int next_free_minor(int *minor)
 {
        int r, m;
 
@@ -847,9 +968,8 @@ static int next_free_minor(struct mapped_device *md, int *minor)
        spin_lock(&_minor_lock);
 
        r = idr_get_new(&_minor_idr, MINOR_ALLOCED, &m);
-       if (r) {
+       if (r)
                goto out;
-       }
 
        if (m >= (1 << MINORBITS)) {
                idr_remove(&_minor_idr, m);
@@ -872,7 +992,7 @@ static struct block_device_operations dm_blk_dops;
 static struct mapped_device *alloc_dev(int minor)
 {
        int r;
-       struct mapped_device *md = kmalloc(sizeof(*md), GFP_KERNEL);
+       struct mapped_device *md = kzalloc(sizeof(*md), GFP_KERNEL);
        void *old_md;
 
        if (!md) {
@@ -881,27 +1001,30 @@ static struct mapped_device *alloc_dev(int minor)
        }
 
        if (!try_module_get(THIS_MODULE))
-               goto bad0;
+               goto bad_module_get;
 
        /* get a minor number for the dev */
        if (minor == DM_ANY_MINOR)
-               r = next_free_minor(md, &minor);
+               r = next_free_minor(&minor);
        else
-               r = specific_minor(md, minor);
+               r = specific_minor(minor);
        if (r < 0)
-               goto bad1;
+               goto bad_minor;
 
-       memset(md, 0, sizeof(*md));
        init_rwsem(&md->io_lock);
-       init_MUTEX(&md->suspend_lock);
+       mutex_init(&md->suspend_lock);
+       spin_lock_init(&md->pushback_lock);
        rwlock_init(&md->map_lock);
        atomic_set(&md->holders, 1);
        atomic_set(&md->open_count, 0);
        atomic_set(&md->event_nr, 0);
+       atomic_set(&md->uevent_seq, 0);
+       INIT_LIST_HEAD(&md->uevent_list);
+       spin_lock_init(&md->uevent_lock);
 
        md->queue = blk_alloc_queue(GFP_KERNEL);
        if (!md->queue)
-               goto bad1;
+               goto bad_queue;
 
        md->queue->queuedata = md;
        md->queue->backing_dev_info.congested_fn = dm_any_congested;
@@ -909,19 +1032,22 @@ static struct mapped_device *alloc_dev(int minor)
        blk_queue_make_request(md->queue, dm_request);
        blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
        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);
-       if (!md->io_pool)
-               goto bad2;
+       if (!md->io_pool)
+               goto bad_io_pool;
 
        md->tio_pool = mempool_create_slab_pool(MIN_IOS, _tio_cache);
        if (!md->tio_pool)
-               goto bad3;
+               goto bad_tio_pool;
+
+       md->bs = bioset_create(16, 16);
+       if (!md->bs)
+               goto bad_no_bioset;
 
        md->disk = alloc_disk(1);
        if (!md->disk)
-               goto bad4;
+               goto bad_disk;
 
        atomic_set(&md->pending, 0);
        init_waitqueue_head(&md->wait);
@@ -936,6 +1062,10 @@ static struct mapped_device *alloc_dev(int minor)
        add_disk(md->disk);
        format_dev_t(md->name, MKDEV(_major, minor));
 
+       md->wq = create_singlethread_workqueue("kdmflush");
+       if (!md->wq)
+               goto bad_thread;
+
        /* Populate the mapping, nobody knows we exist yet */
        spin_lock(&_minor_lock);
        old_md = idr_replace(&_minor_idr, md, minor);
@@ -945,30 +1075,39 @@ static struct mapped_device *alloc_dev(int minor)
 
        return md;
 
- bad4:
+bad_thread:
+       put_disk(md->disk);
+bad_disk:
+       bioset_free(md->bs);
+bad_no_bioset:
        mempool_destroy(md->tio_pool);
- bad3:
+bad_tio_pool:
        mempool_destroy(md->io_pool);
- bad2:
+bad_io_pool:
        blk_cleanup_queue(md->queue);
+bad_queue:
        free_minor(minor);
- bad1:
+bad_minor:
        module_put(THIS_MODULE);
- bad0:
+bad_module_get:
        kfree(md);
        return NULL;
 }
 
+static void unlock_fs(struct mapped_device *md);
+
 static void free_dev(struct mapped_device *md)
 {
        int minor = md->disk->first_minor;
 
        if (md->suspended_bdev) {
-               thaw_bdev(md->suspended_bdev, NULL);
+               unlock_fs(md);
                bdput(md->suspended_bdev);
        }
+       destroy_workqueue(md->wq);
        mempool_destroy(md->tio_pool);
        mempool_destroy(md->io_pool);
+       bioset_free(md->bs);
        del_gendisk(md->disk);
        free_minor(minor);
 
@@ -987,8 +1126,16 @@ static void free_dev(struct mapped_device *md)
  */
 static void event_callback(void *context)
 {
+       unsigned long flags;
+       LIST_HEAD(uevents);
        struct mapped_device *md = (struct mapped_device *) context;
 
+       spin_lock_irqsave(&md->uevent_lock, flags);
+       list_splice_init(&md->uevent_list, &uevents);
+       spin_unlock_irqrestore(&md->uevent_lock, flags);
+
+       dm_send_uevents(&uevents, &md->disk->dev.kobj);
+
        atomic_inc(&md->event_nr);
        wake_up(&md->eventq);
 }
@@ -1004,7 +1151,7 @@ static void __set_size(struct mapped_device *md, sector_t size)
 
 static int __bind(struct mapped_device *md, struct dm_table *t)
 {
-       request_queue_t *q = md->queue;
+       struct request_queue *q = md->queue;
        sector_t size;
 
        size = dm_table_get_size(t);
@@ -1015,7 +1162,8 @@ static int __bind(struct mapped_device *md, struct dm_table *t)
        if (size != get_capacity(md->disk))
                memset(&md->geometry, 0, sizeof(md->geometry));
 
-       __set_size(md, size);
+       if (md->suspended_bdev)
+               __set_size(md, size);
        if (size == 0)
                return 0;
 
@@ -1108,6 +1256,12 @@ void dm_get(struct mapped_device *md)
        atomic_inc(&md->holders);
 }
 
+const char *dm_device_name(struct mapped_device *md)
+{
+       return md->name;
+}
+EXPORT_SYMBOL_GPL(dm_device_name);
+
 void dm_put(struct mapped_device *md)
 {
        struct dm_table *map;
@@ -1128,20 +1282,93 @@ void dm_put(struct mapped_device *md)
                free_dev(md);
        }
 }
+EXPORT_SYMBOL_GPL(dm_put);
+
+static int dm_wait_for_completion(struct mapped_device *md)
+{
+       int r = 0;
+
+       while (1) {
+               set_current_state(TASK_INTERRUPTIBLE);
+
+               smp_mb();
+               if (!atomic_read(&md->pending))
+                       break;
+
+               if (signal_pending(current)) {
+                       r = -EINTR;
+                       break;
+               }
+
+               io_schedule();
+       }
+       set_current_state(TASK_RUNNING);
+
+       return r;
+}
 
 /*
  * Process the deferred bios
  */
-static void __flush_deferred_io(struct mapped_device *md, struct bio *c)
+static void __flush_deferred_io(struct mapped_device *md)
 {
-       struct bio *n;
+       struct bio *c;
 
-       while (c) {
-               n = c->bi_next;
-               c->bi_next = NULL;
-               __split_bio(md, c);
-               c = n;
+       while ((c = bio_list_pop(&md->deferred))) {
+               if (__split_bio(md, c))
+                       bio_io_error(c);
        }
+
+       clear_bit(DMF_BLOCK_IO, &md->flags);
+}
+
+static void __merge_pushback_list(struct mapped_device *md)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&md->pushback_lock, flags);
+       clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
+       bio_list_merge_head(&md->deferred, &md->pushback);
+       bio_list_init(&md->pushback);
+       spin_unlock_irqrestore(&md->pushback_lock, flags);
+}
+
+static void dm_wq_work(struct work_struct *work)
+{
+       struct dm_wq_req *req = container_of(work, struct dm_wq_req, work);
+       struct mapped_device *md = req->md;
+
+       down_write(&md->io_lock);
+       switch (req->type) {
+       case DM_WQ_FLUSH_ALL:
+               __merge_pushback_list(md);
+               /* pass through */
+       case DM_WQ_FLUSH_DEFERRED:
+               __flush_deferred_io(md);
+               break;
+       default:
+               DMERR("dm_wq_work: unrecognised work type %d", req->type);
+               BUG();
+       }
+       up_write(&md->io_lock);
+}
+
+static void dm_wq_queue(struct mapped_device *md, int type, void *context,
+                       struct dm_wq_req *req)
+{
+       req->type = type;
+       req->md = md;
+       req->context = context;
+       INIT_WORK(&req->work, dm_wq_work);
+       queue_work(md->wq, &req->work);
+}
+
+static void dm_queue_flush(struct mapped_device *md, int type, void *context)
+{
+       struct dm_wq_req req;
+
+       dm_wq_queue(md, type, context, &req);
+       flush_workqueue(md->wq);
 }
 
 /*
@@ -1151,17 +1378,22 @@ int dm_swap_table(struct mapped_device *md, struct dm_table *table)
 {
        int r = -EINVAL;
 
-       down(&md->suspend_lock);
+       mutex_lock(&md->suspend_lock);
 
        /* device must be suspended */
        if (!dm_suspended(md))
                goto out;
 
+       /* without bdev, the device size cannot be changed */
+       if (!md->suspended_bdev)
+               if (get_capacity(md->disk) != dm_table_get_size(table))
+                       goto out;
+
        __unbind(md);
        r = __bind(md, table);
 
 out:
-       up(&md->suspend_lock);
+       mutex_unlock(&md->suspend_lock);
        return r;
 }
 
@@ -1207,35 +1439,51 @@ static void unlock_fs(struct mapped_device *md)
  * dm_bind_table, dm_suspend must be called to flush any in
  * flight bios and ensure that any further io gets deferred.
  */
-int dm_suspend(struct mapped_device *md, int do_lockfs)
+int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
 {
        struct dm_table *map = NULL;
        DECLARE_WAITQUEUE(wait, current);
-       struct bio *def;
-       int r = -EINVAL;
+       int r = 0;
+       int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
+       int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
 
-       down(&md->suspend_lock);
+       mutex_lock(&md->suspend_lock);
 
-       if (dm_suspended(md))
-               goto out;
+       if (dm_suspended(md)) {
+               r = -EINVAL;
+               goto out_unlock;
+       }
 
        map = dm_get_table(md);
 
+       /*
+        * DMF_NOFLUSH_SUSPENDING must be set before presuspend.
+        * This flag is cleared before dm_suspend returns.
+        */
+       if (noflush)
+               set_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
+
        /* This does not get reverted if there's an error later. */
        dm_table_presuspend_targets(map);
 
-       md->suspended_bdev = bdget_disk(md->disk, 0);
-       if (!md->suspended_bdev) {
-               DMWARN("bdget failed in dm_suspend");
-               r = -ENOMEM;
-               goto out;
-       }
+       /* bdget() can stall if the pending I/Os are not flushed */
+       if (!noflush) {
+               md->suspended_bdev = bdget_disk(md->disk, 0);
+               if (!md->suspended_bdev) {
+                       DMWARN("bdget failed in dm_suspend");
+                       r = -ENOMEM;
+                       goto flush_and_out;
+               }
 
-       /* Flush I/O to the device. */
-       if (do_lockfs) {
-               r = lock_fs(md);
-               if (r)
-                       goto out;
+               /*
+                * Flush I/O to the device. noflush supersedes do_lockfs,
+                * because lock_fs() needs to flush I/Os.
+                */
+               if (do_lockfs) {
+                       r = lock_fs(md);
+                       if (r)
+                               goto out;
+               }
        }
 
        /*
@@ -1252,39 +1500,36 @@ int dm_suspend(struct mapped_device *md, int do_lockfs)
                dm_table_unplug_all(map);
 
        /*
-        * Then we wait for the already mapped ios to
-        * complete.
+        * Wait for the already-mapped ios to complete.
         */
-       while (1) {
-               set_current_state(TASK_INTERRUPTIBLE);
-
-               if (!atomic_read(&md->pending) || signal_pending(current))
-                       break;
-
-               io_schedule();
-       }
-       set_current_state(TASK_RUNNING);
+       r = dm_wait_for_completion(md);
 
        down_write(&md->io_lock);
        remove_wait_queue(&md->wait, &wait);
 
+       if (noflush)
+               __merge_pushback_list(md);
+       up_write(&md->io_lock);
+
        /* were we interrupted ? */
-       r = -EINTR;
-       if (atomic_read(&md->pending)) {
-               clear_bit(DMF_BLOCK_IO, &md->flags);
-               def = bio_list_get(&md->deferred);
-               __flush_deferred_io(md, def);
-               up_write(&md->io_lock);
+       if (r < 0) {
+               dm_queue_flush(md, DM_WQ_FLUSH_DEFERRED, NULL);
+
                unlock_fs(md);
-               goto out;
+               goto out; /* pushback list is already flushed, so skip flush */
        }
-       up_write(&md->io_lock);
 
        dm_table_postsuspend_targets(map);
 
        set_bit(DMF_SUSPENDED, &md->flags);
 
-       r = 0;
+flush_and_out:
+       if (r && noflush)
+               /*
+                * Because there may be already I/Os in the pushback list,
+                * flush them before return.
+                */
+               dm_queue_flush(md, DM_WQ_FLUSH_ALL, NULL);
 
 out:
        if (r && md->suspended_bdev) {
@@ -1293,17 +1538,18 @@ out:
        }
 
        dm_table_put(map);
-       up(&md->suspend_lock);
+
+out_unlock:
+       mutex_unlock(&md->suspend_lock);
        return r;
 }
 
 int dm_resume(struct mapped_device *md)
 {
        int r = -EINVAL;
-       struct bio *def;
        struct dm_table *map = NULL;
 
-       down(&md->suspend_lock);
+       mutex_lock(&md->suspend_lock);
        if (!dm_suspended(md))
                goto out;
 
@@ -1311,29 +1557,30 @@ int dm_resume(struct mapped_device *md)
        if (!map || !dm_table_get_size(map))
                goto out;
 
-       dm_table_resume_targets(map);
-
-       down_write(&md->io_lock);
-       clear_bit(DMF_BLOCK_IO, &md->flags);
+       r = dm_table_resume_targets(map);
+       if (r)
+               goto out;
 
-       def = bio_list_get(&md->deferred);
-       __flush_deferred_io(md, def);
-       up_write(&md->io_lock);
+       dm_queue_flush(md, DM_WQ_FLUSH_DEFERRED, NULL);
 
        unlock_fs(md);
 
-       bdput(md->suspended_bdev);
-       md->suspended_bdev = NULL;
+       if (md->suspended_bdev) {
+               bdput(md->suspended_bdev);
+               md->suspended_bdev = NULL;
+       }
 
        clear_bit(DMF_SUSPENDED, &md->flags);
 
        dm_table_unplug_all(map);
 
+       dm_kobject_uevent(md);
+
        r = 0;
 
 out:
        dm_table_put(map);
-       up(&md->suspend_lock);
+       mutex_unlock(&md->suspend_lock);
 
        return r;
 }
@@ -1341,6 +1588,16 @@ out:
 /*-----------------------------------------------------------------
  * Event notification.
  *---------------------------------------------------------------*/
+void dm_kobject_uevent(struct mapped_device *md)
+{
+       kobject_uevent(&md->disk->dev.kobj, KOBJ_CHANGE);
+}
+
+uint32_t dm_next_uevent_seq(struct mapped_device *md)
+{
+       return atomic_add_return(1, &md->uevent_seq);
+}
+
 uint32_t dm_get_event_nr(struct mapped_device *md)
 {
        return atomic_read(&md->event_nr);
@@ -1352,6 +1609,15 @@ int dm_wait_event(struct mapped_device *md, int event_nr)
                        (event_nr != atomic_read(&md->event_nr)));
 }
 
+void dm_uevent_add(struct mapped_device *md, struct list_head *elist)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&md->uevent_lock, flags);
+       list_add(elist, &md->uevent_list);
+       spin_unlock_irqrestore(&md->uevent_lock, flags);
+}
+
 /*
  * The gendisk is only valid as long as you have a reference
  * count on 'md'.
@@ -1366,9 +1632,21 @@ int dm_suspended(struct mapped_device *md)
        return test_bit(DMF_SUSPENDED, &md->flags);
 }
 
+int dm_noflush_suspending(struct dm_target *ti)
+{
+       struct mapped_device *md = dm_table_get_md(ti->table);
+       int r = __noflush_suspending(md);
+
+       dm_put(md);
+
+       return r;
+}
+EXPORT_SYMBOL_GPL(dm_noflush_suspending);
+
 static struct block_device_operations dm_blk_dops = {
        .open = dm_blk_open,
        .release = dm_blk_close,
+       .ioctl = dm_blk_ioctl,
        .getgeo = dm_blk_getgeo,
        .owner = THIS_MODULE
 };