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