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