blob: dd5ad6310f54bf64510149f8b5fcea926e328b9d [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 */
Dave Wysochanskic9e45582007-10-19 22:47:53 +010078 unsigned pg_init_retries; /* Number of times to retry pg_init */
79 unsigned pg_init_count; /* Number of times pg_init called */
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 struct work_struct process_queued_ios;
82 struct bio_list queued_ios;
83 unsigned queue_size;
84
85 struct work_struct trigger_event;
86
87 /*
Alasdair G Kergon028867a2007-07-12 17:26:32 +010088 * We must use a mempool of dm_mpath_io structs so that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * can resubmit bios on error.
90 */
91 mempool_t *mpio_pool;
92};
93
94/*
95 * Context information attached to each bio we process.
96 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +010097struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct pgpath *pgpath;
99 struct dm_bio_details details;
100};
101
102typedef int (*action_fn) (struct pgpath *pgpath);
103
104#define MIN_IOS 256 /* Mempool size */
105
Christoph Lametere18b8902006-12-06 20:33:20 -0800106static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700108struct workqueue_struct *kmultipathd;
David Howellsc4028952006-11-22 14:57:56 +0000109static void process_queued_ios(struct work_struct *work);
110static void trigger_event(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112
113/*-----------------------------------------------
114 * Allocation routines
115 *-----------------------------------------------*/
116
117static struct pgpath *alloc_pgpath(void)
118{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700119 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700121 if (pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 pgpath->path.is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 return pgpath;
125}
126
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100127static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 kfree(pgpath);
130}
131
132static struct priority_group *alloc_priority_group(void)
133{
134 struct priority_group *pg;
135
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700136 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700138 if (pg)
139 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 return pg;
142}
143
144static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
145{
146 struct pgpath *pgpath, *tmp;
147
148 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
149 list_del(&pgpath->list);
150 dm_put_device(ti, pgpath->path.dev);
151 free_pgpath(pgpath);
152 }
153}
154
155static void free_priority_group(struct priority_group *pg,
156 struct dm_target *ti)
157{
158 struct path_selector *ps = &pg->ps;
159
160 if (ps->type) {
161 ps->type->destroy(ps);
162 dm_put_path_selector(ps->type);
163 }
164
165 free_pgpaths(&pg->pgpaths, ti);
166 kfree(pg);
167}
168
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700169static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 struct multipath *m;
172
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700173 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 INIT_LIST_HEAD(&m->priority_groups);
176 spin_lock_init(&m->lock);
177 m->queue_io = 1;
David Howellsc4028952006-11-22 14:57:56 +0000178 INIT_WORK(&m->process_queued_ios, process_queued_ios);
179 INIT_WORK(&m->trigger_event, trigger_event);
Matthew Dobson93d23412006-03-26 01:37:50 -0800180 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (!m->mpio_pool) {
182 kfree(m);
183 return NULL;
184 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700185 m->ti = ti;
186 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
189 return m;
190}
191
192static void free_multipath(struct multipath *m)
193{
194 struct priority_group *pg, *tmp;
195 struct hw_handler *hwh = &m->hw_handler;
196
197 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
198 list_del(&pg->list);
199 free_priority_group(pg, m->ti);
200 }
201
202 if (hwh->type) {
203 hwh->type->destroy(hwh);
204 dm_put_hw_handler(hwh->type);
205 }
206
207 mempool_destroy(m->mpio_pool);
208 kfree(m);
209}
210
211
212/*-----------------------------------------------
213 * Path selection
214 *-----------------------------------------------*/
215
216static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
217{
218 struct hw_handler *hwh = &m->hw_handler;
219
220 m->current_pg = pgpath->pg;
221
222 /* Must we initialise the PG first, and queue I/O till it's ready? */
223 if (hwh->type && hwh->type->pg_init) {
224 m->pg_init_required = 1;
225 m->queue_io = 1;
226 } else {
227 m->pg_init_required = 0;
228 m->queue_io = 0;
229 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100230
231 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
234static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
235{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800236 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
239 if (!path)
240 return -ENXIO;
241
242 m->current_pgpath = path_to_pgpath(path);
243
244 if (m->current_pg != pg)
245 __switch_pg(m, m->current_pgpath);
246
247 return 0;
248}
249
250static void __choose_pgpath(struct multipath *m)
251{
252 struct priority_group *pg;
253 unsigned bypassed = 1;
254
255 if (!m->nr_valid_paths)
256 goto failed;
257
258 /* Were we instructed to switch PG? */
259 if (m->next_pg) {
260 pg = m->next_pg;
261 m->next_pg = NULL;
262 if (!__choose_path_in_pg(m, pg))
263 return;
264 }
265
266 /* Don't change PG until it has no remaining paths */
267 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
268 return;
269
270 /*
271 * Loop through priority groups until we find a valid path.
272 * First time we skip PGs marked 'bypassed'.
273 * Second time we only try the ones we skipped.
274 */
275 do {
276 list_for_each_entry(pg, &m->priority_groups, list) {
277 if (pg->bypassed == bypassed)
278 continue;
279 if (!__choose_path_in_pg(m, pg))
280 return;
281 }
282 } while (bypassed--);
283
284failed:
285 m->current_pgpath = NULL;
286 m->current_pg = NULL;
287}
288
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800289/*
290 * Check whether bios must be queued in the device-mapper core rather
291 * than here in the target.
292 *
293 * m->lock must be held on entry.
294 *
295 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
296 * same value then we are not between multipath_presuspend()
297 * and multipath_resume() calls and we have no need to check
298 * for the DMF_NOFLUSH_SUSPENDING flag.
299 */
300static int __must_push_back(struct multipath *m)
301{
302 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
303 dm_noflush_suspending(m->ti));
304}
305
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100306static int map_io(struct multipath *m, struct bio *bio,
307 struct dm_mpath_io *mpio, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800309 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 unsigned long flags;
311 struct pgpath *pgpath;
312
313 spin_lock_irqsave(&m->lock, flags);
314
315 /* Do we need to select a new pgpath? */
316 if (!m->current_pgpath ||
317 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
318 __choose_pgpath(m);
319
320 pgpath = m->current_pgpath;
321
322 if (was_queued)
323 m->queue_size--;
324
325 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700326 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 /* Queue for the daemon to resubmit */
328 bio_list_add(&m->queued_ios, bio);
329 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700330 if ((m->pg_init_required && !m->pg_init_in_progress) ||
331 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700332 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800334 r = DM_MAPIO_SUBMITTED;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800335 } else if (pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 bio->bi_bdev = pgpath->path.dev->bdev;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800337 else if (__must_push_back(m))
338 r = DM_MAPIO_REQUEUE;
339 else
340 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
342 mpio->pgpath = pgpath;
343
344 spin_unlock_irqrestore(&m->lock, flags);
345
346 return r;
347}
348
349/*
350 * If we run out of usable paths, should we queue I/O or error it?
351 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700352static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
353 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 unsigned long flags;
356
357 spin_lock_irqsave(&m->lock, flags);
358
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700359 if (save_old_value)
360 m->saved_queue_if_no_path = m->queue_if_no_path;
361 else
362 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700364 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700365 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 spin_unlock_irqrestore(&m->lock, flags);
368
369 return 0;
370}
371
372/*-----------------------------------------------------------------
373 * The multipath daemon is responsible for resubmitting queued ios.
374 *---------------------------------------------------------------*/
375
376static void dispatch_queued_ios(struct multipath *m)
377{
378 int r;
379 unsigned long flags;
380 struct bio *bio = NULL, *next;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100381 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 union map_info *info;
383
384 spin_lock_irqsave(&m->lock, flags);
385 bio = bio_list_get(&m->queued_ios);
386 spin_unlock_irqrestore(&m->lock, flags);
387
388 while (bio) {
389 next = bio->bi_next;
390 bio->bi_next = NULL;
391
392 info = dm_get_mapinfo(bio);
393 mpio = info->ptr;
394
395 r = map_io(m, bio, mpio, 1);
396 if (r < 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200397 bio_endio(bio, r);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800398 else if (r == DM_MAPIO_REMAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 generic_make_request(bio);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800400 else if (r == DM_MAPIO_REQUEUE)
NeilBrown6712ecf2007-09-27 12:47:43 +0200401 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 bio = next;
404 }
405}
406
David Howellsc4028952006-11-22 14:57:56 +0000407static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
David Howellsc4028952006-11-22 14:57:56 +0000409 struct multipath *m =
410 container_of(work, struct multipath, process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 struct hw_handler *hwh = &m->hw_handler;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700412 struct pgpath *pgpath = NULL;
413 unsigned init_required = 0, must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 unsigned long flags;
415
416 spin_lock_irqsave(&m->lock, flags);
417
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700418 if (!m->queue_size)
419 goto out;
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!m->current_pgpath)
422 __choose_pgpath(m);
423
424 pgpath = m->current_pgpath;
425
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700426 if ((pgpath && !m->queue_io) ||
427 (!pgpath && !m->queue_if_no_path))
428 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700430 if (m->pg_init_required && !m->pg_init_in_progress) {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100431 m->pg_init_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 m->pg_init_required = 0;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700433 m->pg_init_in_progress = 1;
434 init_required = 1;
435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700437out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 spin_unlock_irqrestore(&m->lock, flags);
439
440 if (init_required)
441 hwh->type->pg_init(hwh, pgpath->pg->bypassed, &pgpath->path);
442
443 if (!must_queue)
444 dispatch_queued_ios(m);
445}
446
447/*
448 * An event is triggered whenever a path is taken out of use.
449 * Includes path failure and PG bypass.
450 */
David Howellsc4028952006-11-22 14:57:56 +0000451static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
David Howellsc4028952006-11-22 14:57:56 +0000453 struct multipath *m =
454 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
456 dm_table_event(m->ti->table);
457}
458
459/*-----------------------------------------------------------------
460 * Constructor/argument parsing:
461 * <#multipath feature args> [<arg>]*
462 * <#hw_handler args> [hw_handler [<arg>]*]
463 * <#priority groups>
464 * <initial priority group>
465 * [<selector> <#selector args> [<arg>]*
466 * <#paths> <#per-path selector args>
467 * [<path> [<arg>]* ]+ ]+
468 *---------------------------------------------------------------*/
469struct param {
470 unsigned min;
471 unsigned max;
472 char *error;
473};
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static int read_param(struct param *param, char *str, unsigned *v, char **error)
476{
477 if (!str ||
478 (sscanf(str, "%u", v) != 1) ||
479 (*v < param->min) ||
480 (*v > param->max)) {
481 *error = param->error;
482 return -EINVAL;
483 }
484
485 return 0;
486}
487
488struct arg_set {
489 unsigned argc;
490 char **argv;
491};
492
493static char *shift(struct arg_set *as)
494{
495 char *r;
496
497 if (as->argc) {
498 as->argc--;
499 r = *as->argv;
500 as->argv++;
501 return r;
502 }
503
504 return NULL;
505}
506
507static void consume(struct arg_set *as, unsigned n)
508{
509 BUG_ON (as->argc < n);
510 as->argc -= n;
511 as->argv += n;
512}
513
514static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
515 struct dm_target *ti)
516{
517 int r;
518 struct path_selector_type *pst;
519 unsigned ps_argc;
520
521 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700522 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 };
524
525 pst = dm_get_path_selector(shift(as));
526 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700527 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return -EINVAL;
529 }
530
531 r = read_param(_params, shift(as), &ps_argc, &ti->error);
532 if (r)
533 return -EINVAL;
534
535 r = pst->create(&pg->ps, ps_argc, as->argv);
536 if (r) {
537 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700538 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return r;
540 }
541
542 pg->ps.type = pst;
543 consume(as, ps_argc);
544
545 return 0;
546}
547
548static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
549 struct dm_target *ti)
550{
551 int r;
552 struct pgpath *p;
553
554 /* we need at least a path arg */
555 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700556 ti->error = "no device given";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return NULL;
558 }
559
560 p = alloc_pgpath();
561 if (!p)
562 return NULL;
563
564 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
565 dm_table_get_mode(ti->table), &p->path.dev);
566 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700567 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 goto bad;
569 }
570
571 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
572 if (r) {
573 dm_put_device(ti, p->path.dev);
574 goto bad;
575 }
576
577 return p;
578
579 bad:
580 free_pgpath(p);
581 return NULL;
582}
583
584static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700585 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
587 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700588 {1, 1024, "invalid number of paths"},
589 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 };
591
592 int r;
593 unsigned i, nr_selector_args, nr_params;
594 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700595 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 if (as->argc < 2) {
598 as->argc = 0;
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700599 ti->error = "not enough priority group aruments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 return NULL;
601 }
602
603 pg = alloc_priority_group();
604 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700605 ti->error = "couldn't allocate priority group";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return NULL;
607 }
608 pg->m = m;
609
610 r = parse_path_selector(as, pg, ti);
611 if (r)
612 goto bad;
613
614 /*
615 * read the paths
616 */
617 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
618 if (r)
619 goto bad;
620
621 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
622 if (r)
623 goto bad;
624
625 nr_params = 1 + nr_selector_args;
626 for (i = 0; i < pg->nr_pgpaths; i++) {
627 struct pgpath *pgpath;
628 struct arg_set path_args;
629
630 if (as->argc < nr_params)
631 goto bad;
632
633 path_args.argc = nr_params;
634 path_args.argv = as->argv;
635
636 pgpath = parse_path(&path_args, &pg->ps, ti);
637 if (!pgpath)
638 goto bad;
639
640 pgpath->pg = pg;
641 list_add_tail(&pgpath->list, &pg->pgpaths);
642 consume(as, nr_params);
643 }
644
645 return pg;
646
647 bad:
648 free_priority_group(pg, ti);
649 return NULL;
650}
651
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700652static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
654 int r;
655 struct hw_handler_type *hwht;
656 unsigned hw_argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700657 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700660 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 };
662
663 r = read_param(_params, shift(as), &hw_argc, &ti->error);
664 if (r)
665 return -EINVAL;
666
667 if (!hw_argc)
668 return 0;
669
670 hwht = dm_get_hw_handler(shift(as));
671 if (!hwht) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700672 ti->error = "unknown hardware handler type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return -EINVAL;
674 }
675
Edward Goggin79eb8852007-05-09 02:32:56 -0700676 m->hw_handler.md = dm_table_get_md(ti->table);
677 dm_put(m->hw_handler.md);
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 r = hwht->create(&m->hw_handler, hw_argc - 1, as->argv);
680 if (r) {
681 dm_put_hw_handler(hwht);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700682 ti->error = "hardware handler constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 return r;
684 }
685
686 m->hw_handler.type = hwht;
687 consume(as, hw_argc - 1);
688
689 return 0;
690}
691
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700692static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 int r;
695 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700696 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100697 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100700 {0, 3, "invalid number of feature args"},
701 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 };
703
704 r = read_param(_params, shift(as), &argc, &ti->error);
705 if (r)
706 return -EINVAL;
707
708 if (!argc)
709 return 0;
710
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100711 do {
712 param_name = shift(as);
713 argc--;
714
715 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
716 r = queue_if_no_path(m, 1, 0);
717 continue;
718 }
719
720 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
721 (argc >= 1)) {
722 r = read_param(_params + 1, shift(as),
723 &m->pg_init_retries, &ti->error);
724 argc--;
725 continue;
726 }
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100729 r = -EINVAL;
730 } while (argc && !r);
731
732 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735static int multipath_ctr(struct dm_target *ti, unsigned int argc,
736 char **argv)
737{
738 /* target parameters */
739 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700740 {1, 1024, "invalid number of priority groups"},
741 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 };
743
744 int r;
745 struct multipath *m;
746 struct arg_set as;
747 unsigned pg_count = 0;
748 unsigned next_pg_num;
749
750 as.argc = argc;
751 as.argv = argv;
752
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700753 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700755 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return -EINVAL;
757 }
758
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700759 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (r)
761 goto bad;
762
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700763 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (r)
765 goto bad;
766
767 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
768 if (r)
769 goto bad;
770
771 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
772 if (r)
773 goto bad;
774
775 /* parse the priority groups */
776 while (as.argc) {
777 struct priority_group *pg;
778
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700779 pg = parse_priority_group(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (!pg) {
781 r = -EINVAL;
782 goto bad;
783 }
784
785 m->nr_valid_paths += pg->nr_pgpaths;
786 list_add_tail(&pg->list, &m->priority_groups);
787 pg_count++;
788 pg->pg_num = pg_count;
789 if (!--next_pg_num)
790 m->next_pg = pg;
791 }
792
793 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700794 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 r = -EINVAL;
796 goto bad;
797 }
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return 0;
800
801 bad:
802 free_multipath(m);
803 return r;
804}
805
806static void multipath_dtr(struct dm_target *ti)
807{
808 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700809
810 flush_workqueue(kmultipathd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 free_multipath(m);
812}
813
814/*
815 * Map bios, recording original fields for later in case we have to resubmit
816 */
817static int multipath_map(struct dm_target *ti, struct bio *bio,
818 union map_info *map_context)
819{
820 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100821 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 struct multipath *m = (struct multipath *) ti->private;
823
824 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
825 dm_bio_record(&mpio->details, bio);
826
827 map_context->ptr = mpio;
828 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
829 r = map_io(m, bio, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800830 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 mempool_free(mpio, m->mpio_pool);
832
833 return r;
834}
835
836/*
837 * Take a path out of use.
838 */
839static int fail_path(struct pgpath *pgpath)
840{
841 unsigned long flags;
842 struct multipath *m = pgpath->pg->m;
843
844 spin_lock_irqsave(&m->lock, flags);
845
846 if (!pgpath->path.is_active)
847 goto out;
848
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700849 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
852 pgpath->path.is_active = 0;
853 pgpath->fail_count++;
854
855 m->nr_valid_paths--;
856
857 if (pgpath == m->current_pgpath)
858 m->current_pgpath = NULL;
859
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700860 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
862out:
863 spin_unlock_irqrestore(&m->lock, flags);
864
865 return 0;
866}
867
868/*
869 * Reinstate a previously-failed path
870 */
871static int reinstate_path(struct pgpath *pgpath)
872{
873 int r = 0;
874 unsigned long flags;
875 struct multipath *m = pgpath->pg->m;
876
877 spin_lock_irqsave(&m->lock, flags);
878
879 if (pgpath->path.is_active)
880 goto out;
881
882 if (!pgpath->pg->ps.type) {
883 DMWARN("Reinstate path not supported by path selector %s",
884 pgpath->pg->ps.type->name);
885 r = -EINVAL;
886 goto out;
887 }
888
889 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
890 if (r)
891 goto out;
892
893 pgpath->path.is_active = 1;
894
895 m->current_pgpath = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700896 if (!m->nr_valid_paths++ && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700897 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700899 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901out:
902 spin_unlock_irqrestore(&m->lock, flags);
903
904 return r;
905}
906
907/*
908 * Fail or reinstate all paths that match the provided struct dm_dev.
909 */
910static int action_dev(struct multipath *m, struct dm_dev *dev,
911 action_fn action)
912{
913 int r = 0;
914 struct pgpath *pgpath;
915 struct priority_group *pg;
916
917 list_for_each_entry(pg, &m->priority_groups, list) {
918 list_for_each_entry(pgpath, &pg->pgpaths, list) {
919 if (pgpath->path.dev == dev)
920 r = action(pgpath);
921 }
922 }
923
924 return r;
925}
926
927/*
928 * Temporarily try to avoid having to use the specified PG
929 */
930static void bypass_pg(struct multipath *m, struct priority_group *pg,
931 int bypassed)
932{
933 unsigned long flags;
934
935 spin_lock_irqsave(&m->lock, flags);
936
937 pg->bypassed = bypassed;
938 m->current_pgpath = NULL;
939 m->current_pg = NULL;
940
941 spin_unlock_irqrestore(&m->lock, flags);
942
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700943 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946/*
947 * Switch to using the specified PG from the next I/O that gets mapped
948 */
949static int switch_pg_num(struct multipath *m, const char *pgstr)
950{
951 struct priority_group *pg;
952 unsigned pgnum;
953 unsigned long flags;
954
955 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
956 (pgnum > m->nr_priority_groups)) {
957 DMWARN("invalid PG number supplied to switch_pg_num");
958 return -EINVAL;
959 }
960
961 spin_lock_irqsave(&m->lock, flags);
962 list_for_each_entry(pg, &m->priority_groups, list) {
963 pg->bypassed = 0;
964 if (--pgnum)
965 continue;
966
967 m->current_pgpath = NULL;
968 m->current_pg = NULL;
969 m->next_pg = pg;
970 }
971 spin_unlock_irqrestore(&m->lock, flags);
972
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700973 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return 0;
975}
976
977/*
978 * Set/clear bypassed status of a PG.
979 * PGs are numbered upwards from 1 in the order they were declared.
980 */
981static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
982{
983 struct priority_group *pg;
984 unsigned pgnum;
985
986 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
987 (pgnum > m->nr_priority_groups)) {
988 DMWARN("invalid PG number supplied to bypass_pg");
989 return -EINVAL;
990 }
991
992 list_for_each_entry(pg, &m->priority_groups, list) {
993 if (!--pgnum)
994 break;
995 }
996
997 bypass_pg(m, pg, bypassed);
998 return 0;
999}
1000
1001/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001002 * Should we retry pg_init immediately?
1003 */
1004static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1005{
1006 unsigned long flags;
1007 int limit_reached = 0;
1008
1009 spin_lock_irqsave(&m->lock, flags);
1010
1011 if (m->pg_init_count <= m->pg_init_retries)
1012 m->pg_init_required = 1;
1013 else
1014 limit_reached = 1;
1015
1016 spin_unlock_irqrestore(&m->lock, flags);
1017
1018 return limit_reached;
1019}
1020
1021/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 * pg_init must call this when it has completed its initialisation
1023 */
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -08001024void dm_pg_init_complete(struct dm_path *path, unsigned err_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 struct pgpath *pgpath = path_to_pgpath(path);
1027 struct priority_group *pg = pgpath->pg;
1028 struct multipath *m = pg->m;
1029 unsigned long flags;
1030
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001031 /*
1032 * If requested, retry pg_init until maximum number of retries exceeded.
1033 * If retry not requested and PG already bypassed, always fail the path.
1034 */
1035 if (err_flags & MP_RETRY) {
1036 if (pg_init_limit_reached(m, pgpath))
1037 err_flags |= MP_FAIL_PATH;
1038 } else if (err_flags && pg->bypassed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 err_flags |= MP_FAIL_PATH;
1040
1041 if (err_flags & MP_FAIL_PATH)
1042 fail_path(pgpath);
1043
1044 if (err_flags & MP_BYPASS_PG)
1045 bypass_pg(m, pg, 1);
1046
1047 spin_lock_irqsave(&m->lock, flags);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001048 if (err_flags & ~MP_RETRY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 m->current_pgpath = NULL;
1050 m->current_pg = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -07001051 } else if (!m->pg_init_required)
1052 m->queue_io = 0;
1053
1054 m->pg_init_in_progress = 0;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001055 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 spin_unlock_irqrestore(&m->lock, flags);
1057}
1058
1059/*
1060 * end_io handling
1061 */
1062static int do_end_io(struct multipath *m, struct bio *bio,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001063 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
1065 struct hw_handler *hwh = &m->hw_handler;
1066 unsigned err_flags = MP_FAIL_PATH; /* Default behavior */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001067 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 if (!error)
1070 return 0; /* I/O complete */
1071
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -07001072 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1073 return error;
1074
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001075 if (error == -EOPNOTSUPP)
1076 return error;
1077
Stefan Bader640eb3b2005-11-21 21:32:35 -08001078 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 if (!m->nr_valid_paths) {
Kiyoshi Ueda45e15722006-12-08 02:41:10 -08001080 if (__must_push_back(m)) {
1081 spin_unlock_irqrestore(&m->lock, flags);
1082 return DM_ENDIO_REQUEUE;
1083 } else if (!m->queue_if_no_path) {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001084 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return -EIO;
1086 } else {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001087 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 goto requeue;
1089 }
1090 }
Stefan Bader640eb3b2005-11-21 21:32:35 -08001091 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 if (hwh->type && hwh->type->error)
1094 err_flags = hwh->type->error(hwh, bio);
1095
1096 if (mpio->pgpath) {
1097 if (err_flags & MP_FAIL_PATH)
1098 fail_path(mpio->pgpath);
1099
1100 if (err_flags & MP_BYPASS_PG)
1101 bypass_pg(m, mpio->pgpath->pg, 1);
1102 }
1103
1104 if (err_flags & MP_ERROR_IO)
1105 return -EIO;
1106
1107 requeue:
1108 dm_bio_restore(&mpio->details, bio);
1109
1110 /* queue for the daemon to resubmit or fail */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001111 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 bio_list_add(&m->queued_ios, bio);
1113 m->queue_size++;
1114 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001115 queue_work(kmultipathd, &m->process_queued_ios);
Stefan Bader640eb3b2005-11-21 21:32:35 -08001116 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001118 return DM_ENDIO_INCOMPLETE; /* io not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119}
1120
1121static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1122 int error, union map_info *map_context)
1123{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001124 struct multipath *m = ti->private;
1125 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 struct pgpath *pgpath = mpio->pgpath;
1127 struct path_selector *ps;
1128 int r;
1129
1130 r = do_end_io(m, bio, error, mpio);
1131 if (pgpath) {
1132 ps = &pgpath->pg->ps;
1133 if (ps->type->end_io)
1134 ps->type->end_io(ps, &pgpath->path);
1135 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001136 if (r != DM_ENDIO_INCOMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 mempool_free(mpio, m->mpio_pool);
1138
1139 return r;
1140}
1141
1142/*
1143 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001144 * the last path fails we must error any remaining I/O.
1145 * Note that if the freeze_bdev fails while suspending, the
1146 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 */
1148static void multipath_presuspend(struct dm_target *ti)
1149{
1150 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001152 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153}
1154
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001155/*
1156 * Restore the queue_if_no_path setting.
1157 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158static void multipath_resume(struct dm_target *ti)
1159{
1160 struct multipath *m = (struct multipath *) ti->private;
1161 unsigned long flags;
1162
1163 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001164 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 spin_unlock_irqrestore(&m->lock, flags);
1166}
1167
1168/*
1169 * Info output has the following format:
1170 * num_multipath_feature_args [multipath_feature_args]*
1171 * num_handler_status_args [handler_status_args]*
1172 * num_groups init_group_number
1173 * [A|D|E num_ps_status_args [ps_status_args]*
1174 * num_paths num_selector_args
1175 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1176 *
1177 * Table output has the following format (identical to the constructor string):
1178 * num_feature_args [features_args]*
1179 * num_handler_args hw_handler [hw_handler_args]*
1180 * num_groups init_group_number
1181 * [priority selector-name num_ps_args [ps_args]*
1182 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1183 */
1184static int multipath_status(struct dm_target *ti, status_type_t type,
1185 char *result, unsigned int maxlen)
1186{
1187 int sz = 0;
1188 unsigned long flags;
1189 struct multipath *m = (struct multipath *) ti->private;
1190 struct hw_handler *hwh = &m->hw_handler;
1191 struct priority_group *pg;
1192 struct pgpath *p;
1193 unsigned pg_num;
1194 char state;
1195
1196 spin_lock_irqsave(&m->lock, flags);
1197
1198 /* Features */
1199 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001200 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1201 else {
1202 DMEMIT("%u ", m->queue_if_no_path +
1203 (m->pg_init_retries > 0) * 2);
1204 if (m->queue_if_no_path)
1205 DMEMIT("queue_if_no_path ");
1206 if (m->pg_init_retries)
1207 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
1210 if (hwh->type && hwh->type->status)
1211 sz += hwh->type->status(hwh, type, result + sz, maxlen - sz);
1212 else if (!hwh->type || type == STATUSTYPE_INFO)
1213 DMEMIT("0 ");
1214 else
1215 DMEMIT("1 %s ", hwh->type->name);
1216
1217 DMEMIT("%u ", m->nr_priority_groups);
1218
1219 if (m->next_pg)
1220 pg_num = m->next_pg->pg_num;
1221 else if (m->current_pg)
1222 pg_num = m->current_pg->pg_num;
1223 else
1224 pg_num = 1;
1225
1226 DMEMIT("%u ", pg_num);
1227
1228 switch (type) {
1229 case STATUSTYPE_INFO:
1230 list_for_each_entry(pg, &m->priority_groups, list) {
1231 if (pg->bypassed)
1232 state = 'D'; /* Disabled */
1233 else if (pg == m->current_pg)
1234 state = 'A'; /* Currently Active */
1235 else
1236 state = 'E'; /* Enabled */
1237
1238 DMEMIT("%c ", state);
1239
1240 if (pg->ps.type->status)
1241 sz += pg->ps.type->status(&pg->ps, NULL, type,
1242 result + sz,
1243 maxlen - sz);
1244 else
1245 DMEMIT("0 ");
1246
1247 DMEMIT("%u %u ", pg->nr_pgpaths,
1248 pg->ps.type->info_args);
1249
1250 list_for_each_entry(p, &pg->pgpaths, list) {
1251 DMEMIT("%s %s %u ", p->path.dev->name,
1252 p->path.is_active ? "A" : "F",
1253 p->fail_count);
1254 if (pg->ps.type->status)
1255 sz += pg->ps.type->status(&pg->ps,
1256 &p->path, type, result + sz,
1257 maxlen - sz);
1258 }
1259 }
1260 break;
1261
1262 case STATUSTYPE_TABLE:
1263 list_for_each_entry(pg, &m->priority_groups, list) {
1264 DMEMIT("%s ", pg->ps.type->name);
1265
1266 if (pg->ps.type->status)
1267 sz += pg->ps.type->status(&pg->ps, NULL, type,
1268 result + sz,
1269 maxlen - sz);
1270 else
1271 DMEMIT("0 ");
1272
1273 DMEMIT("%u %u ", pg->nr_pgpaths,
1274 pg->ps.type->table_args);
1275
1276 list_for_each_entry(p, &pg->pgpaths, list) {
1277 DMEMIT("%s ", p->path.dev->name);
1278 if (pg->ps.type->status)
1279 sz += pg->ps.type->status(&pg->ps,
1280 &p->path, type, result + sz,
1281 maxlen - sz);
1282 }
1283 }
1284 break;
1285 }
1286
1287 spin_unlock_irqrestore(&m->lock, flags);
1288
1289 return 0;
1290}
1291
1292static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1293{
1294 int r;
1295 struct dm_dev *dev;
1296 struct multipath *m = (struct multipath *) ti->private;
1297 action_fn action;
1298
1299 if (argc == 1) {
1300 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001301 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001303 return queue_if_no_path(m, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
1306 if (argc != 2)
1307 goto error;
1308
1309 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1310 return bypass_pg_num(m, argv[1], 1);
1311 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1312 return bypass_pg_num(m, argv[1], 0);
1313 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1314 return switch_pg_num(m, argv[1]);
1315 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1316 action = reinstate_path;
1317 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1318 action = fail_path;
1319 else
1320 goto error;
1321
1322 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1323 dm_table_get_mode(ti->table), &dev);
1324 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001325 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 argv[1]);
1327 return -EINVAL;
1328 }
1329
1330 r = action_dev(m, dev, action);
1331
1332 dm_put_device(ti, dev);
1333
1334 return r;
1335
1336error:
1337 DMWARN("Unrecognised multipath message received.");
1338 return -EINVAL;
1339}
1340
Milan Broz9af4aa32006-10-03 01:15:20 -07001341static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1342 struct file *filp, unsigned int cmd,
1343 unsigned long arg)
1344{
1345 struct multipath *m = (struct multipath *) ti->private;
1346 struct block_device *bdev = NULL;
1347 unsigned long flags;
Milan Broze90dae12006-10-03 01:15:22 -07001348 struct file fake_file = {};
1349 struct dentry fake_dentry = {};
Milan Broz9af4aa32006-10-03 01:15:20 -07001350 int r = 0;
1351
Josef Sipekc649bb92006-12-08 02:37:19 -08001352 fake_file.f_path.dentry = &fake_dentry;
Milan Broze90dae12006-10-03 01:15:22 -07001353
Milan Broz9af4aa32006-10-03 01:15:20 -07001354 spin_lock_irqsave(&m->lock, flags);
1355
1356 if (!m->current_pgpath)
1357 __choose_pgpath(m);
1358
Milan Broze90dae12006-10-03 01:15:22 -07001359 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001360 bdev = m->current_pgpath->path.dev->bdev;
Milan Broze90dae12006-10-03 01:15:22 -07001361 fake_dentry.d_inode = bdev->bd_inode;
1362 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1363 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001364
1365 if (m->queue_io)
1366 r = -EAGAIN;
1367 else if (!bdev)
1368 r = -EIO;
1369
1370 spin_unlock_irqrestore(&m->lock, flags);
1371
Milan Broze90dae12006-10-03 01:15:22 -07001372 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1373 bdev->bd_disk, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001374}
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376/*-----------------------------------------------------------------
1377 * Module setup
1378 *---------------------------------------------------------------*/
1379static struct target_type multipath_target = {
1380 .name = "multipath",
Milan Broz9af4aa32006-10-03 01:15:20 -07001381 .version = {1, 0, 5},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 .module = THIS_MODULE,
1383 .ctr = multipath_ctr,
1384 .dtr = multipath_dtr,
1385 .map = multipath_map,
1386 .end_io = multipath_end_io,
1387 .presuspend = multipath_presuspend,
1388 .resume = multipath_resume,
1389 .status = multipath_status,
1390 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001391 .ioctl = multipath_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392};
1393
1394static int __init dm_multipath_init(void)
1395{
1396 int r;
1397
1398 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001399 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (!_mpio_cache)
1401 return -ENOMEM;
1402
1403 r = dm_register_target(&multipath_target);
1404 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001405 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 kmem_cache_destroy(_mpio_cache);
1407 return -EINVAL;
1408 }
1409
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001410 kmultipathd = create_workqueue("kmpathd");
1411 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001412 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001413 dm_unregister_target(&multipath_target);
1414 kmem_cache_destroy(_mpio_cache);
1415 return -ENOMEM;
1416 }
1417
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001418 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 multipath_target.version[0], multipath_target.version[1],
1420 multipath_target.version[2]);
1421
1422 return r;
1423}
1424
1425static void __exit dm_multipath_exit(void)
1426{
1427 int r;
1428
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001429 destroy_workqueue(kmultipathd);
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 r = dm_unregister_target(&multipath_target);
1432 if (r < 0)
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001433 DMERR("target unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 kmem_cache_destroy(_mpio_cache);
1435}
1436
1437EXPORT_SYMBOL_GPL(dm_pg_init_complete);
1438
1439module_init(dm_multipath_init);
1440module_exit(dm_multipath_exit);
1441
1442MODULE_DESCRIPTION(DM_NAME " multipath target");
1443MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1444MODULE_LICENSE("GPL");