dm: tidy dm_suspend
[cascardo/linux.git] / drivers / md / dm.c
index f2d24eb..5191954 100644 (file)
@@ -73,7 +73,7 @@ union map_info *dm_get_mapinfo(struct bio *bio)
 
 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;
@@ -181,7 +181,7 @@ static void local_exit(void)
        DMINFO("cleaned up");
 }
 
-int (*_inits[])(void) __initdata = {
+static int (*_inits[])(void) __initdata = {
        local_init,
        dm_target_init,
        dm_linear_init,
@@ -189,7 +189,7 @@ int (*_inits[])(void) __initdata = {
        dm_interface_init,
 };
 
-void (*_exits[])(void) = {
+static void (*_exits[])(void) = {
        local_exit,
        dm_target_exit,
        dm_linear_exit,
@@ -982,7 +982,7 @@ 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)
@@ -990,11 +990,11 @@ static struct mapped_device *alloc_dev(int minor)
        else
                r = specific_minor(md, 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);
@@ -1006,7 +1006,7 @@ static struct mapped_device *alloc_dev(int minor)
 
        md->queue = blk_alloc_queue(GFP_KERNEL);
        if (!md->queue)
-               goto bad1_free_minor;
+               goto bad_queue;
 
        md->queue->queuedata = md;
        md->queue->backing_dev_info.congested_fn = dm_any_congested;
@@ -1017,11 +1017,11 @@ static struct mapped_device *alloc_dev(int minor)
 
        md->io_pool = mempool_create_slab_pool(MIN_IOS, _io_cache);
        if (!md->io_pool)
-               goto bad2;
+               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)
@@ -1029,7 +1029,7 @@ static struct mapped_device *alloc_dev(int minor)
 
        md->disk = alloc_disk(1);
        if (!md->disk)
-               goto bad4;
+               goto bad_disk;
 
        atomic_set(&md->pending, 0);
        init_waitqueue_head(&md->wait);
@@ -1053,19 +1053,19 @@ static struct mapped_device *alloc_dev(int minor)
 
        return md;
 
- bad4:
+bad_disk:
        bioset_free(md->bs);
- bad_no_bioset:
+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);
- bad1_free_minor:
+bad_queue:
        free_minor(minor);
- bad1:
+bad_minor:
        module_put(THIS_MODULE);
- bad0:
+bad_module_get:
        kfree(md);
        return NULL;
 }
@@ -1262,17 +1262,27 @@ EXPORT_SYMBOL_GPL(dm_put);
 /*
  * 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;
+       while ((c = bio_list_pop(&md->deferred))) {
                if (__split_bio(md, c))
                        bio_io_error(c);
-               c = n;
        }
+
+       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);
 }
 
 /*
@@ -1282,7 +1292,7 @@ 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))
@@ -1297,7 +1307,7 @@ int dm_swap_table(struct mapped_device *md, struct dm_table *table)
        r = __bind(md, table);
 
 out:
-       up(&md->suspend_lock);
+       mutex_unlock(&md->suspend_lock);
        return r;
 }
 
@@ -1346,17 +1356,17 @@ static void unlock_fs(struct mapped_device *md)
 int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
 {
        struct dm_table *map = NULL;
-       unsigned long flags;
        DECLARE_WAITQUEUE(wait, current);
-       struct bio *def;
-       int r = -EINVAL;
+       int pending, 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))
+       if (dm_suspended(md)) {
+               r = -EINVAL;
                goto out_unlock;
+       }
 
        map = dm_get_table(md);
 
@@ -1378,16 +1388,16 @@ int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
                        r = -ENOMEM;
                        goto flush_and_out;
                }
-       }
 
-       /*
-        * Flush I/O to the device.
-        * noflush supersedes do_lockfs, because lock_fs() needs to flush I/Os.
-        */
-       if (do_lockfs && !noflush) {
-               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;
+               }
        }
 
        /*
@@ -1410,7 +1420,9 @@ int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
        while (1) {
                set_current_state(TASK_INTERRUPTIBLE);
 
-               if (!atomic_read(&md->pending) || signal_pending(current))
+               smp_mb();
+               pending = atomic_read(&md->pending);
+               if (!pending || signal_pending(current))
                        break;
 
                io_schedule();
@@ -1420,22 +1432,16 @@ int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
        down_write(&md->io_lock);
        remove_wait_queue(&md->wait, &wait);
 
-       if (noflush) {
-               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 (noflush)
+               __merge_pushback_list(md);
 
        /* 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);
+       if (pending) {
+               __flush_deferred_io(md);
                up_write(&md->io_lock);
+
                unlock_fs(md);
+               r = -EINTR;
                goto out; /* pushback list is already flushed, so skip flush */
        }
        up_write(&md->io_lock);
@@ -1444,8 +1450,6 @@ int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
 
        set_bit(DMF_SUSPENDED, &md->flags);
 
-       r = 0;
-
 flush_and_out:
        if (r && noflush) {
                /*
@@ -1453,15 +1457,8 @@ flush_and_out:
                 * flush them before return.
                 */
                down_write(&md->io_lock);
-
-               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);
-
-               def = bio_list_get(&md->deferred);
-               __flush_deferred_io(md, def);
+               __merge_pushback_list(md);
+               __flush_deferred_io(md);
                up_write(&md->io_lock);
        }
 
@@ -1474,17 +1471,16 @@ out:
        dm_table_put(map);
 
 out_unlock:
-       up(&md->suspend_lock);
+       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;
 
@@ -1497,10 +1493,7 @@ int dm_resume(struct mapped_device *md)
                goto out;
 
        down_write(&md->io_lock);
-       clear_bit(DMF_BLOCK_IO, &md->flags);
-
-       def = bio_list_get(&md->deferred);
-       __flush_deferred_io(md, def);
+       __flush_deferred_io(md);
        up_write(&md->io_lock);
 
        unlock_fs(md);
@@ -1520,7 +1513,7 @@ int dm_resume(struct mapped_device *md)
 
 out:
        dm_table_put(map);
-       up(&md->suspend_lock);
+       mutex_unlock(&md->suspend_lock);
 
        return r;
 }