Merge git://git.kernel.org/pub/scm/linux/kernel/git/rusty/linux-2.6-for-linus
[safe/jmp/linux-2.6] / drivers / md / dm.c
index 421c9f0..424f7b0 100644 (file)
@@ -1,12 +1,11 @@
 /*
  * Copyright (C) 2001, 2002 Sistina Software (UK) Limited.
- * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
+ * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
  *
  * This file is released under the GPL.
  */
 
 #include "dm.h"
-#include "dm-bio-list.h"
 #include "dm-uevent.h"
 
 #include <linux/init.h>
@@ -32,6 +31,7 @@ static unsigned int _major = 0;
 
 static DEFINE_SPINLOCK(_minor_lock);
 /*
+ * For bio-based dm.
  * One of these is allocated per bio.
  */
 struct dm_io {
@@ -43,6 +43,7 @@ struct dm_io {
 };
 
 /*
+ * For bio-based dm.
  * One of these is allocated per target within a bio.  Hopefully
  * this will be simplified out one day.
  */
@@ -54,6 +55,27 @@ struct dm_target_io {
 
 DEFINE_TRACE(block_bio_complete);
 
+/*
+ * For request-based dm.
+ * One of these is allocated per request.
+ */
+struct dm_rq_target_io {
+       struct mapped_device *md;
+       struct dm_target *ti;
+       struct request *orig, clone;
+       int error;
+       union map_info info;
+};
+
+/*
+ * For request-based dm.
+ * One of these is allocated per bio.
+ */
+struct dm_rq_clone_bio_info {
+       struct bio *orig;
+       struct request *rq;
+};
+
 union map_info *dm_get_mapinfo(struct bio *bio)
 {
        if (bio && bio->bi_private)
@@ -66,29 +88,20 @@ union map_info *dm_get_mapinfo(struct bio *bio)
 /*
  * Bits for the md->flags field.
  */
-#define DMF_BLOCK_IO 0
+#define DMF_BLOCK_IO_FOR_SUSPEND 0
 #define DMF_SUSPENDED 1
 #define DMF_FROZEN 2
 #define DMF_FREEING 3
 #define DMF_DELETING 4
 #define DMF_NOFLUSH_SUSPENDING 5
+#define DMF_QUEUE_IO_TO_THREAD 6
 
 /*
  * Work processed by per-device workqueue.
  */
-struct dm_wq_req {
-       enum {
-               DM_WQ_FLUSH_DEFERRED,
-       } type;
-       struct work_struct work;
-       struct mapped_device *md;
-       void *context;
-};
-
 struct mapped_device {
        struct rw_semaphore io_lock;
        struct mutex suspend_lock;
-       spinlock_t pushback_lock;
        rwlock_t map_lock;
        atomic_t holders;
        atomic_t open_count;
@@ -106,8 +119,14 @@ struct mapped_device {
         */
        atomic_t pending;
        wait_queue_head_t wait;
+       struct work_struct work;
        struct bio_list deferred;
-       struct bio_list pushback;
+       spinlock_t deferred_lock;
+
+       /*
+        * An error from the barrier request currently being processed.
+        */
+       int barrier_error;
 
        /*
         * Processing queue (flush/barriers)
@@ -144,11 +163,16 @@ struct mapped_device {
 
        /* forced geometry settings */
        struct hd_geometry geometry;
+
+       /* sysfs handle */
+       struct kobject kobj;
 };
 
 #define MIN_IOS 256
 static struct kmem_cache *_io_cache;
 static struct kmem_cache *_tio_cache;
+static struct kmem_cache *_rq_tio_cache;
+static struct kmem_cache *_rq_bio_info_cache;
 
 static int __init local_init(void)
 {
@@ -164,9 +188,17 @@ static int __init local_init(void)
        if (!_tio_cache)
                goto out_free_io_cache;
 
+       _rq_tio_cache = KMEM_CACHE(dm_rq_target_io, 0);
+       if (!_rq_tio_cache)
+               goto out_free_tio_cache;
+
+       _rq_bio_info_cache = KMEM_CACHE(dm_rq_clone_bio_info, 0);
+       if (!_rq_bio_info_cache)
+               goto out_free_rq_tio_cache;
+
        r = dm_uevent_init();
        if (r)
-               goto out_free_tio_cache;
+               goto out_free_rq_bio_info_cache;
 
        _major = major;
        r = register_blkdev(_major, _name);
@@ -180,6 +212,10 @@ static int __init local_init(void)
 
 out_uevent_exit:
        dm_uevent_exit();
+out_free_rq_bio_info_cache:
+       kmem_cache_destroy(_rq_bio_info_cache);
+out_free_rq_tio_cache:
+       kmem_cache_destroy(_rq_tio_cache);
 out_free_tio_cache:
        kmem_cache_destroy(_tio_cache);
 out_free_io_cache:
@@ -190,6 +226,8 @@ out_free_io_cache:
 
 static void local_exit(void)
 {
+       kmem_cache_destroy(_rq_bio_info_cache);
+       kmem_cache_destroy(_rq_tio_cache);
        kmem_cache_destroy(_tio_cache);
        kmem_cache_destroy(_io_cache);
        unregister_blkdev(_major, _name);
@@ -391,6 +429,10 @@ static void end_io_acct(struct dm_io *io)
        part_stat_add(cpu, &dm_disk(md)->part0, ticks[rw], duration);
        part_stat_unlock();
 
+       /*
+        * After this is decremented the bio must not be touched if it is
+        * a barrier.
+        */
        dm_disk(md)->part0.in_flight = pending =
                atomic_dec_return(&md->pending);
 
@@ -402,19 +444,18 @@ static void end_io_acct(struct dm_io *io)
 /*
  * Add the bio to the list of deferred io.
  */
-static int queue_io(struct mapped_device *md, struct bio *bio)
+static void queue_io(struct mapped_device *md, struct bio *bio)
 {
        down_write(&md->io_lock);
 
-       if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
-               up_write(&md->io_lock);
-               return 1;
-       }
-
+       spin_lock_irq(&md->deferred_lock);
        bio_list_add(&md->deferred, bio);
+       spin_unlock_irq(&md->deferred_lock);
+
+       if (!test_and_set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags))
+               queue_work(md->wq, &md->work);
 
        up_write(&md->io_lock);
-       return 0;               /* deferred successfully */
 }
 
 /*
@@ -483,36 +524,50 @@ static int __noflush_suspending(struct mapped_device *md)
 static void dec_pending(struct dm_io *io, int error)
 {
        unsigned long flags;
+       int io_error;
+       struct bio *bio;
+       struct mapped_device *md = io->md;
 
        /* Push-back supersedes any I/O errors */
-       if (error && !(io->error > 0 && __noflush_suspending(io->md)))
+       if (error && !(io->error > 0 && __noflush_suspending(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);
+                       spin_lock_irqsave(&md->deferred_lock, flags);
+                       if (__noflush_suspending(md))
+                               bio_list_add_head(&md->deferred, io->bio);
                        else
                                /* noflush suspend was interrupted. */
                                io->error = -EIO;
-                       spin_unlock_irqrestore(&io->md->pushback_lock, flags);
+                       spin_unlock_irqrestore(&md->deferred_lock, flags);
                }
 
-               end_io_acct(io);
+               io_error = io->error;
+               bio = io->bio;
 
-               if (io->error != DM_ENDIO_REQUEUE) {
-                       trace_block_bio_complete(io->md->queue, io->bio);
+               if (bio_barrier(bio)) {
+                       /*
+                        * There can be just one barrier request so we use
+                        * a per-device variable for error reporting.
+                        * Note that you can't touch the bio after end_io_acct
+                        */
+                       md->barrier_error = io_error;
+                       end_io_acct(io);
+               } else {
+                       end_io_acct(io);
+
+                       if (io_error != DM_ENDIO_REQUEUE) {
+                               trace_block_bio_complete(md->queue, bio);
 
-                       bio_endio(io->bio, io->error);
+                               bio_endio(bio, io_error);
+                       }
                }
 
-               free_io(io->md, io);
+               free_io(md, io);
        }
 }
 
@@ -520,6 +575,7 @@ static void clone_endio(struct bio *bio, int error)
 {
        int r = 0;
        struct dm_target_io *tio = bio->bi_private;
+       struct dm_io *io = tio->io;
        struct mapped_device *md = tio->io->md;
        dm_endio_fn endio = tio->ti->type->end_io;
 
@@ -543,15 +599,14 @@ static void clone_endio(struct bio *bio, int 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);
        free_tio(md, tio);
+       bio_put(bio);
+       dec_pending(io, error);
 }
 
 static sector_t max_io_len(struct mapped_device *md,
@@ -654,13 +709,19 @@ static struct bio *split_bvec(struct bio *bio, sector_t sector,
 
        clone->bi_sector = sector;
        clone->bi_bdev = bio->bi_bdev;
-       clone->bi_rw = bio->bi_rw;
+       clone->bi_rw = bio->bi_rw & ~(1 << BIO_RW_BARRIER);
        clone->bi_vcnt = 1;
        clone->bi_size = to_bytes(len);
        clone->bi_io_vec->bv_offset = offset;
        clone->bi_io_vec->bv_len = clone->bi_size;
        clone->bi_flags |= 1 << BIO_CLONED;
 
+       if (bio_integrity(bio)) {
+               bio_integrity_clone(clone, bio, GFP_NOIO);
+               bio_integrity_trim(clone,
+                                  bio_sector_offset(bio, idx, offset), len);
+       }
+
        return clone;
 }
 
@@ -675,6 +736,7 @@ static struct bio *clone_bio(struct bio *bio, sector_t sector,
 
        clone = bio_alloc_bioset(GFP_NOIO, bio->bi_max_vecs, bs);
        __bio_clone(clone, bio);
+       clone->bi_rw &= ~(1 << BIO_RW_BARRIER);
        clone->bi_destructor = dm_bio_destructor;
        clone->bi_sector = sector;
        clone->bi_idx = idx;
@@ -682,6 +744,14 @@ static struct bio *clone_bio(struct bio *bio, sector_t sector,
        clone->bi_size = to_bytes(len);
        clone->bi_flags &= ~(1 << BIO_SEG_VALID);
 
+       if (bio_integrity(bio)) {
+               bio_integrity_clone(clone, bio, GFP_NOIO);
+
+               if (idx != bio->bi_idx || clone->bi_size < bio->bi_size)
+                       bio_integrity_trim(clone,
+                                          bio_sector_offset(bio, idx, 0), len);
+       }
+
        return clone;
 }
 
@@ -786,16 +856,21 @@ static int __clone_and_map(struct clone_info *ci)
 }
 
 /*
- * Split the bio into several clones.
+ * Split the bio into several clones and submit it to targets.
  */
-static int __split_bio(struct mapped_device *md, struct bio *bio)
+static void __split_and_process_bio(struct mapped_device *md, struct bio *bio)
 {
        struct clone_info ci;
        int error = 0;
 
        ci.map = dm_get_table(md);
-       if (unlikely(!ci.map))
-               return -EIO;
+       if (unlikely(!ci.map)) {
+               if (!bio_barrier(bio))
+                       bio_io_error(bio);
+               else
+                       md->barrier_error = -EIO;
+               return;
+       }
 
        ci.md = md;
        ci.bio = bio;
@@ -815,8 +890,6 @@ static int __split_bio(struct mapped_device *md, struct bio *bio)
        /* drop the extra reference count */
        dec_pending(ci.io, error);
        dm_table_put(ci.map);
-
-       return 0;
 }
 /*-----------------------------------------------------------------
  * CRUD END
@@ -875,20 +948,10 @@ out:
  */
 static int dm_request(struct request_queue *q, struct bio *bio)
 {
-       int r = -EIO;
        int rw = bio_data_dir(bio);
        struct mapped_device *md = q->queuedata;
        int cpu;
 
-       /*
-        * 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);
 
        cpu = part_stat_lock();
@@ -897,32 +960,26 @@ static int dm_request(struct request_queue *q, struct bio *bio)
        part_stat_unlock();
 
        /*
-        * If we're suspended we have to queue
-        * this io for later.
+        * If we're suspended or the thread is processing barriers
+        * we have to queue this io for later.
         */
-       while (test_bit(DMF_BLOCK_IO, &md->flags)) {
+       if (unlikely(test_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags)) ||
+           unlikely(bio_barrier(bio))) {
                up_read(&md->io_lock);
 
-               if (bio_rw(bio) != READA)
-                       r = queue_io(md, bio);
+               if (unlikely(test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) &&
+                   bio_rw(bio) == READA) {
+                       bio_io_error(bio);
+                       return 0;
+               }
 
-               if (r <= 0)
-                       goto out_req;
+               queue_io(md, bio);
 
-               /*
-                * We're in a while loop, because someone could suspend
-                * before we get to the following read lock.
-                */
-               down_read(&md->io_lock);
+               return 0;
        }
 
-       r = __split_bio(md, bio);
+       __split_and_process_bio(md, bio);
        up_read(&md->io_lock);
-
-out_req:
-       if (r < 0)
-               bio_io_error(bio);
-
        return 0;
 }
 
@@ -943,9 +1000,7 @@ static int dm_any_congested(void *congested_data, int bdi_bits)
        struct mapped_device *md = congested_data;
        struct dm_table *map;
 
-       atomic_inc(&md->pending);
-
-       if (!test_bit(DMF_BLOCK_IO, &md->flags)) {
+       if (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
                map = dm_get_table(md);
                if (map) {
                        r = dm_table_any_congested(map, bdi_bits);
@@ -953,10 +1008,6 @@ static int dm_any_congested(void *congested_data, int bdi_bits)
                }
        }
 
-       if (!atomic_dec_return(&md->pending))
-               /* nudge anyone waiting on suspend queue */
-               wake_up(&md->wait);
-
        return r;
 }
 
@@ -1037,6 +1088,8 @@ out:
 
 static struct block_device_operations dm_blk_dops;
 
+static void dm_wq_work(struct work_struct *work);
+
 /*
  * Allocate and initialise a blank device with a given minor.
  */
@@ -1064,7 +1117,7 @@ static struct mapped_device *alloc_dev(int minor)
 
        init_rwsem(&md->io_lock);
        mutex_init(&md->suspend_lock);
-       spin_lock_init(&md->pushback_lock);
+       spin_lock_init(&md->deferred_lock);
        rwlock_init(&md->map_lock);
        atomic_set(&md->holders, 1);
        atomic_set(&md->open_count, 0);
@@ -1081,6 +1134,7 @@ static struct mapped_device *alloc_dev(int minor)
        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);
+       blk_queue_ordered(md->queue, QUEUE_ORDERED_DRAIN, NULL);
        blk_queue_bounce_limit(md->queue, BLK_BOUNCE_ANY);
        md->queue->unplug_fn = dm_unplug_all;
        blk_queue_merge_bvec(md->queue, dm_merge_bvec);
@@ -1103,6 +1157,7 @@ static struct mapped_device *alloc_dev(int minor)
 
        atomic_set(&md->pending, 0);
        init_waitqueue_head(&md->wait);
+       INIT_WORK(&md->work, dm_wq_work);
        init_waitqueue_head(&md->eventq);
 
        md->disk->major = _major;
@@ -1160,6 +1215,7 @@ static void free_dev(struct mapped_device *md)
        mempool_destroy(md->tio_pool);
        mempool_destroy(md->io_pool);
        bioset_free(md->bs);
+       blk_integrity_unregister(md->disk);
        del_gendisk(md->disk);
        free_minor(minor);
 
@@ -1216,10 +1272,12 @@ static int __bind(struct mapped_device *md, struct dm_table *t)
 
        if (md->suspended_bdev)
                __set_size(md, size);
-       if (size == 0)
+
+       if (!size) {
+               dm_table_destroy(t);
                return 0;
+       }
 
-       dm_table_get(t);
        dm_table_event_callback(t, event_callback, md);
 
        write_lock(&md->map_lock);
@@ -1241,7 +1299,7 @@ static void __unbind(struct mapped_device *md)
        write_lock(&md->map_lock);
        md->map = NULL;
        write_unlock(&md->map_lock);
-       dm_table_put(map);
+       dm_table_destroy(map);
 }
 
 /*
@@ -1255,6 +1313,8 @@ int dm_create(int minor, struct mapped_device **result)
        if (!md)
                return -ENXIO;
 
+       dm_sysfs_init(md);
+
        *result = md;
        return 0;
 }
@@ -1330,25 +1390,32 @@ void dm_put(struct mapped_device *md)
                        dm_table_presuspend_targets(map);
                        dm_table_postsuspend_targets(map);
                }
-               __unbind(md);
+               dm_sysfs_exit(md);
                dm_table_put(map);
+               __unbind(md);
                free_dev(md);
        }
 }
 EXPORT_SYMBOL_GPL(dm_put);
 
-static int dm_wait_for_completion(struct mapped_device *md)
+static int dm_wait_for_completion(struct mapped_device *md, int interruptible)
 {
        int r = 0;
+       DECLARE_WAITQUEUE(wait, current);
+
+       dm_unplug_all(md->queue);
+
+       add_wait_queue(&md->wait, &wait);
 
        while (1) {
-               set_current_state(TASK_INTERRUPTIBLE);
+               set_current_state(interruptible);
 
                smp_mb();
                if (!atomic_read(&md->pending))
                        break;
 
-               if (signal_pending(current)) {
+               if (interruptible == TASK_INTERRUPTIBLE &&
+                   signal_pending(current)) {
                        r = -EINTR;
                        break;
                }
@@ -1357,68 +1424,80 @@ static int dm_wait_for_completion(struct mapped_device *md)
        }
        set_current_state(TASK_RUNNING);
 
+       remove_wait_queue(&md->wait, &wait);
+
        return r;
 }
 
-/*
- * Process the deferred bios
- */
-static void __flush_deferred_io(struct mapped_device *md)
+static int dm_flush(struct mapped_device *md)
 {
-       struct bio *c;
+       dm_wait_for_completion(md, TASK_UNINTERRUPTIBLE);
+       return 0;
+}
+
+static void process_barrier(struct mapped_device *md, struct bio *bio)
+{
+       int error = dm_flush(md);
 
-       while ((c = bio_list_pop(&md->deferred))) {
-               if (__split_bio(md, c))
-                       bio_io_error(c);
+       if (unlikely(error)) {
+               bio_endio(bio, error);
+               return;
+       }
+       if (bio_empty_barrier(bio)) {
+               bio_endio(bio, 0);
+               return;
        }
 
-       clear_bit(DMF_BLOCK_IO, &md->flags);
-}
+       __split_and_process_bio(md, bio);
 
-static void __merge_pushback_list(struct mapped_device *md)
-{
-       unsigned long flags;
+       error = dm_flush(md);
 
-       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);
+       if (!error && md->barrier_error)
+               error = md->barrier_error;
+
+       if (md->barrier_error != DM_ENDIO_REQUEUE)
+               bio_endio(bio, error);
 }
 
+/*
+ * Process the deferred bios
+ */
 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;
+       struct mapped_device *md = container_of(work, struct mapped_device,
+                                               work);
+       struct bio *c;
 
        down_write(&md->io_lock);
-       switch (req->type) {
-       case DM_WQ_FLUSH_DEFERRED:
-               __flush_deferred_io(md);
-               break;
-       default:
-               DMERR("dm_wq_work: unrecognised work type %d", req->type);
-               BUG();
+
+       while (!test_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags)) {
+               spin_lock_irq(&md->deferred_lock);
+               c = bio_list_pop(&md->deferred);
+               spin_unlock_irq(&md->deferred_lock);
+
+               if (!c) {
+                       clear_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
+                       break;
+               }
+
+               up_write(&md->io_lock);
+
+               if (bio_barrier(c))
+                       process_barrier(md, c);
+               else
+                       __split_and_process_bio(md, c);
+
+               down_write(&md->io_lock);
        }
+
        up_write(&md->io_lock);
 }
 
-static void dm_wq_queue(struct mapped_device *md, int type, void *context,
-                       struct dm_wq_req *req)
+static void dm_queue_flush(struct mapped_device *md)
 {
-       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);
+       clear_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
+       smp_mb__after_clear_bit();
+       queue_work(md->wq, &md->work);
 }
 
 /*
@@ -1492,7 +1571,6 @@ static void unlock_fs(struct mapped_device *md)
 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
 {
        struct dm_table *map = NULL;
-       DECLARE_WAITQUEUE(wait, current);
        int r = 0;
        int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
        int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
@@ -1537,38 +1615,54 @@ int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
        }
 
        /*
-        * First we set the BLOCK_IO flag so no more ios will be mapped.
+        * Here we must make sure that no processes are submitting requests
+        * to target drivers i.e. no one may be executing
+        * __split_and_process_bio. This is called from dm_request and
+        * dm_wq_work.
+        *
+        * To get all processes out of __split_and_process_bio in dm_request,
+        * we take the write lock. To prevent any process from reentering
+        * __split_and_process_bio from dm_request, we set
+        * DMF_QUEUE_IO_TO_THREAD.
+        *
+        * To quiesce the thread (dm_wq_work), we set DMF_BLOCK_IO_FOR_SUSPEND
+        * and call flush_workqueue(md->wq). flush_workqueue will wait until
+        * dm_wq_work exits and DMF_BLOCK_IO_FOR_SUSPEND will prevent any
+        * further calls to __split_and_process_bio from dm_wq_work.
         */
        down_write(&md->io_lock);
-       set_bit(DMF_BLOCK_IO, &md->flags);
-
-       add_wait_queue(&md->wait, &wait);
+       set_bit(DMF_BLOCK_IO_FOR_SUSPEND, &md->flags);
+       set_bit(DMF_QUEUE_IO_TO_THREAD, &md->flags);
        up_write(&md->io_lock);
 
-       /* unplug */
-       if (map)
-               dm_table_unplug_all(map);
+       flush_workqueue(md->wq);
 
        /*
-        * Wait for the already-mapped ios to complete.
+        * At this point no more requests are entering target request routines.
+        * We call dm_wait_for_completion to wait for all existing requests
+        * to finish.
         */
-       r = dm_wait_for_completion(md);
+       r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE);
 
        down_write(&md->io_lock);
