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