blob: e54ff372d711b53e652220e8616ddf3d07995f5c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Sistina Software Limited.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4 *
5 * This file is released under the GPL.
6 */
7
8#include "dm.h"
9#include "dm-path-selector.h"
10#include "dm-hw-handler.h"
11#include "dm-bio-list.h"
12#include "dm-bio-record.h"
Mike Andersonb15546f2007-10-19 22:48:02 +010013#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <linux/ctype.h>
16#include <linux/init.h>
17#include <linux/mempool.h>
18#include <linux/module.h>
19#include <linux/pagemap.h>
20#include <linux/slab.h>
21#include <linux/time.h>
22#include <linux/workqueue.h>
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070023#include <scsi/scsi_dh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/atomic.h>
25
Alasdair G Kergon72d94862006-06-26 00:27:35 -070026#define DM_MSG_PREFIX "multipath"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define MESG_STR(x) x, sizeof(x)
28
29/* Path properties */
30struct pgpath {
31 struct list_head list;
32
33 struct priority_group *pg; /* Owning PG */
34 unsigned fail_count; /* Cumulative failure count */
35
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080036 struct dm_path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
39#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
40
41/*
42 * Paths are grouped into Priority Groups and numbered from 1 upwards.
43 * Each has a path selector which controls which path gets used.
44 */
45struct priority_group {
46 struct list_head list;
47
48 struct multipath *m; /* Owning multipath instance */
49 struct path_selector ps;
50
51 unsigned pg_num; /* Reference number */
52 unsigned bypassed; /* Temporarily bypass this PG? */
53
54 unsigned nr_pgpaths; /* Number of paths in PG */
55 struct list_head pgpaths;
56};
57
58/* Multipath context */
59struct multipath {
60 struct list_head list;
61 struct dm_target *ti;
62
63 spinlock_t lock;
64
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070065 const char *hw_handler_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 unsigned nr_priority_groups;
67 struct list_head priority_groups;
68 unsigned pg_init_required; /* pg_init needs calling? */
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -070069 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 unsigned nr_valid_paths; /* Total number of usable paths */
72 struct pgpath *current_pgpath;
73 struct priority_group *current_pg;
74 struct priority_group *next_pg; /* Switch to this PG if set */
75 unsigned repeat_count; /* I/Os left before calling PS again */
76
77 unsigned queue_io; /* Must we queue all I/O? */
78 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
Alasdair G Kergon436d4102005-07-12 15:53:03 -070079 unsigned saved_queue_if_no_path;/* Saved state during suspension */
Dave Wysochanskic9e45582007-10-19 22:47:53 +010080 unsigned pg_init_retries; /* Number of times to retry pg_init */
81 unsigned pg_init_count; /* Number of times pg_init called */
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 struct work_struct process_queued_ios;
84 struct bio_list queued_ios;
85 unsigned queue_size;
86
87 struct work_struct trigger_event;
88
89 /*
Alasdair G Kergon028867a2007-07-12 17:26:32 +010090 * We must use a mempool of dm_mpath_io structs so that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * can resubmit bios on error.
92 */
93 mempool_t *mpio_pool;
94};
95
96/*
97 * Context information attached to each bio we process.
98 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010099struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 struct pgpath *pgpath;
101 struct dm_bio_details details;
102};
103
104typedef int (*action_fn) (struct pgpath *pgpath);
105
106#define MIN_IOS 256 /* Mempool size */
107
Christoph Lametere18b8902006-12-06 20:33:20 -0800108static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Alasdair G Kergon009cd092008-02-08 02:10:35 +0000110static struct workqueue_struct *kmultipathd;
David Howellsc4028952006-11-22 14:57:56 +0000111static void process_queued_ios(struct work_struct *work);
112static void trigger_event(struct work_struct *work);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700113static void pg_init_done(struct dm_path *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115
116/*-----------------------------------------------
117 * Allocation routines
118 *-----------------------------------------------*/
119
120static struct pgpath *alloc_pgpath(void)
121{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700122 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700124 if (pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 pgpath->path.is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 return pgpath;
128}
129
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100130static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 kfree(pgpath);
133}
134
135static struct priority_group *alloc_priority_group(void)
136{
137 struct priority_group *pg;
138
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700139 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700141 if (pg)
142 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return pg;
145}
146
147static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
148{
149 struct pgpath *pgpath, *tmp;
150
151 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
152 list_del(&pgpath->list);
153 dm_put_device(ti, pgpath->path.dev);
154 free_pgpath(pgpath);
155 }
156}
157
158static void free_priority_group(struct priority_group *pg,
159 struct dm_target *ti)
160{
161 struct path_selector *ps = &pg->ps;
162
163 if (ps->type) {
164 ps->type->destroy(ps);
165 dm_put_path_selector(ps->type);
166 }
167
168 free_pgpaths(&pg->pgpaths, ti);
169 kfree(pg);
170}
171
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700172static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
174 struct multipath *m;
175
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700176 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 INIT_LIST_HEAD(&m->priority_groups);
179 spin_lock_init(&m->lock);
180 m->queue_io = 1;
David Howellsc4028952006-11-22 14:57:56 +0000181 INIT_WORK(&m->process_queued_ios, process_queued_ios);
182 INIT_WORK(&m->trigger_event, trigger_event);
Matthew Dobson93d23412006-03-26 01:37:50 -0800183 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!m->mpio_pool) {
185 kfree(m);
186 return NULL;
187 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700188 m->ti = ti;
189 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
192 return m;
193}
194
195static void free_multipath(struct multipath *m)
196{
197 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
200 list_del(&pg->list);
201 free_priority_group(pg, m->ti);
202 }
203
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700204 kfree(m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 mempool_destroy(m->mpio_pool);
206 kfree(m);
207}
208
209
210/*-----------------------------------------------
211 * Path selection
212 *-----------------------------------------------*/
213
214static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
215{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 m->current_pg = pgpath->pg;
217
218 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700219 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 m->pg_init_required = 1;
221 m->queue_io = 1;
222 } else {
223 m->pg_init_required = 0;
224 m->queue_io = 0;
225 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100226
227 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
231{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800232 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
235 if (!path)
236 return -ENXIO;
237
238 m->current_pgpath = path_to_pgpath(path);
239
240 if (m->current_pg != pg)
241 __switch_pg(m, m->current_pgpath);
242
243 return 0;
244}
245
246static void __choose_pgpath(struct multipath *m)
247{
248 struct priority_group *pg;
249 unsigned bypassed = 1;
250
251 if (!m->nr_valid_paths)
252 goto failed;
253
254 /* Were we instructed to switch PG? */
255 if (m->next_pg) {
256 pg = m->next_pg;
257 m->next_pg = NULL;
258 if (!__choose_path_in_pg(m, pg))
259 return;
260 }
261
262 /* Don't change PG until it has no remaining paths */
263 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
264 return;
265
266 /*
267 * Loop through priority groups until we find a valid path.
268 * First time we skip PGs marked 'bypassed'.
269 * Second time we only try the ones we skipped.
270 */
271 do {
272 list_for_each_entry(pg, &m->priority_groups, list) {
273 if (pg->bypassed == bypassed)
274 continue;
275 if (!__choose_path_in_pg(m, pg))
276 return;
277 }
278 } while (bypassed--);
279
280failed:
281 m->current_pgpath = NULL;
282 m->current_pg = NULL;
283}
284
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800285/*
286 * Check whether bios must be queued in the device-mapper core rather
287 * than here in the target.
288 *
289 * m->lock must be held on entry.
290 *
291 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
292 * same value then we are not between multipath_presuspend()
293 * and multipath_resume() calls and we have no need to check
294 * for the DMF_NOFLUSH_SUSPENDING flag.
295 */
296static int __must_push_back(struct multipath *m)
297{
298 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
299 dm_noflush_suspending(m->ti));
300}
301
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100302static int map_io(struct multipath *m, struct bio *bio,
303 struct dm_mpath_io *mpio, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800305 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 unsigned long flags;
307 struct pgpath *pgpath;
308
309 spin_lock_irqsave(&m->lock, flags);
310
311 /* Do we need to select a new pgpath? */
312 if (!m->current_pgpath ||
313 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
314 __choose_pgpath(m);
315
316 pgpath = m->current_pgpath;
317
318 if (was_queued)
319 m->queue_size--;
320
321 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700322 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 /* Queue for the daemon to resubmit */
324 bio_list_add(&m->queued_ios, bio);
325 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700326 if ((m->pg_init_required && !m->pg_init_in_progress) ||
327 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700328 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800330 r = DM_MAPIO_SUBMITTED;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800331 } else if (pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 bio->bi_bdev = pgpath->path.dev->bdev;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800333 else if (__must_push_back(m))
334 r = DM_MAPIO_REQUEUE;
335 else
336 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 mpio->pgpath = pgpath;
339
340 spin_unlock_irqrestore(&m->lock, flags);
341
342 return r;
343}
344
345/*
346 * If we run out of usable paths, should we queue I/O or error it?
347 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700348static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
349 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
351 unsigned long flags;
352
353 spin_lock_irqsave(&m->lock, flags);
354
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700355 if (save_old_value)
356 m->saved_queue_if_no_path = m->queue_if_no_path;
357 else
358 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700360 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700361 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 spin_unlock_irqrestore(&m->lock, flags);
364
365 return 0;
366}
367
368/*-----------------------------------------------------------------
369 * The multipath daemon is responsible for resubmitting queued ios.
370 *---------------------------------------------------------------*/
371
372static void dispatch_queued_ios(struct multipath *m)
373{
374 int r;
375 unsigned long flags;
376 struct bio *bio = NULL, *next;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100377 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 union map_info *info;
379
380 spin_lock_irqsave(&m->lock, flags);
381 bio = bio_list_get(&m->queued_ios);
382 spin_unlock_irqrestore(&m->lock, flags);
383
384 while (bio) {
385 next = bio->bi_next;
386 bio->bi_next = NULL;
387
388 info = dm_get_mapinfo(bio);
389 mpio = info->ptr;
390
391 r = map_io(m, bio, mpio, 1);
392 if (r < 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200393 bio_endio(bio, r);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800394 else if (r == DM_MAPIO_REMAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 generic_make_request(bio);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800396 else if (r == DM_MAPIO_REQUEUE)
NeilBrown6712ecf2007-09-27 12:47:43 +0200397 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 bio = next;
400 }
401}
402
David Howellsc4028952006-11-22 14:57:56 +0000403static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
David Howellsc4028952006-11-22 14:57:56 +0000405 struct multipath *m =
406 container_of(work, struct multipath, process_queued_ios);
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700407 struct pgpath *pgpath = NULL;
408 unsigned init_required = 0, must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 unsigned long flags;
410
411 spin_lock_irqsave(&m->lock, flags);
412
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700413 if (!m->queue_size)
414 goto out;
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 if (!m->current_pgpath)
417 __choose_pgpath(m);
418
419 pgpath = m->current_pgpath;
420
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700421 if ((pgpath && !m->queue_io) ||
422 (!pgpath && !m->queue_if_no_path))
423 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700425 if (m->pg_init_required && !m->pg_init_in_progress) {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100426 m->pg_init_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 m->pg_init_required = 0;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700428 m->pg_init_in_progress = 1;
429 init_required = 1;
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700432out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 spin_unlock_irqrestore(&m->lock, flags);
434
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700435 if (init_required) {
436 struct dm_path *path = &pgpath->path;
437 int ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
438 pg_init_done(path, ret);
439 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (!must_queue)
442 dispatch_queued_ios(m);
443}
444
445/*
446 * An event is triggered whenever a path is taken out of use.
447 * Includes path failure and PG bypass.
448 */
David Howellsc4028952006-11-22 14:57:56 +0000449static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
David Howellsc4028952006-11-22 14:57:56 +0000451 struct multipath *m =
452 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 dm_table_event(m->ti->table);
455}
456
457/*-----------------------------------------------------------------
458 * Constructor/argument parsing:
459 * <#multipath feature args> [<arg>]*
460 * <#hw_handler args> [hw_handler [<arg>]*]
461 * <#priority groups>
462 * <initial priority group>
463 * [<selector> <#selector args> [<arg>]*
464 * <#paths> <#per-path selector args>
465 * [<path> [<arg>]* ]+ ]+
466 *---------------------------------------------------------------*/
467struct param {
468 unsigned min;
469 unsigned max;
470 char *error;
471};
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473static int read_param(struct param *param, char *str, unsigned *v, char **error)
474{
475 if (!str ||
476 (sscanf(str, "%u", v) != 1) ||
477 (*v < param->min) ||
478 (*v > param->max)) {
479 *error = param->error;
480 return -EINVAL;
481 }
482
483 return 0;
484}
485
486struct arg_set {
487 unsigned argc;
488 char **argv;
489};
490
491static char *shift(struct arg_set *as)
492{
493 char *r;
494
495 if (as->argc) {
496 as->argc--;
497 r = *as->argv;
498 as->argv++;
499 return r;
500 }
501
502 return NULL;
503}
504
505static void consume(struct arg_set *as, unsigned n)
506{
507 BUG_ON (as->argc < n);
508 as->argc -= n;
509 as->argv += n;
510}
511
512static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
513 struct dm_target *ti)
514{
515 int r;
516 struct path_selector_type *pst;
517 unsigned ps_argc;
518
519 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700520 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 };
522
523 pst = dm_get_path_selector(shift(as));
524 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700525 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -EINVAL;
527 }
528
529 r = read_param(_params, shift(as), &ps_argc, &ti->error);
530 if (r)
531 return -EINVAL;
532
533 r = pst->create(&pg->ps, ps_argc, as->argv);
534 if (r) {
535 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700536 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return r;
538 }
539
540 pg->ps.type = pst;
541 consume(as, ps_argc);
542
543 return 0;
544}
545
546static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
547 struct dm_target *ti)
548{
549 int r;
550 struct pgpath *p;
551
552 /* we need at least a path arg */
553 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700554 ti->error = "no device given";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return NULL;
556 }
557
558 p = alloc_pgpath();
559 if (!p)
560 return NULL;
561
562 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
563 dm_table_get_mode(ti->table), &p->path.dev);
564 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700565 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 goto bad;
567 }
568
569 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
570 if (r) {
571 dm_put_device(ti, p->path.dev);
572 goto bad;
573 }
574
575 return p;
576
577 bad:
578 free_pgpath(p);
579 return NULL;
580}
581
582static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700583 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700586 {1, 1024, "invalid number of paths"},
587 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 };
589
590 int r;
591 unsigned i, nr_selector_args, nr_params;
592 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700593 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (as->argc < 2) {
596 as->argc = 0;
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700597 ti->error = "not enough priority group aruments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 return NULL;
599 }
600
601 pg = alloc_priority_group();
602 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700603 ti->error = "couldn't allocate priority group";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 return NULL;
605 }
606 pg->m = m;
607
608 r = parse_path_selector(as, pg, ti);
609 if (r)
610 goto bad;
611
612 /*
613 * read the paths
614 */
615 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
616 if (r)
617 goto bad;
618
619 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
620 if (r)
621 goto bad;
622
623 nr_params = 1 + nr_selector_args;
624 for (i = 0; i < pg->nr_pgpaths; i++) {
625 struct pgpath *pgpath;
626 struct arg_set path_args;
627
628 if (as->argc < nr_params)
629 goto bad;
630
631 path_args.argc = nr_params;
632 path_args.argv = as->argv;
633
634 pgpath = parse_path(&path_args, &pg->ps, ti);
635 if (!pgpath)
636 goto bad;
637
638 pgpath->pg = pg;
639 list_add_tail(&pgpath->list, &pg->pgpaths);
640 consume(as, nr_params);
641 }
642
643 return pg;
644
645 bad:
646 free_priority_group(pg, ti);
647 return NULL;
648}
649
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700650static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 unsigned hw_argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700653 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700656 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 };
658
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700659 if (read_param(_params, shift(as), &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return -EINVAL;
661
662 if (!hw_argc)
663 return 0;
664
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700665 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
666 request_module("scsi_dh_%s", m->hw_handler_name);
667 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700668 ti->error = "unknown hardware handler type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -EINVAL;
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 consume(as, hw_argc - 1);
672
673 return 0;
674}
675
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700676static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
678 int r;
679 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700680 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100681 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100684 {0, 3, "invalid number of feature args"},
685 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 };
687
688 r = read_param(_params, shift(as), &argc, &ti->error);
689 if (r)
690 return -EINVAL;
691
692 if (!argc)
693 return 0;
694
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100695 do {
696 param_name = shift(as);
697 argc--;
698
699 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
700 r = queue_if_no_path(m, 1, 0);
701 continue;
702 }
703
704 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
705 (argc >= 1)) {
706 r = read_param(_params + 1, shift(as),
707 &m->pg_init_retries, &ti->error);
708 argc--;
709 continue;
710 }
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100713 r = -EINVAL;
714 } while (argc && !r);
715
716 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719static int multipath_ctr(struct dm_target *ti, unsigned int argc,
720 char **argv)
721{
722 /* target parameters */
723 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700724 {1, 1024, "invalid number of priority groups"},
725 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 };
727
728 int r;
729 struct multipath *m;
730 struct arg_set as;
731 unsigned pg_count = 0;
732 unsigned next_pg_num;
733
734 as.argc = argc;
735 as.argv = argv;
736
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700737 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700739 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 return -EINVAL;
741 }
742
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700743 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (r)
745 goto bad;
746
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700747 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 if (r)
749 goto bad;
750
751 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
752 if (r)
753 goto bad;
754
755 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
756 if (r)
757 goto bad;
758
759 /* parse the priority groups */
760 while (as.argc) {
761 struct priority_group *pg;
762
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700763 pg = parse_priority_group(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (!pg) {
765 r = -EINVAL;
766 goto bad;
767 }
768
769 m->nr_valid_paths += pg->nr_pgpaths;
770 list_add_tail(&pg->list, &m->priority_groups);
771 pg_count++;
772 pg->pg_num = pg_count;
773 if (!--next_pg_num)
774 m->next_pg = pg;
775 }
776
777 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700778 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 r = -EINVAL;
780 goto bad;
781 }
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return 0;
784
785 bad:
786 free_multipath(m);
787 return r;
788}
789
790static void multipath_dtr(struct dm_target *ti)
791{
792 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700793
794 flush_workqueue(kmultipathd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 free_multipath(m);
796}
797
798/*
799 * Map bios, recording original fields for later in case we have to resubmit
800 */
801static int multipath_map(struct dm_target *ti, struct bio *bio,
802 union map_info *map_context)
803{
804 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100805 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 struct multipath *m = (struct multipath *) ti->private;
807
808 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
809 dm_bio_record(&mpio->details, bio);
810
811 map_context->ptr = mpio;
812 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
813 r = map_io(m, bio, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800814 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 mempool_free(mpio, m->mpio_pool);
816
817 return r;
818}
819
820/*
821 * Take a path out of use.
822 */
823static int fail_path(struct pgpath *pgpath)
824{
825 unsigned long flags;
826 struct multipath *m = pgpath->pg->m;
827
828 spin_lock_irqsave(&m->lock, flags);
829
830 if (!pgpath->path.is_active)
831 goto out;
832
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700833 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
836 pgpath->path.is_active = 0;
837 pgpath->fail_count++;
838
839 m->nr_valid_paths--;
840
841 if (pgpath == m->current_pgpath)
842 m->current_pgpath = NULL;
843
Mike Andersonb15546f2007-10-19 22:48:02 +0100844 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
845 pgpath->path.dev->name, m->nr_valid_paths);
846
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700847 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849out:
850 spin_unlock_irqrestore(&m->lock, flags);
851
852 return 0;
853}
854
855/*
856 * Reinstate a previously-failed path
857 */
858static int reinstate_path(struct pgpath *pgpath)
859{
860 int r = 0;
861 unsigned long flags;
862 struct multipath *m = pgpath->pg->m;
863
864 spin_lock_irqsave(&m->lock, flags);
865
866 if (pgpath->path.is_active)
867 goto out;
868
869 if (!pgpath->pg->ps.type) {
870 DMWARN("Reinstate path not supported by path selector %s",
871 pgpath->pg->ps.type->name);
872 r = -EINVAL;
873 goto out;
874 }
875
876 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
877 if (r)
878 goto out;
879
880 pgpath->path.is_active = 1;
881
882 m->current_pgpath = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700883 if (!m->nr_valid_paths++ && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700884 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Mike Andersonb15546f2007-10-19 22:48:02 +0100886 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
887 pgpath->path.dev->name, m->nr_valid_paths);
888
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700889 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891out:
892 spin_unlock_irqrestore(&m->lock, flags);
893
894 return r;
895}
896
897/*
898 * Fail or reinstate all paths that match the provided struct dm_dev.
899 */
900static int action_dev(struct multipath *m, struct dm_dev *dev,
901 action_fn action)
902{
903 int r = 0;
904 struct pgpath *pgpath;
905 struct priority_group *pg;
906
907 list_for_each_entry(pg, &m->priority_groups, list) {
908 list_for_each_entry(pgpath, &pg->pgpaths, list) {
909 if (pgpath->path.dev == dev)
910 r = action(pgpath);
911 }
912 }
913
914 return r;
915}
916
917/*
918 * Temporarily try to avoid having to use the specified PG
919 */
920static void bypass_pg(struct multipath *m, struct priority_group *pg,
921 int bypassed)
922{
923 unsigned long flags;
924
925 spin_lock_irqsave(&m->lock, flags);
926
927 pg->bypassed = bypassed;
928 m->current_pgpath = NULL;
929 m->current_pg = NULL;
930
931 spin_unlock_irqrestore(&m->lock, flags);
932
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700933 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936/*
937 * Switch to using the specified PG from the next I/O that gets mapped
938 */
939static int switch_pg_num(struct multipath *m, const char *pgstr)
940{
941 struct priority_group *pg;
942 unsigned pgnum;
943 unsigned long flags;
944
945 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
946 (pgnum > m->nr_priority_groups)) {
947 DMWARN("invalid PG number supplied to switch_pg_num");
948 return -EINVAL;
949 }
950
951 spin_lock_irqsave(&m->lock, flags);
952 list_for_each_entry(pg, &m->priority_groups, list) {
953 pg->bypassed = 0;
954 if (--pgnum)
955 continue;
956
957 m->current_pgpath = NULL;
958 m->current_pg = NULL;
959 m->next_pg = pg;
960 }
961 spin_unlock_irqrestore(&m->lock, flags);
962
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700963 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return 0;
965}
966
967/*
968 * Set/clear bypassed status of a PG.
969 * PGs are numbered upwards from 1 in the order they were declared.
970 */
971static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
972{
973 struct priority_group *pg;
974 unsigned pgnum;
975
976 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
977 (pgnum > m->nr_priority_groups)) {
978 DMWARN("invalid PG number supplied to bypass_pg");
979 return -EINVAL;
980 }
981
982 list_for_each_entry(pg, &m->priority_groups, list) {
983 if (!--pgnum)
984 break;
985 }
986
987 bypass_pg(m, pg, bypassed);
988 return 0;
989}
990
991/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100992 * Should we retry pg_init immediately?
993 */
994static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
995{
996 unsigned long flags;
997 int limit_reached = 0;
998
999 spin_lock_irqsave(&m->lock, flags);
1000
1001 if (m->pg_init_count <= m->pg_init_retries)
1002 m->pg_init_required = 1;
1003 else
1004 limit_reached = 1;
1005
1006 spin_unlock_irqrestore(&m->lock, flags);
1007
1008 return limit_reached;
1009}
1010
1011/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 * pg_init must call this when it has completed its initialisation
1013 */
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -08001014void dm_pg_init_complete(struct dm_path *path, unsigned err_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016 struct pgpath *pgpath = path_to_pgpath(path);
1017 struct priority_group *pg = pgpath->pg;
1018 struct multipath *m = pg->m;
1019 unsigned long flags;
1020
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001021 /*
1022 * If requested, retry pg_init until maximum number of retries exceeded.
1023 * If retry not requested and PG already bypassed, always fail the path.
1024 */
1025 if (err_flags & MP_RETRY) {
1026 if (pg_init_limit_reached(m, pgpath))
1027 err_flags |= MP_FAIL_PATH;
1028 } else if (err_flags && pg->bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 err_flags |= MP_FAIL_PATH;
1030
1031 if (err_flags & MP_FAIL_PATH)
1032 fail_path(pgpath);
1033
1034 if (err_flags & MP_BYPASS_PG)
1035 bypass_pg(m, pg, 1);
1036
1037 spin_lock_irqsave(&m->lock, flags);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001038 if (err_flags & ~MP_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 m->current_pgpath = NULL;
1040 m->current_pg = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -07001041 } else if (!m->pg_init_required)
1042 m->queue_io = 0;
1043
1044 m->pg_init_in_progress = 0;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001045 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 spin_unlock_irqrestore(&m->lock, flags);
1047}
1048
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001049static void pg_init_done(struct dm_path *path, int errors)
1050{
1051 struct pgpath *pgpath = path_to_pgpath(path);
1052 struct priority_group *pg = pgpath->pg;
1053 struct multipath *m = pg->m;
1054 unsigned long flags;
1055
1056 /* device or driver problems */
1057 switch (errors) {
1058 case SCSI_DH_OK:
1059 break;
1060 case SCSI_DH_NOSYS:
1061 if (!m->hw_handler_name) {
1062 errors = 0;
1063 break;
1064 }
1065 DMERR("Cannot failover device because scsi_dh_%s was not "
1066 "loaded.", m->hw_handler_name);
1067 /*
1068 * Fail path for now, so we do not ping pong
1069 */
1070 fail_path(pgpath);
1071 break;
1072 case SCSI_DH_DEV_TEMP_BUSY:
1073 /*
1074 * Probably doing something like FW upgrade on the
1075 * controller so try the other pg.
1076 */
1077 bypass_pg(m, pg, 1);
1078 break;
1079 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1080 case SCSI_DH_RETRY:
1081 case SCSI_DH_IMM_RETRY:
1082 case SCSI_DH_RES_TEMP_UNAVAIL:
1083 if (pg_init_limit_reached(m, pgpath))
1084 fail_path(pgpath);
1085 errors = 0;
1086 break;
1087 default:
1088 /*
1089 * We probably do not want to fail the path for a device
1090 * error, but this is what the old dm did. In future
1091 * patches we can do more advanced handling.
1092 */
1093 fail_path(pgpath);
1094 }
1095
1096 spin_lock_irqsave(&m->lock, flags);
1097 if (errors) {
1098 DMERR("Could not failover device. Error %d.", errors);
1099 m->current_pgpath = NULL;
1100 m->current_pg = NULL;
1101 } else if (!m->pg_init_required) {
1102 m->queue_io = 0;
1103 pg->bypassed = 0;
1104 }
1105
1106 m->pg_init_in_progress = 0;
1107 queue_work(kmultipathd, &m->process_queued_ios);
1108 spin_unlock_irqrestore(&m->lock, flags);
1109}
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111/*
1112 * end_io handling
1113 */
1114static int do_end_io(struct multipath *m, struct bio *bio,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001115 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Stefan Bader640eb3b2005-11-21 21:32:35 -08001117 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 if (!error)
1120 return 0; /* I/O complete */
1121
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -07001122 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1123 return error;
1124
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001125 if (error == -EOPNOTSUPP)
1126 return error;
1127
Stefan Bader640eb3b2005-11-21 21:32:35 -08001128 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (!m->nr_valid_paths) {
Kiyoshi Ueda45e15722006-12-08 02:41:10 -08001130 if (__must_push_back(m)) {
1131 spin_unlock_irqrestore(&m->lock, flags);
1132 return DM_ENDIO_REQUEUE;
1133 } else if (!m->queue_if_no_path) {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001134 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return -EIO;
1136 } else {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001137 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 goto requeue;
1139 }
1140 }
Stefan Bader640eb3b2005-11-21 21:32:35 -08001141 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001143 if (mpio->pgpath)
1144 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 requeue:
1147 dm_bio_restore(&mpio->details, bio);
1148
1149 /* queue for the daemon to resubmit or fail */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001150 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 bio_list_add(&m->queued_ios, bio);
1152 m->queue_size++;
1153 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001154 queue_work(kmultipathd, &m->process_queued_ios);
Stefan Bader640eb3b2005-11-21 21:32:35 -08001155 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001157 return DM_ENDIO_INCOMPLETE; /* io not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
1160static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1161 int error, union map_info *map_context)
1162{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001163 struct multipath *m = ti->private;
1164 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 struct pgpath *pgpath = mpio->pgpath;
1166 struct path_selector *ps;
1167 int r;
1168
1169 r = do_end_io(m, bio, error, mpio);
1170 if (pgpath) {
1171 ps = &pgpath->pg->ps;
1172 if (ps->type->end_io)
1173 ps->type->end_io(ps, &pgpath->path);
1174 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001175 if (r != DM_ENDIO_INCOMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 mempool_free(mpio, m->mpio_pool);
1177
1178 return r;
1179}
1180
1181/*
1182 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001183 * the last path fails we must error any remaining I/O.
1184 * Note that if the freeze_bdev fails while suspending, the
1185 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 */
1187static void multipath_presuspend(struct dm_target *ti)
1188{
1189 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001191 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001194/*
1195 * Restore the queue_if_no_path setting.
1196 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197static void multipath_resume(struct dm_target *ti)
1198{
1199 struct multipath *m = (struct multipath *) ti->private;
1200 unsigned long flags;
1201
1202 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001203 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 spin_unlock_irqrestore(&m->lock, flags);
1205}
1206
1207/*
1208 * Info output has the following format:
1209 * num_multipath_feature_args [multipath_feature_args]*
1210 * num_handler_status_args [handler_status_args]*
1211 * num_groups init_group_number
1212 * [A|D|E num_ps_status_args [ps_status_args]*
1213 * num_paths num_selector_args
1214 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1215 *
1216 * Table output has the following format (identical to the constructor string):
1217 * num_feature_args [features_args]*
1218 * num_handler_args hw_handler [hw_handler_args]*
1219 * num_groups init_group_number
1220 * [priority selector-name num_ps_args [ps_args]*
1221 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1222 */
1223static int multipath_status(struct dm_target *ti, status_type_t type,
1224 char *result, unsigned int maxlen)
1225{
1226 int sz = 0;
1227 unsigned long flags;
1228 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 struct priority_group *pg;
1230 struct pgpath *p;
1231 unsigned pg_num;
1232 char state;
1233
1234 spin_lock_irqsave(&m->lock, flags);
1235
1236 /* Features */
1237 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001238 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1239 else {
1240 DMEMIT("%u ", m->queue_if_no_path +
1241 (m->pg_init_retries > 0) * 2);
1242 if (m->queue_if_no_path)
1243 DMEMIT("queue_if_no_path ");
1244 if (m->pg_init_retries)
1245 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001248 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 DMEMIT("0 ");
1250 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001251 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 DMEMIT("%u ", m->nr_priority_groups);
1254
1255 if (m->next_pg)
1256 pg_num = m->next_pg->pg_num;
1257 else if (m->current_pg)
1258 pg_num = m->current_pg->pg_num;
1259 else
1260 pg_num = 1;
1261
1262 DMEMIT("%u ", pg_num);
1263
1264 switch (type) {
1265 case STATUSTYPE_INFO:
1266 list_for_each_entry(pg, &m->priority_groups, list) {
1267 if (pg->bypassed)
1268 state = 'D'; /* Disabled */
1269 else if (pg == m->current_pg)
1270 state = 'A'; /* Currently Active */
1271 else
1272 state = 'E'; /* Enabled */
1273
1274 DMEMIT("%c ", state);
1275
1276 if (pg->ps.type->status)
1277 sz += pg->ps.type->status(&pg->ps, NULL, type,
1278 result + sz,
1279 maxlen - sz);
1280 else
1281 DMEMIT("0 ");
1282
1283 DMEMIT("%u %u ", pg->nr_pgpaths,
1284 pg->ps.type->info_args);
1285
1286 list_for_each_entry(p, &pg->pgpaths, list) {
1287 DMEMIT("%s %s %u ", p->path.dev->name,
1288 p->path.is_active ? "A" : "F",
1289 p->fail_count);
1290 if (pg->ps.type->status)
1291 sz += pg->ps.type->status(&pg->ps,
1292 &p->path, type, result + sz,
1293 maxlen - sz);
1294 }
1295 }
1296 break;
1297
1298 case STATUSTYPE_TABLE:
1299 list_for_each_entry(pg, &m->priority_groups, list) {
1300 DMEMIT("%s ", pg->ps.type->name);
1301
1302 if (pg->ps.type->status)
1303 sz += pg->ps.type->status(&pg->ps, NULL, type,
1304 result + sz,
1305 maxlen - sz);
1306 else
1307 DMEMIT("0 ");
1308
1309 DMEMIT("%u %u ", pg->nr_pgpaths,
1310 pg->ps.type->table_args);
1311
1312 list_for_each_entry(p, &pg->pgpaths, list) {
1313 DMEMIT("%s ", p->path.dev->name);
1314 if (pg->ps.type->status)
1315 sz += pg->ps.type->status(&pg->ps,
1316 &p->path, type, result + sz,
1317 maxlen - sz);
1318 }
1319 }
1320 break;
1321 }
1322
1323 spin_unlock_irqrestore(&m->lock, flags);
1324
1325 return 0;
1326}
1327
1328static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1329{
1330 int r;
1331 struct dm_dev *dev;
1332 struct multipath *m = (struct multipath *) ti->private;
1333 action_fn action;
1334
1335 if (argc == 1) {
1336 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001337 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001339 return queue_if_no_path(m, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341
1342 if (argc != 2)
1343 goto error;
1344
1345 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1346 return bypass_pg_num(m, argv[1], 1);
1347 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1348 return bypass_pg_num(m, argv[1], 0);
1349 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1350 return switch_pg_num(m, argv[1]);
1351 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1352 action = reinstate_path;
1353 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1354 action = fail_path;
1355 else
1356 goto error;
1357
1358 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1359 dm_table_get_mode(ti->table), &dev);
1360 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001361 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 argv[1]);
1363 return -EINVAL;
1364 }
1365
1366 r = action_dev(m, dev, action);
1367
1368 dm_put_device(ti, dev);
1369
1370 return r;
1371
1372error:
1373 DMWARN("Unrecognised multipath message received.");
1374 return -EINVAL;
1375}
1376
Milan Broz9af4aa32006-10-03 01:15:20 -07001377static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1378 struct file *filp, unsigned int cmd,
1379 unsigned long arg)
1380{
1381 struct multipath *m = (struct multipath *) ti->private;
1382 struct block_device *bdev = NULL;
1383 unsigned long flags;
Milan Broze90dae12006-10-03 01:15:22 -07001384 struct file fake_file = {};
1385 struct dentry fake_dentry = {};
Milan Broz9af4aa32006-10-03 01:15:20 -07001386 int r = 0;
1387
Josef Sipekc649bb92006-12-08 02:37:19 -08001388 fake_file.f_path.dentry = &fake_dentry;
Milan Broze90dae12006-10-03 01:15:22 -07001389
Milan Broz9af4aa32006-10-03 01:15:20 -07001390 spin_lock_irqsave(&m->lock, flags);
1391
1392 if (!m->current_pgpath)
1393 __choose_pgpath(m);
1394
Milan Broze90dae12006-10-03 01:15:22 -07001395 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001396 bdev = m->current_pgpath->path.dev->bdev;
Milan Broze90dae12006-10-03 01:15:22 -07001397 fake_dentry.d_inode = bdev->bd_inode;
1398 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1399 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001400
1401 if (m->queue_io)
1402 r = -EAGAIN;
1403 else if (!bdev)
1404 r = -EIO;
1405
1406 spin_unlock_irqrestore(&m->lock, flags);
1407
Milan Broze90dae12006-10-03 01:15:22 -07001408 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1409 bdev->bd_disk, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001410}
1411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412/*-----------------------------------------------------------------
1413 * Module setup
1414 *---------------------------------------------------------------*/
1415static struct target_type multipath_target = {
1416 .name = "multipath",
Milan Broz9af4aa32006-10-03 01:15:20 -07001417 .version = {1, 0, 5},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 .module = THIS_MODULE,
1419 .ctr = multipath_ctr,
1420 .dtr = multipath_dtr,
1421 .map = multipath_map,
1422 .end_io = multipath_end_io,
1423 .presuspend = multipath_presuspend,
1424 .resume = multipath_resume,
1425 .status = multipath_status,
1426 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001427 .ioctl = multipath_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428};
1429
1430static int __init dm_multipath_init(void)
1431{
1432 int r;
1433
1434 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001435 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 if (!_mpio_cache)
1437 return -ENOMEM;
1438
1439 r = dm_register_target(&multipath_target);
1440 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001441 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 kmem_cache_destroy(_mpio_cache);
1443 return -EINVAL;
1444 }
1445
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001446 kmultipathd = create_workqueue("kmpathd");
1447 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001448 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001449 dm_unregister_target(&multipath_target);
1450 kmem_cache_destroy(_mpio_cache);
1451 return -ENOMEM;
1452 }
1453
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001454 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 multipath_target.version[0], multipath_target.version[1],
1456 multipath_target.version[2]);
1457
1458 return r;
1459}
1460
1461static void __exit dm_multipath_exit(void)
1462{
1463 int r;
1464
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001465 destroy_workqueue(kmultipathd);
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 r = dm_unregister_target(&multipath_target);
1468 if (r < 0)
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001469 DMERR("target unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 kmem_cache_destroy(_mpio_cache);
1471}
1472
1473EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1474
1475module_init(dm_multipath_init);
1476module_exit(dm_multipath_exit);
1477
1478MODULE_DESCRIPTION(DM_NAME " multipath target");
1479MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1480MODULE_LICENSE("GPL");