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