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