USB: musb free_irq bugfix
[safe/jmp/linux-2.6] / drivers / md / dm-mpath.c
index f9b7b32..095f77b 100644 (file)
@@ -5,11 +5,12 @@
  * This file is released under the GPL.
  */
 
-#include "dm.h"
+#include <linux/device-mapper.h>
+
 #include "dm-path-selector.h"
-#include "dm-hw-handler.h"
 #include "dm-bio-list.h"
 #include "dm-bio-record.h"
+#include "dm-uevent.h"
 
 #include <linux/ctype.h>
 #include <linux/init.h>
 #include <linux/slab.h>
 #include <linux/time.h>
 #include <linux/workqueue.h>
+#include <scsi/scsi_dh.h>
 #include <asm/atomic.h>
 
+#define DM_MSG_PREFIX "multipath"
 #define MESG_STR(x) x, sizeof(x)
 
 /* Path properties */
@@ -28,9 +31,11 @@ struct pgpath {
        struct list_head list;
 
        struct priority_group *pg;      /* Owning PG */
+       unsigned is_active;             /* Path status */
        unsigned fail_count;            /* Cumulative failure count */
 
-       struct path path;
+       struct dm_path path;
+       struct work_struct deactivate_path;
 };
 
 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
@@ -59,7 +64,9 @@ struct multipath {
 
        spinlock_t lock;
 
-       struct hw_handler hw_handler;
+       const char *hw_handler_name;
+       struct work_struct activate_path;
+       struct pgpath *pgpath_to_activate;
        unsigned nr_priority_groups;
        struct list_head priority_groups;
        unsigned pg_init_required;      /* pg_init needs calling? */
@@ -74,6 +81,8 @@ struct multipath {
        unsigned queue_io;              /* Must we queue all I/O? */
        unsigned queue_if_no_path;      /* Queue I/O if last path fails? */
        unsigned saved_queue_if_no_path;/* Saved state during suspension */
+       unsigned pg_init_retries;       /* Number of times to retry pg_init */
+       unsigned pg_init_count;         /* Number of times pg_init called */
 
        struct work_struct process_queued_ios;
        struct bio_list queued_ios;
@@ -82,7 +91,7 @@ struct multipath {
        struct work_struct trigger_event;
 
        /*
-        * We must use a mempool of mpath_io structs so that we
+        * We must use a mempool of dm_mpath_io structs so that we
         * can resubmit bios on error.
         */
        mempool_t *mpio_pool;
@@ -91,7 +100,7 @@ struct multipath {
 /*
  * Context information attached to each bio we process.
  */
-struct mpath_io {
+struct dm_mpath_io {
        struct pgpath *pgpath;
        struct dm_bio_details details;
 };
@@ -100,11 +109,13 @@ typedef int (*action_fn) (struct pgpath *pgpath);
 
 #define MIN_IOS 256    /* Mempool size */
 
-static kmem_cache_t *_mpio_cache;
+static struct kmem_cache *_mpio_cache;
 
-struct workqueue_struct *kmultipathd;
-static void process_queued_ios(void *data);
-static void trigger_event(void *data);
+static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
+static void process_queued_ios(struct work_struct *work);
+static void trigger_event(struct work_struct *work);
+static void activate_path(struct work_struct *work);
+static void deactivate_path(struct work_struct *work);
 
 
 /*-----------------------------------------------
@@ -113,42 +124,56 @@ static void trigger_event(void *data);
 
 static struct pgpath *alloc_pgpath(void)
 {
-       struct pgpath *pgpath = kmalloc(sizeof(*pgpath), GFP_KERNEL);
+       struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
 
        if (pgpath) {
-               memset(pgpath, 0, sizeof(*pgpath));
-               pgpath->path.is_active = 1;
+               pgpath->is_active = 1;
+               INIT_WORK(&pgpath->deactivate_path, deactivate_path);
        }
 
        return pgpath;
 }
 
-static inline void free_pgpath(struct pgpath *pgpath)
+static void free_pgpath(struct pgpath *pgpath)
 {
        kfree(pgpath);
 }
 
+static void deactivate_path(struct work_struct *work)
+{
+       struct pgpath *pgpath =
+               container_of(work, struct pgpath, deactivate_path);
+
+       blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
+}
+
 static struct priority_group *alloc_priority_group(void)
 {
        struct priority_group *pg;
 
-       pg = kmalloc(sizeof(*pg), GFP_KERNEL);
-       if (!pg)
-               return NULL;
+       pg = kzalloc(sizeof(*pg), GFP_KERNEL);
 
-       memset(pg, 0, sizeof(*pg));
-       INIT_LIST_HEAD(&pg->pgpaths);
+       if (pg)
+               INIT_LIST_HEAD(&pg->pgpaths);
 
        return pg;
 }
 
 static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
 {
+       unsigned long flags;
        struct pgpath *pgpath, *tmp;
+       struct multipath *m = ti->private;
 
        list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
                list_del(&pgpath->list);
+               if (m->hw_handler_name)
+                       scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
                dm_put_device(ti, pgpath->path.dev);
+               spin_lock_irqsave(&m->lock, flags);
+               if (m->pgpath_to_activate == pgpath)
+                       m->pgpath_to_activate = NULL;
+               spin_unlock_irqrestore(&m->lock, flags);
                free_pgpath(pgpath);
        }
 }
@@ -167,24 +192,25 @@ static void free_priority_group(struct priority_group *pg,
        kfree(pg);
 }
 
-static struct multipath *alloc_multipath(void)
+static struct multipath *alloc_multipath(struct dm_target *ti)
 {
        struct multipath *m;
 
-       m = kmalloc(sizeof(*m), GFP_KERNEL);
+       m = kzalloc(sizeof(*m), GFP_KERNEL);
        if (m) {
-               memset(m, 0, sizeof(*m));
                INIT_LIST_HEAD(&m->priority_groups);
                spin_lock_init(&m->lock);
                m->queue_io = 1;
-               INIT_WORK(&m->process_queued_ios, process_queued_ios, m);
-               INIT_WORK(&m->trigger_event, trigger_event, m);
-               m->mpio_pool = mempool_create(MIN_IOS, mempool_alloc_slab,
-                                             mempool_free_slab, _mpio_cache);
+               INIT_WORK(&m->process_queued_ios, process_queued_ios);
+               INIT_WORK(&m->trigger_event, trigger_event);
+               INIT_WORK(&m->activate_path, activate_path);
+               m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
                if (!m->mpio_pool) {
                        kfree(m);
                        return NULL;
                }
+               m->ti = ti;
+               ti->private = m;
        }
 
        return m;
@@ -193,18 +219,13 @@ static struct multipath *alloc_multipath(void)
 static void free_multipath(struct multipath *m)
 {
        struct priority_group *pg, *tmp;
-       struct hw_handler *hwh = &m->hw_handler;
 
        list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
                list_del(&pg->list);
                free_priority_group(pg, m->ti);
        }
 
-       if (hwh->type) {
-               hwh->type->destroy(hwh);
-               dm_put_hw_handler(hwh->type);
-       }
-
+       kfree(m->hw_handler_name);
        mempool_destroy(m->mpio_pool);
        kfree(m);
 }
@@ -216,23 +237,23 @@ static void free_multipath(struct multipath *m)
 
 static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
 {
-       struct hw_handler *hwh = &m->hw_handler;
-
        m->current_pg = pgpath->pg;
 
        /* Must we initialise the PG first, and queue I/O till it's ready? */
-       if (hwh->type && hwh->type->pg_init) {
+       if (m->hw_handler_name) {
                m->pg_init_required = 1;
                m->queue_io = 1;
        } else {
                m->pg_init_required = 0;
                m->queue_io = 0;
        }
+
+       m->pg_init_count = 0;
 }
 
 static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
 {
-       struct path *path;
+       struct dm_path *path;
 
        path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
        if (!path)
@@ -285,10 +306,27 @@ failed:
        m->current_pg = NULL;
 }
 
-static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio,
-                 unsigned was_queued)
+/*
+ * Check whether bios must be queued in the device-mapper core rather
+ * than here in the target.
+ *
+ * m->lock must be held on entry.
+ *
+ * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
+ * same value then we are not between multipath_presuspend()
+ * and multipath_resume() calls and we have no need to check
+ * for the DMF_NOFLUSH_SUSPENDING flag.
+ */
+static int __must_push_back(struct multipath *m)
+{
+       return (m->queue_if_no_path != m->saved_queue_if_no_path &&
+               dm_noflush_suspending(m->ti));
+}
+
+static int map_io(struct multipath *m, struct bio *bio,
+                 struct dm_mpath_io *mpio, unsigned was_queued)
 {
-       int r = 1;
+       int r = DM_MAPIO_REMAPPED;
        unsigned long flags;
        struct pgpath *pgpath;
 
@@ -313,11 +351,13 @@ static int map_io(struct multipath *m, struct bio *bio, struct mpath_io *mpio,
                    !m->queue_io)
                        queue_work(kmultipathd, &m->process_queued_ios);
                pgpath = NULL;
-               r = 0;
-       } else if (!pgpath)
-               r = -EIO;               /* Failed */
-       else
+               r = DM_MAPIO_SUBMITTED;
+       } else if (pgpath)
                bio->bi_bdev = pgpath->path.dev->bdev;
+       else if (__must_push_back(m))
+               r = DM_MAPIO_REQUEUE;
+       else
+               r = -EIO;       /* Failed */
 
        mpio->pgpath = pgpath;
 
@@ -358,7 +398,7 @@ static void dispatch_queued_ios(struct multipath *m)
        int r;
        unsigned long flags;
        struct bio *bio = NULL, *next;
-       struct mpath_io *mpio;
+       struct dm_mpath_io *mpio;
        union map_info *info;
 
        spin_lock_irqsave(&m->lock, flags);
@@ -374,18 +414,20 @@ static void dispatch_queued_ios(struct multipath *m)
 
                r = map_io(m, bio, mpio, 1);
                if (r < 0)
-                       bio_endio(bio, bio->bi_size, r);
-               else if (r == 1)
+                       bio_endio(bio, r);
+               else if (r == DM_MAPIO_REMAPPED)
                        generic_make_request(bio);
+               else if (r == DM_MAPIO_REQUEUE)
+                       bio_endio(bio, -EIO);
 
                bio = next;
        }
 }
 
-static void process_queued_ios(void *data)
+static void process_queued_ios(struct work_struct *work)
 {
-       struct multipath *m = (struct multipath *) data;
-       struct hw_handler *hwh = &m->hw_handler;
+       struct multipath *m =
+               container_of(work, struct multipath, process_queued_ios);
        struct pgpath *pgpath = NULL;
        unsigned init_required = 0, must_queue = 1;
        unsigned long flags;
@@ -404,7 +446,9 @@ static void process_queued_ios(void *data)
            (!pgpath && !m->queue_if_no_path))
                must_queue = 0;
 
-       if (m->pg_init_required && !m->pg_init_in_progress) {
+       if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
+               m->pgpath_to_activate = pgpath;
+               m->pg_init_count++;
                m->pg_init_required = 0;
                m->pg_init_in_progress = 1;
                init_required = 1;
@@ -414,7 +458,7 @@ out:
        spin_unlock_irqrestore(&m->lock, flags);
 
        if (init_required)
-               hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
+               queue_work(kmpath_handlerd, &m->activate_path);
 
        if (!must_queue)
                dispatch_queued_ios(m);
@@ -424,9 +468,10 @@ out:
  * An event is triggered whenever a path is taken out of use.
  * Includes path failure and PG bypass.
  */
-static void trigger_event(void *data)
+static void trigger_event(struct work_struct *work)
 {
-       struct multipath *m = (struct multipath *) data;
+       struct multipath *m =
+               container_of(work, struct multipath, trigger_event);
 
        dm_table_event(m->ti->table);
 }
@@ -447,8 +492,6 @@ struct param {
        char *error;
 };
 
-#define ESTR(s) ("dm-multipath: " s)
-
 static int read_param(struct param *param, char *str, unsigned *v, char **error)
 {
        if (!str ||
@@ -496,23 +539,25 @@ static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
        unsigned ps_argc;
 
        static struct param _params[] = {
-               {0, 1024, ESTR("invalid number of path selector args")},
+               {0, 1024, "invalid number of path selector args"},
        };
 
        pst = dm_get_path_selector(shift(as));
        if (!pst) {
-               ti->error = ESTR("unknown path selector type");
+               ti->error = "unknown path selector type";
                return -EINVAL;
        }
 
        r = read_param(_params, shift(as), &ps_argc, &ti->error);
-       if (r)
+       if (r) {
+               dm_put_path_selector(pst);
                return -EINVAL;
+       }
 
        r = pst->create(&pg->ps, ps_argc, as->argv);
        if (r) {
                dm_put_path_selector(pst);
-               ti->error = ESTR("path selector constructor failed");
+               ti->error = "path selector constructor failed";
                return r;
        }
 
@@ -527,24 +572,34 @@ static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
 {
        int r;
        struct pgpath *p;
+       struct multipath *m = ti->private;
 
        /* we need at least a path arg */
        if (as->argc < 1) {
-               ti->error = ESTR("no device given");
-               return NULL;
+               ti->error = "no device given";
+               return ERR_PTR(-EINVAL);
        }
 
        p = alloc_pgpath();
        if (!p)
-               return NULL;
+               return ERR_PTR(-ENOMEM);
 
        r = dm_get_device(ti, shift(as), ti->begin, ti->len,
                          dm_table_get_mode(ti->table), &p->path.dev);
        if (r) {
-               ti->error = ESTR("error getting device");
+               ti->error = "error getting device";
                goto bad;
        }
 
+       if (m->hw_handler_name) {
+               r = scsi_dh_attach(bdev_get_queue(p->path.dev->bdev),
+                                  m->hw_handler_name);
+               if (r < 0) {
+                       dm_put_device(ti, p->path.dev);
+                       goto bad;
+               }
+       }
+
        r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
        if (r) {
                dm_put_device(ti, p->path.dev);
@@ -555,32 +610,32 @@ static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
 
  bad:
        free_pgpath(p);
-       return NULL;
+       return ERR_PTR(r);
 }
 
 static struct priority_group *parse_priority_group(struct arg_set *as,
-                                                  struct multipath *m,
-                                                  struct dm_target *ti)
+                                                  struct multipath *m)
 {
        static struct param _params[] = {
-               {1, 1024, ESTR("invalid number of paths")},
-               {0, 1024, ESTR("invalid number of selector args")}
+               {1, 1024, "invalid number of paths"},
+               {0, 1024, "invalid number of selector args"}
        };
 
        int r;
        unsigned i, nr_selector_args, nr_params;
        struct priority_group *pg;
+       struct dm_target *ti = m->ti;
 
        if (as->argc < 2) {
                as->argc = 0;
-               ti->error = ESTR("not enough priority group aruments");
-               return NULL;
+               ti->error = "not enough priority group arguments";
+               return ERR_PTR(-EINVAL);
        }
 
        pg = alloc_priority_group();
        if (!pg) {
-               ti->error = ESTR("couldn't allocate priority group");
-               return NULL;
+               ti->error = "couldn't allocate priority group";
+               return ERR_PTR(-ENOMEM);
        }
        pg->m = m;
 
@@ -604,15 +659,19 @@ static struct priority_group *parse_priority_group(struct arg_set *as,
                struct pgpath *pgpath;
                struct arg_set path_args;
 
-               if (as->argc < nr_params)
+               if (as->argc < nr_params) {
+                       ti->error = "not enough path parameters";
                        goto bad;
+               }
 
                path_args.argc = nr_params;
                path_args.argv = as->argv;
 
                pgpath = parse_path(&path_args, &pg->ps, ti);
-               if (!pgpath)
+               if (IS_ERR(pgpath)) {
+                       r = PTR_ERR(pgpath);
                        goto bad;
+               }
 
                pgpath->pg = pg;
                list_add_tail(&pgpath->list, &pg->pgpaths);
@@ -623,54 +682,51 @@ static struct priority_group *parse_priority_group(struct arg_set *as,
 
  bad:
        free_priority_group(pg, ti);
-       return NULL;
+       return ERR_PTR(r);
 }
 
-static int parse_hw_handler(struct arg_set *as, struct multipath *m,
-                           struct dm_target *ti)
+static int parse_hw_handler(struct arg_set *as, struct multipath *m)
 {
-       int r;
-       struct hw_handler_type *hwht;
        unsigned hw_argc;
+       struct dm_target *ti = m->ti;
 
        static struct param _params[] = {
-               {0, 1024, ESTR("invalid number of hardware handler args")},
+               {0, 1024, "invalid number of hardware handler args"},
        };
 
-       r = read_param(_params, shift(as), &hw_argc, &ti->error);
-       if (r)
+       if (read_param(_params, shift(as), &hw_argc, &ti->error))
                return -EINVAL;
 
        if (!hw_argc)
                return 0;
 
-       hwht = dm_get_hw_handler(shift(as));
-       if (!hwht) {
-               ti->error = ESTR("unknown hardware handler type");
+       m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
+       request_module("scsi_dh_%s", m->hw_handler_name);
+       if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
+               ti->error = "unknown hardware handler type";
+               kfree(m->hw_handler_name);
+               m->hw_handler_name = NULL;
                return -EINVAL;
        }
 
-       r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
-       if (r) {
-               dm_put_hw_handler(hwht);
-               ti->error = ESTR("hardware handler constructor failed");
-               return r;
-       }
-
-       m->hw_handler.type = hwht;
+       if (hw_argc > 1)
+               DMWARN("Ignoring user-specified arguments for "
+                      "hardware handler \"%s\"", m->hw_handler_name);
        consume(as, hw_argc - 1);
 
        return 0;
 }
 
-static int parse_features(struct arg_set *as, struct multipath *m,
-                         struct dm_target *ti)
+static int parse_features(struct arg_set *as, struct multipath *m)
 {
        int r;
        unsigned argc;
+       struct dm_target *ti = m->ti;
+       const char *param_name;
 
        static struct param _params[] = {
-               {0, 1, ESTR("invalid number of feature args")},
+               {0, 3, "invalid number of feature args"},
+               {1, 50, "pg_init_retries must be between 1 and 50"},
        };
 
        r = read_param(_params, shift(as), &argc, &ti->error);
@@ -680,12 +736,28 @@ static int parse_features(struct arg_set *as, struct multipath *m,
        if (!argc)
                return 0;
 
-       if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
-               return queue_if_no_path(m, 1, 0);
-       else {
+       do {
+               param_name = shift(as);
+               argc--;
+
+               if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
+                       r = queue_if_no_path(m, 1, 0);
+                       continue;
+               }
+
+               if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
+                   (argc >= 1)) {
+                       r = read_param(_params + 1, shift(as),
+                                      &m->pg_init_retries, &ti->error);
+                       argc--;
+                       continue;
+               }
+
                ti->error = "Unrecognised multipath feature request";
-               return -EINVAL;
-       }
+               r = -EINVAL;
+       } while (argc && !r);
+
+       return r;
 }
 
 static int multipath_ctr(struct dm_target *ti, unsigned int argc,
@@ -693,8 +765,8 @@ static int multipath_ctr(struct dm_target *ti, unsigned int argc,
 {
        /* target parameters */
        static struct param _params[] = {
-               {1, 1024, ESTR("invalid number of priority groups")},
-               {1, 1024, ESTR("invalid initial priority group number")},
+               {1, 1024, "invalid number of priority groups"},
+               {1, 1024, "invalid initial priority group number"},
        };
 
        int r;
@@ -706,17 +778,17 @@ static int multipath_ctr(struct dm_target *ti, unsigned int argc,
        as.argc = argc;
        as.argv = argv;
 
-       m = alloc_multipath();
+       m = alloc_multipath(ti);
        if (!m) {
-               ti->error = ESTR("can't allocate multipath");
+               ti->error = "can't allocate multipath";
                return -EINVAL;
        }
 
-       r = parse_features(&as, m, ti);
+       r = parse_features(&as, m);
        if (r)
                goto bad;
 
-       r = parse_hw_handler(&as, m, ti);
+       r = parse_hw_handler(&as, m);
        if (r)
                goto bad;
 
@@ -732,9 +804,9 @@ static int multipath_ctr(struct dm_target *ti, unsigned int argc,
        while (as.argc) {
                struct priority_group *pg;
 
-               pg = parse_priority_group(&as, m, ti);
-               if (!pg) {
-                       r = -EINVAL;
+               pg = parse_priority_group(&as, m);
+               if (IS_ERR(pg)) {
+                       r = PTR_ERR(pg);
                        goto bad;
                }
 
@@ -747,14 +819,11 @@ static int multipath_ctr(struct dm_target *ti, unsigned int argc,
        }
 
        if (pg_count != m->nr_priority_groups) {
-               ti->error = ESTR("priority group count mismatch");
+               ti->error = "priority group count mismatch";
                r = -EINVAL;
                goto bad;
        }
 
-       ti->private = m;
-       m->ti = ti;
-
        return 0;
 
  bad:
@@ -766,6 +835,7 @@ static void multipath_dtr(struct dm_target *ti)
 {
        struct multipath *m = (struct multipath *) ti->private;
 
+       flush_workqueue(kmpath_handlerd);
        flush_workqueue(kmultipathd);
        free_multipath(m);
 }
@@ -777,19 +847,16 @@ static int multipath_map(struct dm_target *ti, struct bio *bio,
                         union map_info *map_context)
 {
        int r;
-       struct mpath_io *mpio;
+       struct dm_mpath_io *mpio;
        struct multipath *m = (struct multipath *) ti->private;
 
-       if (bio_barrier(bio))
-               return -EOPNOTSUPP;
-
        mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
        dm_bio_record(&mpio->details, bio);
 
        map_context->ptr = mpio;
-       bio->bi_rw |= (1 << BIO_RW_FAILFAST);
+       bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
        r = map_io(m, bio, mpio, 0);
-       if (r < 0)
+       if (r < 0 || r == DM_MAPIO_REQUEUE)
                mempool_free(mpio, m->mpio_pool);
 
        return r;
@@ -805,13 +872,13 @@ static int fail_path(struct pgpath *pgpath)
 
        spin_lock_irqsave(&m->lock, flags);
 
-       if (!pgpath->path.is_active)
+       if (!pgpath->is_active)
                goto out;
 
-       DMWARN("dm-multipath: Failing path %s.", pgpath->path.dev->name);
+       DMWARN("Failing path %s.", pgpath->path.dev->name);
 
        pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
-       pgpath->path.is_active = 0;
+       pgpath->is_active = 0;
        pgpath->fail_count++;
 
        m->nr_valid_paths--;
@@ -819,7 +886,11 @@ static int fail_path(struct pgpath *pgpath)
        if (pgpath == m->current_pgpath)
                m->current_pgpath = NULL;
 
-       queue_work(kmultipathd, &m->trigger_event);
+       dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
+                     pgpath->path.dev->name, m->nr_valid_paths);
+
+       schedule_work(&m->trigger_event);
+       queue_work(kmultipathd, &pgpath->deactivate_path);
 
 out:
        spin_unlock_irqrestore(&m->lock, flags);
@@ -838,10 +909,10 @@ static int reinstate_path(struct pgpath *pgpath)
 
        spin_lock_irqsave(&m->lock, flags);
 
-       if (pgpath->path.is_active)
+       if (pgpath->is_active)
                goto out;
 
-       if (!pgpath->pg->ps.type) {
+       if (!pgpath->pg->ps.type->reinstate_path) {
                DMWARN("Reinstate path not supported by path selector %s",
                       pgpath->pg->ps.type->name);
                r = -EINVAL;
@@ -852,13 +923,16 @@ static int reinstate_path(struct pgpath *pgpath)
        if (r)
                goto out;
 
-       pgpath->path.is_active = 1;
+       pgpath->is_active = 1;
 
        m->current_pgpath = NULL;
        if (!m->nr_valid_paths++ && m->queue_size)
                queue_work(kmultipathd, &m->process_queued_ios);
 
-       queue_work(kmultipathd, &m->trigger_event);
+       dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
+                     pgpath->path.dev->name, m->nr_valid_paths);
+
+       schedule_work(&m->trigger_event);
 
 out:
        spin_unlock_irqrestore(&m->lock, flags);
@@ -902,7 +976,7 @@ static void bypass_pg(struct multipath *m, struct priority_group *pg,
 
        spin_unlock_irqrestore(&m->lock, flags);
 
-       queue_work(kmultipathd, &m->trigger_event);
+       schedule_work(&m->trigger_event);
 }
 
 /*
@@ -932,7 +1006,7 @@ static int switch_pg_num(struct multipath *m, const char *pgstr)
        }
        spin_unlock_irqrestore(&m->lock, flags);
 
-       queue_work(kmultipathd, &m->trigger_event);
+       schedule_work(&m->trigger_event);
        return 0;
 }
 
@@ -961,45 +1035,112 @@ static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
 }
 
 /*
- * pg_init must call this when it has completed its initialisation
+ * Should we retry pg_init immediately?
  */
-void dm_pg_init_complete(struct path *path, unsigned err_flags)
+static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
+{
+       unsigned long flags;
+       int limit_reached = 0;
+
+       spin_lock_irqsave(&m->lock, flags);
+
+       if (m->pg_init_count <= m->pg_init_retries)
+               m->pg_init_required = 1;
+       else
+               limit_reached = 1;
+
+       spin_unlock_irqrestore(&m->lock, flags);
+
+       return limit_reached;
+}
+
+static void pg_init_done(struct dm_path *path, int errors)
 {
        struct pgpath *pgpath = path_to_pgpath(path);
        struct priority_group *pg = pgpath->pg;
        struct multipath *m = pg->m;
        unsigned long flags;
 
-       /* We insist on failing the path if the PG is already bypassed. */
-       if (err_flags && pg->bypassed)
-               err_flags |= MP_FAIL_PATH;
-
-       if (err_flags & MP_FAIL_PATH)
+       /* device or driver problems */
+       switch (errors) {
+       case SCSI_DH_OK:
+               break;
+       case SCSI_DH_NOSYS:
+               if (!m->hw_handler_name) {
+                       errors = 0;
+                       break;
+               }
+               DMERR("Cannot failover device because scsi_dh_%s was not "
+                     "loaded.", m->hw_handler_name);
+               /*
+                * Fail path for now, so we do not ping pong
+                */
                fail_path(pgpath);
-
-       if (err_flags & MP_BYPASS_PG)
+               break;
+       case SCSI_DH_DEV_TEMP_BUSY:
+               /*
+                * Probably doing something like FW upgrade on the
+                * controller so try the other pg.
+                */
                bypass_pg(m, pg, 1);
+               break;
+       /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
+       case SCSI_DH_RETRY:
+       case SCSI_DH_IMM_RETRY:
+       case SCSI_DH_RES_TEMP_UNAVAIL:
+               if (pg_init_limit_reached(m, pgpath))
+                       fail_path(pgpath);
+               errors = 0;
+               break;
+       default:
+               /*
+                * We probably do not want to fail the path for a device
+                * error, but this is what the old dm did. In future
+                * patches we can do more advanced handling.
+                */
+               fail_path(pgpath);
+       }
 
        spin_lock_irqsave(&m->lock, flags);
-       if (err_flags) {
+       if (errors) {
+               DMERR("Could not failover device. Error %d.", errors);
                m->current_pgpath = NULL;
                m->current_pg = NULL;
-       } else if (!m->pg_init_required)
+       } else if (!m->pg_init_required) {
                m->queue_io = 0;
+               pg->bypassed = 0;
+       }
 
        m->pg_init_in_progress = 0;
        queue_work(kmultipathd, &m->process_queued_ios);
        spin_unlock_irqrestore(&m->lock, flags);
 }
 
+static void activate_path(struct work_struct *work)
+{
+       int ret;
+       struct multipath *m =
+               container_of(work, struct multipath, activate_path);
+       struct dm_path *path;
+       unsigned long flags;
+
+       spin_lock_irqsave(&m->lock, flags);
+       path = &m->pgpath_to_activate->path;
+       m->pgpath_to_activate = NULL;
+       spin_unlock_irqrestore(&m->lock, flags);
+       if (!path)
+               return;
+       ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
+       pg_init_done(path, ret);
+}
+
 /*
  * end_io handling
  */
 static int do_end_io(struct multipath *m, struct bio *bio,
-                    int error, struct mpath_io *mpio)
+                    int error, struct dm_mpath_io *mpio)
 {
-       struct hw_handler *hwh = &m->hw_handler;
-       unsigned err_flags = MP_FAIL_PATH;      /* Default behavior */
+       unsigned long flags;
 
        if (!error)
                return 0;       /* I/O complete */
@@ -1010,51 +1151,43 @@ static int do_end_io(struct multipath *m, struct bio *bio,
        if (error == -EOPNOTSUPP)
                return error;
 
-       spin_lock(&m->lock);
+       spin_lock_irqsave(&m->lock, flags);
        if (!m->nr_valid_paths) {
-               if (!m->queue_if_no_path) {
-                       spin_unlock(&m->lock);
+               if (__must_push_back(m)) {
+                       spin_unlock_irqrestore(&m->lock, flags);
+                       return DM_ENDIO_REQUEUE;
+               } else if (!m->queue_if_no_path) {
+                       spin_unlock_irqrestore(&m->lock, flags);
                        return -EIO;
                } else {
-                       spin_unlock(&m->lock);
+                       spin_unlock_irqrestore(&m->lock, flags);
                        goto requeue;
                }
        }
-       spin_unlock(&m->lock);
-
-       if (hwh->type && hwh->type->error)
-               err_flags = hwh->type->error(hwh, bio);
-
-       if (mpio->pgpath) {
-               if (err_flags & MP_FAIL_PATH)
-                       fail_path(mpio->pgpath);
-
-               if (err_flags & MP_BYPASS_PG)
-                       bypass_pg(m, mpio->pgpath->pg, 1);
-       }
+       spin_unlock_irqrestore(&m->lock, flags);
 
-       if (err_flags & MP_ERROR_IO)
-               return -EIO;
+       if (mpio->pgpath)
+               fail_path(mpio->pgpath);
 
       requeue:
        dm_bio_restore(&mpio->details, bio);
 
        /* queue for the daemon to resubmit or fail */
-       spin_lock(&m->lock);
+       spin_lock_irqsave(&m->lock, flags);
        bio_list_add(&m->queued_ios, bio);
        m->queue_size++;
        if (!m->queue_io)
                queue_work(kmultipathd, &m->process_queued_ios);
-       spin_unlock(&m->lock);
+       spin_unlock_irqrestore(&m->lock, flags);
 
-       return 1;       /* io not complete */
+       return DM_ENDIO_INCOMPLETE;     /* io not complete */
 }
 
 static int multipath_end_io(struct dm_target *ti, struct bio *bio,
                            int error, union map_info *map_context)
 {
-       struct multipath *m = (struct multipath *) ti->private;
-       struct mpath_io *mpio = (struct mpath_io *) map_context->ptr;
+       struct multipath *m = ti->private;
+       struct dm_mpath_io *mpio = map_context->ptr;
        struct pgpath *pgpath = mpio->pgpath;
        struct path_selector *ps;
        int r;
@@ -1065,7 +1198,7 @@ static int multipath_end_io(struct dm_target *ti, struct bio *bio,
                if (ps->type->end_io)
                        ps->type->end_io(ps, &pgpath->path);
        }
-       if (r <= 0)
+       if (r != DM_ENDIO_INCOMPLETE)
                mempool_free(mpio, m->mpio_pool);
 
        return r;
@@ -1119,7 +1252,6 @@ static int multipath_status(struct dm_target *ti, status_type_t type,
        int sz = 0;
        unsigned long flags;
        struct multipath *m = (struct multipath *) ti->private;
-       struct hw_handler *hwh = &m->hw_handler;
        struct priority_group *pg;
        struct pgpath *p;
        unsigned pg_num;
@@ -1129,18 +1261,20 @@ static int multipath_status(struct dm_target *ti, status_type_t type,
 
        /* Features */
        if (type == STATUSTYPE_INFO)
-               DMEMIT("1 %u ", m->queue_size);
-       else if (m->queue_if_no_path)
-               DMEMIT("1 queue_if_no_path ");
-       else
-               DMEMIT("0 ");
+               DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
+       else {
+               DMEMIT("%u ", m->queue_if_no_path +
+                             (m->pg_init_retries > 0) * 2);
+               if (m->queue_if_no_path)
+                       DMEMIT("queue_if_no_path ");
+               if (m->pg_init_retries)
+                       DMEMIT("pg_init_retries %u ", m->pg_init_retries);
+       }
 
-       if (hwh->type && hwh->type->status)
-               sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
-       else if (!hwh->type || type == STATUSTYPE_INFO)
+       if (!m->hw_handler_name || type == STATUSTYPE_INFO)
                DMEMIT("0 ");
        else
-               DMEMIT("1 %s ", hwh->type->name);
+               DMEMIT("1 %s ", m->hw_handler_name);
 
        DMEMIT("%u ", m->nr_priority_groups);
 
@@ -1177,7 +1311,7 @@ static int multipath_status(struct dm_target *ti, status_type_t type,
 
                        list_for_each_entry(p, &pg->pgpaths, list) {
                                DMEMIT("%s %s %u ", p->path.dev->name,
-                                      p->path.is_active ? "A" : "F",
+                                      p->is_active ? "A" : "F",
                                       p->fail_count);
                                if (pg->ps.type->status)
                                        sz += pg->ps.type->status(&pg->ps,
@@ -1250,7 +1384,7 @@ static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
        r = dm_get_device(ti, argv[1], ti->begin, ti->len,
                          dm_table_get_mode(ti->table), &dev);
        if (r) {
-               DMWARN("dm-multipath message: error getting device %s",
+               DMWARN("message: error getting device %s",
                       argv[1]);
                return -EINVAL;
        }
@@ -1266,12 +1400,41 @@ error:
        return -EINVAL;
 }
 
+static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
+                          unsigned long arg)
+{
+       struct multipath *m = (struct multipath *) ti->private;
+       struct block_device *bdev = NULL;
+       fmode_t mode = 0;
+       unsigned long flags;
+       int r = 0;
+
+       spin_lock_irqsave(&m->lock, flags);
+
+       if (!m->current_pgpath)
+               __choose_pgpath(m);
+
+       if (m->current_pgpath) {
+               bdev = m->current_pgpath->path.dev->bdev;
+               mode = m->current_pgpath->path.dev->mode;
+       }
+
+       if (m->queue_io)
+               r = -EAGAIN;
+       else if (!bdev)
+               r = -EIO;
+
+       spin_unlock_irqrestore(&m->lock, flags);
+
+       return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
+}
+
 /*-----------------------------------------------------------------
  * Module setup
  *---------------------------------------------------------------*/
 static struct target_type multipath_target = {
        .name = "multipath",
-       .version = {1, 0, 4},
+       .version = {1, 0, 5},
        .module = THIS_MODULE,
        .ctr = multipath_ctr,
        .dtr = multipath_dtr,
@@ -1281,6 +1444,7 @@ static struct target_type multipath_target = {
        .resume = multipath_resume,
        .status = multipath_status,
        .message = multipath_message,
+       .ioctl  = multipath_ioctl,
 };
 
 static int __init dm_multipath_init(void)
@@ -1288,28 +1452,41 @@ static int __init dm_multipath_init(void)
        int r;
 
        /* allocate a slab for the dm_ios */
-       _mpio_cache = kmem_cache_create("dm_mpath", sizeof(struct mpath_io),
-                                       0, 0, NULL, NULL);
+       _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
        if (!_mpio_cache)
                return -ENOMEM;
 
        r = dm_register_target(&multipath_target);
        if (r < 0) {
-               DMERR("%s: register failed %d", multipath_target.name, r);
+               DMERR("register failed %d", r);
                kmem_cache_destroy(_mpio_cache);
                return -EINVAL;
        }
 
        kmultipathd = create_workqueue("kmpathd");
        if (!kmultipathd) {
-               DMERR("%s: failed to create workqueue kmpathd",
-                               multipath_target.name);
+               DMERR("failed to create workqueue kmpathd");
+               dm_unregister_target(&multipath_target);
+               kmem_cache_destroy(_mpio_cache);
+               return -ENOMEM;
+       }
+
+       /*
+        * A separate workqueue is used to handle the device handlers
+        * to avoid overloading existing workqueue. Overloading the
+        * old workqueue would also create a bottleneck in the
+        * path of the storage hardware device activation.
+        */
+       kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
+       if (!kmpath_handlerd) {
+               DMERR("failed to create workqueue kmpath_handlerd");
+               destroy_workqueue(kmultipathd);
                dm_unregister_target(&multipath_target);
                kmem_cache_destroy(_mpio_cache);
                return -ENOMEM;
        }
 
-       DMINFO("dm-multipath version %u.%u.%u loaded",
+       DMINFO("version %u.%u.%u loaded",
               multipath_target.version[0], multipath_target.version[1],
               multipath_target.version[2]);
 
@@ -1318,19 +1495,13 @@ static int __init dm_multipath_init(void)
 
 static void __exit dm_multipath_exit(void)
 {
-       int r;
-
+       destroy_workqueue(kmpath_handlerd);
        destroy_workqueue(kmultipathd);
 
-       r = dm_unregister_target(&multipath_target);
-       if (r < 0)
-               DMERR("%s: target unregister failed %d",
-                     multipath_target.name, r);
+       dm_unregister_target(&multipath_target);
        kmem_cache_destroy(_mpio_cache);
 }
 
-EXPORT_SYMBOL_GPL(dm_pg_init_complete);
-
 module_init(dm_multipath_init);
 module_exit(dm_multipath_exit);