blob: 9f570b2ab7b46e028f9eb792dcf4cc65cd129c1b [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "dm-bio-list.h"
11#include "dm-bio-record.h"
Mike Andersonb15546f2007-10-19 22:48:02 +010012#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <linux/ctype.h>
15#include <linux/init.h>
16#include <linux/mempool.h>
17#include <linux/module.h>
18#include <linux/pagemap.h>
19#include <linux/slab.h>
20#include <linux/time.h>
21#include <linux/workqueue.h>
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070022#include <scsi/scsi_dh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/atomic.h>
24
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "multipath"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define MESG_STR(x) x, sizeof(x)
27
28/* Path properties */
29struct pgpath {
30 struct list_head list;
31
32 struct priority_group *pg; /* Owning PG */
33 unsigned fail_count; /* Cumulative failure count */
34
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080035 struct dm_path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
38#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
39
40/*
41 * Paths are grouped into Priority Groups and numbered from 1 upwards.
42 * Each has a path selector which controls which path gets used.
43 */
44struct priority_group {
45 struct list_head list;
46
47 struct multipath *m; /* Owning multipath instance */
48 struct path_selector ps;
49
50 unsigned pg_num; /* Reference number */
51 unsigned bypassed; /* Temporarily bypass this PG? */
52
53 unsigned nr_pgpaths; /* Number of paths in PG */
54 struct list_head pgpaths;
55};
56
57/* Multipath context */
58struct multipath {
59 struct list_head list;
60 struct dm_target *ti;
61
62 spinlock_t lock;
63
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070064 const char *hw_handler_name;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -070065 struct work_struct activate_path;
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
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700110static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
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 Seetharamanbab7cfc2008-05-01 14:50:22 -0700113static void activate_path(struct work_struct *work);
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);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700183 INIT_WORK(&m->activate_path, activate_path);
Matthew Dobson93d23412006-03-26 01:37:50 -0800184 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (!m->mpio_pool) {
186 kfree(m);
187 return NULL;
188 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700189 m->ti = ti;
190 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
193 return m;
194}
195
196static void free_multipath(struct multipath *m)
197{
198 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
201 list_del(&pg->list);
202 free_priority_group(pg, m->ti);
203 }
204
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700205 kfree(m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 mempool_destroy(m->mpio_pool);
207 kfree(m);
208}
209
210
211/*-----------------------------------------------
212 * Path selection
213 *-----------------------------------------------*/
214
215static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
216{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 m->current_pg = pgpath->pg;
218
219 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700220 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 m->pg_init_required = 1;
222 m->queue_io = 1;
223 } else {
224 m->pg_init_required = 0;
225 m->queue_io = 0;
226 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100227
228 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
232{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800233 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
236 if (!path)
237 return -ENXIO;
238
239 m->current_pgpath = path_to_pgpath(path);
240
241 if (m->current_pg != pg)
242 __switch_pg(m, m->current_pgpath);
243
244 return 0;
245}
246
247static void __choose_pgpath(struct multipath *m)
248{
249 struct priority_group *pg;
250 unsigned bypassed = 1;
251
252 if (!m->nr_valid_paths)
253 goto failed;
254
255 /* Were we instructed to switch PG? */
256 if (m->next_pg) {
257 pg = m->next_pg;
258 m->next_pg = NULL;
259 if (!__choose_path_in_pg(m, pg))
260 return;
261 }
262
263 /* Don't change PG until it has no remaining paths */
264 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
265 return;
266
267 /*
268 * Loop through priority groups until we find a valid path.
269 * First time we skip PGs marked 'bypassed'.
270 * Second time we only try the ones we skipped.
271 */
272 do {
273 list_for_each_entry(pg, &m->priority_groups, list) {
274 if (pg->bypassed == bypassed)
275 continue;
276 if (!__choose_path_in_pg(m, pg))
277 return;
278 }
279 } while (bypassed--);
280
281failed:
282 m->current_pgpath = NULL;
283 m->current_pg = NULL;
284}
285
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800286/*
287 * Check whether bios must be queued in the device-mapper core rather
288 * than here in the target.
289 *
290 * m->lock must be held on entry.
291 *
292 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
293 * same value then we are not between multipath_presuspend()
294 * and multipath_resume() calls and we have no need to check
295 * for the DMF_NOFLUSH_SUSPENDING flag.
296 */
297static int __must_push_back(struct multipath *m)
298{
299 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
300 dm_noflush_suspending(m->ti));
301}
302
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100303static int map_io(struct multipath *m, struct bio *bio,
304 struct dm_mpath_io *mpio, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800306 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 unsigned long flags;
308 struct pgpath *pgpath;
309
310 spin_lock_irqsave(&m->lock, flags);
311
312 /* Do we need to select a new pgpath? */
313 if (!m->current_pgpath ||
314 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
315 __choose_pgpath(m);
316
317 pgpath = m->current_pgpath;
318
319 if (was_queued)
320 m->queue_size--;
321
322 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700323 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 /* Queue for the daemon to resubmit */
325 bio_list_add(&m->queued_ios, bio);
326 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700327 if ((m->pg_init_required && !m->pg_init_in_progress) ||
328 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700329 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800331 r = DM_MAPIO_SUBMITTED;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800332 } else if (pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 bio->bi_bdev = pgpath->path.dev->bdev;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800334 else if (__must_push_back(m))
335 r = DM_MAPIO_REQUEUE;
336 else
337 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 mpio->pgpath = pgpath;
340
341 spin_unlock_irqrestore(&m->lock, flags);
342
343 return r;
344}
345
346/*
347 * If we run out of usable paths, should we queue I/O or error it?
348 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700349static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
350 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
352 unsigned long flags;
353
354 spin_lock_irqsave(&m->lock, flags);
355
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700356 if (save_old_value)
357 m->saved_queue_if_no_path = m->queue_if_no_path;
358 else
359 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700361 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700362 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364 spin_unlock_irqrestore(&m->lock, flags);
365
366 return 0;
367}
368
369/*-----------------------------------------------------------------
370 * The multipath daemon is responsible for resubmitting queued ios.
371 *---------------------------------------------------------------*/
372
373static void dispatch_queued_ios(struct multipath *m)
374{
375 int r;
376 unsigned long flags;
377 struct bio *bio = NULL, *next;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100378 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 union map_info *info;
380
381 spin_lock_irqsave(&m->lock, flags);
382 bio = bio_list_get(&m->queued_ios);
383 spin_unlock_irqrestore(&m->lock, flags);
384
385 while (bio) {
386 next = bio->bi_next;
387 bio->bi_next = NULL;
388
389 info = dm_get_mapinfo(bio);
390 mpio = info->ptr;
391
392 r = map_io(m, bio, mpio, 1);
393 if (r < 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200394 bio_endio(bio, r);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800395 else if (r == DM_MAPIO_REMAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 generic_make_request(bio);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800397 else if (r == DM_MAPIO_REQUEUE)
NeilBrown6712ecf2007-09-27 12:47:43 +0200398 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 bio = next;
401 }
402}
403
David Howellsc4028952006-11-22 14:57:56 +0000404static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
David Howellsc4028952006-11-22 14:57:56 +0000406 struct multipath *m =
407 container_of(work, struct multipath, process_queued_ios);
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700408 struct pgpath *pgpath = NULL;
409 unsigned init_required = 0, must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 unsigned long flags;
411
412 spin_lock_irqsave(&m->lock, flags);
413
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700414 if (!m->queue_size)
415 goto out;
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!m->current_pgpath)
418 __choose_pgpath(m);
419
420 pgpath = m->current_pgpath;
421
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700422 if ((pgpath && !m->queue_io) ||
423 (!pgpath && !m->queue_if_no_path))
424 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700426 if (m->pg_init_required && !m->pg_init_in_progress) {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100427 m->pg_init_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 m->pg_init_required = 0;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700429 m->pg_init_in_progress = 1;
430 init_required = 1;
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700433out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 spin_unlock_irqrestore(&m->lock, flags);
435
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700436 if (init_required)
437 queue_work(kmpath_handlerd, &m->activate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (!must_queue)
440 dispatch_queued_ios(m);
441}
442
443/*
444 * An event is triggered whenever a path is taken out of use.
445 * Includes path failure and PG bypass.
446 */
David Howellsc4028952006-11-22 14:57:56 +0000447static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
David Howellsc4028952006-11-22 14:57:56 +0000449 struct multipath *m =
450 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 dm_table_event(m->ti->table);
453}
454
455/*-----------------------------------------------------------------
456 * Constructor/argument parsing:
457 * <#multipath feature args> [<arg>]*
458 * <#hw_handler args> [hw_handler [<arg>]*]
459 * <#priority groups>
460 * <initial priority group>
461 * [<selector> <#selector args> [<arg>]*
462 * <#paths> <#per-path selector args>
463 * [<path> [<arg>]* ]+ ]+
464 *---------------------------------------------------------------*/
465struct param {
466 unsigned min;
467 unsigned max;
468 char *error;
469};
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471static int read_param(struct param *param, char *str, unsigned *v, char **error)
472{
473 if (!str ||
474 (sscanf(str, "%u", v) != 1) ||
475 (*v < param->min) ||
476 (*v > param->max)) {
477 *error = param->error;
478 return -EINVAL;
479 }
480
481 return 0;
482}
483
484struct arg_set {
485 unsigned argc;
486 char **argv;
487};
488
489static char *shift(struct arg_set *as)
490{
491 char *r;
492
493 if (as->argc) {
494 as->argc--;
495 r = *as->argv;
496 as->argv++;
497 return r;
498 }
499
500 return NULL;
501}
502
503static void consume(struct arg_set *as, unsigned n)
504{
505 BUG_ON (as->argc < n);
506 as->argc -= n;
507 as->argv += n;
508}
509
510static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
511 struct dm_target *ti)
512{
513 int r;
514 struct path_selector_type *pst;
515 unsigned ps_argc;
516
517 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700518 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 };
520
521 pst = dm_get_path_selector(shift(as));
522 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700523 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return -EINVAL;
525 }
526
527 r = read_param(_params, shift(as), &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100528 if (r) {
529 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
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";
Chandra Seetharamanfe9233f2008-05-23 18:16:40 -0700669 kfree(m->hw_handler_name);
670 m->hw_handler_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return -EINVAL;
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 consume(as, hw_argc - 1);
674
675 return 0;
676}
677
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700678static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
680 int r;
681 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700682 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100683 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100686 {0, 3, "invalid number of feature args"},
687 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 };
689
690 r = read_param(_params, shift(as), &argc, &ti->error);
691 if (r)
692 return -EINVAL;
693
694 if (!argc)
695 return 0;
696
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100697 do {
698 param_name = shift(as);
699 argc--;
700
701 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
702 r = queue_if_no_path(m, 1, 0);
703 continue;
704 }
705
706 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
707 (argc >= 1)) {
708 r = read_param(_params + 1, shift(as),
709 &m->pg_init_retries, &ti->error);
710 argc--;
711 continue;
712 }
713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100715 r = -EINVAL;
716 } while (argc && !r);
717
718 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721static int multipath_ctr(struct dm_target *ti, unsigned int argc,
722 char **argv)
723{
724 /* target parameters */
725 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700726 {1, 1024, "invalid number of priority groups"},
727 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 };
729
730 int r;
731 struct multipath *m;
732 struct arg_set as;
733 unsigned pg_count = 0;
734 unsigned next_pg_num;
735
736 as.argc = argc;
737 as.argv = argv;
738
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700739 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700741 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return -EINVAL;
743 }
744
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700745 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (r)
747 goto bad;
748
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700749 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 if (r)
751 goto bad;
752
753 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
754 if (r)
755 goto bad;
756
757 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
758 if (r)
759 goto bad;
760
761 /* parse the priority groups */
762 while (as.argc) {
763 struct priority_group *pg;
764
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700765 pg = parse_priority_group(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (!pg) {
767 r = -EINVAL;
768 goto bad;
769 }
770
771 m->nr_valid_paths += pg->nr_pgpaths;
772 list_add_tail(&pg->list, &m->priority_groups);
773 pg_count++;
774 pg->pg_num = pg_count;
775 if (!--next_pg_num)
776 m->next_pg = pg;
777 }
778
779 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700780 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 r = -EINVAL;
782 goto bad;
783 }
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return 0;
786
787 bad:
788 free_multipath(m);
789 return r;
790}
791
792static void multipath_dtr(struct dm_target *ti)
793{
794 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700795
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700796 flush_workqueue(kmpath_handlerd);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700797 flush_workqueue(kmultipathd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 free_multipath(m);
799}
800
801/*
802 * Map bios, recording original fields for later in case we have to resubmit
803 */
804static int multipath_map(struct dm_target *ti, struct bio *bio,
805 union map_info *map_context)
806{
807 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100808 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 struct multipath *m = (struct multipath *) ti->private;
810
811 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
812 dm_bio_record(&mpio->details, bio);
813
814 map_context->ptr = mpio;
815 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
816 r = map_io(m, bio, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800817 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 mempool_free(mpio, m->mpio_pool);
819
820 return r;
821}
822
823/*
824 * Take a path out of use.
825 */
826static int fail_path(struct pgpath *pgpath)
827{
828 unsigned long flags;
829 struct multipath *m = pgpath->pg->m;
830
831 spin_lock_irqsave(&m->lock, flags);
832
833 if (!pgpath->path.is_active)
834 goto out;
835
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700836 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
839 pgpath->path.is_active = 0;
840 pgpath->fail_count++;
841
842 m->nr_valid_paths--;
843
844 if (pgpath == m->current_pgpath)
845 m->current_pgpath = NULL;
846
Mike Andersonb15546f2007-10-19 22:48:02 +0100847 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
848 pgpath->path.dev->name, m->nr_valid_paths);
849
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700850 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852out:
853 spin_unlock_irqrestore(&m->lock, flags);
854
855 return 0;
856}
857
858/*
859 * Reinstate a previously-failed path
860 */
861static int reinstate_path(struct pgpath *pgpath)
862{
863 int r = 0;
864 unsigned long flags;
865 struct multipath *m = pgpath->pg->m;
866
867 spin_lock_irqsave(&m->lock, flags);
868
869 if (pgpath->path.is_active)
870 goto out;
871
872 if (!pgpath->pg->ps.type) {
873 DMWARN("Reinstate path not supported by path selector %s",
874 pgpath->pg->ps.type->name);
875 r = -EINVAL;
876 goto out;
877 }
878
879 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
880 if (r)
881 goto out;
882
883 pgpath->path.is_active = 1;
884
885 m->current_pgpath = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700886 if (!m->nr_valid_paths++ && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700887 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Mike Andersonb15546f2007-10-19 22:48:02 +0100889 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
890 pgpath->path.dev->name, m->nr_valid_paths);
891
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700892 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894out:
895 spin_unlock_irqrestore(&m->lock, flags);
896
897 return r;
898}
899
900/*
901 * Fail or reinstate all paths that match the provided struct dm_dev.
902 */
903static int action_dev(struct multipath *m, struct dm_dev *dev,
904 action_fn action)
905{
906 int r = 0;
907 struct pgpath *pgpath;
908 struct priority_group *pg;
909
910 list_for_each_entry(pg, &m->priority_groups, list) {
911 list_for_each_entry(pgpath, &pg->pgpaths, list) {
912 if (pgpath->path.dev == dev)
913 r = action(pgpath);
914 }
915 }
916
917 return r;
918}
919
920/*
921 * Temporarily try to avoid having to use the specified PG
922 */
923static void bypass_pg(struct multipath *m, struct priority_group *pg,
924 int bypassed)
925{
926 unsigned long flags;
927
928 spin_lock_irqsave(&m->lock, flags);
929
930 pg->bypassed = bypassed;
931 m->current_pgpath = NULL;
932 m->current_pg = NULL;
933
934 spin_unlock_irqrestore(&m->lock, flags);
935
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700936 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937}
938
939/*
940 * Switch to using the specified PG from the next I/O that gets mapped
941 */
942static int switch_pg_num(struct multipath *m, const char *pgstr)
943{
944 struct priority_group *pg;
945 unsigned pgnum;
946 unsigned long flags;
947
948 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
949 (pgnum > m->nr_priority_groups)) {
950 DMWARN("invalid PG number supplied to switch_pg_num");
951 return -EINVAL;
952 }
953
954 spin_lock_irqsave(&m->lock, flags);
955 list_for_each_entry(pg, &m->priority_groups, list) {
956 pg->bypassed = 0;
957 if (--pgnum)
958 continue;
959
960 m->current_pgpath = NULL;
961 m->current_pg = NULL;
962 m->next_pg = pg;
963 }
964 spin_unlock_irqrestore(&m->lock, flags);
965
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700966 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return 0;
968}
969
970/*
971 * Set/clear bypassed status of a PG.
972 * PGs are numbered upwards from 1 in the order they were declared.
973 */
974static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
975{
976 struct priority_group *pg;
977 unsigned pgnum;
978
979 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
980 (pgnum > m->nr_priority_groups)) {
981 DMWARN("invalid PG number supplied to bypass_pg");
982 return -EINVAL;
983 }
984
985 list_for_each_entry(pg, &m->priority_groups, list) {
986 if (!--pgnum)
987 break;
988 }
989
990 bypass_pg(m, pg, bypassed);
991 return 0;
992}
993
994/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100995 * Should we retry pg_init immediately?
996 */
997static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
998{
999 unsigned long flags;
1000 int limit_reached = 0;
1001
1002 spin_lock_irqsave(&m->lock, flags);
1003
1004 if (m->pg_init_count <= m->pg_init_retries)
1005 m->pg_init_required = 1;
1006 else
1007 limit_reached = 1;
1008
1009 spin_unlock_irqrestore(&m->lock, flags);
1010
1011 return limit_reached;
1012}
1013
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001014static void pg_init_done(struct dm_path *path, int errors)
1015{
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
1021 /* device or driver problems */
1022 switch (errors) {
1023 case SCSI_DH_OK:
1024 break;
1025 case SCSI_DH_NOSYS:
1026 if (!m->hw_handler_name) {
1027 errors = 0;
1028 break;
1029 }
1030 DMERR("Cannot failover device because scsi_dh_%s was not "
1031 "loaded.", m->hw_handler_name);
1032 /*
1033 * Fail path for now, so we do not ping pong
1034 */
1035 fail_path(pgpath);
1036 break;
1037 case SCSI_DH_DEV_TEMP_BUSY:
1038 /*
1039 * Probably doing something like FW upgrade on the
1040 * controller so try the other pg.
1041 */
1042 bypass_pg(m, pg, 1);
1043 break;
1044 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1045 case SCSI_DH_RETRY:
1046 case SCSI_DH_IMM_RETRY:
1047 case SCSI_DH_RES_TEMP_UNAVAIL:
1048 if (pg_init_limit_reached(m, pgpath))
1049 fail_path(pgpath);
1050 errors = 0;
1051 break;
1052 default:
1053 /*
1054 * We probably do not want to fail the path for a device
1055 * error, but this is what the old dm did. In future
1056 * patches we can do more advanced handling.
1057 */
1058 fail_path(pgpath);
1059 }
1060
1061 spin_lock_irqsave(&m->lock, flags);
1062 if (errors) {
1063 DMERR("Could not failover device. Error %d.", errors);
1064 m->current_pgpath = NULL;
1065 m->current_pg = NULL;
1066 } else if (!m->pg_init_required) {
1067 m->queue_io = 0;
1068 pg->bypassed = 0;
1069 }
1070
1071 m->pg_init_in_progress = 0;
1072 queue_work(kmultipathd, &m->process_queued_ios);
1073 spin_unlock_irqrestore(&m->lock, flags);
1074}
1075
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001076static void activate_path(struct work_struct *work)
1077{
1078 int ret;
1079 struct multipath *m =
1080 container_of(work, struct multipath, activate_path);
1081 struct dm_path *path = &m->current_pgpath->path;
1082
1083 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1084 pg_init_done(path, ret);
1085}
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/*
1088 * end_io handling
1089 */
1090static int do_end_io(struct multipath *m, struct bio *bio,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001091 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Stefan Bader640eb3b2005-11-21 21:32:35 -08001093 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
1095 if (!error)
1096 return 0; /* I/O complete */
1097
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -07001098 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1099 return error;
1100
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001101 if (error == -EOPNOTSUPP)
1102 return error;
1103
Stefan Bader640eb3b2005-11-21 21:32:35 -08001104 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (!m->nr_valid_paths) {
Kiyoshi Ueda45e15722006-12-08 02:41:10 -08001106 if (__must_push_back(m)) {
1107 spin_unlock_irqrestore(&m->lock, flags);
1108 return DM_ENDIO_REQUEUE;
1109 } else if (!m->queue_if_no_path) {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001110 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 return -EIO;
1112 } else {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001113 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 goto requeue;
1115 }
1116 }
Stefan Bader640eb3b2005-11-21 21:32:35 -08001117 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001119 if (mpio->pgpath)
1120 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
1122 requeue:
1123 dm_bio_restore(&mpio->details, bio);
1124
1125 /* queue for the daemon to resubmit or fail */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001126 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 bio_list_add(&m->queued_ios, bio);
1128 m->queue_size++;
1129 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001130 queue_work(kmultipathd, &m->process_queued_ios);
Stefan Bader640eb3b2005-11-21 21:32:35 -08001131 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001133 return DM_ENDIO_INCOMPLETE; /* io not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134}
1135
1136static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1137 int error, union map_info *map_context)
1138{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001139 struct multipath *m = ti->private;
1140 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 struct pgpath *pgpath = mpio->pgpath;
1142 struct path_selector *ps;
1143 int r;
1144
1145 r = do_end_io(m, bio, error, mpio);
1146 if (pgpath) {
1147 ps = &pgpath->pg->ps;
1148 if (ps->type->end_io)
1149 ps->type->end_io(ps, &pgpath->path);
1150 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001151 if (r != DM_ENDIO_INCOMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 mempool_free(mpio, m->mpio_pool);
1153
1154 return r;
1155}
1156
1157/*
1158 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001159 * the last path fails we must error any remaining I/O.
1160 * Note that if the freeze_bdev fails while suspending, the
1161 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 */
1163static void multipath_presuspend(struct dm_target *ti)
1164{
1165 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001167 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001170/*
1171 * Restore the queue_if_no_path setting.
1172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173static void multipath_resume(struct dm_target *ti)
1174{
1175 struct multipath *m = (struct multipath *) ti->private;
1176 unsigned long flags;
1177
1178 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001179 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 spin_unlock_irqrestore(&m->lock, flags);
1181}
1182
1183/*
1184 * Info output has the following format:
1185 * num_multipath_feature_args [multipath_feature_args]*
1186 * num_handler_status_args [handler_status_args]*
1187 * num_groups init_group_number
1188 * [A|D|E num_ps_status_args [ps_status_args]*
1189 * num_paths num_selector_args
1190 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1191 *
1192 * Table output has the following format (identical to the constructor string):
1193 * num_feature_args [features_args]*
1194 * num_handler_args hw_handler [hw_handler_args]*
1195 * num_groups init_group_number
1196 * [priority selector-name num_ps_args [ps_args]*
1197 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1198 */
1199static int multipath_status(struct dm_target *ti, status_type_t type,
1200 char *result, unsigned int maxlen)
1201{
1202 int sz = 0;
1203 unsigned long flags;
1204 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 struct priority_group *pg;
1206 struct pgpath *p;
1207 unsigned pg_num;
1208 char state;
1209
1210 spin_lock_irqsave(&m->lock, flags);
1211
1212 /* Features */
1213 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001214 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1215 else {
1216 DMEMIT("%u ", m->queue_if_no_path +
1217 (m->pg_init_retries > 0) * 2);
1218 if (m->queue_if_no_path)
1219 DMEMIT("queue_if_no_path ");
1220 if (m->pg_init_retries)
1221 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001224 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 DMEMIT("0 ");
1226 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001227 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 DMEMIT("%u ", m->nr_priority_groups);
1230
1231 if (m->next_pg)
1232 pg_num = m->next_pg->pg_num;
1233 else if (m->current_pg)
1234 pg_num = m->current_pg->pg_num;
1235 else
1236 pg_num = 1;
1237
1238 DMEMIT("%u ", pg_num);
1239
1240 switch (type) {
1241 case STATUSTYPE_INFO:
1242 list_for_each_entry(pg, &m->priority_groups, list) {
1243 if (pg->bypassed)
1244 state = 'D'; /* Disabled */
1245 else if (pg == m->current_pg)
1246 state = 'A'; /* Currently Active */
1247 else
1248 state = 'E'; /* Enabled */
1249
1250 DMEMIT("%c ", state);
1251
1252 if (pg->ps.type->status)
1253 sz += pg->ps.type->status(&pg->ps, NULL, type,
1254 result + sz,
1255 maxlen - sz);
1256 else
1257 DMEMIT("0 ");
1258
1259 DMEMIT("%u %u ", pg->nr_pgpaths,
1260 pg->ps.type->info_args);
1261
1262 list_for_each_entry(p, &pg->pgpaths, list) {
1263 DMEMIT("%s %s %u ", p->path.dev->name,
1264 p->path.is_active ? "A" : "F",
1265 p->fail_count);
1266 if (pg->ps.type->status)
1267 sz += pg->ps.type->status(&pg->ps,
1268 &p->path, type, result + sz,
1269 maxlen - sz);
1270 }
1271 }
1272 break;
1273
1274 case STATUSTYPE_TABLE:
1275 list_for_each_entry(pg, &m->priority_groups, list) {
1276 DMEMIT("%s ", pg->ps.type->name);
1277
1278 if (pg->ps.type->status)
1279 sz += pg->ps.type->status(&pg->ps, NULL, type,
1280 result + sz,
1281 maxlen - sz);
1282 else
1283 DMEMIT("0 ");
1284
1285 DMEMIT("%u %u ", pg->nr_pgpaths,
1286 pg->ps.type->table_args);
1287
1288 list_for_each_entry(p, &pg->pgpaths, list) {
1289 DMEMIT("%s ", p->path.dev->name);
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
1299 spin_unlock_irqrestore(&m->lock, flags);
1300
1301 return 0;
1302}
1303
1304static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1305{
1306 int r;
1307 struct dm_dev *dev;
1308 struct multipath *m = (struct multipath *) ti->private;
1309 action_fn action;
1310
1311 if (argc == 1) {
1312 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001313 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001315 return queue_if_no_path(m, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
1318 if (argc != 2)
1319 goto error;
1320
1321 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1322 return bypass_pg_num(m, argv[1], 1);
1323 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1324 return bypass_pg_num(m, argv[1], 0);
1325 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1326 return switch_pg_num(m, argv[1]);
1327 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1328 action = reinstate_path;
1329 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1330 action = fail_path;
1331 else
1332 goto error;
1333
1334 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1335 dm_table_get_mode(ti->table), &dev);
1336 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001337 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 argv[1]);
1339 return -EINVAL;
1340 }
1341
1342 r = action_dev(m, dev, action);
1343
1344 dm_put_device(ti, dev);
1345
1346 return r;
1347
1348error:
1349 DMWARN("Unrecognised multipath message received.");
1350 return -EINVAL;
1351}
1352
Milan Broz9af4aa32006-10-03 01:15:20 -07001353static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1354 struct file *filp, unsigned int cmd,
1355 unsigned long arg)
1356{
1357 struct multipath *m = (struct multipath *) ti->private;
1358 struct block_device *bdev = NULL;
1359 unsigned long flags;
Milan Broze90dae12006-10-03 01:15:22 -07001360 struct file fake_file = {};
1361 struct dentry fake_dentry = {};
Milan Broz9af4aa32006-10-03 01:15:20 -07001362 int r = 0;
1363
Josef Sipekc649bb92006-12-08 02:37:19 -08001364 fake_file.f_path.dentry = &fake_dentry;
Milan Broze90dae12006-10-03 01:15:22 -07001365
Milan Broz9af4aa32006-10-03 01:15:20 -07001366 spin_lock_irqsave(&m->lock, flags);
1367
1368 if (!m->current_pgpath)
1369 __choose_pgpath(m);
1370
Milan Broze90dae12006-10-03 01:15:22 -07001371 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001372 bdev = m->current_pgpath->path.dev->bdev;
Milan Broze90dae12006-10-03 01:15:22 -07001373 fake_dentry.d_inode = bdev->bd_inode;
1374 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1375 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001376
1377 if (m->queue_io)
1378 r = -EAGAIN;
1379 else if (!bdev)
1380 r = -EIO;
1381
1382 spin_unlock_irqrestore(&m->lock, flags);
1383
Milan Broze90dae12006-10-03 01:15:22 -07001384 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1385 bdev->bd_disk, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001386}
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388/*-----------------------------------------------------------------
1389 * Module setup
1390 *---------------------------------------------------------------*/
1391static struct target_type multipath_target = {
1392 .name = "multipath",
Milan Broz9af4aa32006-10-03 01:15:20 -07001393 .version = {1, 0, 5},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 .module = THIS_MODULE,
1395 .ctr = multipath_ctr,
1396 .dtr = multipath_dtr,
1397 .map = multipath_map,
1398 .end_io = multipath_end_io,
1399 .presuspend = multipath_presuspend,
1400 .resume = multipath_resume,
1401 .status = multipath_status,
1402 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001403 .ioctl = multipath_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404};
1405
1406static int __init dm_multipath_init(void)
1407{
1408 int r;
1409
1410 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001411 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (!_mpio_cache)
1413 return -ENOMEM;
1414
1415 r = dm_register_target(&multipath_target);
1416 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001417 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 kmem_cache_destroy(_mpio_cache);
1419 return -EINVAL;
1420 }
1421
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001422 kmultipathd = create_workqueue("kmpathd");
1423 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001424 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001425 dm_unregister_target(&multipath_target);
1426 kmem_cache_destroy(_mpio_cache);
1427 return -ENOMEM;
1428 }
1429
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001430 /*
1431 * A separate workqueue is used to handle the device handlers
1432 * to avoid overloading existing workqueue. Overloading the
1433 * old workqueue would also create a bottleneck in the
1434 * path of the storage hardware device activation.
1435 */
1436 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1437 if (!kmpath_handlerd) {
1438 DMERR("failed to create workqueue kmpath_handlerd");
1439 destroy_workqueue(kmultipathd);
1440 dm_unregister_target(&multipath_target);
1441 kmem_cache_destroy(_mpio_cache);
1442 return -ENOMEM;
1443 }
1444
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001445 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 multipath_target.version[0], multipath_target.version[1],
1447 multipath_target.version[2]);
1448
1449 return r;
1450}
1451
1452static void __exit dm_multipath_exit(void)
1453{
1454 int r;
1455
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001456 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001457 destroy_workqueue(kmultipathd);
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 r = dm_unregister_target(&multipath_target);
1460 if (r < 0)
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001461 DMERR("target unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 kmem_cache_destroy(_mpio_cache);
1463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465module_init(dm_multipath_init);
1466module_exit(dm_multipath_exit);
1467
1468MODULE_DESCRIPTION(DM_NAME " multipath target");
1469MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1470MODULE_LICENSE("GPL");