blob: c0950a0b5312158909ed7662a308cae9a440d151 [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"
13
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>
22#include <asm/atomic.h>
23
Alasdair G Kergon72d94862006-06-26 00:27:35 -070024#define DM_MSG_PREFIX "multipath"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define MESG_STR(x) x, sizeof(x)
26
27/* Path properties */
28struct pgpath {
29 struct list_head list;
30
31 struct priority_group *pg; /* Owning PG */
32 unsigned fail_count; /* Cumulative failure count */
33
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080034 struct dm_path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
37#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
38
39/*
40 * Paths are grouped into Priority Groups and numbered from 1 upwards.
41 * Each has a path selector which controls which path gets used.
42 */
43struct priority_group {
44 struct list_head list;
45
46 struct multipath *m; /* Owning multipath instance */
47 struct path_selector ps;
48
49 unsigned pg_num; /* Reference number */
50 unsigned bypassed; /* Temporarily bypass this PG? */
51
52 unsigned nr_pgpaths; /* Number of paths in PG */
53 struct list_head pgpaths;
54};
55
56/* Multipath context */
57struct multipath {
58 struct list_head list;
59 struct dm_target *ti;
60
61 spinlock_t lock;
62
63 struct hw_handler hw_handler;
64 unsigned nr_priority_groups;
65 struct list_head priority_groups;
66 unsigned pg_init_required; /* pg_init needs calling? */
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -070067 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 unsigned nr_valid_paths; /* Total number of usable paths */
70 struct pgpath *current_pgpath;
71 struct priority_group *current_pg;
72 struct priority_group *next_pg; /* Switch to this PG if set */
73 unsigned repeat_count; /* I/Os left before calling PS again */
74
75 unsigned queue_io; /* Must we queue all I/O? */
76 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
Alasdair G Kergon436d4102005-07-12 15:53:03 -070077 unsigned saved_queue_if_no_path;/* Saved state during suspension */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 struct work_struct process_queued_ios;
80 struct bio_list queued_ios;
81 unsigned queue_size;
82
83 struct work_struct trigger_event;
84
85 /*
Alasdair G Kergon028867a2007-07-12 17:26:32 +010086 * We must use a mempool of dm_mpath_io structs so that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 * can resubmit bios on error.
88 */
89 mempool_t *mpio_pool;
90};
91
92/*
93 * Context information attached to each bio we process.
94 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010095struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct pgpath *pgpath;
97 struct dm_bio_details details;
98};
99
100typedef int (*action_fn) (struct pgpath *pgpath);
101
102#define MIN_IOS 256 /* Mempool size */
103
Christoph Lametere18b8902006-12-06 20:33:20 -0800104static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700106struct workqueue_struct *kmultipathd;
David Howellsc4028952006-11-22 14:57:56 +0000107static void process_queued_ios(struct work_struct *work);
108static void trigger_event(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110
111/*-----------------------------------------------
112 * Allocation routines
113 *-----------------------------------------------*/
114
115static struct pgpath *alloc_pgpath(void)
116{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700117 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700119 if (pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 pgpath->path.is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 return pgpath;
123}
124
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100125static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 kfree(pgpath);
128}
129
130static struct priority_group *alloc_priority_group(void)
131{
132 struct priority_group *pg;
133
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700134 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700136 if (pg)
137 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 return pg;
140}
141
142static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
143{
144 struct pgpath *pgpath, *tmp;
145
146 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
147 list_del(&pgpath->list);
148 dm_put_device(ti, pgpath->path.dev);
149 free_pgpath(pgpath);
150 }
151}
152
153static void free_priority_group(struct priority_group *pg,
154 struct dm_target *ti)
155{
156 struct path_selector *ps = &pg->ps;
157
158 if (ps->type) {
159 ps->type->destroy(ps);
160 dm_put_path_selector(ps->type);
161 }
162
163 free_pgpaths(&pg->pgpaths, ti);
164 kfree(pg);
165}
166
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700167static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct multipath *m;
170
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700171 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 INIT_LIST_HEAD(&m->priority_groups);
174 spin_lock_init(&m->lock);
175 m->queue_io = 1;
David Howellsc4028952006-11-22 14:57:56 +0000176 INIT_WORK(&m->process_queued_ios, process_queued_ios);
177 INIT_WORK(&m->trigger_event, trigger_event);
Matthew Dobson93d23412006-03-26 01:37:50 -0800178 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (!m->mpio_pool) {
180 kfree(m);
181 return NULL;
182 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700183 m->ti = ti;
184 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 }
186
187 return m;
188}
189
190static void free_multipath(struct multipath *m)
191{
192 struct priority_group *pg, *tmp;
193 struct hw_handler *hwh = &m->hw_handler;
194
195 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
196 list_del(&pg->list);
197 free_priority_group(pg, m->ti);
198 }
199
200 if (hwh->type) {
201 hwh->type->destroy(hwh);
202 dm_put_hw_handler(hwh->type);
203 }
204
205 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{
216 struct hw_handler *hwh = &m->hw_handler;
217
218 m->current_pg = pgpath->pg;
219
220 /* Must we initialise the PG first, and queue I/O till it's ready? */
221 if (hwh->type && hwh->type->pg_init) {
222 m->pg_init_required = 1;
223 m->queue_io = 1;
224 } else {
225 m->pg_init_required = 0;
226 m->queue_io = 0;
227 }
228}
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)
393 bio_endio(bio, bio->bi_size, 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)
397 bio_endio(bio, bio->bi_size, -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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 struct hw_handler *hwh = &m->hw_handler;
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) {
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
435 if (init_required)
436 hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
437
438 if (!must_queue)
439 dispatch_queued_ios(m);
440}
441
442/*
443 * An event is triggered whenever a path is taken out of use.
444 * Includes path failure and PG bypass.
445 */
David Howellsc4028952006-11-22 14:57:56 +0000446static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
David Howellsc4028952006-11-22 14:57:56 +0000448 struct multipath *m =
449 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 dm_table_event(m->ti->table);
452}
453
454/*-----------------------------------------------------------------
455 * Constructor/argument parsing:
456 * <#multipath feature args> [<arg>]*
457 * <#hw_handler args> [hw_handler [<arg>]*]
458 * <#priority groups>
459 * <initial priority group>
460 * [<selector> <#selector args> [<arg>]*
461 * <#paths> <#per-path selector args>
462 * [<path> [<arg>]* ]+ ]+
463 *---------------------------------------------------------------*/
464struct param {
465 unsigned min;
466 unsigned max;
467 char *error;
468};
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470static int read_param(struct param *param, char *str, unsigned *v, char **error)
471{
472 if (!str ||
473 (sscanf(str, "%u", v) != 1) ||
474 (*v < param->min) ||
475 (*v > param->max)) {
476 *error = param->error;
477 return -EINVAL;
478 }
479
480 return 0;
481}
482
483struct arg_set {
484 unsigned argc;
485 char **argv;
486};
487
488static char *shift(struct arg_set *as)
489{
490 char *r;
491
492 if (as->argc) {
493 as->argc--;
494 r = *as->argv;
495 as->argv++;
496 return r;
497 }
498
499 return NULL;
500}
501
502static void consume(struct arg_set *as, unsigned n)
503{
504 BUG_ON (as->argc < n);
505 as->argc -= n;
506 as->argv += n;
507}
508
509static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
510 struct dm_target *ti)
511{
512 int r;
513 struct path_selector_type *pst;
514 unsigned ps_argc;
515
516 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700517 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 };
519
520 pst = dm_get_path_selector(shift(as));
521 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700522 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 return -EINVAL;
524 }
525
526 r = read_param(_params, shift(as), &ps_argc, &ti->error);
527 if (r)
528 return -EINVAL;
529
530 r = pst->create(&pg->ps, ps_argc, as->argv);
531 if (r) {
532 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700533 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return r;
535 }
536
537 pg->ps.type = pst;
538 consume(as, ps_argc);
539
540 return 0;
541}
542
543static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
544 struct dm_target *ti)
545{
546 int r;
547 struct pgpath *p;
548
549 /* we need at least a path arg */
550 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700551 ti->error = "no device given";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return NULL;
553 }
554
555 p = alloc_pgpath();
556 if (!p)
557 return NULL;
558
559 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
560 dm_table_get_mode(ti->table), &p->path.dev);
561 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700562 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto bad;
564 }
565
566 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
567 if (r) {
568 dm_put_device(ti, p->path.dev);
569 goto bad;
570 }
571
572 return p;
573
574 bad:
575 free_pgpath(p);
576 return NULL;
577}
578
579static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700580 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700583 {1, 1024, "invalid number of paths"},
584 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 };
586
587 int r;
588 unsigned i, nr_selector_args, nr_params;
589 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700590 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 if (as->argc < 2) {
593 as->argc = 0;
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700594 ti->error = "not enough priority group aruments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return NULL;
596 }
597
598 pg = alloc_priority_group();
599 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700600 ti->error = "couldn't allocate priority group";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return NULL;
602 }
603 pg->m = m;
604
605 r = parse_path_selector(as, pg, ti);
606 if (r)
607 goto bad;
608
609 /*
610 * read the paths
611 */
612 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
613 if (r)
614 goto bad;
615
616 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
617 if (r)
618 goto bad;
619
620 nr_params = 1 + nr_selector_args;
621 for (i = 0; i < pg->nr_pgpaths; i++) {
622 struct pgpath *pgpath;
623 struct arg_set path_args;
624
625 if (as->argc < nr_params)
626 goto bad;
627
628 path_args.argc = nr_params;
629 path_args.argv = as->argv;
630
631 pgpath = parse_path(&path_args, &pg->ps, ti);
632 if (!pgpath)
633 goto bad;
634
635 pgpath->pg = pg;
636 list_add_tail(&pgpath->list, &pg->pgpaths);
637 consume(as, nr_params);
638 }
639
640 return pg;
641
642 bad:
643 free_priority_group(pg, ti);
644 return NULL;
645}
646
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700647static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 int r;
650 struct hw_handler_type *hwht;
651 unsigned hw_argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700652 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700655 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 };
657
658 r = read_param(_params, shift(as), &hw_argc, &ti->error);
659 if (r)
660 return -EINVAL;
661
662 if (!hw_argc)
663 return 0;
664
665 hwht = dm_get_hw_handler(shift(as));
666 if (!hwht) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700667 ti->error = "unknown hardware handler type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 return -EINVAL;
669 }
670
Edward Goggin79eb8852007-05-09 02:32:56 -0700671 m->hw_handler.md = dm_table_get_md(ti->table);
672 dm_put(m->hw_handler.md);
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
675 if (r) {
676 dm_put_hw_handler(hwht);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700677 ti->error = "hardware handler constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return r;
679 }
680
681 m->hw_handler.type = hwht;
682 consume(as, hw_argc - 1);
683
684 return 0;
685}
686
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700687static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 int r;
690 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700691 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700694 {0, 1, "invalid number of feature args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 };
696
697 r = read_param(_params, shift(as), &argc, &ti->error);
698 if (r)
699 return -EINVAL;
700
701 if (!argc)
702 return 0;
703
704 if (!strnicmp(shift(as), MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700705 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 else {
707 ti->error = "Unrecognised multipath feature request";
708 return -EINVAL;
709 }
710}
711
712static int multipath_ctr(struct dm_target *ti, unsigned int argc,
713 char **argv)
714{
715 /* target parameters */
716 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700717 {1, 1024, "invalid number of priority groups"},
718 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 };
720
721 int r;
722 struct multipath *m;
723 struct arg_set as;
724 unsigned pg_count = 0;
725 unsigned next_pg_num;
726
727 as.argc = argc;
728 as.argv = argv;
729
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700730 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700732 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return -EINVAL;
734 }
735
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700736 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (r)
738 goto bad;
739
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700740 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (r)
742 goto bad;
743
744 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
745 if (r)
746 goto bad;
747
748 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
749 if (r)
750 goto bad;
751
752 /* parse the priority groups */
753 while (as.argc) {
754 struct priority_group *pg;
755
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700756 pg = parse_priority_group(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!pg) {
758 r = -EINVAL;
759 goto bad;
760 }
761
762 m->nr_valid_paths += pg->nr_pgpaths;
763 list_add_tail(&pg->list, &m->priority_groups);
764 pg_count++;
765 pg->pg_num = pg_count;
766 if (!--next_pg_num)
767 m->next_pg = pg;
768 }
769
770 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700771 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 r = -EINVAL;
773 goto bad;
774 }
775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return 0;
777
778 bad:
779 free_multipath(m);
780 return r;
781}
782
783static void multipath_dtr(struct dm_target *ti)
784{
785 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700786
787 flush_workqueue(kmultipathd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 free_multipath(m);
789}
790
791/*
792 * Map bios, recording original fields for later in case we have to resubmit
793 */
794static int multipath_map(struct dm_target *ti, struct bio *bio,
795 union map_info *map_context)
796{
797 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100798 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 struct multipath *m = (struct multipath *) ti->private;
800
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -0700801 if (bio_barrier(bio))
802 return -EOPNOTSUPP;
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
805 dm_bio_record(&mpio->details, bio);
806
807 map_context->ptr = mpio;
808 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
809 r = map_io(m, bio, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800810 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 mempool_free(mpio, m->mpio_pool);
812
813 return r;
814}
815
816/*
817 * Take a path out of use.
818 */
819static int fail_path(struct pgpath *pgpath)
820{
821 unsigned long flags;
822 struct multipath *m = pgpath->pg->m;
823
824 spin_lock_irqsave(&m->lock, flags);
825
826 if (!pgpath->path.is_active)
827 goto out;
828
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700829 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
832 pgpath->path.is_active = 0;
833 pgpath->fail_count++;
834
835 m->nr_valid_paths--;
836
837 if (pgpath == m->current_pgpath)
838 m->current_pgpath = NULL;
839
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700840 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842out:
843 spin_unlock_irqrestore(&m->lock, flags);
844
845 return 0;
846}
847
848/*
849 * Reinstate a previously-failed path
850 */
851static int reinstate_path(struct pgpath *pgpath)
852{
853 int r = 0;
854 unsigned long flags;
855 struct multipath *m = pgpath->pg->m;
856
857 spin_lock_irqsave(&m->lock, flags);
858
859 if (pgpath->path.is_active)
860 goto out;
861
862 if (!pgpath->pg->ps.type) {
863 DMWARN("Reinstate path not supported by path selector %s",
864 pgpath->pg->ps.type->name);
865 r = -EINVAL;
866 goto out;
867 }
868
869 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
870 if (r)
871 goto out;
872
873 pgpath->path.is_active = 1;
874
875 m->current_pgpath = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700876 if (!m->nr_valid_paths++ && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700877 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700879 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881out:
882 spin_unlock_irqrestore(&m->lock, flags);
883
884 return r;
885}
886
887/*
888 * Fail or reinstate all paths that match the provided struct dm_dev.
889 */
890static int action_dev(struct multipath *m, struct dm_dev *dev,
891 action_fn action)
892{
893 int r = 0;
894 struct pgpath *pgpath;
895 struct priority_group *pg;
896
897 list_for_each_entry(pg, &m->priority_groups, list) {
898 list_for_each_entry(pgpath, &pg->pgpaths, list) {
899 if (pgpath->path.dev == dev)
900 r = action(pgpath);
901 }
902 }
903
904 return r;
905}
906
907/*
908 * Temporarily try to avoid having to use the specified PG
909 */
910static void bypass_pg(struct multipath *m, struct priority_group *pg,
911 int bypassed)
912{
913 unsigned long flags;
914
915 spin_lock_irqsave(&m->lock, flags);
916
917 pg->bypassed = bypassed;
918 m->current_pgpath = NULL;
919 m->current_pg = NULL;
920
921 spin_unlock_irqrestore(&m->lock, flags);
922
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700923 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
926/*
927 * Switch to using the specified PG from the next I/O that gets mapped
928 */
929static int switch_pg_num(struct multipath *m, const char *pgstr)
930{
931 struct priority_group *pg;
932 unsigned pgnum;
933 unsigned long flags;
934
935 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
936 (pgnum > m->nr_priority_groups)) {
937 DMWARN("invalid PG number supplied to switch_pg_num");
938 return -EINVAL;
939 }
940
941 spin_lock_irqsave(&m->lock, flags);
942 list_for_each_entry(pg, &m->priority_groups, list) {
943 pg->bypassed = 0;
944 if (--pgnum)
945 continue;
946
947 m->current_pgpath = NULL;
948 m->current_pg = NULL;
949 m->next_pg = pg;
950 }
951 spin_unlock_irqrestore(&m->lock, flags);
952
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700953 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 return 0;
955}
956
957/*
958 * Set/clear bypassed status of a PG.
959 * PGs are numbered upwards from 1 in the order they were declared.
960 */
961static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
962{
963 struct priority_group *pg;
964 unsigned pgnum;
965
966 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
967 (pgnum > m->nr_priority_groups)) {
968 DMWARN("invalid PG number supplied to bypass_pg");
969 return -EINVAL;
970 }
971
972 list_for_each_entry(pg, &m->priority_groups, list) {
973 if (!--pgnum)
974 break;
975 }
976
977 bypass_pg(m, pg, bypassed);
978 return 0;
979}
980
981/*
982 * pg_init must call this when it has completed its initialisation
983 */
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800984void dm_pg_init_complete(struct dm_path *path, unsigned err_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct pgpath *pgpath = path_to_pgpath(path);
987 struct priority_group *pg = pgpath->pg;
988 struct multipath *m = pg->m;
989 unsigned long flags;
990
991 /* We insist on failing the path if the PG is already bypassed. */
992 if (err_flags && pg->bypassed)
993 err_flags |= MP_FAIL_PATH;
994
995 if (err_flags & MP_FAIL_PATH)
996 fail_path(pgpath);
997
998 if (err_flags & MP_BYPASS_PG)
999 bypass_pg(m, pg, 1);
1000
1001 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -07001002 if (err_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 m->current_pgpath = NULL;
1004 m->current_pg = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -07001005 } else if (!m->pg_init_required)
1006 m->queue_io = 0;
1007
1008 m->pg_init_in_progress = 0;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001009 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 spin_unlock_irqrestore(&m->lock, flags);
1011}
1012
1013/*
1014 * end_io handling
1015 */
1016static int do_end_io(struct multipath *m, struct bio *bio,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001017 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct hw_handler *hwh = &m->hw_handler;
1020 unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001021 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 if (!error)
1024 return 0; /* I/O complete */
1025
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -07001026 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1027 return error;
1028
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001029 if (error == -EOPNOTSUPP)
1030 return error;
1031
Stefan Bader640eb3b2005-11-21 21:32:35 -08001032 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 if (!m->nr_valid_paths) {
Kiyoshi Ueda45e15722006-12-08 02:41:10 -08001034 if (__must_push_back(m)) {
1035 spin_unlock_irqrestore(&m->lock, flags);
1036 return DM_ENDIO_REQUEUE;
1037 } else if (!m->queue_if_no_path) {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001038 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return -EIO;
1040 } else {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001041 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 goto requeue;
1043 }
1044 }
Stefan Bader640eb3b2005-11-21 21:32:35 -08001045 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 if (hwh->type && hwh->type->error)
1048 err_flags = hwh->type->error(hwh, bio);
1049
1050 if (mpio->pgpath) {
1051 if (err_flags & MP_FAIL_PATH)
1052 fail_path(mpio->pgpath);
1053
1054 if (err_flags & MP_BYPASS_PG)
1055 bypass_pg(m, mpio->pgpath->pg, 1);
1056 }
1057
1058 if (err_flags & MP_ERROR_IO)
1059 return -EIO;
1060
1061 requeue:
1062 dm_bio_restore(&mpio->details, bio);
1063
1064 /* queue for the daemon to resubmit or fail */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001065 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 bio_list_add(&m->queued_ios, bio);
1067 m->queue_size++;
1068 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001069 queue_work(kmultipathd, &m->process_queued_ios);
Stefan Bader640eb3b2005-11-21 21:32:35 -08001070 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001072 return DM_ENDIO_INCOMPLETE; /* io not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
1075static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1076 int error, union map_info *map_context)
1077{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001078 struct multipath *m = ti->private;
1079 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct pgpath *pgpath = mpio->pgpath;
1081 struct path_selector *ps;
1082 int r;
1083
1084 r = do_end_io(m, bio, error, mpio);
1085 if (pgpath) {
1086 ps = &pgpath->pg->ps;
1087 if (ps->type->end_io)
1088 ps->type->end_io(ps, &pgpath->path);
1089 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001090 if (r != DM_ENDIO_INCOMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 mempool_free(mpio, m->mpio_pool);
1092
1093 return r;
1094}
1095
1096/*
1097 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001098 * the last path fails we must error any remaining I/O.
1099 * Note that if the freeze_bdev fails while suspending, the
1100 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
1102static void multipath_presuspend(struct dm_target *ti)
1103{
1104 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001106 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001109/*
1110 * Restore the queue_if_no_path setting.
1111 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112static void multipath_resume(struct dm_target *ti)
1113{
1114 struct multipath *m = (struct multipath *) ti->private;
1115 unsigned long flags;
1116
1117 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001118 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 spin_unlock_irqrestore(&m->lock, flags);
1120}
1121
1122/*
1123 * Info output has the following format:
1124 * num_multipath_feature_args [multipath_feature_args]*
1125 * num_handler_status_args [handler_status_args]*
1126 * num_groups init_group_number
1127 * [A|D|E num_ps_status_args [ps_status_args]*
1128 * num_paths num_selector_args
1129 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1130 *
1131 * Table output has the following format (identical to the constructor string):
1132 * num_feature_args [features_args]*
1133 * num_handler_args hw_handler [hw_handler_args]*
1134 * num_groups init_group_number
1135 * [priority selector-name num_ps_args [ps_args]*
1136 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1137 */
1138static int multipath_status(struct dm_target *ti, status_type_t type,
1139 char *result, unsigned int maxlen)
1140{
1141 int sz = 0;
1142 unsigned long flags;
1143 struct multipath *m = (struct multipath *) ti->private;
1144 struct hw_handler *hwh = &m->hw_handler;
1145 struct priority_group *pg;
1146 struct pgpath *p;
1147 unsigned pg_num;
1148 char state;
1149
1150 spin_lock_irqsave(&m->lock, flags);
1151
1152 /* Features */
1153 if (type == STATUSTYPE_INFO)
1154 DMEMIT("1 %u ", m->queue_size);
1155 else if (m->queue_if_no_path)
1156 DMEMIT("1 queue_if_no_path ");
1157 else
1158 DMEMIT("0 ");
1159
1160 if (hwh->type && hwh->type->status)
1161 sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
1162 else if (!hwh->type || type == STATUSTYPE_INFO)
1163 DMEMIT("0 ");
1164 else
1165 DMEMIT("1 %s ", hwh->type->name);
1166
1167 DMEMIT("%u ", m->nr_priority_groups);
1168
1169 if (m->next_pg)
1170 pg_num = m->next_pg->pg_num;
1171 else if (m->current_pg)
1172 pg_num = m->current_pg->pg_num;
1173 else
1174 pg_num = 1;
1175
1176 DMEMIT("%u ", pg_num);
1177
1178 switch (type) {
1179 case STATUSTYPE_INFO:
1180 list_for_each_entry(pg, &m->priority_groups, list) {
1181 if (pg->bypassed)
1182 state = 'D'; /* Disabled */
1183 else if (pg == m->current_pg)
1184 state = 'A'; /* Currently Active */
1185 else
1186 state = 'E'; /* Enabled */
1187
1188 DMEMIT("%c ", state);
1189
1190 if (pg->ps.type->status)
1191 sz += pg->ps.type->status(&pg->ps, NULL, type,
1192 result + sz,
1193 maxlen - sz);
1194 else
1195 DMEMIT("0 ");
1196
1197 DMEMIT("%u %u ", pg->nr_pgpaths,
1198 pg->ps.type->info_args);
1199
1200 list_for_each_entry(p, &pg->pgpaths, list) {
1201 DMEMIT("%s %s %u ", p->path.dev->name,
1202 p->path.is_active ? "A" : "F",
1203 p->fail_count);
1204 if (pg->ps.type->status)
1205 sz += pg->ps.type->status(&pg->ps,
1206 &p->path, type, result + sz,
1207 maxlen - sz);
1208 }
1209 }
1210 break;
1211
1212 case STATUSTYPE_TABLE:
1213 list_for_each_entry(pg, &m->priority_groups, list) {
1214 DMEMIT("%s ", pg->ps.type->name);
1215
1216 if (pg->ps.type->status)
1217 sz += pg->ps.type->status(&pg->ps, NULL, type,
1218 result + sz,
1219 maxlen - sz);
1220 else
1221 DMEMIT("0 ");
1222
1223 DMEMIT("%u %u ", pg->nr_pgpaths,
1224 pg->ps.type->table_args);
1225
1226 list_for_each_entry(p, &pg->pgpaths, list) {
1227 DMEMIT("%s ", p->path.dev->name);
1228 if (pg->ps.type->status)
1229 sz += pg->ps.type->status(&pg->ps,
1230 &p->path, type, result + sz,
1231 maxlen - sz);
1232 }
1233 }
1234 break;
1235 }
1236
1237 spin_unlock_irqrestore(&m->lock, flags);
1238
1239 return 0;
1240}
1241
1242static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1243{
1244 int r;
1245 struct dm_dev *dev;
1246 struct multipath *m = (struct multipath *) ti->private;
1247 action_fn action;
1248
1249 if (argc == 1) {
1250 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001251 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001253 return queue_if_no_path(m, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 }
1255
1256 if (argc != 2)
1257 goto error;
1258
1259 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1260 return bypass_pg_num(m, argv[1], 1);
1261 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1262 return bypass_pg_num(m, argv[1], 0);
1263 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1264 return switch_pg_num(m, argv[1]);
1265 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1266 action = reinstate_path;
1267 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1268 action = fail_path;
1269 else
1270 goto error;
1271
1272 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1273 dm_table_get_mode(ti->table), &dev);
1274 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001275 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 argv[1]);
1277 return -EINVAL;
1278 }
1279
1280 r = action_dev(m, dev, action);
1281
1282 dm_put_device(ti, dev);
1283
1284 return r;
1285
1286error:
1287 DMWARN("Unrecognised multipath message received.");
1288 return -EINVAL;
1289}
1290
Milan Broz9af4aa32006-10-03 01:15:20 -07001291static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1292 struct file *filp, unsigned int cmd,
1293 unsigned long arg)
1294{
1295 struct multipath *m = (struct multipath *) ti->private;
1296 struct block_device *bdev = NULL;
1297 unsigned long flags;
Milan Broze90dae12006-10-03 01:15:22 -07001298 struct file fake_file = {};
1299 struct dentry fake_dentry = {};
Milan Broz9af4aa32006-10-03 01:15:20 -07001300 int r = 0;
1301
Josef Sipekc649bb92006-12-08 02:37:19 -08001302 fake_file.f_path.dentry = &fake_dentry;
Milan Broze90dae12006-10-03 01:15:22 -07001303
Milan Broz9af4aa32006-10-03 01:15:20 -07001304 spin_lock_irqsave(&m->lock, flags);
1305
1306 if (!m->current_pgpath)
1307 __choose_pgpath(m);
1308
Milan Broze90dae12006-10-03 01:15:22 -07001309 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001310 bdev = m->current_pgpath->path.dev->bdev;
Milan Broze90dae12006-10-03 01:15:22 -07001311 fake_dentry.d_inode = bdev->bd_inode;
1312 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1313 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001314
1315 if (m->queue_io)
1316 r = -EAGAIN;
1317 else if (!bdev)
1318 r = -EIO;
1319
1320 spin_unlock_irqrestore(&m->lock, flags);
1321
Milan Broze90dae12006-10-03 01:15:22 -07001322 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1323 bdev->bd_disk, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001324}
1325
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326/*-----------------------------------------------------------------
1327 * Module setup
1328 *---------------------------------------------------------------*/
1329static struct target_type multipath_target = {
1330 .name = "multipath",
Milan Broz9af4aa32006-10-03 01:15:20 -07001331 .version = {1, 0, 5},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 .module = THIS_MODULE,
1333 .ctr = multipath_ctr,
1334 .dtr = multipath_dtr,
1335 .map = multipath_map,
1336 .end_io = multipath_end_io,
1337 .presuspend = multipath_presuspend,
1338 .resume = multipath_resume,
1339 .status = multipath_status,
1340 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001341 .ioctl = multipath_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342};
1343
1344static int __init dm_multipath_init(void)
1345{
1346 int r;
1347
1348 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001349 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 if (!_mpio_cache)
1351 return -ENOMEM;
1352
1353 r = dm_register_target(&multipath_target);
1354 if (r < 0) {
1355 DMERR("%s: register failed %d", multipath_target.name, r);
1356 kmem_cache_destroy(_mpio_cache);
1357 return -EINVAL;
1358 }
1359
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001360 kmultipathd = create_workqueue("kmpathd");
1361 if (!kmultipathd) {
1362 DMERR("%s: failed to create workqueue kmpathd",
1363 multipath_target.name);
1364 dm_unregister_target(&multipath_target);
1365 kmem_cache_destroy(_mpio_cache);
1366 return -ENOMEM;
1367 }
1368
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001369 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 multipath_target.version[0], multipath_target.version[1],
1371 multipath_target.version[2]);
1372
1373 return r;
1374}
1375
1376static void __exit dm_multipath_exit(void)
1377{
1378 int r;
1379
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001380 destroy_workqueue(kmultipathd);
1381
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 r = dm_unregister_target(&multipath_target);
1383 if (r < 0)
1384 DMERR("%s: target unregister failed %d",
1385 multipath_target.name, r);
1386 kmem_cache_destroy(_mpio_cache);
1387}
1388
1389EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1390
1391module_init(dm_multipath_init);
1392module_exit(dm_multipath_exit);
1393
1394MODULE_DESCRIPTION(DM_NAME " multipath target");
1395MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1396MODULE_LICENSE("GPL");