Rework helper thread. IO may be submitted to a worker thread with queue_io(). queue_io() sets DMF_BLOCK_IO so that all further IO goes to the thread. When the thread finishes its work, it clears DMF_BLOCK_IO and from this point on, requests are submitted from dm_request again. Add new flag DMF_BLOCK_FOR_SUSPEND that is set when the IO needs to be blocked because of an ongoing suspend (DMF_BLOCK_IO had this meaning before this patch). Functionality change: READA is still submitted when suspended. I'm not sure how much it matters. Signed-off-by: Mikulas Patocka --- drivers/md/dm.c | 64 ++++++++++++++++++++++---------------------------------- 1 file changed, 26 insertions(+), 38 deletions(-) Index: linux-2.6.28-clean/drivers/md/dm.c =================================================================== --- linux-2.6.28-clean.orig/drivers/md/dm.c 2009-01-12 15:17:14.000000000 +0100 +++ linux-2.6.28-clean/drivers/md/dm.c 2009-01-12 15:17:18.000000000 +0100 @@ -64,11 +64,12 @@ union map_info *dm_get_mapinfo(struct bi * Bits for the md->flags field. */ #define DMF_BLOCK_IO 0 -#define DMF_SUSPENDED 1 -#define DMF_FROZEN 2 -#define DMF_FREEING 3 -#define DMF_DELETING 4 -#define DMF_NOFLUSH_SUSPENDING 5 +#define DMF_BLOCK_FOR_SUSPEND 1 +#define DMF_SUSPENDED 2 +#define DMF_FROZEN 3 +#define DMF_FREEING 4 +#define DMF_DELETING 5 +#define DMF_NOFLUSH_SUSPENDING 6 /* * Work processed by per-device workqueue. @@ -391,19 +392,13 @@ 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; - } - bio_list_add(&md->deferred, bio); - + if (!test_and_set_bit(DMF_BLOCK_IO, &md->flags)) + queue_work(md->wq, &md->work); up_write(&md->io_lock); - return 0; /* deferred successfully */ } /* @@ -865,7 +860,6 @@ 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; @@ -890,31 +884,17 @@ static int dm_request(struct request_que * If we're suspended we have to queue * this io for later. */ - while (test_bit(DMF_BLOCK_IO, &md->flags)) { + if (unlikely(test_bit(DMF_BLOCK_IO, &md->flags))) { up_read(&md->io_lock); - if (bio_rw(bio) != READA) - r = queue_io(md, bio); - - 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; } __process_bio(md, bio); up_read(&md->io_lock); return 0; - -out_req: - if (r < 0) - bio_io_error(bio); - - return 0; } static void dm_unplug_all(struct request_queue *q) @@ -936,7 +916,7 @@ static int dm_any_congested(void *conges atomic_inc(&md->pending); - if (!test_bit(DMF_BLOCK_IO, &md->flags)) { + if (!test_bit(DMF_BLOCK_FOR_SUSPEND, &md->flags)) { map = dm_get_table(md); if (map) { r = dm_table_any_congested(map, bdi_bits); @@ -1378,22 +1358,28 @@ static void __merge_pushback_list(struct static void dm_wq_work(struct work_struct *work) { struct mapped_device *md = container_of(work, struct mapped_device, work); - struct bio *c; - down_write(&md->io_lock); - while ((c = bio_list_pop(&md->deferred))) { + while (!test_bit(DMF_BLOCK_FOR_SUSPEND, &md->flags)) { + struct bio *c = bio_list_pop(&md->deferred); + if (!c) { + clear_bit(DMF_BLOCK_IO, &md->flags); + break; + } + up_write(&md->io_lock); + __process_bio(md, c); + + down_write(&md->io_lock); } - clear_bit(DMF_BLOCK_IO, &md->flags); up_write(&md->io_lock); } static void dm_queue_flush(struct mapped_device *md) { + clear_bit(DMF_BLOCK_FOR_SUSPEND, &md->flags); queue_work(md->wq, &md->work); - flush_workqueue(md->wq); } /* @@ -1514,6 +1500,7 @@ int dm_suspend(struct mapped_device *md, * First we set the BLOCK_IO flag so no more ios will be mapped. */ down_write(&md->io_lock); + set_bit(DMF_BLOCK_FOR_SUSPEND, &md->flags); set_bit(DMF_BLOCK_IO, &md->flags); up_write(&md->io_lock); @@ -1521,6 +1508,7 @@ int dm_suspend(struct mapped_device *md, /* * Wait for the already-mapped ios to complete. */ + flush_workqueue(md->wq); r = dm_wait_for_completion(md, TASK_INTERRUPTIBLE); down_write(&md->io_lock);