-       remove_wait_queue(&md->wait, &wait);
-
        if (noflush)
-               __merge_pushback_list(md);
+               clear_bit(DMF_NOFLUSH_SUSPENDING, &md->flags);
        up_write(&md->io_lock);
 
        /* were we interrupted ? */
        if (r < 0) {
-               dm_queue_flush(md, DM_WQ_FLUSH_DEFERRED, NULL);
+               dm_queue_flush(md);
 
                unlock_fs(md);
                goto out; /* pushback list is already flushed, so skip flush */
        }
 
+       /*
+        * If dm_wait_for_completion returned 0, the device is completely
+        * quiescent now. There is no request-processing activity. All new
+        * requests are being added to md->deferred list.
+        */
+
        dm_table_postsuspend_targets(map);
 
        set_bit(DMF_SUSPENDED, &md->flags);
@@ -1603,7 +1697,7 @@ int dm_resume(struct mapped_device *md)
        if (r)
                goto out;
 
-       dm_queue_flush(md, DM_WQ_FLUSH_DEFERRED, NULL);
+       dm_queue_flush(md);
 
        unlock_fs(md);
 
@@ -1669,6 +1763,27 @@ struct gendisk *dm_disk(struct mapped_device *md)
        return md->disk;
 }
 
+struct kobject *dm_kobject(struct mapped_device *md)
+{
+       return &md->kobj;
+}
+
+/*
+ * struct mapped_device should not be exported outside of dm.c
+ * so use this check to verify that kobj is part of md structure
+ */
+struct mapped_device *dm_get_from_kobject(struct kobject *kobj)
+{
+       struct mapped_device *md;
+
+       md = container_of(kobj, struct mapped_device, kobj);
+       if (&md->kobj != kobj)
+               return NULL;
+
+       dm_get(md);
+       return md;
+}
+
 int dm_suspended(struct mapped_device *md)
 {
        return test_bit(DMF_SUSPENDED, &md->flags);