]> pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/md/dm.c
Merge branch 'powerpc-next' of master.kernel.org:/pub/scm/linux/kernel/git/galak...
[linux-2.6-omap-h63xx.git] / drivers / md / dm.c
index 11f422ecfda000a6ee35ead97dbf8d4c0d433e08..6617ce4af09579285a2be1090adde8c9f61a3fb4 100644 (file)
@@ -71,6 +71,19 @@ union map_info *dm_get_mapinfo(struct bio *bio)
 #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 mutex suspend_lock;
@@ -95,6 +108,11 @@ struct mapped_device {
        struct bio_list deferred;
        struct bio_list pushback;
 
+       /*
+        * Processing queue (flush/barriers)
+        */
+       struct workqueue_struct *wq;
+
        /*
         * The current mapping.
         */
@@ -1044,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);
@@ -1053,6 +1075,8 @@ static struct mapped_device *alloc_dev(int minor)
 
        return md;
 
+bad_thread:
+       put_disk(md->disk);
 bad_disk:
        bioset_free(md->bs);
 bad_no_bioset:
@@ -1080,6 +1104,7 @@ static void free_dev(struct mapped_device *md)
                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);
@@ -1259,6 +1284,29 @@ void dm_put(struct mapped_device *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
  */
@@ -1285,6 +1333,44 @@ static void __merge_pushback_list(struct mapped_device *md)
        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);
+}
+
 /*
  * Swap in a new table (destroying old one).
  */
@@ -1357,7 +1443,7 @@ int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
 {
        struct dm_table *map = NULL;
        DECLARE_WAITQUEUE(wait, current);
-       int pending, r = 0;
+       int r = 0;
        int do_lockfs = suspend_flags & DM_SUSPEND_LOCKFS_FLAG ? 1 : 0;
        int noflush = suspend_flags & DM_SUSPEND_NOFLUSH_FLAG ? 1 : 0;
 
@@ -1414,20 +1500,9 @@ int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
                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);
-
-               smp_mb();
-               pending = atomic_read(&md->pending);
-               if (!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);
@@ -1437,13 +1512,10 @@ int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
        up_write(&md->io_lock);
 
        /* were we interrupted ? */
-       if (pending) {
-               down_write(&md->io_lock);
-               __flush_deferred_io(md);
-               up_write(&md->io_lock);
+       if (r < 0) {
+               dm_queue_flush(md, DM_WQ_FLUSH_DEFERRED, NULL);
 
                unlock_fs(md);
-               r = -EINTR;
                goto out; /* pushback list is already flushed, so skip flush */
        }
 
@@ -1452,16 +1524,12 @@ int dm_suspend(struct mapped_device *md, unsigned suspend_flags)
        set_bit(DMF_SUSPENDED, &md->flags);
 
 flush_and_out:
-       if (r && noflush) {
+       if (r && noflush)
                /*
                 * Because there may be already I/Os in the pushback list,
                 * flush them before return.
                 */
-               down_write(&md->io_lock);
-               __merge_pushback_list(md);
-               __flush_deferred_io(md);
-               up_write(&md->io_lock);
-       }
+               dm_queue_flush(md, DM_WQ_FLUSH_ALL, NULL);
 
 out:
        if (r && md->suspended_bdev) {
@@ -1493,9 +1561,7 @@ int dm_resume(struct mapped_device *md)
        if (r)
                goto out;
 
-       down_write(&md->io_lock);
-       __flush_deferred_io(md);
-       up_write(&md->io_lock);
+       dm_queue_flush(md, DM_WQ_FLUSH_DEFERRED, NULL);
 
        unlock_fs(md);