fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / md / dm-mpath.c
index cf70a0d..3aa0135 100644 (file)
@@ -21,6 +21,7 @@
 #include <linux/workqueue.h>
 #include <asm/atomic.h>
 
+#define DM_MSG_PREFIX "multipath"
 #define MESG_STR(x) x, sizeof(x)
 
 /* Path properties */
@@ -30,7 +31,7 @@ struct pgpath {
        struct priority_group *pg;      /* Owning PG */
        unsigned fail_count;            /* Cumulative failure count */
 
-       struct path path;
+       struct dm_path path;
 };
 
 #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
@@ -100,11 +101,11 @@ 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 void process_queued_ios(struct work_struct *work);
+static void trigger_event(struct work_struct *work);
 
 
 /*-----------------------------------------------
@@ -113,12 +114,10 @@ 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));
+       if (pgpath)
                pgpath->path.is_active = 1;
-       }
 
        return pgpath;
 }
@@ -132,12 +131,10 @@ 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;
 }
@@ -167,24 +164,24 @@ 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);
+               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;
@@ -232,7 +229,7 @@ static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
 
 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 +282,27 @@ failed:
        m->current_pg = NULL;
 }
 
+/*
+ * 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 mpath_io *mpio,
                  unsigned was_queued)
 {
-       int r = 1;
+       int r = DM_MAPIO_REMAPPED;
        unsigned long flags;
        struct pgpath *pgpath;
 
@@ -313,11 +327,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;
 
@@ -375,16 +391,19 @@ 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)
+               else if (r == DM_MAPIO_REMAPPED)
                        generic_make_request(bio);
+               else if (r == DM_MAPIO_REQUEUE)
+                       bio_endio(bio, bio->bi_size, -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 multipath *m =
+               container_of(work, struct multipath, process_queued_ios);
        struct hw_handler *hwh = &m->hw_handler;
        struct pgpath *pgpath = NULL;
        unsigned init_required = 0, must_queue = 1;
@@ -424,9 +443,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 +467,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,12 +514,12 @@ 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;
        }
 
@@ -512,7 +530,7 @@ static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
        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;
        }
 
@@ -530,7 +548,7 @@ static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
 
        /* we need at least a path arg */
        if (as->argc < 1) {
-               ti->error = ESTR("no device given");
+               ti->error = "no device given";
                return NULL;
        }
 
@@ -541,7 +559,7 @@ static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
        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;
        }
 
@@ -559,27 +577,27 @@ static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
 }
 
 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");
+               ti->error = "not enough priority group aruments";
                return NULL;
        }
 
        pg = alloc_priority_group();
        if (!pg) {
-               ti->error = ESTR("couldn't allocate priority group");
+               ti->error = "couldn't allocate priority group";
                return NULL;
        }
        pg->m = m;
@@ -626,15 +644,15 @@ static struct priority_group *parse_priority_group(struct arg_set *as,
        return NULL;
 }
 
-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);
@@ -646,14 +664,14 @@ static int parse_hw_handler(struct arg_set *as, struct multipath *m,
 
        hwht = dm_get_hw_handler(shift(as));
        if (!hwht) {
-               ti->error = ESTR("unknown hardware handler type");
+               ti->error = "unknown hardware handler type";
                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");
+               ti->error = "hardware handler constructor failed";
                return r;
        }
 
@@ -663,14 +681,14 @@ static int parse_hw_handler(struct arg_set *as, struct multipath *m,
        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;
 
        static struct param _params[] = {
-               {0, 1, ESTR("invalid number of feature args")},
+               {0, 1, "invalid number of feature args"},
        };
 
        r = read_param(_params, shift(as), &argc, &ti->error);
@@ -693,8 +711,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,19 +724,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;
        }
 
-       m->ti = ti;
-
-       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;
 
@@ -734,7 +750,7 @@ 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);
+               pg = parse_priority_group(&as, m);
                if (!pg) {
                        r = -EINVAL;
                        goto bad;
@@ -749,13 +765,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;
-
        return 0;
 
  bad:
@@ -790,7 +804,7 @@ static int multipath_map(struct dm_target *ti, struct bio *bio,
        map_context->ptr = mpio;
        bio->bi_rw |= (1 << BIO_RW_FAILFAST);
        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;
@@ -809,7 +823,7 @@ static int fail_path(struct pgpath *pgpath)
        if (!pgpath->path.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;
@@ -964,7 +978,7 @@ static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
 /*
  * pg_init must call this when it has completed its initialisation
  */
-void dm_pg_init_complete(struct path *path, unsigned err_flags)
+void dm_pg_init_complete(struct dm_path *path, unsigned err_flags)
 {
        struct pgpath *pgpath = path_to_pgpath(path);
        struct priority_group *pg = pgpath->pg;
@@ -1014,7 +1028,10 @@ static int do_end_io(struct multipath *m, struct bio *bio,
 
        spin_lock_irqsave(&m->lock, flags);
        if (!m->nr_valid_paths) {
-               if (!m->queue_if_no_path) {
+               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 {
@@ -1049,7 +1066,7 @@ static int do_end_io(struct multipath *m, struct bio *bio,
                queue_work(kmultipathd, &m->process_queued_ios);
        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,
@@ -1067,7 +1084,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;
@@ -1252,7 +1269,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;
        }
@@ -1268,12 +1285,47 @@ error:
        return -EINVAL;
 }
 
+static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
+                          struct file *filp, unsigned int cmd,
+                          unsigned long arg)
+{
+       struct multipath *m = (struct multipath *) ti->private;
+       struct block_device *bdev = NULL;
+       unsigned long flags;
+       struct file fake_file = {};
+       struct dentry fake_dentry = {};
+       int r = 0;
+
+       fake_file.f_path.dentry = &fake_dentry;
+
+       spin_lock_irqsave(&m->lock, flags);
+
+       if (!m->current_pgpath)
+               __choose_pgpath(m);
+
+       if (m->current_pgpath) {
+               bdev = m->current_pgpath->path.dev->bdev;
+               fake_dentry.d_inode = bdev->bd_inode;
+               fake_file.f_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->bd_inode, &fake_file,
+                                        bdev->bd_disk, 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,
@@ -1283,6 +1335,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)
@@ -1311,7 +1364,7 @@ static int __init dm_multipath_init(void)
                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]);