]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/md/dm-mpath.c
dma: remove use of __devexit_p
[mirror_ubuntu-bionic-kernel.git] / drivers / md / dm-mpath.c
CommitLineData
1da177e4
LT
1/*
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
586e80e6
MP
8#include <linux/device-mapper.h>
9
1da177e4 10#include "dm-path-selector.h"
b15546f9 11#include "dm-uevent.h"
1da177e4
LT
12
13#include <linux/ctype.h>
14#include <linux/init.h>
15#include <linux/mempool.h>
16#include <linux/module.h>
17#include <linux/pagemap.h>
18#include <linux/slab.h>
19#include <linux/time.h>
20#include <linux/workqueue.h>
35991652 21#include <linux/delay.h>
cfae5c9b 22#include <scsi/scsi_dh.h>
60063497 23#include <linux/atomic.h>
1da177e4 24
72d94861 25#define DM_MSG_PREFIX "multipath"
4e2d19e4
CS
26#define DM_PG_INIT_DELAY_MSECS 2000
27#define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
1da177e4
LT
28
29/* Path properties */
30struct pgpath {
31 struct list_head list;
32
33 struct priority_group *pg; /* Owning PG */
6680073d 34 unsigned is_active; /* Path status */
1da177e4
LT
35 unsigned fail_count; /* Cumulative failure count */
36
c922d5f7 37 struct dm_path path;
4e2d19e4 38 struct delayed_work activate_path;
1da177e4
LT
39};
40
41#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
42
43/*
44 * Paths are grouped into Priority Groups and numbered from 1 upwards.
45 * Each has a path selector which controls which path gets used.
46 */
47struct priority_group {
48 struct list_head list;
49
50 struct multipath *m; /* Owning multipath instance */
51 struct path_selector ps;
52
53 unsigned pg_num; /* Reference number */
54 unsigned bypassed; /* Temporarily bypass this PG? */
55
56 unsigned nr_pgpaths; /* Number of paths in PG */
57 struct list_head pgpaths;
58};
59
60/* Multipath context */
61struct multipath {
62 struct list_head list;
63 struct dm_target *ti;
64
cfae5c9b 65 const char *hw_handler_name;
2bfd2e13 66 char *hw_handler_params;
4e2d19e4 67
1fbdd2b3
MS
68 spinlock_t lock;
69
1da177e4
LT
70 unsigned nr_priority_groups;
71 struct list_head priority_groups;
4e2d19e4
CS
72
73 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
74
1da177e4 75 unsigned pg_init_required; /* pg_init needs calling? */
c3cd4f6b 76 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
4e2d19e4 77 unsigned pg_init_delay_retry; /* Delay pg_init retry? */
1da177e4
LT
78
79 unsigned nr_valid_paths; /* Total number of usable paths */
80 struct pgpath *current_pgpath;
81 struct priority_group *current_pg;
82 struct priority_group *next_pg; /* Switch to this PG if set */
83 unsigned repeat_count; /* I/Os left before calling PS again */
84
1fbdd2b3
MS
85 unsigned queue_io:1; /* Must we queue all I/O? */
86 unsigned queue_if_no_path:1; /* Queue I/O if last path fails? */
87 unsigned saved_queue_if_no_path:1; /* Saved state during suspension */
a58a935d 88 unsigned retain_attached_hw_handler:1; /* If there's already a hw_handler present, don't change it. */
1fbdd2b3 89
c9e45581
DW
90 unsigned pg_init_retries; /* Number of times to retry pg_init */
91 unsigned pg_init_count; /* Number of times pg_init called */
4e2d19e4 92 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
1da177e4 93
1fbdd2b3 94 unsigned queue_size;
1da177e4 95 struct work_struct process_queued_ios;
f40c67f0 96 struct list_head queued_ios;
1da177e4
LT
97
98 struct work_struct trigger_event;
99
100 /*
028867ac 101 * We must use a mempool of dm_mpath_io structs so that we
1da177e4
LT
102 * can resubmit bios on error.
103 */
104 mempool_t *mpio_pool;
6380f26f
MA
105
106 struct mutex work_mutex;
1da177e4
LT
107};
108
109/*
110 * Context information attached to each bio we process.
111 */
028867ac 112struct dm_mpath_io {
1da177e4 113 struct pgpath *pgpath;
02ab823f 114 size_t nr_bytes;
1da177e4
LT
115};
116
117typedef int (*action_fn) (struct pgpath *pgpath);
118
119#define MIN_IOS 256 /* Mempool size */
120
e18b890b 121static struct kmem_cache *_mpio_cache;
1da177e4 122
bab7cfc7 123static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
c4028958
DH
124static void process_queued_ios(struct work_struct *work);
125static void trigger_event(struct work_struct *work);
bab7cfc7 126static void activate_path(struct work_struct *work);
1da177e4
LT
127
128
129/*-----------------------------------------------
130 * Allocation routines
131 *-----------------------------------------------*/
132
133static struct pgpath *alloc_pgpath(void)
134{
e69fae56 135 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1da177e4 136
224cb3e9 137 if (pgpath) {
6680073d 138 pgpath->is_active = 1;
4e2d19e4 139 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
224cb3e9 140 }
1da177e4
LT
141
142 return pgpath;
143}
144
028867ac 145static void free_pgpath(struct pgpath *pgpath)
1da177e4
LT
146{
147 kfree(pgpath);
148}
149
150static struct priority_group *alloc_priority_group(void)
151{
152 struct priority_group *pg;
153
e69fae56 154 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1da177e4 155
e69fae56
MM
156 if (pg)
157 INIT_LIST_HEAD(&pg->pgpaths);
1da177e4
LT
158
159 return pg;
160}
161
162static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
163{
164 struct pgpath *pgpath, *tmp;
ae11b1b3 165 struct multipath *m = ti->private;
1da177e4
LT
166
167 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
168 list_del(&pgpath->list);
ae11b1b3
HR
169 if (m->hw_handler_name)
170 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
1da177e4
LT
171 dm_put_device(ti, pgpath->path.dev);
172 free_pgpath(pgpath);
173 }
174}
175
176static void free_priority_group(struct priority_group *pg,
177 struct dm_target *ti)
178{
179 struct path_selector *ps = &pg->ps;
180
181 if (ps->type) {
182 ps->type->destroy(ps);
183 dm_put_path_selector(ps->type);
184 }
185
186 free_pgpaths(&pg->pgpaths, ti);
187 kfree(pg);
188}
189
28f16c20 190static struct multipath *alloc_multipath(struct dm_target *ti)
1da177e4
LT
191{
192 struct multipath *m;
193
e69fae56 194 m = kzalloc(sizeof(*m), GFP_KERNEL);
1da177e4 195 if (m) {
1da177e4 196 INIT_LIST_HEAD(&m->priority_groups);
f40c67f0 197 INIT_LIST_HEAD(&m->queued_ios);
1da177e4
LT
198 spin_lock_init(&m->lock);
199 m->queue_io = 1;
4e2d19e4 200 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
c4028958
DH
201 INIT_WORK(&m->process_queued_ios, process_queued_ios);
202 INIT_WORK(&m->trigger_event, trigger_event);
2bded7bd 203 init_waitqueue_head(&m->pg_init_wait);
6380f26f 204 mutex_init(&m->work_mutex);
93d2341c 205 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
1da177e4
LT
206 if (!m->mpio_pool) {
207 kfree(m);
208 return NULL;
209 }
28f16c20
MM
210 m->ti = ti;
211 ti->private = m;
1da177e4
LT
212 }
213
214 return m;
215}
216
217static void free_multipath(struct multipath *m)
218{
219 struct priority_group *pg, *tmp;
1da177e4
LT
220
221 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
222 list_del(&pg->list);
223 free_priority_group(pg, m->ti);
224 }
225
cfae5c9b 226 kfree(m->hw_handler_name);
2bfd2e13 227 kfree(m->hw_handler_params);
1da177e4
LT
228 mempool_destroy(m->mpio_pool);
229 kfree(m);
230}
231
466891f9
JN
232static int set_mapinfo(struct multipath *m, union map_info *info)
233{
234 struct dm_mpath_io *mpio;
235
236 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
237 if (!mpio)
238 return -ENOMEM;
239
240 memset(mpio, 0, sizeof(*mpio));
241 info->ptr = mpio;
242
243 return 0;
244}
245
246static void clear_mapinfo(struct multipath *m, union map_info *info)
247{
248 struct dm_mpath_io *mpio = info->ptr;
249
250 info->ptr = NULL;
251 mempool_free(mpio, m->mpio_pool);
252}
1da177e4
LT
253
254/*-----------------------------------------------
255 * Path selection
256 *-----------------------------------------------*/
257
fb612642
KU
258static void __pg_init_all_paths(struct multipath *m)
259{
260 struct pgpath *pgpath;
4e2d19e4 261 unsigned long pg_init_delay = 0;
fb612642
KU
262
263 m->pg_init_count++;
264 m->pg_init_required = 0;
4e2d19e4
CS
265 if (m->pg_init_delay_retry)
266 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
267 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
fb612642
KU
268 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
269 /* Skip failed paths */
270 if (!pgpath->is_active)
271 continue;
4e2d19e4
CS
272 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
273 pg_init_delay))
fb612642
KU
274 m->pg_init_in_progress++;
275 }
276}
277
1da177e4
LT
278static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
279{
1da177e4
LT
280 m->current_pg = pgpath->pg;
281
282 /* Must we initialise the PG first, and queue I/O till it's ready? */
cfae5c9b 283 if (m->hw_handler_name) {
1da177e4
LT
284 m->pg_init_required = 1;
285 m->queue_io = 1;
286 } else {
287 m->pg_init_required = 0;
288 m->queue_io = 0;
289 }
c9e45581
DW
290
291 m->pg_init_count = 0;
1da177e4
LT
292}
293
02ab823f
KU
294static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
295 size_t nr_bytes)
1da177e4 296{
c922d5f7 297 struct dm_path *path;
1da177e4 298
02ab823f 299 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
1da177e4
LT
300 if (!path)
301 return -ENXIO;
302
303 m->current_pgpath = path_to_pgpath(path);
304
305 if (m->current_pg != pg)
306 __switch_pg(m, m->current_pgpath);
307
308 return 0;
309}
310
02ab823f 311static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
1da177e4
LT
312{
313 struct priority_group *pg;
314 unsigned bypassed = 1;
315
316 if (!m->nr_valid_paths)
317 goto failed;
318
319 /* Were we instructed to switch PG? */
320 if (m->next_pg) {
321 pg = m->next_pg;
322 m->next_pg = NULL;
02ab823f 323 if (!__choose_path_in_pg(m, pg, nr_bytes))
1da177e4
LT
324 return;
325 }
326
327 /* Don't change PG until it has no remaining paths */
02ab823f 328 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
1da177e4
LT
329 return;
330
331 /*
332 * Loop through priority groups until we find a valid path.
333 * First time we skip PGs marked 'bypassed'.
f220fd4e
MC
334 * Second time we only try the ones we skipped, but set
335 * pg_init_delay_retry so we do not hammer controllers.
1da177e4
LT
336 */
337 do {
338 list_for_each_entry(pg, &m->priority_groups, list) {
339 if (pg->bypassed == bypassed)
340 continue;
f220fd4e
MC
341 if (!__choose_path_in_pg(m, pg, nr_bytes)) {
342 if (!bypassed)
343 m->pg_init_delay_retry = 1;
1da177e4 344 return;
f220fd4e 345 }
1da177e4
LT
346 }
347 } while (bypassed--);
348
349failed:
350 m->current_pgpath = NULL;
351 m->current_pg = NULL;
352}
353
45e15720
KU
354/*
355 * Check whether bios must be queued in the device-mapper core rather
356 * than here in the target.
357 *
358 * m->lock must be held on entry.
359 *
360 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
361 * same value then we are not between multipath_presuspend()
362 * and multipath_resume() calls and we have no need to check
363 * for the DMF_NOFLUSH_SUSPENDING flag.
364 */
365static int __must_push_back(struct multipath *m)
366{
367 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
368 dm_noflush_suspending(m->ti));
369}
370
f40c67f0 371static int map_io(struct multipath *m, struct request *clone,
466891f9 372 union map_info *map_context, unsigned was_queued)
1da177e4 373{
d2a7ad29 374 int r = DM_MAPIO_REMAPPED;
f40c67f0 375 size_t nr_bytes = blk_rq_bytes(clone);
1da177e4
LT
376 unsigned long flags;
377 struct pgpath *pgpath;
f40c67f0 378 struct block_device *bdev;
466891f9 379 struct dm_mpath_io *mpio = map_context->ptr;
1da177e4
LT
380
381 spin_lock_irqsave(&m->lock, flags);
382
383 /* Do we need to select a new pgpath? */
384 if (!m->current_pgpath ||
385 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
02ab823f 386 __choose_pgpath(m, nr_bytes);
1da177e4
LT
387
388 pgpath = m->current_pgpath;
389
390 if (was_queued)
391 m->queue_size--;
392
393 if ((pgpath && m->queue_io) ||
436d4108 394 (!pgpath && m->queue_if_no_path)) {
1da177e4 395 /* Queue for the daemon to resubmit */
f40c67f0 396 list_add_tail(&clone->queuelist, &m->queued_ios);
1da177e4 397 m->queue_size++;
c3cd4f6b
AK
398 if ((m->pg_init_required && !m->pg_init_in_progress) ||
399 !m->queue_io)
c557308e 400 queue_work(kmultipathd, &m->process_queued_ios);
1da177e4 401 pgpath = NULL;
d2a7ad29 402 r = DM_MAPIO_SUBMITTED;
f40c67f0
KU
403 } else if (pgpath) {
404 bdev = pgpath->path.dev->bdev;
405 clone->q = bdev_get_queue(bdev);
406 clone->rq_disk = bdev->bd_disk;
407 } else if (__must_push_back(m))
45e15720
KU
408 r = DM_MAPIO_REQUEUE;
409 else
410 r = -EIO; /* Failed */
1da177e4
LT
411
412 mpio->pgpath = pgpath;
02ab823f
KU
413 mpio->nr_bytes = nr_bytes;
414
415 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
416 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
417 nr_bytes);
1da177e4
LT
418
419 spin_unlock_irqrestore(&m->lock, flags);
420
421 return r;
422}
423
424/*
425 * If we run out of usable paths, should we queue I/O or error it?
426 */
485ef69e
AK
427static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
428 unsigned save_old_value)
1da177e4
LT
429{
430 unsigned long flags;
431
432 spin_lock_irqsave(&m->lock, flags);
433
485ef69e
AK
434 if (save_old_value)
435 m->saved_queue_if_no_path = m->queue_if_no_path;
436 else
437 m->saved_queue_if_no_path = queue_if_no_path;
1da177e4 438 m->queue_if_no_path = queue_if_no_path;
c3cd4f6b 439 if (!m->queue_if_no_path && m->queue_size)
c557308e 440 queue_work(kmultipathd, &m->process_queued_ios);
1da177e4
LT
441
442 spin_unlock_irqrestore(&m->lock, flags);
443
444 return 0;
445}
446
447/*-----------------------------------------------------------------
448 * The multipath daemon is responsible for resubmitting queued ios.
449 *---------------------------------------------------------------*/
450
451static void dispatch_queued_ios(struct multipath *m)
452{
453 int r;
454 unsigned long flags;
1da177e4 455 union map_info *info;
f40c67f0
KU
456 struct request *clone, *n;
457 LIST_HEAD(cl);
1da177e4
LT
458
459 spin_lock_irqsave(&m->lock, flags);
f40c67f0 460 list_splice_init(&m->queued_ios, &cl);
1da177e4
LT
461 spin_unlock_irqrestore(&m->lock, flags);
462
f40c67f0
KU
463 list_for_each_entry_safe(clone, n, &cl, queuelist) {
464 list_del_init(&clone->queuelist);
1da177e4 465
f40c67f0 466 info = dm_get_rq_mapinfo(clone);
1da177e4 467
466891f9 468 r = map_io(m, clone, info, 1);
f40c67f0 469 if (r < 0) {
466891f9 470 clear_mapinfo(m, info);
f40c67f0
KU
471 dm_kill_unmapped_request(clone, r);
472 } else if (r == DM_MAPIO_REMAPPED)
473 dm_dispatch_request(clone);
474 else if (r == DM_MAPIO_REQUEUE) {
466891f9 475 clear_mapinfo(m, info);
f40c67f0
KU
476 dm_requeue_unmapped_request(clone);
477 }
1da177e4
LT
478 }
479}
480
c4028958 481static void process_queued_ios(struct work_struct *work)
1da177e4 482{
c4028958
DH
483 struct multipath *m =
484 container_of(work, struct multipath, process_queued_ios);
fb612642 485 struct pgpath *pgpath = NULL;
e54f77dd 486 unsigned must_queue = 1;
1da177e4
LT
487 unsigned long flags;
488
489 spin_lock_irqsave(&m->lock, flags);
490
491 if (!m->current_pgpath)
02ab823f 492 __choose_pgpath(m, 0);
1da177e4
LT
493
494 pgpath = m->current_pgpath;
495
c3cd4f6b
AK
496 if ((pgpath && !m->queue_io) ||
497 (!pgpath && !m->queue_if_no_path))
498 must_queue = 0;
1da177e4 499
fb612642
KU
500 if (m->pg_init_required && !m->pg_init_in_progress && pgpath)
501 __pg_init_all_paths(m);
502
1da177e4 503 spin_unlock_irqrestore(&m->lock, flags);
1da177e4
LT
504 if (!must_queue)
505 dispatch_queued_ios(m);
506}
507
508/*
509 * An event is triggered whenever a path is taken out of use.
510 * Includes path failure and PG bypass.
511 */
c4028958 512static void trigger_event(struct work_struct *work)
1da177e4 513{
c4028958
DH
514 struct multipath *m =
515 container_of(work, struct multipath, trigger_event);
1da177e4
LT
516
517 dm_table_event(m->ti->table);
518}
519
520/*-----------------------------------------------------------------
521 * Constructor/argument parsing:
522 * <#multipath feature args> [<arg>]*
523 * <#hw_handler args> [hw_handler [<arg>]*]
524 * <#priority groups>
525 * <initial priority group>
526 * [<selector> <#selector args> [<arg>]*
527 * <#paths> <#per-path selector args>
528 * [<path> [<arg>]* ]+ ]+
529 *---------------------------------------------------------------*/
498f0103 530static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
1da177e4
LT
531 struct dm_target *ti)
532{
533 int r;
534 struct path_selector_type *pst;
535 unsigned ps_argc;
536
498f0103 537 static struct dm_arg _args[] = {
72d94861 538 {0, 1024, "invalid number of path selector args"},
1da177e4
LT
539 };
540
498f0103 541 pst = dm_get_path_selector(dm_shift_arg(as));
1da177e4 542 if (!pst) {
72d94861 543 ti->error = "unknown path selector type";
1da177e4
LT
544 return -EINVAL;
545 }
546
498f0103 547 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
371b2e34
MP
548 if (r) {
549 dm_put_path_selector(pst);
1da177e4 550 return -EINVAL;
371b2e34 551 }
1da177e4
LT
552
553 r = pst->create(&pg->ps, ps_argc, as->argv);
554 if (r) {
555 dm_put_path_selector(pst);
72d94861 556 ti->error = "path selector constructor failed";
1da177e4
LT
557 return r;
558 }
559
560 pg->ps.type = pst;
498f0103 561 dm_consume_args(as, ps_argc);
1da177e4
LT
562
563 return 0;
564}
565
498f0103 566static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
1da177e4
LT
567 struct dm_target *ti)
568{
569 int r;
570 struct pgpath *p;
ae11b1b3 571 struct multipath *m = ti->private;
a58a935d
MS
572 struct request_queue *q = NULL;
573 const char *attached_handler_name;
1da177e4
LT
574
575 /* we need at least a path arg */
576 if (as->argc < 1) {
72d94861 577 ti->error = "no device given";
01460f35 578 return ERR_PTR(-EINVAL);
1da177e4
LT
579 }
580
581 p = alloc_pgpath();
582 if (!p)
01460f35 583 return ERR_PTR(-ENOMEM);
1da177e4 584
498f0103 585 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
8215d6ec 586 &p->path.dev);
1da177e4 587 if (r) {
72d94861 588 ti->error = "error getting device";
1da177e4
LT
589 goto bad;
590 }
591
a58a935d
MS
592 if (m->retain_attached_hw_handler || m->hw_handler_name)
593 q = bdev_get_queue(p->path.dev->bdev);
594
595 if (m->retain_attached_hw_handler) {
596 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
597 if (attached_handler_name) {
598 /*
599 * Reset hw_handler_name to match the attached handler
600 * and clear any hw_handler_params associated with the
601 * ignored handler.
602 *
603 * NB. This modifies the table line to show the actual
604 * handler instead of the original table passed in.
605 */
606 kfree(m->hw_handler_name);
607 m->hw_handler_name = attached_handler_name;
608
609 kfree(m->hw_handler_params);
610 m->hw_handler_params = NULL;
611 }
612 }
a0cf7ea9 613
a58a935d
MS
614 if (m->hw_handler_name) {
615 /*
616 * Increments scsi_dh reference, even when using an
617 * already-attached handler.
618 */
a0cf7ea9
HR
619 r = scsi_dh_attach(q, m->hw_handler_name);
620 if (r == -EBUSY) {
621 /*
a58a935d 622 * Already attached to different hw_handler:
a0cf7ea9
HR
623 * try to reattach with correct one.
624 */
625 scsi_dh_detach(q);
626 r = scsi_dh_attach(q, m->hw_handler_name);
627 }
628
ae11b1b3 629 if (r < 0) {
a0cf7ea9 630 ti->error = "error attaching hardware handler";
ae11b1b3
HR
631 dm_put_device(ti, p->path.dev);
632 goto bad;
633 }
2bfd2e13
CS
634
635 if (m->hw_handler_params) {
636 r = scsi_dh_set_params(q, m->hw_handler_params);
637 if (r < 0) {
638 ti->error = "unable to set hardware "
639 "handler parameters";
640 scsi_dh_detach(q);
641 dm_put_device(ti, p->path.dev);
642 goto bad;
643 }
644 }
ae11b1b3
HR
645 }
646
1da177e4
LT
647 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
648 if (r) {
649 dm_put_device(ti, p->path.dev);
650 goto bad;
651 }
652
653 return p;
654
655 bad:
656 free_pgpath(p);
01460f35 657 return ERR_PTR(r);
1da177e4
LT
658}
659
498f0103 660static struct priority_group *parse_priority_group(struct dm_arg_set *as,
28f16c20 661 struct multipath *m)
1da177e4 662{
498f0103 663 static struct dm_arg _args[] = {
72d94861
AK
664 {1, 1024, "invalid number of paths"},
665 {0, 1024, "invalid number of selector args"}
1da177e4
LT
666 };
667
668 int r;
498f0103 669 unsigned i, nr_selector_args, nr_args;
1da177e4 670 struct priority_group *pg;
28f16c20 671 struct dm_target *ti = m->ti;
1da177e4
LT
672
673 if (as->argc < 2) {
674 as->argc = 0;
01460f35
BM
675 ti->error = "not enough priority group arguments";
676 return ERR_PTR(-EINVAL);
1da177e4
LT
677 }
678
679 pg = alloc_priority_group();
680 if (!pg) {
72d94861 681 ti->error = "couldn't allocate priority group";
01460f35 682 return ERR_PTR(-ENOMEM);
1da177e4
LT
683 }
684 pg->m = m;
685
686 r = parse_path_selector(as, pg, ti);
687 if (r)
688 goto bad;
689
690 /*
691 * read the paths
692 */
498f0103 693 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
1da177e4
LT
694 if (r)
695 goto bad;
696
498f0103 697 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
1da177e4
LT
698 if (r)
699 goto bad;
700
498f0103 701 nr_args = 1 + nr_selector_args;
1da177e4
LT
702 for (i = 0; i < pg->nr_pgpaths; i++) {
703 struct pgpath *pgpath;
498f0103 704 struct dm_arg_set path_args;
1da177e4 705
498f0103 706 if (as->argc < nr_args) {
148acff6 707 ti->error = "not enough path parameters";
6bbf79a1 708 r = -EINVAL;
1da177e4 709 goto bad;
148acff6 710 }
1da177e4 711
498f0103 712 path_args.argc = nr_args;
1da177e4
LT
713 path_args.argv = as->argv;
714
715 pgpath = parse_path(&path_args, &pg->ps, ti);
01460f35
BM
716 if (IS_ERR(pgpath)) {
717 r = PTR_ERR(pgpath);
1da177e4 718 goto bad;
01460f35 719 }
1da177e4
LT
720
721 pgpath->pg = pg;
722 list_add_tail(&pgpath->list, &pg->pgpaths);
498f0103 723 dm_consume_args(as, nr_args);
1da177e4
LT
724 }
725
726 return pg;
727
728 bad:
729 free_priority_group(pg, ti);
01460f35 730 return ERR_PTR(r);
1da177e4
LT
731}
732
498f0103 733static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
1da177e4 734{
1da177e4 735 unsigned hw_argc;
2bfd2e13 736 int ret;
28f16c20 737 struct dm_target *ti = m->ti;
1da177e4 738
498f0103 739 static struct dm_arg _args[] = {
72d94861 740 {0, 1024, "invalid number of hardware handler args"},
1da177e4
LT
741 };
742
498f0103 743 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
1da177e4
LT
744 return -EINVAL;
745
746 if (!hw_argc)
747 return 0;
748
498f0103 749 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
510193a2
MS
750 if (!try_then_request_module(scsi_dh_handler_exist(m->hw_handler_name),
751 "scsi_dh_%s", m->hw_handler_name)) {
72d94861 752 ti->error = "unknown hardware handler type";
2bfd2e13
CS
753 ret = -EINVAL;
754 goto fail;
1da177e4 755 }
14e98c5c 756
2bfd2e13
CS
757 if (hw_argc > 1) {
758 char *p;
759 int i, j, len = 4;
760
761 for (i = 0; i <= hw_argc - 2; i++)
762 len += strlen(as->argv[i]) + 1;
763 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
764 if (!p) {
765 ti->error = "memory allocation failed";
766 ret = -ENOMEM;
767 goto fail;
768 }
769 j = sprintf(p, "%d", hw_argc - 1);
770 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
771 j = sprintf(p, "%s", as->argv[i]);
772 }
498f0103 773 dm_consume_args(as, hw_argc - 1);
1da177e4
LT
774
775 return 0;
2bfd2e13
CS
776fail:
777 kfree(m->hw_handler_name);
778 m->hw_handler_name = NULL;
779 return ret;
1da177e4
LT
780}
781
498f0103 782static int parse_features(struct dm_arg_set *as, struct multipath *m)
1da177e4
LT
783{
784 int r;
785 unsigned argc;
28f16c20 786 struct dm_target *ti = m->ti;
498f0103 787 const char *arg_name;
1da177e4 788
498f0103 789 static struct dm_arg _args[] = {
a58a935d 790 {0, 6, "invalid number of feature args"},
c9e45581 791 {1, 50, "pg_init_retries must be between 1 and 50"},
4e2d19e4 792 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
1da177e4
LT
793 };
794
498f0103 795 r = dm_read_arg_group(_args, as, &argc, &ti->error);
1da177e4
LT
796 if (r)
797 return -EINVAL;
798
799 if (!argc)
800 return 0;
801
c9e45581 802 do {
498f0103 803 arg_name = dm_shift_arg(as);
c9e45581
DW
804 argc--;
805
498f0103 806 if (!strcasecmp(arg_name, "queue_if_no_path")) {
c9e45581
DW
807 r = queue_if_no_path(m, 1, 0);
808 continue;
809 }
810
a58a935d
MS
811 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
812 m->retain_attached_hw_handler = 1;
813 continue;
814 }
815
498f0103 816 if (!strcasecmp(arg_name, "pg_init_retries") &&
c9e45581 817 (argc >= 1)) {
498f0103 818 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
c9e45581
DW
819 argc--;
820 continue;
821 }
822
498f0103 823 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
4e2d19e4 824 (argc >= 1)) {
498f0103 825 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
4e2d19e4
CS
826 argc--;
827 continue;
828 }
829
1da177e4 830 ti->error = "Unrecognised multipath feature request";
c9e45581
DW
831 r = -EINVAL;
832 } while (argc && !r);
833
834 return r;
1da177e4
LT
835}
836
837static int multipath_ctr(struct dm_target *ti, unsigned int argc,
838 char **argv)
839{
498f0103
MS
840 /* target arguments */
841 static struct dm_arg _args[] = {
a490a07a
MS
842 {0, 1024, "invalid number of priority groups"},
843 {0, 1024, "invalid initial priority group number"},
1da177e4
LT
844 };
845
846 int r;
847 struct multipath *m;
498f0103 848 struct dm_arg_set as;
1da177e4
LT
849 unsigned pg_count = 0;
850 unsigned next_pg_num;
851
852 as.argc = argc;
853 as.argv = argv;
854
28f16c20 855 m = alloc_multipath(ti);
1da177e4 856 if (!m) {
72d94861 857 ti->error = "can't allocate multipath";
1da177e4
LT
858 return -EINVAL;
859 }
860
28f16c20 861 r = parse_features(&as, m);
1da177e4
LT
862 if (r)
863 goto bad;
864
28f16c20 865 r = parse_hw_handler(&as, m);
1da177e4
LT
866 if (r)
867 goto bad;
868
498f0103 869 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
1da177e4
LT
870 if (r)
871 goto bad;
872
498f0103 873 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
1da177e4
LT
874 if (r)
875 goto bad;
876
a490a07a
MS
877 if ((!m->nr_priority_groups && next_pg_num) ||
878 (m->nr_priority_groups && !next_pg_num)) {
879 ti->error = "invalid initial priority group";
880 r = -EINVAL;
881 goto bad;
882 }
883
1da177e4
LT
884 /* parse the priority groups */
885 while (as.argc) {
886 struct priority_group *pg;
887
28f16c20 888 pg = parse_priority_group(&as, m);
01460f35
BM
889 if (IS_ERR(pg)) {
890 r = PTR_ERR(pg);
1da177e4
LT
891 goto bad;
892 }
893
894 m->nr_valid_paths += pg->nr_pgpaths;
895 list_add_tail(&pg->list, &m->priority_groups);
896 pg_count++;
897 pg->pg_num = pg_count;
898 if (!--next_pg_num)
899 m->next_pg = pg;
900 }
901
902 if (pg_count != m->nr_priority_groups) {
72d94861 903 ti->error = "priority group count mismatch";
1da177e4
LT
904 r = -EINVAL;
905 goto bad;
906 }
907
8627921f 908 ti->num_flush_requests = 1;
959eb4e5 909 ti->num_discard_requests = 1;
8627921f 910
1da177e4
LT
911 return 0;
912
913 bad:
914 free_multipath(m);
915 return r;
916}
917
2bded7bd
KU
918static void multipath_wait_for_pg_init_completion(struct multipath *m)
919{
920 DECLARE_WAITQUEUE(wait, current);
921 unsigned long flags;
922
923 add_wait_queue(&m->pg_init_wait, &wait);
924
925 while (1) {
926 set_current_state(TASK_UNINTERRUPTIBLE);
927
928 spin_lock_irqsave(&m->lock, flags);
929 if (!m->pg_init_in_progress) {
930 spin_unlock_irqrestore(&m->lock, flags);
931 break;
932 }
933 spin_unlock_irqrestore(&m->lock, flags);
934
935 io_schedule();
936 }
937 set_current_state(TASK_RUNNING);
938
939 remove_wait_queue(&m->pg_init_wait, &wait);
940}
941
942static void flush_multipath_work(struct multipath *m)
1da177e4 943{
bab7cfc7 944 flush_workqueue(kmpath_handlerd);
2bded7bd 945 multipath_wait_for_pg_init_completion(m);
a044d016 946 flush_workqueue(kmultipathd);
43829731 947 flush_work(&m->trigger_event);
6df400ab
KU
948}
949
950static void multipath_dtr(struct dm_target *ti)
951{
952 struct multipath *m = ti->private;
953
2bded7bd 954 flush_multipath_work(m);
1da177e4
LT
955 free_multipath(m);
956}
957
958/*
f40c67f0 959 * Map cloned requests
1da177e4 960 */
f40c67f0 961static int multipath_map(struct dm_target *ti, struct request *clone,
1da177e4
LT
962 union map_info *map_context)
963{
964 int r;
1da177e4
LT
965 struct multipath *m = (struct multipath *) ti->private;
966
466891f9 967 if (set_mapinfo(m, map_context) < 0)
f40c67f0
KU
968 /* ENOMEM, requeue */
969 return DM_MAPIO_REQUEUE;
1da177e4 970
f40c67f0 971 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
466891f9 972 r = map_io(m, clone, map_context, 0);
45e15720 973 if (r < 0 || r == DM_MAPIO_REQUEUE)
466891f9 974 clear_mapinfo(m, map_context);
1da177e4
LT
975
976 return r;
977}
978
979/*
980 * Take a path out of use.
981 */
982static int fail_path(struct pgpath *pgpath)
983{
984 unsigned long flags;
985 struct multipath *m = pgpath->pg->m;
986
987 spin_lock_irqsave(&m->lock, flags);
988
6680073d 989 if (!pgpath->is_active)
1da177e4
LT
990 goto out;
991
72d94861 992 DMWARN("Failing path %s.", pgpath->path.dev->name);
1da177e4
LT
993
994 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
6680073d 995 pgpath->is_active = 0;
1da177e4
LT
996 pgpath->fail_count++;
997
998 m->nr_valid_paths--;
999
1000 if (pgpath == m->current_pgpath)
1001 m->current_pgpath = NULL;
1002
b15546f9
MA
1003 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
1004 pgpath->path.dev->name, m->nr_valid_paths);
1005
fe9cf30e 1006 schedule_work(&m->trigger_event);
1da177e4
LT
1007
1008out:
1009 spin_unlock_irqrestore(&m->lock, flags);
1010
1011 return 0;
1012}
1013
1014/*
1015 * Reinstate a previously-failed path
1016 */
1017static int reinstate_path(struct pgpath *pgpath)
1018{
1019 int r = 0;
1020 unsigned long flags;
1021 struct multipath *m = pgpath->pg->m;
1022
1023 spin_lock_irqsave(&m->lock, flags);
1024
6680073d 1025 if (pgpath->is_active)
1da177e4
LT
1026 goto out;
1027
def052d2 1028 if (!pgpath->pg->ps.type->reinstate_path) {
1da177e4
LT
1029 DMWARN("Reinstate path not supported by path selector %s",
1030 pgpath->pg->ps.type->name);
1031 r = -EINVAL;
1032 goto out;
1033 }
1034
1035 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1036 if (r)
1037 goto out;
1038
6680073d 1039 pgpath->is_active = 1;
1da177e4 1040
e54f77dd
CS
1041 if (!m->nr_valid_paths++ && m->queue_size) {
1042 m->current_pgpath = NULL;
c557308e 1043 queue_work(kmultipathd, &m->process_queued_ios);
e54f77dd 1044 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
4e2d19e4 1045 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
e54f77dd
CS
1046 m->pg_init_in_progress++;
1047 }
1da177e4 1048
b15546f9
MA
1049 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1050 pgpath->path.dev->name, m->nr_valid_paths);
1051
fe9cf30e 1052 schedule_work(&m->trigger_event);
1da177e4
LT
1053
1054out:
1055 spin_unlock_irqrestore(&m->lock, flags);
1056
1057 return r;
1058}
1059
1060/*
1061 * Fail or reinstate all paths that match the provided struct dm_dev.
1062 */
1063static int action_dev(struct multipath *m, struct dm_dev *dev,
1064 action_fn action)
1065{
19040c0b 1066 int r = -EINVAL;
1da177e4
LT
1067 struct pgpath *pgpath;
1068 struct priority_group *pg;
1069
1070 list_for_each_entry(pg, &m->priority_groups, list) {
1071 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1072 if (pgpath->path.dev == dev)
1073 r = action(pgpath);
1074 }
1075 }
1076
1077 return r;
1078}
1079
1080/*
1081 * Temporarily try to avoid having to use the specified PG
1082 */
1083static void bypass_pg(struct multipath *m, struct priority_group *pg,
1084 int bypassed)
1085{
1086 unsigned long flags;
1087
1088 spin_lock_irqsave(&m->lock, flags);
1089
1090 pg->bypassed = bypassed;
1091 m->current_pgpath = NULL;
1092 m->current_pg = NULL;
1093
1094 spin_unlock_irqrestore(&m->lock, flags);
1095
fe9cf30e 1096 schedule_work(&m->trigger_event);
1da177e4
LT
1097}
1098
1099/*
1100 * Switch to using the specified PG from the next I/O that gets mapped
1101 */
1102static int switch_pg_num(struct multipath *m, const char *pgstr)
1103{
1104 struct priority_group *pg;
1105 unsigned pgnum;
1106 unsigned long flags;
31998ef1 1107 char dummy;
1da177e4 1108
31998ef1 1109 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1da177e4
LT
1110 (pgnum > m->nr_priority_groups)) {
1111 DMWARN("invalid PG number supplied to switch_pg_num");
1112 return -EINVAL;
1113 }
1114
1115 spin_lock_irqsave(&m->lock, flags);
1116 list_for_each_entry(pg, &m->priority_groups, list) {
1117 pg->bypassed = 0;
1118 if (--pgnum)
1119 continue;
1120
1121 m->current_pgpath = NULL;
1122 m->current_pg = NULL;
1123 m->next_pg = pg;
1124 }
1125 spin_unlock_irqrestore(&m->lock, flags);
1126
fe9cf30e 1127 schedule_work(&m->trigger_event);
1da177e4
LT
1128 return 0;
1129}
1130
1131/*
1132 * Set/clear bypassed status of a PG.
1133 * PGs are numbered upwards from 1 in the order they were declared.
1134 */
1135static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1136{
1137 struct priority_group *pg;
1138 unsigned pgnum;
31998ef1 1139 char dummy;
1da177e4 1140
31998ef1 1141 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1da177e4
LT
1142 (pgnum > m->nr_priority_groups)) {
1143 DMWARN("invalid PG number supplied to bypass_pg");
1144 return -EINVAL;
1145 }
1146
1147 list_for_each_entry(pg, &m->priority_groups, list) {
1148 if (!--pgnum)
1149 break;
1150 }
1151
1152 bypass_pg(m, pg, bypassed);
1153 return 0;
1154}
1155
c9e45581
DW
1156/*
1157 * Should we retry pg_init immediately?
1158 */
1159static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1160{
1161 unsigned long flags;
1162 int limit_reached = 0;
1163
1164 spin_lock_irqsave(&m->lock, flags);
1165
1166 if (m->pg_init_count <= m->pg_init_retries)
1167 m->pg_init_required = 1;
1168 else
1169 limit_reached = 1;
1170
1171 spin_unlock_irqrestore(&m->lock, flags);
1172
1173 return limit_reached;
1174}
1175
3ae31f6a 1176static void pg_init_done(void *data, int errors)
cfae5c9b 1177{
83c0d5d5 1178 struct pgpath *pgpath = data;
cfae5c9b
CS
1179 struct priority_group *pg = pgpath->pg;
1180 struct multipath *m = pg->m;
1181 unsigned long flags;
4e2d19e4 1182 unsigned delay_retry = 0;
cfae5c9b
CS
1183
1184 /* device or driver problems */
1185 switch (errors) {
1186 case SCSI_DH_OK:
1187 break;
1188 case SCSI_DH_NOSYS:
1189 if (!m->hw_handler_name) {
1190 errors = 0;
1191 break;
1192 }
f7b934c8
MB
1193 DMERR("Could not failover the device: Handler scsi_dh_%s "
1194 "Error %d.", m->hw_handler_name, errors);
cfae5c9b
CS
1195 /*
1196 * Fail path for now, so we do not ping pong
1197 */
1198 fail_path(pgpath);
1199 break;
1200 case SCSI_DH_DEV_TEMP_BUSY:
1201 /*
1202 * Probably doing something like FW upgrade on the
1203 * controller so try the other pg.
1204 */
1205 bypass_pg(m, pg, 1);
1206 break;
cfae5c9b 1207 case SCSI_DH_RETRY:
4e2d19e4
CS
1208 /* Wait before retrying. */
1209 delay_retry = 1;
cfae5c9b
CS
1210 case SCSI_DH_IMM_RETRY:
1211 case SCSI_DH_RES_TEMP_UNAVAIL:
1212 if (pg_init_limit_reached(m, pgpath))
1213 fail_path(pgpath);
1214 errors = 0;
1215 break;
1216 default:
1217 /*
1218 * We probably do not want to fail the path for a device
1219 * error, but this is what the old dm did. In future
1220 * patches we can do more advanced handling.
1221 */
1222 fail_path(pgpath);
1223 }
1224
1225 spin_lock_irqsave(&m->lock, flags);
1226 if (errors) {
e54f77dd
CS
1227 if (pgpath == m->current_pgpath) {
1228 DMERR("Could not failover device. Error %d.", errors);
1229 m->current_pgpath = NULL;
1230 m->current_pg = NULL;
1231 }
d0259bf0 1232 } else if (!m->pg_init_required)
cfae5c9b 1233 pg->bypassed = 0;
cfae5c9b 1234
d0259bf0
KU
1235 if (--m->pg_init_in_progress)
1236 /* Activations of other paths are still on going */
1237 goto out;
1238
1239 if (!m->pg_init_required)
1240 m->queue_io = 0;
1241
4e2d19e4 1242 m->pg_init_delay_retry = delay_retry;
d0259bf0
KU
1243 queue_work(kmultipathd, &m->process_queued_ios);
1244
2bded7bd
KU
1245 /*
1246 * Wake up any thread waiting to suspend.
1247 */
1248 wake_up(&m->pg_init_wait);
1249
d0259bf0 1250out:
cfae5c9b
CS
1251 spin_unlock_irqrestore(&m->lock, flags);
1252}
1253
bab7cfc7
CS
1254static void activate_path(struct work_struct *work)
1255{
e54f77dd 1256 struct pgpath *pgpath =
4e2d19e4 1257 container_of(work, struct pgpath, activate_path.work);
bab7cfc7 1258
3ae31f6a 1259 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
83c0d5d5 1260 pg_init_done, pgpath);
bab7cfc7
CS
1261}
1262
1da177e4
LT
1263/*
1264 * end_io handling
1265 */
f40c67f0 1266static int do_end_io(struct multipath *m, struct request *clone,
028867ac 1267 int error, struct dm_mpath_io *mpio)
1da177e4 1268{
f40c67f0
KU
1269 /*
1270 * We don't queue any clone request inside the multipath target
1271 * during end I/O handling, since those clone requests don't have
1272 * bio clones. If we queue them inside the multipath target,
1273 * we need to make bio clones, that requires memory allocation.
1274 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1275 * don't have bio clones.)
1276 * Instead of queueing the clone request here, we queue the original
1277 * request into dm core, which will remake a clone request and
1278 * clone bios for it and resubmit it later.
1279 */
1280 int r = DM_ENDIO_REQUEUE;
640eb3b0 1281 unsigned long flags;
1da177e4 1282
f40c67f0 1283 if (!error && !clone->errors)
1da177e4
LT
1284 return 0; /* I/O complete */
1285
6f13f6fb 1286 if (error == -EOPNOTSUPP || error == -EREMOTEIO || error == -EILSEQ)
959eb4e5
MS
1287 return error;
1288
cfae5c9b
CS
1289 if (mpio->pgpath)
1290 fail_path(mpio->pgpath);
1da177e4 1291
640eb3b0 1292 spin_lock_irqsave(&m->lock, flags);
751b2a7d
HR
1293 if (!m->nr_valid_paths) {
1294 if (!m->queue_if_no_path) {
1295 if (!__must_push_back(m))
1296 r = -EIO;
1297 } else {
1298 if (error == -EBADE)
1299 r = error;
1300 }
1301 }
640eb3b0 1302 spin_unlock_irqrestore(&m->lock, flags);
1da177e4 1303
f40c67f0 1304 return r;
1da177e4
LT
1305}
1306
f40c67f0 1307static int multipath_end_io(struct dm_target *ti, struct request *clone,
1da177e4
LT
1308 int error, union map_info *map_context)
1309{
028867ac
AK
1310 struct multipath *m = ti->private;
1311 struct dm_mpath_io *mpio = map_context->ptr;
a71a261f 1312 struct pgpath *pgpath;
1da177e4
LT
1313 struct path_selector *ps;
1314 int r;
1315
466891f9
JN
1316 BUG_ON(!mpio);
1317
f40c67f0 1318 r = do_end_io(m, clone, error, mpio);
a71a261f 1319 pgpath = mpio->pgpath;
1da177e4
LT
1320 if (pgpath) {
1321 ps = &pgpath->pg->ps;
1322 if (ps->type->end_io)
02ab823f 1323 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
1da177e4 1324 }
466891f9 1325 clear_mapinfo(m, map_context);
1da177e4
LT
1326
1327 return r;
1328}
1329
1330/*
1331 * Suspend can't complete until all the I/O is processed so if
436d4108
AK
1332 * the last path fails we must error any remaining I/O.
1333 * Note that if the freeze_bdev fails while suspending, the
1334 * queue_if_no_path state is lost - userspace should reset it.
1da177e4
LT
1335 */
1336static void multipath_presuspend(struct dm_target *ti)
1337{
1338 struct multipath *m = (struct multipath *) ti->private;
1da177e4 1339
485ef69e 1340 queue_if_no_path(m, 0, 1);
1da177e4
LT
1341}
1342
6df400ab
KU
1343static void multipath_postsuspend(struct dm_target *ti)
1344{
6380f26f
MA
1345 struct multipath *m = ti->private;
1346
1347 mutex_lock(&m->work_mutex);
2bded7bd 1348 flush_multipath_work(m);
6380f26f 1349 mutex_unlock(&m->work_mutex);
6df400ab
KU
1350}
1351
436d4108
AK
1352/*
1353 * Restore the queue_if_no_path setting.
1354 */
1da177e4
LT
1355static void multipath_resume(struct dm_target *ti)
1356{
1357 struct multipath *m = (struct multipath *) ti->private;
1358 unsigned long flags;
1359
1360 spin_lock_irqsave(&m->lock, flags);
436d4108 1361 m->queue_if_no_path = m->saved_queue_if_no_path;
1da177e4
LT
1362 spin_unlock_irqrestore(&m->lock, flags);
1363}
1364
1365/*
1366 * Info output has the following format:
1367 * num_multipath_feature_args [multipath_feature_args]*
1368 * num_handler_status_args [handler_status_args]*
1369 * num_groups init_group_number
1370 * [A|D|E num_ps_status_args [ps_status_args]*
1371 * num_paths num_selector_args
1372 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1373 *
1374 * Table output has the following format (identical to the constructor string):
1375 * num_feature_args [features_args]*
1376 * num_handler_args hw_handler [hw_handler_args]*
1377 * num_groups init_group_number
1378 * [priority selector-name num_ps_args [ps_args]*
1379 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1380 */
1381static int multipath_status(struct dm_target *ti, status_type_t type,
1f4e0ff0 1382 unsigned status_flags, char *result, unsigned maxlen)
1da177e4
LT
1383{
1384 int sz = 0;
1385 unsigned long flags;
1386 struct multipath *m = (struct multipath *) ti->private;
1da177e4
LT
1387 struct priority_group *pg;
1388 struct pgpath *p;
1389 unsigned pg_num;
1390 char state;
1391
1392 spin_lock_irqsave(&m->lock, flags);
1393
1394 /* Features */
1395 if (type == STATUSTYPE_INFO)
c9e45581
DW
1396 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1397 else {
1398 DMEMIT("%u ", m->queue_if_no_path +
4e2d19e4 1399 (m->pg_init_retries > 0) * 2 +
a58a935d
MS
1400 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1401 m->retain_attached_hw_handler);
c9e45581
DW
1402 if (m->queue_if_no_path)
1403 DMEMIT("queue_if_no_path ");
1404 if (m->pg_init_retries)
1405 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
4e2d19e4
CS
1406 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1407 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
a58a935d
MS
1408 if (m->retain_attached_hw_handler)
1409 DMEMIT("retain_attached_hw_handler ");
c9e45581 1410 }
1da177e4 1411
cfae5c9b 1412 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
1da177e4
LT
1413 DMEMIT("0 ");
1414 else
cfae5c9b 1415 DMEMIT("1 %s ", m->hw_handler_name);
1da177e4
LT
1416
1417 DMEMIT("%u ", m->nr_priority_groups);
1418
1419 if (m->next_pg)
1420 pg_num = m->next_pg->pg_num;
1421 else if (m->current_pg)
1422 pg_num = m->current_pg->pg_num;
1423 else
a490a07a 1424 pg_num = (m->nr_priority_groups ? 1 : 0);
1da177e4
LT
1425
1426 DMEMIT("%u ", pg_num);
1427
1428 switch (type) {
1429 case STATUSTYPE_INFO:
1430 list_for_each_entry(pg, &m->priority_groups, list) {
1431 if (pg->bypassed)
1432 state = 'D'; /* Disabled */
1433 else if (pg == m->current_pg)
1434 state = 'A'; /* Currently Active */
1435 else
1436 state = 'E'; /* Enabled */
1437
1438 DMEMIT("%c ", state);
1439
1440 if (pg->ps.type->status)
1441 sz += pg->ps.type->status(&pg->ps, NULL, type,
1442 result + sz,
1443 maxlen - sz);
1444 else
1445 DMEMIT("0 ");
1446
1447 DMEMIT("%u %u ", pg->nr_pgpaths,
1448 pg->ps.type->info_args);
1449
1450 list_for_each_entry(p, &pg->pgpaths, list) {
1451 DMEMIT("%s %s %u ", p->path.dev->name,
6680073d 1452 p->is_active ? "A" : "F",
1da177e4
LT
1453 p->fail_count);
1454 if (pg->ps.type->status)
1455 sz += pg->ps.type->status(&pg->ps,
1456 &p->path, type, result + sz,
1457 maxlen - sz);
1458 }
1459 }
1460 break;
1461
1462 case STATUSTYPE_TABLE:
1463 list_for_each_entry(pg, &m->priority_groups, list) {
1464 DMEMIT("%s ", pg->ps.type->name);
1465
1466 if (pg->ps.type->status)
1467 sz += pg->ps.type->status(&pg->ps, NULL, type,
1468 result + sz,
1469 maxlen - sz);
1470 else
1471 DMEMIT("0 ");
1472
1473 DMEMIT("%u %u ", pg->nr_pgpaths,
1474 pg->ps.type->table_args);
1475
1476 list_for_each_entry(p, &pg->pgpaths, list) {
1477 DMEMIT("%s ", p->path.dev->name);
1478 if (pg->ps.type->status)
1479 sz += pg->ps.type->status(&pg->ps,
1480 &p->path, type, result + sz,
1481 maxlen - sz);
1482 }
1483 }
1484 break;
1485 }
1486
1487 spin_unlock_irqrestore(&m->lock, flags);
1488
1489 return 0;
1490}
1491
1492static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1493{
6380f26f 1494 int r = -EINVAL;
1da177e4
LT
1495 struct dm_dev *dev;
1496 struct multipath *m = (struct multipath *) ti->private;
1497 action_fn action;
1498
6380f26f
MA
1499 mutex_lock(&m->work_mutex);
1500
c2f3d24b
KU
1501 if (dm_suspended(ti)) {
1502 r = -EBUSY;
1503 goto out;
1504 }
1505
1da177e4 1506 if (argc == 1) {
498f0103 1507 if (!strcasecmp(argv[0], "queue_if_no_path")) {
6380f26f
MA
1508 r = queue_if_no_path(m, 1, 0);
1509 goto out;
498f0103 1510 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
6380f26f
MA
1511 r = queue_if_no_path(m, 0, 0);
1512 goto out;
1513 }
1da177e4
LT
1514 }
1515
6380f26f
MA
1516 if (argc != 2) {
1517 DMWARN("Unrecognised multipath message received.");
1518 goto out;
1519 }
1da177e4 1520
498f0103 1521 if (!strcasecmp(argv[0], "disable_group")) {
6380f26f
MA
1522 r = bypass_pg_num(m, argv[1], 1);
1523 goto out;
498f0103 1524 } else if (!strcasecmp(argv[0], "enable_group")) {
6380f26f
MA
1525 r = bypass_pg_num(m, argv[1], 0);
1526 goto out;
498f0103 1527 } else if (!strcasecmp(argv[0], "switch_group")) {
6380f26f
MA
1528 r = switch_pg_num(m, argv[1]);
1529 goto out;
498f0103 1530 } else if (!strcasecmp(argv[0], "reinstate_path"))
1da177e4 1531 action = reinstate_path;
498f0103 1532 else if (!strcasecmp(argv[0], "fail_path"))
1da177e4 1533 action = fail_path;
6380f26f
MA
1534 else {
1535 DMWARN("Unrecognised multipath message received.");
1536 goto out;
1537 }
1da177e4 1538
8215d6ec 1539 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
1da177e4 1540 if (r) {
72d94861 1541 DMWARN("message: error getting device %s",
1da177e4 1542 argv[1]);
6380f26f 1543 goto out;
1da177e4
LT
1544 }
1545
1546 r = action_dev(m, dev, action);
1547
1548 dm_put_device(ti, dev);
1549
6380f26f
MA
1550out:
1551 mutex_unlock(&m->work_mutex);
1da177e4 1552 return r;
1da177e4
LT
1553}
1554
647b3d00 1555static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
9af4aa30
MB
1556 unsigned long arg)
1557{
35991652 1558 struct multipath *m = ti->private;
7ba10aa6 1559 struct pgpath *pgpath;
35991652
MP
1560 struct block_device *bdev;
1561 fmode_t mode;
9af4aa30 1562 unsigned long flags;
35991652
MP
1563 int r;
1564
1565again:
1566 bdev = NULL;
1567 mode = 0;
1568 r = 0;
9af4aa30
MB
1569
1570 spin_lock_irqsave(&m->lock, flags);
1571
1572 if (!m->current_pgpath)
02ab823f 1573 __choose_pgpath(m, 0);
9af4aa30 1574
7ba10aa6
MS
1575 pgpath = m->current_pgpath;
1576
1577 if (pgpath) {
1578 bdev = pgpath->path.dev->bdev;
1579 mode = pgpath->path.dev->mode;
e90dae1f 1580 }
9af4aa30 1581
7ba10aa6 1582 if ((pgpath && m->queue_io) || (!pgpath && m->queue_if_no_path))
9af4aa30
MB
1583 r = -EAGAIN;
1584 else if (!bdev)
1585 r = -EIO;
1586
1587 spin_unlock_irqrestore(&m->lock, flags);
1588
ec8013be
PB
1589 /*
1590 * Only pass ioctls through if the device sizes match exactly.
1591 */
1592 if (!r && ti->len != i_size_read(bdev->bd_inode) >> SECTOR_SHIFT)
1593 r = scsi_verify_blk_ioctl(NULL, cmd);
1594
35991652
MP
1595 if (r == -EAGAIN && !fatal_signal_pending(current)) {
1596 queue_work(kmultipathd, &m->process_queued_ios);
1597 msleep(10);
1598 goto again;
1599 }
1600
633a08b8 1601 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
9af4aa30
MB
1602}
1603
af4874e0
MS
1604static int multipath_iterate_devices(struct dm_target *ti,
1605 iterate_devices_callout_fn fn, void *data)
1606{
1607 struct multipath *m = ti->private;
1608 struct priority_group *pg;
1609 struct pgpath *p;
1610 int ret = 0;
1611
1612 list_for_each_entry(pg, &m->priority_groups, list) {
1613 list_for_each_entry(p, &pg->pgpaths, list) {
5dea271b 1614 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
af4874e0
MS
1615 if (ret)
1616 goto out;
1617 }
1618 }
1619
1620out:
1621 return ret;
1622}
1623
f40c67f0
KU
1624static int __pgpath_busy(struct pgpath *pgpath)
1625{
1626 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1627
1628 return dm_underlying_device_busy(q);
1629}
1630
1631/*
1632 * We return "busy", only when we can map I/Os but underlying devices
1633 * are busy (so even if we map I/Os now, the I/Os will wait on
1634 * the underlying queue).
1635 * In other words, if we want to kill I/Os or queue them inside us
1636 * due to map unavailability, we don't return "busy". Otherwise,
1637 * dm core won't give us the I/Os and we can't do what we want.
1638 */
1639static int multipath_busy(struct dm_target *ti)
1640{
1641 int busy = 0, has_active = 0;
1642 struct multipath *m = ti->private;
1643 struct priority_group *pg;
1644 struct pgpath *pgpath;
1645 unsigned long flags;
1646
1647 spin_lock_irqsave(&m->lock, flags);
1648
1649 /* Guess which priority_group will be used at next mapping time */
1650 if (unlikely(!m->current_pgpath && m->next_pg))
1651 pg = m->next_pg;
1652 else if (likely(m->current_pg))
1653 pg = m->current_pg;
1654 else
1655 /*
1656 * We don't know which pg will be used at next mapping time.
1657 * We don't call __choose_pgpath() here to avoid to trigger
1658 * pg_init just by busy checking.
1659 * So we don't know whether underlying devices we will be using
1660 * at next mapping time are busy or not. Just try mapping.
1661 */
1662 goto out;
1663
1664 /*
1665 * If there is one non-busy active path at least, the path selector
1666 * will be able to select it. So we consider such a pg as not busy.
1667 */
1668 busy = 1;
1669 list_for_each_entry(pgpath, &pg->pgpaths, list)
1670 if (pgpath->is_active) {
1671 has_active = 1;
1672
1673 if (!__pgpath_busy(pgpath)) {
1674 busy = 0;
1675 break;
1676 }
1677 }
1678
1679 if (!has_active)
1680 /*
1681 * No active path in this pg, so this pg won't be used and
1682 * the current_pg will be changed at next mapping time.
1683 * We need to try mapping to determine it.
1684 */
1685 busy = 0;
1686
1687out:
1688 spin_unlock_irqrestore(&m->lock, flags);
1689
1690 return busy;
1691}
1692
1da177e4
LT
1693/*-----------------------------------------------------------------
1694 * Module setup
1695 *---------------------------------------------------------------*/
1696static struct target_type multipath_target = {
1697 .name = "multipath",
a58a935d 1698 .version = {1, 5, 0},
1da177e4
LT
1699 .module = THIS_MODULE,
1700 .ctr = multipath_ctr,
1701 .dtr = multipath_dtr,
f40c67f0
KU
1702 .map_rq = multipath_map,
1703 .rq_end_io = multipath_end_io,
1da177e4 1704 .presuspend = multipath_presuspend,
6df400ab 1705 .postsuspend = multipath_postsuspend,
1da177e4
LT
1706 .resume = multipath_resume,
1707 .status = multipath_status,
1708 .message = multipath_message,
9af4aa30 1709 .ioctl = multipath_ioctl,
af4874e0 1710 .iterate_devices = multipath_iterate_devices,
f40c67f0 1711 .busy = multipath_busy,
1da177e4
LT
1712};
1713
1714static int __init dm_multipath_init(void)
1715{
1716 int r;
1717
1718 /* allocate a slab for the dm_ios */
028867ac 1719 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
1da177e4
LT
1720 if (!_mpio_cache)
1721 return -ENOMEM;
1722
1723 r = dm_register_target(&multipath_target);
1724 if (r < 0) {
0cd33124 1725 DMERR("register failed %d", r);
1da177e4
LT
1726 kmem_cache_destroy(_mpio_cache);
1727 return -EINVAL;
1728 }
1729
4d4d66ab 1730 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
c557308e 1731 if (!kmultipathd) {
0cd33124 1732 DMERR("failed to create workqueue kmpathd");
c557308e
AK
1733 dm_unregister_target(&multipath_target);
1734 kmem_cache_destroy(_mpio_cache);
1735 return -ENOMEM;
1736 }
1737
bab7cfc7
CS
1738 /*
1739 * A separate workqueue is used to handle the device handlers
1740 * to avoid overloading existing workqueue. Overloading the
1741 * old workqueue would also create a bottleneck in the
1742 * path of the storage hardware device activation.
1743 */
4d4d66ab
TH
1744 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
1745 WQ_MEM_RECLAIM);
bab7cfc7
CS
1746 if (!kmpath_handlerd) {
1747 DMERR("failed to create workqueue kmpath_handlerd");
1748 destroy_workqueue(kmultipathd);
1749 dm_unregister_target(&multipath_target);
1750 kmem_cache_destroy(_mpio_cache);
1751 return -ENOMEM;
1752 }
1753
72d94861 1754 DMINFO("version %u.%u.%u loaded",
1da177e4
LT
1755 multipath_target.version[0], multipath_target.version[1],
1756 multipath_target.version[2]);
1757
1758 return r;
1759}
1760
1761static void __exit dm_multipath_exit(void)
1762{
bab7cfc7 1763 destroy_workqueue(kmpath_handlerd);
c557308e
AK
1764 destroy_workqueue(kmultipathd);
1765
10d3bd09 1766 dm_unregister_target(&multipath_target);
1da177e4
LT
1767 kmem_cache_destroy(_mpio_cache);
1768}
1769
1da177e4
LT
1770module_init(dm_multipath_init);
1771module_exit(dm_multipath_exit);
1772
1773MODULE_DESCRIPTION(DM_NAME " multipath target");
1774MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1775MODULE_LICENSE("GPL");