13bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Copyright (C) 2003 Sistina Software Limited.
41da177e4SLinus Torvalds * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds * This file is released under the GPL.
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
9586e80e6SMikulas Patocka #include <linux/device-mapper.h>
10586e80e6SMikulas Patocka
114cc96131SMike Snitzer #include "dm-rq.h"
1276e33fe4SMike Snitzer #include "dm-bio-record.h"
131da177e4SLinus Torvalds #include "dm-path-selector.h"
14b15546f9SMike Anderson #include "dm-uevent.h"
151da177e4SLinus Torvalds
16e5863d9aSMike Snitzer #include <linux/blkdev.h>
171da177e4SLinus Torvalds #include <linux/ctype.h>
181da177e4SLinus Torvalds #include <linux/init.h>
191da177e4SLinus Torvalds #include <linux/mempool.h>
201da177e4SLinus Torvalds #include <linux/module.h>
211da177e4SLinus Torvalds #include <linux/pagemap.h>
221da177e4SLinus Torvalds #include <linux/slab.h>
231da177e4SLinus Torvalds #include <linux/time.h>
24be240ff5SAnatol Pomazau #include <linux/timer.h>
251da177e4SLinus Torvalds #include <linux/workqueue.h>
2635991652SMikulas Patocka #include <linux/delay.h>
27cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h>
2860063497SArun Sharma #include <linux/atomic.h>
2978ce23b5SMike Snitzer #include <linux/blk-mq.h>
301da177e4SLinus Torvalds
31a7e8f7fbSTetsuo Handa static struct workqueue_struct *dm_mpath_wq;
32a7e8f7fbSTetsuo Handa
3372d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath"
344e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000
3586a3238cSHeinz Mauelshagen #define DM_PG_INIT_DELAY_DEFAULT ((unsigned int) -1)
36be240ff5SAnatol Pomazau #define QUEUE_IF_NO_PATH_TIMEOUT_DEFAULT 0
37be240ff5SAnatol Pomazau
38be240ff5SAnatol Pomazau static unsigned long queue_if_no_path_timeout_secs = QUEUE_IF_NO_PATH_TIMEOUT_DEFAULT;
391da177e4SLinus Torvalds
401da177e4SLinus Torvalds /* Path properties */
411da177e4SLinus Torvalds struct pgpath {
421da177e4SLinus Torvalds struct list_head list;
431da177e4SLinus Torvalds
441da177e4SLinus Torvalds struct priority_group *pg; /* Owning PG */
4586a3238cSHeinz Mauelshagen unsigned int fail_count; /* Cumulative failure count */
461da177e4SLinus Torvalds
47c922d5f7SJosef "Jeff" Sipek struct dm_path path;
484e2d19e4SChandra Seetharaman struct delayed_work activate_path;
49be7d31ccSMike Snitzer
50be7d31ccSMike Snitzer bool is_active:1; /* Path status */
511da177e4SLinus Torvalds };
521da177e4SLinus Torvalds
531da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
541da177e4SLinus Torvalds
551da177e4SLinus Torvalds /*
561da177e4SLinus Torvalds * Paths are grouped into Priority Groups and numbered from 1 upwards.
571da177e4SLinus Torvalds * Each has a path selector which controls which path gets used.
581da177e4SLinus Torvalds */
591da177e4SLinus Torvalds struct priority_group {
601da177e4SLinus Torvalds struct list_head list;
611da177e4SLinus Torvalds
621da177e4SLinus Torvalds struct multipath *m; /* Owning multipath instance */
631da177e4SLinus Torvalds struct path_selector ps;
641da177e4SLinus Torvalds
6586a3238cSHeinz Mauelshagen unsigned int pg_num; /* Reference number */
6686a3238cSHeinz Mauelshagen unsigned int nr_pgpaths; /* Number of paths in PG */
671da177e4SLinus Torvalds struct list_head pgpaths;
68be7d31ccSMike Snitzer
69be7d31ccSMike Snitzer bool bypassed:1; /* Temporarily bypass this PG? */
701da177e4SLinus Torvalds };
711da177e4SLinus Torvalds
721da177e4SLinus Torvalds /* Multipath context */
731da177e4SLinus Torvalds struct multipath {
74848b8aefSMike Snitzer unsigned long flags; /* Multipath state flags */
754e2d19e4SChandra Seetharaman
761fbdd2b3SMike Snitzer spinlock_t lock;
77848b8aefSMike Snitzer enum dm_queue_mode queue_mode;
784e2d19e4SChandra Seetharaman
791da177e4SLinus Torvalds struct pgpath *current_pgpath;
801da177e4SLinus Torvalds struct priority_group *current_pg;
811da177e4SLinus Torvalds struct priority_group *next_pg; /* Switch to this PG if set */
821da177e4SLinus Torvalds
83848b8aefSMike Snitzer atomic_t nr_valid_paths; /* Total number of usable paths */
8486a3238cSHeinz Mauelshagen unsigned int nr_priority_groups;
85848b8aefSMike Snitzer struct list_head priority_groups;
861fbdd2b3SMike Snitzer
87848b8aefSMike Snitzer const char *hw_handler_name;
88848b8aefSMike Snitzer char *hw_handler_params;
89848b8aefSMike Snitzer wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
9086a3238cSHeinz Mauelshagen unsigned int pg_init_retries; /* Number of times to retry pg_init */
9186a3238cSHeinz Mauelshagen unsigned int pg_init_delay_msecs; /* Number of msecs before pg_init retry */
9291e968aaSMike Snitzer atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */
9391e968aaSMike Snitzer atomic_t pg_init_count; /* Number of times pg_init called */
9491e968aaSMike Snitzer
956380f26fSMike Anderson struct mutex work_mutex;
9620800cb3SMike Snitzer struct work_struct trigger_event;
97848b8aefSMike Snitzer struct dm_target *ti;
9876e33fe4SMike Snitzer
9976e33fe4SMike Snitzer struct work_struct process_queued_bios;
10076e33fe4SMike Snitzer struct bio_list queued_bios;
101be240ff5SAnatol Pomazau
102be240ff5SAnatol Pomazau struct timer_list nopath_timer; /* Timeout for queue_if_no_path */
1031da177e4SLinus Torvalds };
1041da177e4SLinus Torvalds
1051da177e4SLinus Torvalds /*
10676e33fe4SMike Snitzer * Context information attached to each io we process.
1071da177e4SLinus Torvalds */
108028867acSAlasdair G Kergon struct dm_mpath_io {
1091da177e4SLinus Torvalds struct pgpath *pgpath;
11002ab823fSKiyoshi Ueda size_t nr_bytes;
111c06dfd12SGabriel Krisman Bertazi u64 start_time_ns;
1121da177e4SLinus Torvalds };
1131da177e4SLinus Torvalds
1141da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath);
1151da177e4SLinus Torvalds
116bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
117c4028958SDavid Howells static void trigger_event(struct work_struct *work);
11889bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath);
11989bfce76SBart Van Assche static void activate_path_work(struct work_struct *work);
12076e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work);
121be240ff5SAnatol Pomazau static void queue_if_no_path_timeout_work(struct timer_list *t);
1221da177e4SLinus Torvalds
123a4a82ce3SHeinz Mauelshagen /*
124a4a82ce3SHeinz Mauelshagen *-----------------------------------------------
125518257b1SMike Snitzer * Multipath state flags.
126a4a82ce3SHeinz Mauelshagen *-----------------------------------------------
127a4a82ce3SHeinz Mauelshagen */
128518257b1SMike Snitzer #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */
129518257b1SMike Snitzer #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */
130518257b1SMike Snitzer #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */
131518257b1SMike Snitzer #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */
132518257b1SMike Snitzer #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */
133518257b1SMike Snitzer #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */
134518257b1SMike Snitzer #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */
1351da177e4SLinus Torvalds
mpath_double_check_test_bit(int MPATHF_bit,struct multipath * m)136374117adSMike Snitzer static bool mpath_double_check_test_bit(int MPATHF_bit, struct multipath *m)
137374117adSMike Snitzer {
138374117adSMike Snitzer bool r = test_bit(MPATHF_bit, &m->flags);
139374117adSMike Snitzer
140374117adSMike Snitzer if (r) {
141374117adSMike Snitzer unsigned long flags;
1420ef0b471SHeinz Mauelshagen
143374117adSMike Snitzer spin_lock_irqsave(&m->lock, flags);
144374117adSMike Snitzer r = test_bit(MPATHF_bit, &m->flags);
145374117adSMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
146374117adSMike Snitzer }
147374117adSMike Snitzer
148374117adSMike Snitzer return r;
149374117adSMike Snitzer }
150374117adSMike Snitzer
151a4a82ce3SHeinz Mauelshagen /*
152a4a82ce3SHeinz Mauelshagen *-----------------------------------------------
1531da177e4SLinus Torvalds * Allocation routines
154a4a82ce3SHeinz Mauelshagen *-----------------------------------------------
155a4a82ce3SHeinz Mauelshagen */
alloc_pgpath(void)1561da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void)
1571da177e4SLinus Torvalds {
158e69fae56SMicha³ Miros³aw struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
1591da177e4SLinus Torvalds
160848b8aefSMike Snitzer if (!pgpath)
161848b8aefSMike Snitzer return NULL;
162848b8aefSMike Snitzer
163be7d31ccSMike Snitzer pgpath->is_active = true;
1641da177e4SLinus Torvalds
1651da177e4SLinus Torvalds return pgpath;
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds
free_pgpath(struct pgpath * pgpath)168028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath)
1691da177e4SLinus Torvalds {
1701da177e4SLinus Torvalds kfree(pgpath);
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds
alloc_priority_group(void)1731da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds struct priority_group *pg;
1761da177e4SLinus Torvalds
177e69fae56SMicha³ Miros³aw pg = kzalloc(sizeof(*pg), GFP_KERNEL);
1781da177e4SLinus Torvalds
179e69fae56SMicha³ Miros³aw if (pg)
1801da177e4SLinus Torvalds INIT_LIST_HEAD(&pg->pgpaths);
1811da177e4SLinus Torvalds
1821da177e4SLinus Torvalds return pg;
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds
free_pgpaths(struct list_head * pgpaths,struct dm_target * ti)1851da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds struct pgpath *pgpath, *tmp;
1881da177e4SLinus Torvalds
1891da177e4SLinus Torvalds list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
1901da177e4SLinus Torvalds list_del(&pgpath->list);
1911da177e4SLinus Torvalds dm_put_device(ti, pgpath->path.dev);
1921da177e4SLinus Torvalds free_pgpath(pgpath);
1931da177e4SLinus Torvalds }
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds
free_priority_group(struct priority_group * pg,struct dm_target * ti)1961da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg,
1971da177e4SLinus Torvalds struct dm_target *ti)
1981da177e4SLinus Torvalds {
1991da177e4SLinus Torvalds struct path_selector *ps = &pg->ps;
2001da177e4SLinus Torvalds
2011da177e4SLinus Torvalds if (ps->type) {
2021da177e4SLinus Torvalds ps->type->destroy(ps);
2031da177e4SLinus Torvalds dm_put_path_selector(ps->type);
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds
2061da177e4SLinus Torvalds free_pgpaths(&pg->pgpaths, ti);
2071da177e4SLinus Torvalds kfree(pg);
2081da177e4SLinus Torvalds }
2091da177e4SLinus Torvalds
alloc_multipath(struct dm_target * ti)210e83068a5SMike Snitzer static struct multipath *alloc_multipath(struct dm_target *ti)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds struct multipath *m;
2131da177e4SLinus Torvalds
214e69fae56SMicha³ Miros³aw m = kzalloc(sizeof(*m), GFP_KERNEL);
2151da177e4SLinus Torvalds if (m) {
2161da177e4SLinus Torvalds INIT_LIST_HEAD(&m->priority_groups);
2171da177e4SLinus Torvalds spin_lock_init(&m->lock);
21891e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, 0);
219c4028958SDavid Howells INIT_WORK(&m->trigger_event, trigger_event);
2206380f26fSMike Anderson mutex_init(&m->work_mutex);
2218637a6bfSMike Snitzer
222e83068a5SMike Snitzer m->queue_mode = DM_TYPE_NONE;
223e83068a5SMike Snitzer
224e83068a5SMike Snitzer m->ti = ti;
225e83068a5SMike Snitzer ti->private = m;
226be240ff5SAnatol Pomazau
227be240ff5SAnatol Pomazau timer_setup(&m->nopath_timer, queue_if_no_path_timeout_work, 0);
228e83068a5SMike Snitzer }
229e83068a5SMike Snitzer
230e83068a5SMike Snitzer return m;
231e83068a5SMike Snitzer }
232e83068a5SMike Snitzer
alloc_multipath_stage2(struct dm_target * ti,struct multipath * m)233e83068a5SMike Snitzer static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
234e83068a5SMike Snitzer {
235e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_NONE) {
236953923c0SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED;
2378d47e659SMike Snitzer } else if (m->queue_mode == DM_TYPE_BIO_BASED) {
23876e33fe4SMike Snitzer INIT_WORK(&m->process_queued_bios, process_queued_bios);
23976e33fe4SMike Snitzer /*
24076e33fe4SMike Snitzer * bio-based doesn't support any direct scsi_dh management;
24176e33fe4SMike Snitzer * it just discovers if a scsi_dh is attached.
24276e33fe4SMike Snitzer */
24376e33fe4SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
24476e33fe4SMike Snitzer }
24576e33fe4SMike Snitzer
246e83068a5SMike Snitzer dm_table_set_type(ti->table, m->queue_mode);
2471da177e4SLinus Torvalds
248c3736674SMike Snitzer /*
249c3736674SMike Snitzer * Init fields that are only used when a scsi_dh is attached
250c3736674SMike Snitzer * - must do this unconditionally (really doesn't hurt non-SCSI uses)
251c3736674SMike Snitzer */
252c3736674SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags);
253c3736674SMike Snitzer atomic_set(&m->pg_init_in_progress, 0);
254c3736674SMike Snitzer atomic_set(&m->pg_init_count, 0);
255c3736674SMike Snitzer m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
256c3736674SMike Snitzer init_waitqueue_head(&m->pg_init_wait);
257c3736674SMike Snitzer
258e83068a5SMike Snitzer return 0;
2591da177e4SLinus Torvalds }
2601da177e4SLinus Torvalds
free_multipath(struct multipath * m)2611da177e4SLinus Torvalds static void free_multipath(struct multipath *m)
2621da177e4SLinus Torvalds {
2631da177e4SLinus Torvalds struct priority_group *pg, *tmp;
2641da177e4SLinus Torvalds
2651da177e4SLinus Torvalds list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
2661da177e4SLinus Torvalds list_del(&pg->list);
2671da177e4SLinus Torvalds free_priority_group(pg, m->ti);
2681da177e4SLinus Torvalds }
2691da177e4SLinus Torvalds
270cfae5c9bSChandra Seetharaman kfree(m->hw_handler_name);
2712bfd2e13SChandra Seetharaman kfree(m->hw_handler_params);
272d5ffebddSMike Snitzer mutex_destroy(&m->work_mutex);
2731da177e4SLinus Torvalds kfree(m);
2741da177e4SLinus Torvalds }
2751da177e4SLinus Torvalds
get_mpio(union map_info * info)2762eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info)
2772eff1924SMike Snitzer {
2782eff1924SMike Snitzer return info->ptr;
2792eff1924SMike Snitzer }
2802eff1924SMike Snitzer
multipath_per_bio_data_size(void)281bf661be1SMike Snitzer static size_t multipath_per_bio_data_size(void)
28276e33fe4SMike Snitzer {
283bf661be1SMike Snitzer return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
28476e33fe4SMike Snitzer }
28576e33fe4SMike Snitzer
get_mpio_from_bio(struct bio * bio)286bf661be1SMike Snitzer static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
287bf661be1SMike Snitzer {
288bf661be1SMike Snitzer return dm_per_bio_data(bio, multipath_per_bio_data_size());
289bf661be1SMike Snitzer }
290bf661be1SMike Snitzer
get_bio_details_from_mpio(struct dm_mpath_io * mpio)291d07a241dSMike Snitzer static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio)
292bf661be1SMike Snitzer {
293bf661be1SMike Snitzer /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
294bf661be1SMike Snitzer void *bio_details = mpio + 1;
295bf661be1SMike Snitzer return bio_details;
296bf661be1SMike Snitzer }
297bf661be1SMike Snitzer
multipath_init_per_bio_data(struct bio * bio,struct dm_mpath_io ** mpio_p)29863f6e6fdSMike Snitzer static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p)
29976e33fe4SMike Snitzer {
30076e33fe4SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
301d07a241dSMike Snitzer struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio);
30276e33fe4SMike Snitzer
303d0442f80SMike Snitzer mpio->nr_bytes = bio->bi_iter.bi_size;
304d0442f80SMike Snitzer mpio->pgpath = NULL;
305c06dfd12SGabriel Krisman Bertazi mpio->start_time_ns = 0;
306bf661be1SMike Snitzer *mpio_p = mpio;
307d0442f80SMike Snitzer
308d0442f80SMike Snitzer dm_bio_record(bio_details, bio);
30976e33fe4SMike Snitzer }
31076e33fe4SMike Snitzer
311a4a82ce3SHeinz Mauelshagen /*
312a4a82ce3SHeinz Mauelshagen *-----------------------------------------------
3131da177e4SLinus Torvalds * Path selection
314a4a82ce3SHeinz Mauelshagen *-----------------------------------------------
315a4a82ce3SHeinz Mauelshagen */
__pg_init_all_paths(struct multipath * m)3163e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m)
317fb612642SKiyoshi Ueda {
318fb612642SKiyoshi Ueda struct pgpath *pgpath;
3194e2d19e4SChandra Seetharaman unsigned long pg_init_delay = 0;
320fb612642SKiyoshi Ueda
321b194679fSBart Van Assche lockdep_assert_held(&m->lock);
322b194679fSBart Van Assche
32391e968aaSMike Snitzer if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
3243e9f1be1SHannes Reinecke return 0;
32517f4ff45SHannes Reinecke
32691e968aaSMike Snitzer atomic_inc(&m->pg_init_count);
327518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
3283e9f1be1SHannes Reinecke
3293e9f1be1SHannes Reinecke /* Check here to reset pg_init_required */
3303e9f1be1SHannes Reinecke if (!m->current_pg)
3313e9f1be1SHannes Reinecke return 0;
3323e9f1be1SHannes Reinecke
333518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
3344e2d19e4SChandra Seetharaman pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
3354e2d19e4SChandra Seetharaman m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
336fb612642SKiyoshi Ueda list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
337fb612642SKiyoshi Ueda /* Skip failed paths */
338fb612642SKiyoshi Ueda if (!pgpath->is_active)
339fb612642SKiyoshi Ueda continue;
3404e2d19e4SChandra Seetharaman if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
3414e2d19e4SChandra Seetharaman pg_init_delay))
34291e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress);
343fb612642SKiyoshi Ueda }
34491e968aaSMike Snitzer return atomic_read(&m->pg_init_in_progress);
345fb612642SKiyoshi Ueda }
346fb612642SKiyoshi Ueda
pg_init_all_paths(struct multipath * m)347c1d7ecf7SBart Van Assche static int pg_init_all_paths(struct multipath *m)
3481da177e4SLinus Torvalds {
349c1d7ecf7SBart Van Assche int ret;
3502da1610aSMike Snitzer unsigned long flags;
3512da1610aSMike Snitzer
3522da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags);
353c1d7ecf7SBart Van Assche ret = __pg_init_all_paths(m);
3542da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
355c1d7ecf7SBart Van Assche
356c1d7ecf7SBart Van Assche return ret;
3572da1610aSMike Snitzer }
3582da1610aSMike Snitzer
__switch_pg(struct multipath * m,struct priority_group * pg)3592da1610aSMike Snitzer static void __switch_pg(struct multipath *m, struct priority_group *pg)
3602da1610aSMike Snitzer {
36169cea0d4SMike Snitzer lockdep_assert_held(&m->lock);
36269cea0d4SMike Snitzer
3632da1610aSMike Snitzer m->current_pg = pg;
3641da177e4SLinus Torvalds
3651da177e4SLinus Torvalds /* Must we initialise the PG first, and queue I/O till it's ready? */
366cfae5c9bSChandra Seetharaman if (m->hw_handler_name) {
367518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
368518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags);
3691da177e4SLinus Torvalds } else {
370518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
371518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags);
3721da177e4SLinus Torvalds }
373c9e45581SDave Wysochanski
37491e968aaSMike Snitzer atomic_set(&m->pg_init_count, 0);
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds
choose_path_in_pg(struct multipath * m,struct priority_group * pg,size_t nr_bytes)3772da1610aSMike Snitzer static struct pgpath *choose_path_in_pg(struct multipath *m,
3782da1610aSMike Snitzer struct priority_group *pg,
37902ab823fSKiyoshi Ueda size_t nr_bytes)
3801da177e4SLinus Torvalds {
3812da1610aSMike Snitzer unsigned long flags;
382c922d5f7SJosef "Jeff" Sipek struct dm_path *path;
3832da1610aSMike Snitzer struct pgpath *pgpath;
3841da177e4SLinus Torvalds
38590a4323cSMike Snitzer path = pg->ps.type->select_path(&pg->ps, nr_bytes);
3861da177e4SLinus Torvalds if (!path)
3872da1610aSMike Snitzer return ERR_PTR(-ENXIO);
3881da177e4SLinus Torvalds
3892da1610aSMike Snitzer pgpath = path_to_pgpath(path);
3901da177e4SLinus Torvalds
391506458efSWill Deacon if (unlikely(READ_ONCE(m->current_pg) != pg)) {
3922da1610aSMike Snitzer /* Only update current_pgpath if pg changed */
3932da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags);
3942da1610aSMike Snitzer m->current_pgpath = pgpath;
3952da1610aSMike Snitzer __switch_pg(m, pg);
3962da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
3971da177e4SLinus Torvalds }
3981da177e4SLinus Torvalds
3992da1610aSMike Snitzer return pgpath;
4002da1610aSMike Snitzer }
4012da1610aSMike Snitzer
choose_pgpath(struct multipath * m,size_t nr_bytes)4022da1610aSMike Snitzer static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
4031da177e4SLinus Torvalds {
4042da1610aSMike Snitzer unsigned long flags;
4051da177e4SLinus Torvalds struct priority_group *pg;
4062da1610aSMike Snitzer struct pgpath *pgpath;
40786a3238cSHeinz Mauelshagen unsigned int bypassed = 1;
4081da177e4SLinus Torvalds
40991e968aaSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) {
41069cea0d4SMike Snitzer spin_lock_irqsave(&m->lock, flags);
411518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags);
41269cea0d4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
4131da177e4SLinus Torvalds goto failed;
4141f271972SBenjamin Marzinski }
4151da177e4SLinus Torvalds
4161da177e4SLinus Torvalds /* Were we instructed to switch PG? */
417506458efSWill Deacon if (READ_ONCE(m->next_pg)) {
4182da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags);
4191da177e4SLinus Torvalds pg = m->next_pg;
4202da1610aSMike Snitzer if (!pg) {
4212da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
4222da1610aSMike Snitzer goto check_current_pg;
4232da1610aSMike Snitzer }
4241da177e4SLinus Torvalds m->next_pg = NULL;
4252da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
4262da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes);
4272da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath))
4282da1610aSMike Snitzer return pgpath;
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds
4311da177e4SLinus Torvalds /* Don't change PG until it has no remaining paths */
4322da1610aSMike Snitzer check_current_pg:
433506458efSWill Deacon pg = READ_ONCE(m->current_pg);
4342da1610aSMike Snitzer if (pg) {
4352da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes);
4362da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath))
4372da1610aSMike Snitzer return pgpath;
4382da1610aSMike Snitzer }
4391da177e4SLinus Torvalds
4401da177e4SLinus Torvalds /*
4411da177e4SLinus Torvalds * Loop through priority groups until we find a valid path.
4421da177e4SLinus Torvalds * First time we skip PGs marked 'bypassed'.
443f220fd4eSMike Christie * Second time we only try the ones we skipped, but set
444f220fd4eSMike Christie * pg_init_delay_retry so we do not hammer controllers.
4451da177e4SLinus Torvalds */
4461da177e4SLinus Torvalds do {
4471da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) {
448d19a55ccSMike Snitzer if (pg->bypassed == !!bypassed)
4491da177e4SLinus Torvalds continue;
4502da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes);
4512da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) {
45269cea0d4SMike Snitzer if (!bypassed) {
45369cea0d4SMike Snitzer spin_lock_irqsave(&m->lock, flags);
454518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
45569cea0d4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
45669cea0d4SMike Snitzer }
4572da1610aSMike Snitzer return pgpath;
4581da177e4SLinus Torvalds }
459f220fd4eSMike Christie }
4601da177e4SLinus Torvalds } while (bypassed--);
4611da177e4SLinus Torvalds
4621da177e4SLinus Torvalds failed:
4632da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags);
4641da177e4SLinus Torvalds m->current_pgpath = NULL;
4651da177e4SLinus Torvalds m->current_pg = NULL;
4662da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
4672da1610aSMike Snitzer
4682da1610aSMike Snitzer return NULL;
4691da177e4SLinus Torvalds }
4701da177e4SLinus Torvalds
47145e15720SKiyoshi Ueda /*
472ac75b09fSMike Snitzer * dm_report_EIO() is a macro instead of a function to make pr_debug_ratelimited()
47386331f39SBart Van Assche * report the function name and line number of the function from which
47486331f39SBart Van Assche * it has been invoked.
47545e15720SKiyoshi Ueda */
47686331f39SBart Van Assche #define dm_report_EIO(m) \
477ac75b09fSMike Snitzer DMDEBUG_LIMIT("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d", \
478d4a512edSMike Snitzer dm_table_device_name((m)->ti->table), \
47986331f39SBart Van Assche test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \
48086331f39SBart Van Assche test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \
4816cc435faSHeinz Mauelshagen dm_noflush_suspending((m)->ti))
48245e15720SKiyoshi Ueda
48336fcffccSHannes Reinecke /*
484c1fd0abeSMike Snitzer * Check whether bios must be queued in the device-mapper core rather
485c1fd0abeSMike Snitzer * than here in the target.
486c1fd0abeSMike Snitzer */
__must_push_back(struct multipath * m)487a862e4e2SMike Snitzer static bool __must_push_back(struct multipath *m)
488c1fd0abeSMike Snitzer {
489a862e4e2SMike Snitzer return dm_noflush_suspending(m->ti);
490c1fd0abeSMike Snitzer }
491c1fd0abeSMike Snitzer
must_push_back_rq(struct multipath * m)492c1fd0abeSMike Snitzer static bool must_push_back_rq(struct multipath *m)
493c1fd0abeSMike Snitzer {
49473265f3fSMike Snitzer unsigned long flags;
49573265f3fSMike Snitzer bool ret;
49673265f3fSMike Snitzer
49773265f3fSMike Snitzer spin_lock_irqsave(&m->lock, flags);
49873265f3fSMike Snitzer ret = (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) || __must_push_back(m));
49973265f3fSMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
50073265f3fSMike Snitzer
50173265f3fSMike Snitzer return ret;
502c1fd0abeSMike Snitzer }
503c1fd0abeSMike Snitzer
504c1fd0abeSMike Snitzer /*
50576e33fe4SMike Snitzer * Map cloned requests (request-based multipath)
50636fcffccSHannes Reinecke */
multipath_clone_and_map(struct dm_target * ti,struct request * rq,union map_info * map_context,struct request ** __clone)507eb8db831SChristoph Hellwig static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
508e5863d9aSMike Snitzer union map_info *map_context,
509eb8db831SChristoph Hellwig struct request **__clone)
5101da177e4SLinus Torvalds {
5117943bd6dSMike Snitzer struct multipath *m = ti->private;
512eb8db831SChristoph Hellwig size_t nr_bytes = blk_rq_bytes(rq);
5131da177e4SLinus Torvalds struct pgpath *pgpath;
514f40c67f0SKiyoshi Ueda struct block_device *bdev;
515eb8db831SChristoph Hellwig struct dm_mpath_io *mpio = get_mpio(map_context);
5167083abbbSBart Van Assche struct request_queue *q;
517eb8db831SChristoph Hellwig struct request *clone;
5181da177e4SLinus Torvalds
5191da177e4SLinus Torvalds /* Do we need to select a new pgpath? */
520506458efSWill Deacon pgpath = READ_ONCE(m->current_pgpath);
521374117adSMike Snitzer if (!pgpath || !mpath_double_check_test_bit(MPATHF_QUEUE_IO, m))
5222da1610aSMike Snitzer pgpath = choose_pgpath(m, nr_bytes);
5231da177e4SLinus Torvalds
5249bf59a61SMike Snitzer if (!pgpath) {
525c1fd0abeSMike Snitzer if (must_push_back_rq(m))
526b88efd43SMike Snitzer return DM_MAPIO_DELAY_REQUEUE;
52718a482f5SChristoph Hellwig dm_report_EIO(m); /* Failed */
528f98e0eb6SChristoph Hellwig return DM_MAPIO_KILL;
529374117adSMike Snitzer } else if (mpath_double_check_test_bit(MPATHF_QUEUE_IO, m) ||
530374117adSMike Snitzer mpath_double_check_test_bit(MPATHF_PG_INIT_REQUIRED, m)) {
531459b5401SMing Lei pg_init_all_paths(m);
532c1d7ecf7SBart Van Assche return DM_MAPIO_DELAY_REQUEUE;
5339bf59a61SMike Snitzer }
5346afbc01dSMike Snitzer
535e8099177SHannes Reinecke mpio->pgpath = pgpath;
536e8099177SHannes Reinecke mpio->nr_bytes = nr_bytes;
5372eb6e1e3SKeith Busch
5382eb6e1e3SKeith Busch bdev = pgpath->path.dev->bdev;
5397083abbbSBart Van Assche q = bdev_get_queue(bdev);
5400bf6d96cSChristoph Hellwig clone = blk_mq_alloc_request(q, rq->cmd_flags | REQ_NOMERGE,
541ff005a06SChristoph Hellwig BLK_MQ_REQ_NOWAIT);
5426599c84eSBart Van Assche if (IS_ERR(clone)) {
5436599c84eSBart Van Assche /* EBUSY, ENODEV or EWOULDBLOCK: requeue */
544848b8aefSMike Snitzer if (blk_queue_dying(q)) {
5457083abbbSBart Van Assche atomic_inc(&m->pg_init_in_progress);
5467083abbbSBart Van Assche activate_or_offline_path(pgpath);
547050af08fSMing Lei return DM_MAPIO_DELAY_REQUEUE;
5487083abbbSBart Van Assche }
549050af08fSMing Lei
550050af08fSMing Lei /*
551050af08fSMing Lei * blk-mq's SCHED_RESTART can cover this requeue, so we
552050af08fSMing Lei * needn't deal with it by DELAY_REQUEUE. More importantly,
553050af08fSMing Lei * we have to return DM_MAPIO_REQUEUE so that blk-mq can
554050af08fSMing Lei * get the queue busy feedback (via BLK_STS_RESOURCE),
555050af08fSMing Lei * otherwise I/O merging can suffer.
556050af08fSMing Lei */
557050af08fSMing Lei return DM_MAPIO_REQUEUE;
5584c6dd53dSMike Snitzer }
5596599c84eSBart Van Assche clone->bio = clone->biotail = NULL;
5606599c84eSBart Van Assche clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
5616599c84eSBart Van Assche *__clone = clone;
5622eb6e1e3SKeith Busch
563e8099177SHannes Reinecke if (pgpath->pg->ps.type->start_io)
564e8099177SHannes Reinecke pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
565e8099177SHannes Reinecke &pgpath->path,
566e8099177SHannes Reinecke nr_bytes);
5672eb6e1e3SKeith Busch return DM_MAPIO_REMAPPED;
5681da177e4SLinus Torvalds }
5691da177e4SLinus Torvalds
multipath_release_clone(struct request * clone,union map_info * map_context)5705de719e3SYufen Yu static void multipath_release_clone(struct request *clone,
5715de719e3SYufen Yu union map_info *map_context)
572e5863d9aSMike Snitzer {
5735de719e3SYufen Yu if (unlikely(map_context)) {
5745de719e3SYufen Yu /*
5755de719e3SYufen Yu * non-NULL map_context means caller is still map
5765de719e3SYufen Yu * method; must undo multipath_clone_and_map()
5775de719e3SYufen Yu */
5785de719e3SYufen Yu struct dm_mpath_io *mpio = get_mpio(map_context);
5795de719e3SYufen Yu struct pgpath *pgpath = mpio->pgpath;
5805de719e3SYufen Yu
5815de719e3SYufen Yu if (pgpath && pgpath->pg->ps.type->end_io)
5825de719e3SYufen Yu pgpath->pg->ps.type->end_io(&pgpath->pg->ps,
5835de719e3SYufen Yu &pgpath->path,
584087615bfSGabriel Krisman Bertazi mpio->nr_bytes,
585087615bfSGabriel Krisman Bertazi clone->io_start_time_ns);
5865de719e3SYufen Yu }
5875de719e3SYufen Yu
5880bf6d96cSChristoph Hellwig blk_mq_free_request(clone);
589e5863d9aSMike Snitzer }
590e5863d9aSMike Snitzer
5911da177e4SLinus Torvalds /*
59276e33fe4SMike Snitzer * Map cloned bios (bio-based multipath)
59376e33fe4SMike Snitzer */
5940001ec56SMike Snitzer
__multipath_queue_bio(struct multipath * m,struct bio * bio)59517213ec1SMike Snitzer static void __multipath_queue_bio(struct multipath *m, struct bio *bio)
59617213ec1SMike Snitzer {
59717213ec1SMike Snitzer /* Queue for the daemon to resubmit */
59817213ec1SMike Snitzer bio_list_add(&m->queued_bios, bio);
59917213ec1SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
60017213ec1SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios);
60117213ec1SMike Snitzer }
60217213ec1SMike Snitzer
multipath_queue_bio(struct multipath * m,struct bio * bio)603f45f1186SMike Snitzer static void multipath_queue_bio(struct multipath *m, struct bio *bio)
604f45f1186SMike Snitzer {
605f45f1186SMike Snitzer unsigned long flags;
606f45f1186SMike Snitzer
607f45f1186SMike Snitzer spin_lock_irqsave(&m->lock, flags);
60817213ec1SMike Snitzer __multipath_queue_bio(m, bio);
609f45f1186SMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
610f45f1186SMike Snitzer }
611f45f1186SMike Snitzer
__map_bio(struct multipath * m,struct bio * bio)6120001ec56SMike Snitzer static struct pgpath *__map_bio(struct multipath *m, struct bio *bio)
61376e33fe4SMike Snitzer {
61476e33fe4SMike Snitzer struct pgpath *pgpath;
61576e33fe4SMike Snitzer unsigned long flags;
61676e33fe4SMike Snitzer
61776e33fe4SMike Snitzer /* Do we need to select a new pgpath? */
618506458efSWill Deacon pgpath = READ_ONCE(m->current_pgpath);
619374117adSMike Snitzer if (!pgpath || !mpath_double_check_test_bit(MPATHF_QUEUE_IO, m))
6200001ec56SMike Snitzer pgpath = choose_pgpath(m, bio->bi_iter.bi_size);
62176e33fe4SMike Snitzer
62217213ec1SMike Snitzer if (!pgpath) {
62376e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags);
62417213ec1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
62517213ec1SMike Snitzer __multipath_queue_bio(m, bio);
62617213ec1SMike Snitzer pgpath = ERR_PTR(-EAGAIN);
62717213ec1SMike Snitzer }
62876e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
629848b8aefSMike Snitzer
630374117adSMike Snitzer } else if (mpath_double_check_test_bit(MPATHF_QUEUE_IO, m) ||
631374117adSMike Snitzer mpath_double_check_test_bit(MPATHF_PG_INIT_REQUIRED, m)) {
632f45f1186SMike Snitzer multipath_queue_bio(m, bio);
63376e33fe4SMike Snitzer pg_init_all_paths(m);
6340001ec56SMike Snitzer return ERR_PTR(-EAGAIN);
63576e33fe4SMike Snitzer }
63676e33fe4SMike Snitzer
6370001ec56SMike Snitzer return pgpath;
63876e33fe4SMike Snitzer }
63976e33fe4SMike Snitzer
__multipath_map_bio(struct multipath * m,struct bio * bio,struct dm_mpath_io * mpio)6400001ec56SMike Snitzer static int __multipath_map_bio(struct multipath *m, struct bio *bio,
6410001ec56SMike Snitzer struct dm_mpath_io *mpio)
6420001ec56SMike Snitzer {
643dbaf971cSMike Snitzer struct pgpath *pgpath = __map_bio(m, bio);
6440001ec56SMike Snitzer
6450001ec56SMike Snitzer if (IS_ERR(pgpath))
6460001ec56SMike Snitzer return DM_MAPIO_SUBMITTED;
6470001ec56SMike Snitzer
64876e33fe4SMike Snitzer if (!pgpath) {
649a862e4e2SMike Snitzer if (__must_push_back(m))
65076e33fe4SMike Snitzer return DM_MAPIO_REQUEUE;
65118a482f5SChristoph Hellwig dm_report_EIO(m);
652846785e6SChristoph Hellwig return DM_MAPIO_KILL;
65376e33fe4SMike Snitzer }
65476e33fe4SMike Snitzer
65576e33fe4SMike Snitzer mpio->pgpath = pgpath;
65676e33fe4SMike Snitzer
657c06dfd12SGabriel Krisman Bertazi if (dm_ps_use_hr_timer(pgpath->pg->ps.type))
658c06dfd12SGabriel Krisman Bertazi mpio->start_time_ns = ktime_get_ns();
659c06dfd12SGabriel Krisman Bertazi
6604e4cbee9SChristoph Hellwig bio->bi_status = 0;
66174d46992SChristoph Hellwig bio_set_dev(bio, pgpath->path.dev->bdev);
6621eff9d32SJens Axboe bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
66376e33fe4SMike Snitzer
66476e33fe4SMike Snitzer if (pgpath->pg->ps.type->start_io)
66576e33fe4SMike Snitzer pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
66676e33fe4SMike Snitzer &pgpath->path,
667d0442f80SMike Snitzer mpio->nr_bytes);
66876e33fe4SMike Snitzer return DM_MAPIO_REMAPPED;
66976e33fe4SMike Snitzer }
67076e33fe4SMike Snitzer
multipath_map_bio(struct dm_target * ti,struct bio * bio)67176e33fe4SMike Snitzer static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
67276e33fe4SMike Snitzer {
67376e33fe4SMike Snitzer struct multipath *m = ti->private;
674bf661be1SMike Snitzer struct dm_mpath_io *mpio = NULL;
675bf661be1SMike Snitzer
67663f6e6fdSMike Snitzer multipath_init_per_bio_data(bio, &mpio);
67776e33fe4SMike Snitzer return __multipath_map_bio(m, bio, mpio);
67876e33fe4SMike Snitzer }
67976e33fe4SMike Snitzer
process_queued_io_list(struct multipath * m)6807e48c768SMike Snitzer static void process_queued_io_list(struct multipath *m)
68176e33fe4SMike Snitzer {
682953923c0SMike Snitzer if (m->queue_mode == DM_TYPE_REQUEST_BASED)
6837e48c768SMike Snitzer dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
6848d47e659SMike Snitzer else if (m->queue_mode == DM_TYPE_BIO_BASED)
68576e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios);
68676e33fe4SMike Snitzer }
68776e33fe4SMike Snitzer
process_queued_bios(struct work_struct * work)68876e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work)
68976e33fe4SMike Snitzer {
69076e33fe4SMike Snitzer int r;
69176e33fe4SMike Snitzer unsigned long flags;
69276e33fe4SMike Snitzer struct bio *bio;
69376e33fe4SMike Snitzer struct bio_list bios;
69476e33fe4SMike Snitzer struct blk_plug plug;
69576e33fe4SMike Snitzer struct multipath *m =
69676e33fe4SMike Snitzer container_of(work, struct multipath, process_queued_bios);
69776e33fe4SMike Snitzer
69876e33fe4SMike Snitzer bio_list_init(&bios);
69976e33fe4SMike Snitzer
70076e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags);
70176e33fe4SMike Snitzer
70276e33fe4SMike Snitzer if (bio_list_empty(&m->queued_bios)) {
70376e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
70476e33fe4SMike Snitzer return;
70576e33fe4SMike Snitzer }
70676e33fe4SMike Snitzer
70776e33fe4SMike Snitzer bio_list_merge(&bios, &m->queued_bios);
70876e33fe4SMike Snitzer bio_list_init(&m->queued_bios);
70976e33fe4SMike Snitzer
71076e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
71176e33fe4SMike Snitzer
71276e33fe4SMike Snitzer blk_start_plug(&plug);
71376e33fe4SMike Snitzer while ((bio = bio_list_pop(&bios))) {
7141836df08SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
7150ef0b471SHeinz Mauelshagen
7161836df08SMike Snitzer dm_bio_restore(get_bio_details_from_mpio(mpio), bio);
7171836df08SMike Snitzer r = __multipath_map_bio(m, bio, mpio);
718846785e6SChristoph Hellwig switch (r) {
719846785e6SChristoph Hellwig case DM_MAPIO_KILL:
7204e4cbee9SChristoph Hellwig bio->bi_status = BLK_STS_IOERR;
7214e4cbee9SChristoph Hellwig bio_endio(bio);
722047385b3SDan Carpenter break;
723846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE:
7244e4cbee9SChristoph Hellwig bio->bi_status = BLK_STS_DM_REQUEUE;
72576e33fe4SMike Snitzer bio_endio(bio);
726846785e6SChristoph Hellwig break;
727846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED:
728ed00aabdSChristoph Hellwig submit_bio_noacct(bio);
729846785e6SChristoph Hellwig break;
7308192a0cdSWang Sheng-Hui case DM_MAPIO_SUBMITTED:
7319157c8d3SBart Van Assche break;
7329157c8d3SBart Van Assche default:
7339157c8d3SBart Van Assche WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r);
734846785e6SChristoph Hellwig }
73576e33fe4SMike Snitzer }
73676e33fe4SMike Snitzer blk_finish_plug(&plug);
73776e33fe4SMike Snitzer }
73876e33fe4SMike Snitzer
73976e33fe4SMike Snitzer /*
7401da177e4SLinus Torvalds * If we run out of usable paths, should we queue I/O or error it?
7411da177e4SLinus Torvalds */
queue_if_no_path(struct multipath * m,bool f_queue_if_no_path,bool save_old_value,const char * caller)7421c131886SHeinz Mauelshagen static int queue_if_no_path(struct multipath *m, bool f_queue_if_no_path,
7434c3f4838SMike Snitzer bool save_old_value, const char *caller)
7441da177e4SLinus Torvalds {
7451da177e4SLinus Torvalds unsigned long flags;
746553ec94cSMike Snitzer bool queue_if_no_path_bit, saved_queue_if_no_path_bit;
747d4a512edSMike Snitzer const char *dm_dev_name = dm_table_device_name(m->ti->table);
7484c3f4838SMike Snitzer
7491c131886SHeinz Mauelshagen DMDEBUG("%s: %s caller=%s f_queue_if_no_path=%d save_old_value=%d",
7501c131886SHeinz Mauelshagen dm_dev_name, __func__, caller, f_queue_if_no_path, save_old_value);
7511da177e4SLinus Torvalds
7521da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags);
753553ec94cSMike Snitzer
754553ec94cSMike Snitzer queue_if_no_path_bit = test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
755553ec94cSMike Snitzer saved_queue_if_no_path_bit = test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
756553ec94cSMike Snitzer
757553ec94cSMike Snitzer if (save_old_value) {
758553ec94cSMike Snitzer if (unlikely(!queue_if_no_path_bit && saved_queue_if_no_path_bit)) {
759553ec94cSMike Snitzer DMERR("%s: QIFNP disabled but saved as enabled, saving again loses state, not saving!",
7604c3f4838SMike Snitzer dm_dev_name);
761553ec94cSMike Snitzer } else
762553ec94cSMike Snitzer assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path_bit);
7631c131886SHeinz Mauelshagen } else if (!f_queue_if_no_path && saved_queue_if_no_path_bit) {
764553ec94cSMike Snitzer /* due to "fail_if_no_path" message, need to honor it. */
765553ec94cSMike Snitzer clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
766553ec94cSMike Snitzer }
7671c131886SHeinz Mauelshagen assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, f_queue_if_no_path);
768553ec94cSMike Snitzer
7694c3f4838SMike Snitzer DMDEBUG("%s: after %s changes; QIFNP = %d; SQIFNP = %d; DNFS = %d",
7704c3f4838SMike Snitzer dm_dev_name, __func__,
7714c3f4838SMike Snitzer test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags),
7724c3f4838SMike Snitzer test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags),
7734c3f4838SMike Snitzer dm_noflush_suspending(m->ti));
7744c3f4838SMike Snitzer
7751da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags);
7761da177e4SLinus Torvalds
7771c131886SHeinz Mauelshagen if (!f_queue_if_no_path) {
77863d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table);
7797e48c768SMike Snitzer process_queued_io_list(m);
78076e33fe4SMike Snitzer }
78163d832c3SHannes Reinecke
7821da177e4SLinus Torvalds return 0;
7831da177e4SLinus Torvalds }
7841da177e4SLinus Torvalds
7851da177e4SLinus Torvalds /*
786be240ff5SAnatol Pomazau * If the queue_if_no_path timeout fires, turn off queue_if_no_path and
787be240ff5SAnatol Pomazau * process any queued I/O.
788be240ff5SAnatol Pomazau */
queue_if_no_path_timeout_work(struct timer_list * t)789be240ff5SAnatol Pomazau static void queue_if_no_path_timeout_work(struct timer_list *t)
790be240ff5SAnatol Pomazau {
791be240ff5SAnatol Pomazau struct multipath *m = from_timer(m, t, nopath_timer);
792be240ff5SAnatol Pomazau
793d4a512edSMike Snitzer DMWARN("queue_if_no_path timeout on %s, failing queued IO",
794d4a512edSMike Snitzer dm_table_device_name(m->ti->table));
7954c3f4838SMike Snitzer queue_if_no_path(m, false, false, __func__);
796be240ff5SAnatol Pomazau }
797be240ff5SAnatol Pomazau
798be240ff5SAnatol Pomazau /*
799be240ff5SAnatol Pomazau * Enable the queue_if_no_path timeout if necessary.
800be240ff5SAnatol Pomazau * Called with m->lock held.
801be240ff5SAnatol Pomazau */
enable_nopath_timeout(struct multipath * m)802be240ff5SAnatol Pomazau static void enable_nopath_timeout(struct multipath *m)
803be240ff5SAnatol Pomazau {
804be240ff5SAnatol Pomazau unsigned long queue_if_no_path_timeout =
805be240ff5SAnatol Pomazau READ_ONCE(queue_if_no_path_timeout_secs) * HZ;
806be240ff5SAnatol Pomazau
807be240ff5SAnatol Pomazau lockdep_assert_held(&m->lock);
808be240ff5SAnatol Pomazau
809be240ff5SAnatol Pomazau if (queue_if_no_path_timeout > 0 &&
810be240ff5SAnatol Pomazau atomic_read(&m->nr_valid_paths) == 0 &&
811be240ff5SAnatol Pomazau test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
812be240ff5SAnatol Pomazau mod_timer(&m->nopath_timer,
813be240ff5SAnatol Pomazau jiffies + queue_if_no_path_timeout);
814be240ff5SAnatol Pomazau }
815be240ff5SAnatol Pomazau }
816be240ff5SAnatol Pomazau
disable_nopath_timeout(struct multipath * m)817be240ff5SAnatol Pomazau static void disable_nopath_timeout(struct multipath *m)
818be240ff5SAnatol Pomazau {
819be240ff5SAnatol Pomazau del_timer_sync(&m->nopath_timer);
820be240ff5SAnatol Pomazau }
821be240ff5SAnatol Pomazau
822be240ff5SAnatol Pomazau /*
8231da177e4SLinus Torvalds * An event is triggered whenever a path is taken out of use.
8241da177e4SLinus Torvalds * Includes path failure and PG bypass.
8251da177e4SLinus Torvalds */
trigger_event(struct work_struct * work)826c4028958SDavid Howells static void trigger_event(struct work_struct *work)
8271da177e4SLinus Torvalds {
828c4028958SDavid Howells struct multipath *m =
829c4028958SDavid Howells container_of(work, struct multipath, trigger_event);
8301da177e4SLinus Torvalds
8311da177e4SLinus Torvalds dm_table_event(m->ti->table);
8321da177e4SLinus Torvalds }
8331da177e4SLinus Torvalds
834a4a82ce3SHeinz Mauelshagen /*
835a4a82ce3SHeinz Mauelshagen *---------------------------------------------------------------
8361da177e4SLinus Torvalds * Constructor/argument parsing:
8371da177e4SLinus Torvalds * <#multipath feature args> [<arg>]*
8381da177e4SLinus Torvalds * <#hw_handler args> [hw_handler [<arg>]*]
8391da177e4SLinus Torvalds * <#priority groups>
8401da177e4SLinus Torvalds * <initial priority group>
8411da177e4SLinus Torvalds * [<selector> <#selector args> [<arg>]*
8421da177e4SLinus Torvalds * <#paths> <#per-path selector args>
8431da177e4SLinus Torvalds * [<path> [<arg>]* ]+ ]+
844a4a82ce3SHeinz Mauelshagen *---------------------------------------------------------------
845a4a82ce3SHeinz Mauelshagen */
parse_path_selector(struct dm_arg_set * as,struct priority_group * pg,struct dm_target * ti)846498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
8471da177e4SLinus Torvalds struct dm_target *ti)
8481da177e4SLinus Torvalds {
8491da177e4SLinus Torvalds int r;
8501da177e4SLinus Torvalds struct path_selector_type *pst;
85186a3238cSHeinz Mauelshagen unsigned int ps_argc;
8521da177e4SLinus Torvalds
8535916a22bSEric Biggers static const struct dm_arg _args[] = {
85472d94861SAlasdair G Kergon {0, 1024, "invalid number of path selector args"},
8551da177e4SLinus Torvalds };
8561da177e4SLinus Torvalds
857498f0103SMike Snitzer pst = dm_get_path_selector(dm_shift_arg(as));
8581da177e4SLinus Torvalds if (!pst) {
85972d94861SAlasdair G Kergon ti->error = "unknown path selector type";
8601da177e4SLinus Torvalds return -EINVAL;
8611da177e4SLinus Torvalds }
8621da177e4SLinus Torvalds
863498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
864371b2e34SMikulas Patocka if (r) {
865371b2e34SMikulas Patocka dm_put_path_selector(pst);
8661da177e4SLinus Torvalds return -EINVAL;
867371b2e34SMikulas Patocka }
8681da177e4SLinus Torvalds
8691da177e4SLinus Torvalds r = pst->create(&pg->ps, ps_argc, as->argv);
8701da177e4SLinus Torvalds if (r) {
8711da177e4SLinus Torvalds dm_put_path_selector(pst);
87272d94861SAlasdair G Kergon ti->error = "path selector constructor failed";
8731da177e4SLinus Torvalds return r;
8741da177e4SLinus Torvalds }
8751da177e4SLinus Torvalds
8761da177e4SLinus Torvalds pg->ps.type = pst;
877498f0103SMike Snitzer dm_consume_args(as, ps_argc);
8781da177e4SLinus Torvalds
8791da177e4SLinus Torvalds return 0;
8801da177e4SLinus Torvalds }
8811da177e4SLinus Torvalds
setup_scsi_dh(struct block_device * bdev,struct multipath * m,const char ** attached_handler_name,char ** error)882e8f74a0fSMike Snitzer static int setup_scsi_dh(struct block_device *bdev, struct multipath *m,
883b592211cSMike Snitzer const char **attached_handler_name, char **error)
8841da177e4SLinus Torvalds {
885848b8aefSMike Snitzer struct request_queue *q = bdev_get_queue(bdev);
886848b8aefSMike Snitzer int r;
887a0cf7ea9SHannes Reinecke
888374117adSMike Snitzer if (mpath_double_check_test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, m)) {
8891bab0de0SChristoph Hellwig retain:
890b592211cSMike Snitzer if (*attached_handler_name) {
891a58a935dSMike Snitzer /*
89254cd640dStang.junhui * Clear any hw_handler_params associated with a
89354cd640dStang.junhui * handler that isn't already attached.
89454cd640dStang.junhui */
895b592211cSMike Snitzer if (m->hw_handler_name && strcmp(*attached_handler_name, m->hw_handler_name)) {
89654cd640dStang.junhui kfree(m->hw_handler_params);
89754cd640dStang.junhui m->hw_handler_params = NULL;
89854cd640dStang.junhui }
89954cd640dStang.junhui
90054cd640dStang.junhui /*
901a58a935dSMike Snitzer * Reset hw_handler_name to match the attached handler
902a58a935dSMike Snitzer *
903a58a935dSMike Snitzer * NB. This modifies the table line to show the actual
904a58a935dSMike Snitzer * handler instead of the original table passed in.
905a58a935dSMike Snitzer */
906a58a935dSMike Snitzer kfree(m->hw_handler_name);
907b592211cSMike Snitzer m->hw_handler_name = *attached_handler_name;
908b592211cSMike Snitzer *attached_handler_name = NULL;
909a58a935dSMike Snitzer }
910a58a935dSMike Snitzer }
911a58a935dSMike Snitzer
912a58a935dSMike Snitzer if (m->hw_handler_name) {
913a0cf7ea9SHannes Reinecke r = scsi_dh_attach(q, m->hw_handler_name);
914a0cf7ea9SHannes Reinecke if (r == -EBUSY) {
915168678d7SMike Snitzer DMINFO("retaining handler on device %pg", bdev);
9161bab0de0SChristoph Hellwig goto retain;
9171bab0de0SChristoph Hellwig }
918ae11b1b3SHannes Reinecke if (r < 0) {
919848b8aefSMike Snitzer *error = "error attaching hardware handler";
920848b8aefSMike Snitzer return r;
921ae11b1b3SHannes Reinecke }
9222bfd2e13SChandra Seetharaman
9232bfd2e13SChandra Seetharaman if (m->hw_handler_params) {
9242bfd2e13SChandra Seetharaman r = scsi_dh_set_params(q, m->hw_handler_params);
9252bfd2e13SChandra Seetharaman if (r < 0) {
926848b8aefSMike Snitzer *error = "unable to set hardware handler parameters";
927848b8aefSMike Snitzer return r;
928848b8aefSMike Snitzer }
929848b8aefSMike Snitzer }
930848b8aefSMike Snitzer }
931848b8aefSMike Snitzer
932848b8aefSMike Snitzer return 0;
933848b8aefSMike Snitzer }
934848b8aefSMike Snitzer
parse_path(struct dm_arg_set * as,struct path_selector * ps,struct dm_target * ti)935848b8aefSMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
936848b8aefSMike Snitzer struct dm_target *ti)
937848b8aefSMike Snitzer {
938848b8aefSMike Snitzer int r;
939848b8aefSMike Snitzer struct pgpath *p;
940848b8aefSMike Snitzer struct multipath *m = ti->private;
941e8f74a0fSMike Snitzer struct request_queue *q;
942b592211cSMike Snitzer const char *attached_handler_name = NULL;
943848b8aefSMike Snitzer
944848b8aefSMike Snitzer /* we need at least a path arg */
945848b8aefSMike Snitzer if (as->argc < 1) {
946848b8aefSMike Snitzer ti->error = "no device given";
947848b8aefSMike Snitzer return ERR_PTR(-EINVAL);
948848b8aefSMike Snitzer }
949848b8aefSMike Snitzer
950848b8aefSMike Snitzer p = alloc_pgpath();
951848b8aefSMike Snitzer if (!p)
952848b8aefSMike Snitzer return ERR_PTR(-ENOMEM);
953848b8aefSMike Snitzer
954848b8aefSMike Snitzer r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
955848b8aefSMike Snitzer &p->path.dev);
956848b8aefSMike Snitzer if (r) {
957848b8aefSMike Snitzer ti->error = "error getting device";
9582bfd2e13SChandra Seetharaman goto bad;
9592bfd2e13SChandra Seetharaman }
960848b8aefSMike Snitzer
961e8f74a0fSMike Snitzer q = bdev_get_queue(p->path.dev->bdev);
962e8f74a0fSMike Snitzer attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
963e457edf0SMike Snitzer if (attached_handler_name || m->hw_handler_name) {
964848b8aefSMike Snitzer INIT_DELAYED_WORK(&p->activate_path, activate_path_work);
965b592211cSMike Snitzer r = setup_scsi_dh(p->path.dev->bdev, m, &attached_handler_name, &ti->error);
966940bc471SMartin Wilck kfree(attached_handler_name);
967848b8aefSMike Snitzer if (r) {
968848b8aefSMike Snitzer dm_put_device(ti, p->path.dev);
969848b8aefSMike Snitzer goto bad;
9702bfd2e13SChandra Seetharaman }
971ae11b1b3SHannes Reinecke }
972ae11b1b3SHannes Reinecke
9731da177e4SLinus Torvalds r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
9741da177e4SLinus Torvalds if (r) {
9751da177e4SLinus Torvalds dm_put_device(ti, p->path.dev);
9761da177e4SLinus Torvalds goto bad;
9771da177e4SLinus Torvalds }
9781da177e4SLinus Torvalds
9791da177e4SLinus Torvalds return p;
9801da177e4SLinus Torvalds bad:
9811da177e4SLinus Torvalds free_pgpath(p);
98201460f35SBenjamin Marzinski return ERR_PTR(r);
9831da177e4SLinus Torvalds }
9841da177e4SLinus Torvalds
parse_priority_group(struct dm_arg_set * as,struct multipath * m)985498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as,
98628f16c20SMicha³ Miros³aw struct multipath *m)
9871da177e4SLinus Torvalds {
9885916a22bSEric Biggers static const struct dm_arg _args[] = {
98972d94861SAlasdair G Kergon {1, 1024, "invalid number of paths"},
99072d94861SAlasdair G Kergon {0, 1024, "invalid number of selector args"}
9911da177e4SLinus Torvalds };
9921da177e4SLinus Torvalds
9931da177e4SLinus Torvalds int r;
99486a3238cSHeinz Mauelshagen unsigned int i, nr_selector_args, nr_args;
9951da177e4SLinus Torvalds struct priority_group *pg;
99628f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti;
9971da177e4SLinus Torvalds
9981da177e4SLinus Torvalds if (as->argc < 2) {
9991da177e4SLinus Torvalds as->argc = 0;
100001460f35SBenjamin Marzinski ti->error = "not enough priority group arguments";
100101460f35SBenjamin Marzinski return ERR_PTR(-EINVAL);
10021da177e4SLinus Torvalds }
10031da177e4SLinus Torvalds
10041da177e4SLinus Torvalds pg = alloc_priority_group();
10051da177e4SLinus Torvalds if (!pg) {
100672d94861SAlasdair G Kergon ti->error = "couldn't allocate priority group";
100701460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM);
10081da177e4SLinus Torvalds }
10091da177e4SLinus Torvalds pg->m = m;
10101da177e4SLinus Torvalds
10111da177e4SLinus Torvalds r = parse_path_selector(as, pg, ti);
10121da177e4SLinus Torvalds if (r)
10131da177e4SLinus Torvalds goto bad;
10141da177e4SLinus Torvalds
10151da177e4SLinus Torvalds /*
10161da177e4SLinus Torvalds * read the paths
10171da177e4SLinus Torvalds */
1018498f0103SMike Snitzer r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
10191da177e4SLinus Torvalds if (r)
10201da177e4SLinus Torvalds goto bad;
10211da177e4SLinus Torvalds
1022498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
10231da177e4SLinus Torvalds if (r)
10241da177e4SLinus Torvalds goto bad;
10251da177e4SLinus Torvalds
1026498f0103SMike Snitzer nr_args = 1 + nr_selector_args;
10271da177e4SLinus Torvalds for (i = 0; i < pg->nr_pgpaths; i++) {
10281da177e4SLinus Torvalds struct pgpath *pgpath;
1029498f0103SMike Snitzer struct dm_arg_set path_args;
10301da177e4SLinus Torvalds
1031498f0103SMike Snitzer if (as->argc < nr_args) {
1032148acff6SMikulas Patocka ti->error = "not enough path parameters";
10336bbf79a1SAlasdair G Kergon r = -EINVAL;
10341da177e4SLinus Torvalds goto bad;
1035148acff6SMikulas Patocka }
10361da177e4SLinus Torvalds
1037498f0103SMike Snitzer path_args.argc = nr_args;
10381da177e4SLinus Torvalds path_args.argv = as->argv;
10391da177e4SLinus Torvalds
10401da177e4SLinus Torvalds pgpath = parse_path(&path_args, &pg->ps, ti);
104101460f35SBenjamin Marzinski if (IS_ERR(pgpath)) {
104201460f35SBenjamin Marzinski r = PTR_ERR(pgpath);
10431da177e4SLinus Torvalds goto bad;
104401460f35SBenjamin Marzinski }
10451da177e4SLinus Torvalds
10461da177e4SLinus Torvalds pgpath->pg = pg;
10471da177e4SLinus Torvalds list_add_tail(&pgpath->list, &pg->pgpaths);
1048498f0103SMike Snitzer dm_consume_args(as, nr_args);
10491da177e4SLinus Torvalds }
10501da177e4SLinus Torvalds
10511da177e4SLinus Torvalds return pg;
10521da177e4SLinus Torvalds
10531da177e4SLinus Torvalds bad:
10541da177e4SLinus Torvalds free_priority_group(pg, ti);
105501460f35SBenjamin Marzinski return ERR_PTR(r);
10561da177e4SLinus Torvalds }
10571da177e4SLinus Torvalds
parse_hw_handler(struct dm_arg_set * as,struct multipath * m)1058498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
10591da177e4SLinus Torvalds {
106086a3238cSHeinz Mauelshagen unsigned int hw_argc;
10612bfd2e13SChandra Seetharaman int ret;
106228f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti;
10631da177e4SLinus Torvalds
10645916a22bSEric Biggers static const struct dm_arg _args[] = {
106572d94861SAlasdair G Kergon {0, 1024, "invalid number of hardware handler args"},
10661da177e4SLinus Torvalds };
10671da177e4SLinus Torvalds
1068498f0103SMike Snitzer if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
10691da177e4SLinus Torvalds return -EINVAL;
10701da177e4SLinus Torvalds
10711da177e4SLinus Torvalds if (!hw_argc)
10721da177e4SLinus Torvalds return 0;
10731da177e4SLinus Torvalds
10748d47e659SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) {
107576e33fe4SMike Snitzer dm_consume_args(as, hw_argc);
107676e33fe4SMike Snitzer DMERR("bio-based multipath doesn't allow hardware handler args");
107776e33fe4SMike Snitzer return 0;
107876e33fe4SMike Snitzer }
107976e33fe4SMike Snitzer
1080498f0103SMike Snitzer m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
1081f97dc421Stang.junhui if (!m->hw_handler_name)
1082f97dc421Stang.junhui return -EINVAL;
108314e98c5cSChandra Seetharaman
10842bfd2e13SChandra Seetharaman if (hw_argc > 1) {
10852bfd2e13SChandra Seetharaman char *p;
10862bfd2e13SChandra Seetharaman int i, j, len = 4;
10872bfd2e13SChandra Seetharaman
10882bfd2e13SChandra Seetharaman for (i = 0; i <= hw_argc - 2; i++)
10892bfd2e13SChandra Seetharaman len += strlen(as->argv[i]) + 1;
10902bfd2e13SChandra Seetharaman p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
10912bfd2e13SChandra Seetharaman if (!p) {
10922bfd2e13SChandra Seetharaman ti->error = "memory allocation failed";
10932bfd2e13SChandra Seetharaman ret = -ENOMEM;
10942bfd2e13SChandra Seetharaman goto fail;
10952bfd2e13SChandra Seetharaman }
10962bfd2e13SChandra Seetharaman j = sprintf(p, "%d", hw_argc - 1);
10972bfd2e13SChandra Seetharaman for (i = 0, p += j + 1; i <= hw_argc - 2; i++, p += j + 1)
10982bfd2e13SChandra Seetharaman j = sprintf(p, "%s", as->argv[i]);
10992bfd2e13SChandra Seetharaman }
1100498f0103SMike Snitzer dm_consume_args(as, hw_argc - 1);
11011da177e4SLinus Torvalds
11021da177e4SLinus Torvalds return 0;
11032bfd2e13SChandra Seetharaman fail:
11042bfd2e13SChandra Seetharaman kfree(m->hw_handler_name);
11052bfd2e13SChandra Seetharaman m->hw_handler_name = NULL;
11062bfd2e13SChandra Seetharaman return ret;
11071da177e4SLinus Torvalds }
11081da177e4SLinus Torvalds
parse_features(struct dm_arg_set * as,struct multipath * m)1109498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m)
11101da177e4SLinus Torvalds {
11111da177e4SLinus Torvalds int r;
111286a3238cSHeinz Mauelshagen unsigned int argc;
111328f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti;
1114498f0103SMike Snitzer const char *arg_name;
11151da177e4SLinus Torvalds
11165916a22bSEric Biggers static const struct dm_arg _args[] = {
1117e83068a5SMike Snitzer {0, 8, "invalid number of feature args"},
1118c9e45581SDave Wysochanski {1, 50, "pg_init_retries must be between 1 and 50"},
11194e2d19e4SChandra Seetharaman {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
11201da177e4SLinus Torvalds };
11211da177e4SLinus Torvalds
1122498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &argc, &ti->error);
11231da177e4SLinus Torvalds if (r)
11241da177e4SLinus Torvalds return -EINVAL;
11251da177e4SLinus Torvalds
11261da177e4SLinus Torvalds if (!argc)
11271da177e4SLinus Torvalds return 0;
11281da177e4SLinus Torvalds
1129c9e45581SDave Wysochanski do {
1130498f0103SMike Snitzer arg_name = dm_shift_arg(as);
1131c9e45581SDave Wysochanski argc--;
1132c9e45581SDave Wysochanski
1133498f0103SMike Snitzer if (!strcasecmp(arg_name, "queue_if_no_path")) {
11344c3f4838SMike Snitzer r = queue_if_no_path(m, true, false, __func__);
1135c9e45581SDave Wysochanski continue;
11361da177e4SLinus Torvalds }
1137c9e45581SDave Wysochanski
1138a58a935dSMike Snitzer if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
1139518257b1SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
1140a58a935dSMike Snitzer continue;
1141a58a935dSMike Snitzer }
1142a58a935dSMike Snitzer
1143498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_retries") &&
1144c9e45581SDave Wysochanski (argc >= 1)) {
1145498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
1146c9e45581SDave Wysochanski argc--;
1147c9e45581SDave Wysochanski continue;
1148c9e45581SDave Wysochanski }
1149c9e45581SDave Wysochanski
1150498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
11514e2d19e4SChandra Seetharaman (argc >= 1)) {
1152498f0103SMike Snitzer r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
11534e2d19e4SChandra Seetharaman argc--;
11544e2d19e4SChandra Seetharaman continue;
11554e2d19e4SChandra Seetharaman }
11564e2d19e4SChandra Seetharaman
1157e83068a5SMike Snitzer if (!strcasecmp(arg_name, "queue_mode") &&
1158e83068a5SMike Snitzer (argc >= 1)) {
1159e83068a5SMike Snitzer const char *queue_mode_name = dm_shift_arg(as);
1160e83068a5SMike Snitzer
1161e83068a5SMike Snitzer if (!strcasecmp(queue_mode_name, "bio"))
1162e83068a5SMike Snitzer m->queue_mode = DM_TYPE_BIO_BASED;
1163953923c0SMike Snitzer else if (!strcasecmp(queue_mode_name, "rq") ||
1164953923c0SMike Snitzer !strcasecmp(queue_mode_name, "mq"))
1165e83068a5SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED;
1166e83068a5SMike Snitzer else {
1167e83068a5SMike Snitzer ti->error = "Unknown 'queue_mode' requested";
1168e83068a5SMike Snitzer r = -EINVAL;
1169e83068a5SMike Snitzer }
1170e83068a5SMike Snitzer argc--;
1171e83068a5SMike Snitzer continue;
1172e83068a5SMike Snitzer }
1173e83068a5SMike Snitzer
1174c9e45581SDave Wysochanski ti->error = "Unrecognised multipath feature request";
1175c9e45581SDave Wysochanski r = -EINVAL;
1176c9e45581SDave Wysochanski } while (argc && !r);
1177c9e45581SDave Wysochanski
1178c9e45581SDave Wysochanski return r;
11791da177e4SLinus Torvalds }
11801da177e4SLinus Torvalds
multipath_ctr(struct dm_target * ti,unsigned int argc,char ** argv)118186a3238cSHeinz Mauelshagen static int multipath_ctr(struct dm_target *ti, unsigned int argc, char **argv)
11821da177e4SLinus Torvalds {
1183498f0103SMike Snitzer /* target arguments */
11845916a22bSEric Biggers static const struct dm_arg _args[] = {
1185a490a07aSMike Snitzer {0, 1024, "invalid number of priority groups"},
1186a490a07aSMike Snitzer {0, 1024, "invalid initial priority group number"},
11871da177e4SLinus Torvalds };
11881da177e4SLinus Torvalds
11891da177e4SLinus Torvalds int r;
11901da177e4SLinus Torvalds struct multipath *m;
1191498f0103SMike Snitzer struct dm_arg_set as;
119286a3238cSHeinz Mauelshagen unsigned int pg_count = 0;
119386a3238cSHeinz Mauelshagen unsigned int next_pg_num;
1194be240ff5SAnatol Pomazau unsigned long flags;
11951da177e4SLinus Torvalds
11961da177e4SLinus Torvalds as.argc = argc;
11971da177e4SLinus Torvalds as.argv = argv;
11981da177e4SLinus Torvalds
1199e83068a5SMike Snitzer m = alloc_multipath(ti);
12001da177e4SLinus Torvalds if (!m) {
120172d94861SAlasdair G Kergon ti->error = "can't allocate multipath";
12021da177e4SLinus Torvalds return -EINVAL;
12031da177e4SLinus Torvalds }
12041da177e4SLinus Torvalds
120528f16c20SMicha³ Miros³aw r = parse_features(&as, m);
12061da177e4SLinus Torvalds if (r)
12071da177e4SLinus Torvalds goto bad;
12081da177e4SLinus Torvalds
1209e83068a5SMike Snitzer r = alloc_multipath_stage2(ti, m);
1210e83068a5SMike Snitzer if (r)
1211e83068a5SMike Snitzer goto bad;
1212e83068a5SMike Snitzer
121328f16c20SMicha³ Miros³aw r = parse_hw_handler(&as, m);
12141da177e4SLinus Torvalds if (r)
12151da177e4SLinus Torvalds goto bad;
12161da177e4SLinus Torvalds
1217498f0103SMike Snitzer r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
12181da177e4SLinus Torvalds if (r)
12191da177e4SLinus Torvalds goto bad;
12201da177e4SLinus Torvalds
1221498f0103SMike Snitzer r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
12221da177e4SLinus Torvalds if (r)
12231da177e4SLinus Torvalds goto bad;
12241da177e4SLinus Torvalds
1225a490a07aSMike Snitzer if ((!m->nr_priority_groups && next_pg_num) ||
1226a490a07aSMike Snitzer (m->nr_priority_groups && !next_pg_num)) {
1227a490a07aSMike Snitzer ti->error = "invalid initial priority group";
1228a490a07aSMike Snitzer r = -EINVAL;
1229a490a07aSMike Snitzer goto bad;
1230a490a07aSMike Snitzer }
1231a490a07aSMike Snitzer
12321da177e4SLinus Torvalds /* parse the priority groups */
12331da177e4SLinus Torvalds while (as.argc) {
12341da177e4SLinus Torvalds struct priority_group *pg;
123586a3238cSHeinz Mauelshagen unsigned int nr_valid_paths = atomic_read(&m->nr_valid_paths);
12361da177e4SLinus Torvalds
123728f16c20SMicha³ Miros³aw pg = parse_priority_group(&as, m);
123801460f35SBenjamin Marzinski if (IS_ERR(pg)) {
123901460f35SBenjamin Marzinski r = PTR_ERR(pg);
12401da177e4SLinus Torvalds goto bad;
12411da177e4SLinus Torvalds }
12421da177e4SLinus Torvalds
124391e968aaSMike Snitzer nr_valid_paths += pg->nr_pgpaths;
124491e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, nr_valid_paths);
124591e968aaSMike Snitzer
12461da177e4SLinus Torvalds list_add_tail(&pg->list, &m->priority_groups);
12471da177e4SLinus Torvalds pg_count++;
12481da177e4SLinus Torvalds pg->pg_num = pg_count;
12491da177e4SLinus Torvalds if (!--next_pg_num)
12501da177e4SLinus Torvalds m->next_pg = pg;
12511da177e4SLinus Torvalds }
12521da177e4SLinus Torvalds
12531da177e4SLinus Torvalds if (pg_count != m->nr_priority_groups) {
125472d94861SAlasdair G Kergon ti->error = "priority group count mismatch";
12551da177e4SLinus Torvalds r = -EINVAL;
12561da177e4SLinus Torvalds goto bad;
12571da177e4SLinus Torvalds }
12581da177e4SLinus Torvalds
1259be240ff5SAnatol Pomazau spin_lock_irqsave(&m->lock, flags);
1260be240ff5SAnatol Pomazau enable_nopath_timeout(m);
1261be240ff5SAnatol Pomazau spin_unlock_irqrestore(&m->lock, flags);
1262be240ff5SAnatol Pomazau
126355a62eefSAlasdair G Kergon ti->num_flush_bios = 1;
126455a62eefSAlasdair G Kergon ti->num_discard_bios = 1;
1265ac62d620SChristoph Hellwig ti->num_write_zeroes_bios = 1;
12668d47e659SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED)
1267bf661be1SMike Snitzer ti->per_io_data_size = multipath_per_bio_data_size();
1268eb8db831SChristoph Hellwig else
12698637a6bfSMike Snitzer ti->per_io_data_size = sizeof(struct dm_mpath_io);
12708627921fSMikulas Patocka
12711da177e4SLinus Torvalds return 0;
12721da177e4SLinus Torvalds
12731da177e4SLinus Torvalds bad:
12741da177e4SLinus Torvalds free_multipath(m);
12751da177e4SLinus Torvalds return r;
12761da177e4SLinus Torvalds }
12771da177e4SLinus Torvalds
multipath_wait_for_pg_init_completion(struct multipath * m)12782bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m)
12792bded7bdSKiyoshi Ueda {
12809f4c3f87SBart Van Assche DEFINE_WAIT(wait);
12812bded7bdSKiyoshi Ueda
12822bded7bdSKiyoshi Ueda while (1) {
12839f4c3f87SBart Van Assche prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
12842bded7bdSKiyoshi Ueda
128591e968aaSMike Snitzer if (!atomic_read(&m->pg_init_in_progress))
12862bded7bdSKiyoshi Ueda break;
12872bded7bdSKiyoshi Ueda
12882bded7bdSKiyoshi Ueda io_schedule();
12892bded7bdSKiyoshi Ueda }
12909f4c3f87SBart Van Assche finish_wait(&m->pg_init_wait, &wait);
12912bded7bdSKiyoshi Ueda }
12922bded7bdSKiyoshi Ueda
flush_multipath_work(struct multipath * m)12932bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m)
12941da177e4SLinus Torvalds {
1295848b8aefSMike Snitzer if (m->hw_handler_name) {
1296c322ee93SMike Snitzer unsigned long flags;
1297954a73d5SShiva Krishna Merla
1298c322ee93SMike Snitzer if (!atomic_read(&m->pg_init_in_progress))
1299c322ee93SMike Snitzer goto skip;
1300c322ee93SMike Snitzer
1301c322ee93SMike Snitzer spin_lock_irqsave(&m->lock, flags);
1302c322ee93SMike Snitzer if (atomic_read(&m->pg_init_in_progress) &&
1303c322ee93SMike Snitzer !test_and_set_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) {
1304c322ee93SMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
1305c322ee93SMike Snitzer
1306bab7cfc7SChandra Seetharaman flush_workqueue(kmpath_handlerd);
13072bded7bdSKiyoshi Ueda multipath_wait_for_pg_init_completion(m);
1308954a73d5SShiva Krishna Merla
1309c322ee93SMike Snitzer spin_lock_irqsave(&m->lock, flags);
1310518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
13116df400abSKiyoshi Ueda }
1312c322ee93SMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
1313c322ee93SMike Snitzer }
1314c322ee93SMike Snitzer skip:
1315935fcc56Swuzhouhui if (m->queue_mode == DM_TYPE_BIO_BASED)
1316935fcc56Swuzhouhui flush_work(&m->process_queued_bios);
1317848b8aefSMike Snitzer flush_work(&m->trigger_event);
1318848b8aefSMike Snitzer }
1319848b8aefSMike Snitzer
multipath_dtr(struct dm_target * ti)13206df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti)
13216df400abSKiyoshi Ueda {
13226df400abSKiyoshi Ueda struct multipath *m = ti->private;
13236df400abSKiyoshi Ueda
1324be240ff5SAnatol Pomazau disable_nopath_timeout(m);
13252bded7bdSKiyoshi Ueda flush_multipath_work(m);
13261da177e4SLinus Torvalds free_multipath(m);
13271da177e4SLinus Torvalds }
13281da177e4SLinus Torvalds
13291da177e4SLinus Torvalds /*
13301da177e4SLinus Torvalds * Take a path out of use.
13311da177e4SLinus Torvalds */
fail_path(struct pgpath * pgpath)13321da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath)
13331da177e4SLinus Torvalds {
13341da177e4SLinus Torvalds unsigned long flags;
13351da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m;
13361da177e4SLinus Torvalds
13371da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags);
13381da177e4SLinus Torvalds
13396680073dSKiyoshi Ueda if (!pgpath->is_active)
13401da177e4SLinus Torvalds goto out;
13411da177e4SLinus Torvalds
134204867370SMike Snitzer DMWARN("%s: Failing path %s.",
1343d4a512edSMike Snitzer dm_table_device_name(m->ti->table),
134404867370SMike Snitzer pgpath->path.dev->name);
13451da177e4SLinus Torvalds
13461da177e4SLinus Torvalds pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
1347be7d31ccSMike Snitzer pgpath->is_active = false;
13481da177e4SLinus Torvalds pgpath->fail_count++;
13491da177e4SLinus Torvalds
135091e968aaSMike Snitzer atomic_dec(&m->nr_valid_paths);
13511da177e4SLinus Torvalds
13521da177e4SLinus Torvalds if (pgpath == m->current_pgpath)
13531da177e4SLinus Torvalds m->current_pgpath = NULL;
13541da177e4SLinus Torvalds
1355b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
135691e968aaSMike Snitzer pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
1357b15546f9SMike Anderson
1358a7e8f7fbSTetsuo Handa queue_work(dm_mpath_wq, &m->trigger_event);
13591da177e4SLinus Torvalds
1360be240ff5SAnatol Pomazau enable_nopath_timeout(m);
1361be240ff5SAnatol Pomazau
13621da177e4SLinus Torvalds out:
13631da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags);
13641da177e4SLinus Torvalds
13651da177e4SLinus Torvalds return 0;
13661da177e4SLinus Torvalds }
13671da177e4SLinus Torvalds
13681da177e4SLinus Torvalds /*
13691da177e4SLinus Torvalds * Reinstate a previously-failed path
13701da177e4SLinus Torvalds */
reinstate_path(struct pgpath * pgpath)13711da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath)
13721da177e4SLinus Torvalds {
137363d832c3SHannes Reinecke int r = 0, run_queue = 0;
13741da177e4SLinus Torvalds unsigned long flags;
13751da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m;
137686a3238cSHeinz Mauelshagen unsigned int nr_valid_paths;
13771da177e4SLinus Torvalds
13781da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags);
13791da177e4SLinus Torvalds
13806680073dSKiyoshi Ueda if (pgpath->is_active)
13811da177e4SLinus Torvalds goto out;
13821da177e4SLinus Torvalds
138304867370SMike Snitzer DMWARN("%s: Reinstating path %s.",
1384d4a512edSMike Snitzer dm_table_device_name(m->ti->table),
138504867370SMike Snitzer pgpath->path.dev->name);
13861da177e4SLinus Torvalds
13871da177e4SLinus Torvalds r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
13881da177e4SLinus Torvalds if (r)
13891da177e4SLinus Torvalds goto out;
13901da177e4SLinus Torvalds
1391be7d31ccSMike Snitzer pgpath->is_active = true;
13921da177e4SLinus Torvalds
139391e968aaSMike Snitzer nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
139491e968aaSMike Snitzer if (nr_valid_paths == 1) {
13951da177e4SLinus Torvalds m->current_pgpath = NULL;
139663d832c3SHannes Reinecke run_queue = 1;
1397e54f77ddSChandra Seetharaman } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
13984e2d19e4SChandra Seetharaman if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
139991e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress);
1400e54f77ddSChandra Seetharaman }
14011da177e4SLinus Torvalds
1402b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
140391e968aaSMike Snitzer pgpath->path.dev->name, nr_valid_paths);
1404b15546f9SMike Anderson
1405fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event);
14061da177e4SLinus Torvalds
14071da177e4SLinus Torvalds out:
14081da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags);
140976e33fe4SMike Snitzer if (run_queue) {
141063d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table);
14117e48c768SMike Snitzer process_queued_io_list(m);
141276e33fe4SMike Snitzer }
14131da177e4SLinus Torvalds
1414be240ff5SAnatol Pomazau if (pgpath->is_active)
1415be240ff5SAnatol Pomazau disable_nopath_timeout(m);
1416be240ff5SAnatol Pomazau
14171da177e4SLinus Torvalds return r;
14181da177e4SLinus Torvalds }
14191da177e4SLinus Torvalds
14201da177e4SLinus Torvalds /*
14211da177e4SLinus Torvalds * Fail or reinstate all paths that match the provided struct dm_dev.
14221da177e4SLinus Torvalds */
action_dev(struct multipath * m,struct dm_dev * dev,action_fn action)14231da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev,
14241da177e4SLinus Torvalds action_fn action)
14251da177e4SLinus Torvalds {
142619040c0bSMike Snitzer int r = -EINVAL;
14271da177e4SLinus Torvalds struct pgpath *pgpath;
14281da177e4SLinus Torvalds struct priority_group *pg;
14291da177e4SLinus Torvalds
14301da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) {
14311da177e4SLinus Torvalds list_for_each_entry(pgpath, &pg->pgpaths, list) {
14321da177e4SLinus Torvalds if (pgpath->path.dev == dev)
14331da177e4SLinus Torvalds r = action(pgpath);
14341da177e4SLinus Torvalds }
14351da177e4SLinus Torvalds }
14361da177e4SLinus Torvalds
14371da177e4SLinus Torvalds return r;
14381da177e4SLinus Torvalds }
14391da177e4SLinus Torvalds
14401da177e4SLinus Torvalds /*
14411da177e4SLinus Torvalds * Temporarily try to avoid having to use the specified PG
14421da177e4SLinus Torvalds */
bypass_pg(struct multipath * m,struct priority_group * pg,bool bypassed)14431da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg,
1444be7d31ccSMike Snitzer bool bypassed)
14451da177e4SLinus Torvalds {
14461da177e4SLinus Torvalds unsigned long flags;
14471da177e4SLinus Torvalds
14481da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags);
14491da177e4SLinus Torvalds
14501da177e4SLinus Torvalds pg->bypassed = bypassed;
14511da177e4SLinus Torvalds m->current_pgpath = NULL;
14521da177e4SLinus Torvalds m->current_pg = NULL;
14531da177e4SLinus Torvalds
14541da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags);
14551da177e4SLinus Torvalds
1456fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event);
14571da177e4SLinus Torvalds }
14581da177e4SLinus Torvalds
14591da177e4SLinus Torvalds /*
14601da177e4SLinus Torvalds * Switch to using the specified PG from the next I/O that gets mapped
14611da177e4SLinus Torvalds */
switch_pg_num(struct multipath * m,const char * pgstr)14621da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr)
14631da177e4SLinus Torvalds {
14641da177e4SLinus Torvalds struct priority_group *pg;
146586a3238cSHeinz Mauelshagen unsigned int pgnum;
14661da177e4SLinus Torvalds unsigned long flags;
146731998ef1SMikulas Patocka char dummy;
14681da177e4SLinus Torvalds
146931998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1470cc5bd925Stang.junhui !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
14711c131886SHeinz Mauelshagen DMWARN("invalid PG number supplied to %s", __func__);
14721da177e4SLinus Torvalds return -EINVAL;
14731da177e4SLinus Torvalds }
14741da177e4SLinus Torvalds
14751da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags);
14761da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) {
1477be7d31ccSMike Snitzer pg->bypassed = false;
14781da177e4SLinus Torvalds if (--pgnum)
14791da177e4SLinus Torvalds continue;
14801da177e4SLinus Torvalds
14811da177e4SLinus Torvalds m->current_pgpath = NULL;
14821da177e4SLinus Torvalds m->current_pg = NULL;
14831da177e4SLinus Torvalds m->next_pg = pg;
14841da177e4SLinus Torvalds }
14851da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags);
14861da177e4SLinus Torvalds
1487fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event);
14881da177e4SLinus Torvalds return 0;
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds
14911da177e4SLinus Torvalds /*
14921da177e4SLinus Torvalds * Set/clear bypassed status of a PG.
14931da177e4SLinus Torvalds * PGs are numbered upwards from 1 in the order they were declared.
14941da177e4SLinus Torvalds */
bypass_pg_num(struct multipath * m,const char * pgstr,bool bypassed)1495be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
14961da177e4SLinus Torvalds {
14971da177e4SLinus Torvalds struct priority_group *pg;
149886a3238cSHeinz Mauelshagen unsigned int pgnum;
149931998ef1SMikulas Patocka char dummy;
15001da177e4SLinus Torvalds
150131998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
1502cc5bd925Stang.junhui !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
15031da177e4SLinus Torvalds DMWARN("invalid PG number supplied to bypass_pg");
15041da177e4SLinus Torvalds return -EINVAL;
15051da177e4SLinus Torvalds }
15061da177e4SLinus Torvalds
15071da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) {
15081da177e4SLinus Torvalds if (!--pgnum)
15091da177e4SLinus Torvalds break;
15101da177e4SLinus Torvalds }
15111da177e4SLinus Torvalds
15121da177e4SLinus Torvalds bypass_pg(m, pg, bypassed);
15131da177e4SLinus Torvalds return 0;
15141da177e4SLinus Torvalds }
15151da177e4SLinus Torvalds
15161da177e4SLinus Torvalds /*
1517c9e45581SDave Wysochanski * Should we retry pg_init immediately?
1518c9e45581SDave Wysochanski */
pg_init_limit_reached(struct multipath * m,struct pgpath * pgpath)1519be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1520c9e45581SDave Wysochanski {
1521c9e45581SDave Wysochanski unsigned long flags;
1522be7d31ccSMike Snitzer bool limit_reached = false;
1523c9e45581SDave Wysochanski
1524c9e45581SDave Wysochanski spin_lock_irqsave(&m->lock, flags);
1525c9e45581SDave Wysochanski
152691e968aaSMike Snitzer if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
152791e968aaSMike Snitzer !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
1528518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
1529c9e45581SDave Wysochanski else
1530be7d31ccSMike Snitzer limit_reached = true;
1531c9e45581SDave Wysochanski
1532c9e45581SDave Wysochanski spin_unlock_irqrestore(&m->lock, flags);
1533c9e45581SDave Wysochanski
1534c9e45581SDave Wysochanski return limit_reached;
1535c9e45581SDave Wysochanski }
1536c9e45581SDave Wysochanski
pg_init_done(void * data,int errors)15373ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors)
1538cfae5c9bSChandra Seetharaman {
153983c0d5d5SMoger, Babu struct pgpath *pgpath = data;
1540cfae5c9bSChandra Seetharaman struct priority_group *pg = pgpath->pg;
1541cfae5c9bSChandra Seetharaman struct multipath *m = pg->m;
1542cfae5c9bSChandra Seetharaman unsigned long flags;
1543be7d31ccSMike Snitzer bool delay_retry = false;
1544cfae5c9bSChandra Seetharaman
1545cfae5c9bSChandra Seetharaman /* device or driver problems */
1546cfae5c9bSChandra Seetharaman switch (errors) {
1547cfae5c9bSChandra Seetharaman case SCSI_DH_OK:
1548cfae5c9bSChandra Seetharaman break;
1549cfae5c9bSChandra Seetharaman case SCSI_DH_NOSYS:
1550cfae5c9bSChandra Seetharaman if (!m->hw_handler_name) {
1551cfae5c9bSChandra Seetharaman errors = 0;
1552cfae5c9bSChandra Seetharaman break;
1553cfae5c9bSChandra Seetharaman }
1554f7b934c8SMoger, Babu DMERR("Could not failover the device: Handler scsi_dh_%s "
1555f7b934c8SMoger, Babu "Error %d.", m->hw_handler_name, errors);
1556cfae5c9bSChandra Seetharaman /*
1557cfae5c9bSChandra Seetharaman * Fail path for now, so we do not ping pong
1558cfae5c9bSChandra Seetharaman */
1559cfae5c9bSChandra Seetharaman fail_path(pgpath);
1560cfae5c9bSChandra Seetharaman break;
1561cfae5c9bSChandra Seetharaman case SCSI_DH_DEV_TEMP_BUSY:
1562cfae5c9bSChandra Seetharaman /*
1563cfae5c9bSChandra Seetharaman * Probably doing something like FW upgrade on the
1564cfae5c9bSChandra Seetharaman * controller so try the other pg.
1565cfae5c9bSChandra Seetharaman */
1566be7d31ccSMike Snitzer bypass_pg(m, pg, true);
1567cfae5c9bSChandra Seetharaman break;
1568cfae5c9bSChandra Seetharaman case SCSI_DH_RETRY:
15694e2d19e4SChandra Seetharaman /* Wait before retrying. */
15704ecc5081Szhengbin delay_retry = true;
1571df561f66SGustavo A. R. Silva fallthrough;
1572cfae5c9bSChandra Seetharaman case SCSI_DH_IMM_RETRY:
1573cfae5c9bSChandra Seetharaman case SCSI_DH_RES_TEMP_UNAVAIL:
1574cfae5c9bSChandra Seetharaman if (pg_init_limit_reached(m, pgpath))
1575cfae5c9bSChandra Seetharaman fail_path(pgpath);
1576cfae5c9bSChandra Seetharaman errors = 0;
1577cfae5c9bSChandra Seetharaman break;
1578ec31f3f7SMike Snitzer case SCSI_DH_DEV_OFFLINED:
1579cfae5c9bSChandra Seetharaman default:
1580cfae5c9bSChandra Seetharaman /*
1581cfae5c9bSChandra Seetharaman * We probably do not want to fail the path for a device
1582cfae5c9bSChandra Seetharaman * error, but this is what the old dm did. In future
1583cfae5c9bSChandra Seetharaman * patches we can do more advanced handling.
1584cfae5c9bSChandra Seetharaman */
1585cfae5c9bSChandra Seetharaman fail_path(pgpath);
1586cfae5c9bSChandra Seetharaman }
1587cfae5c9bSChandra Seetharaman
1588cfae5c9bSChandra Seetharaman spin_lock_irqsave(&m->lock, flags);
1589cfae5c9bSChandra Seetharaman if (errors) {
1590e54f77ddSChandra Seetharaman if (pgpath == m->current_pgpath) {
1591cfae5c9bSChandra Seetharaman DMERR("Could not failover device. Error %d.", errors);
1592cfae5c9bSChandra Seetharaman m->current_pgpath = NULL;
1593cfae5c9bSChandra Seetharaman m->current_pg = NULL;
1594e54f77ddSChandra Seetharaman }
1595518257b1SMike Snitzer } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
1596be7d31ccSMike Snitzer pg->bypassed = false;
1597cfae5c9bSChandra Seetharaman
159891e968aaSMike Snitzer if (atomic_dec_return(&m->pg_init_in_progress) > 0)
1599d0259bf0SKiyoshi Ueda /* Activations of other paths are still on going */
1600d0259bf0SKiyoshi Ueda goto out;
1601d0259bf0SKiyoshi Ueda
1602518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
1603518257b1SMike Snitzer if (delay_retry)
1604518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1605518257b1SMike Snitzer else
1606518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
1607518257b1SMike Snitzer
16083e9f1be1SHannes Reinecke if (__pg_init_all_paths(m))
16093e9f1be1SHannes Reinecke goto out;
16103e9f1be1SHannes Reinecke }
1611518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags);
1612d0259bf0SKiyoshi Ueda
16137e48c768SMike Snitzer process_queued_io_list(m);
161476e33fe4SMike Snitzer
16152bded7bdSKiyoshi Ueda /*
16162bded7bdSKiyoshi Ueda * Wake up any thread waiting to suspend.
16172bded7bdSKiyoshi Ueda */
16182bded7bdSKiyoshi Ueda wake_up(&m->pg_init_wait);
16192bded7bdSKiyoshi Ueda
1620d0259bf0SKiyoshi Ueda out:
1621cfae5c9bSChandra Seetharaman spin_unlock_irqrestore(&m->lock, flags);
1622cfae5c9bSChandra Seetharaman }
1623cfae5c9bSChandra Seetharaman
activate_or_offline_path(struct pgpath * pgpath)162489bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath)
1625bab7cfc7SChandra Seetharaman {
1626f10e06b7SMike Snitzer struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1627bab7cfc7SChandra Seetharaman
1628f10e06b7SMike Snitzer if (pgpath->is_active && !blk_queue_dying(q))
1629f10e06b7SMike Snitzer scsi_dh_activate(q, pg_init_done, pgpath);
16303a017509SHannes Reinecke else
16313a017509SHannes Reinecke pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
1632bab7cfc7SChandra Seetharaman }
1633bab7cfc7SChandra Seetharaman
activate_path_work(struct work_struct * work)163489bfce76SBart Van Assche static void activate_path_work(struct work_struct *work)
163589bfce76SBart Van Assche {
163689bfce76SBart Van Assche struct pgpath *pgpath =
163789bfce76SBart Van Assche container_of(work, struct pgpath, activate_path.work);
163889bfce76SBart Van Assche
163989bfce76SBart Van Assche activate_or_offline_path(pgpath);
164089bfce76SBart Van Assche }
164189bfce76SBart Van Assche
multipath_end_io(struct dm_target * ti,struct request * clone,blk_status_t error,union map_info * map_context)1642b79f10eeSChristoph Hellwig static int multipath_end_io(struct dm_target *ti, struct request *clone,
16432a842acaSChristoph Hellwig blk_status_t error, union map_info *map_context)
16441da177e4SLinus Torvalds {
1645b79f10eeSChristoph Hellwig struct dm_mpath_io *mpio = get_mpio(map_context);
1646b79f10eeSChristoph Hellwig struct pgpath *pgpath = mpio->pgpath;
16477ed8578aSChristoph Hellwig int r = DM_ENDIO_DONE;
1648b79f10eeSChristoph Hellwig
1649f40c67f0SKiyoshi Ueda /*
1650f40c67f0SKiyoshi Ueda * We don't queue any clone request inside the multipath target
1651f40c67f0SKiyoshi Ueda * during end I/O handling, since those clone requests don't have
1652f40c67f0SKiyoshi Ueda * bio clones. If we queue them inside the multipath target,
1653f40c67f0SKiyoshi Ueda * we need to make bio clones, that requires memory allocation.
16544cc96131SMike Snitzer * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
1655f40c67f0SKiyoshi Ueda * don't have bio clones.)
1656f40c67f0SKiyoshi Ueda * Instead of queueing the clone request here, we queue the original
1657f40c67f0SKiyoshi Ueda * request into dm core, which will remake a clone request and
1658f40c67f0SKiyoshi Ueda * clone bios for it and resubmit it later.
1659f40c67f0SKiyoshi Ueda */
1660a1275677SKeith Busch if (error && blk_path_error(error)) {
1661b79f10eeSChristoph Hellwig struct multipath *m = ti->private;
16621da177e4SLinus Torvalds
1663ac514ffcSMike Snitzer if (error == BLK_STS_RESOURCE)
1664ac514ffcSMike Snitzer r = DM_ENDIO_DELAY_REQUEUE;
1665ac514ffcSMike Snitzer else
16667ed8578aSChristoph Hellwig r = DM_ENDIO_REQUEUE;
16671da177e4SLinus Torvalds
1668b79f10eeSChristoph Hellwig if (pgpath)
1669b79f10eeSChristoph Hellwig fail_path(pgpath);
16701da177e4SLinus Torvalds
167173265f3fSMike Snitzer if (!atomic_read(&m->nr_valid_paths) &&
1672c1fd0abeSMike Snitzer !must_push_back_rq(m)) {
16732a842acaSChristoph Hellwig if (error == BLK_STS_IOERR)
167418a482f5SChristoph Hellwig dm_report_EIO(m);
16757ed8578aSChristoph Hellwig /* complete with the original error */
16767ed8578aSChristoph Hellwig r = DM_ENDIO_DONE;
16777ed8578aSChristoph Hellwig }
16781da177e4SLinus Torvalds }
16791da177e4SLinus Torvalds
16801da177e4SLinus Torvalds if (pgpath) {
1681b79f10eeSChristoph Hellwig struct path_selector *ps = &pgpath->pg->ps;
1682b79f10eeSChristoph Hellwig
16831da177e4SLinus Torvalds if (ps->type->end_io)
1684087615bfSGabriel Krisman Bertazi ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes,
1685087615bfSGabriel Krisman Bertazi clone->io_start_time_ns);
16861da177e4SLinus Torvalds }
16871da177e4SLinus Torvalds
16887ed8578aSChristoph Hellwig return r;
16891da177e4SLinus Torvalds }
16901da177e4SLinus Torvalds
multipath_end_io_bio(struct dm_target * ti,struct bio * clone,blk_status_t * error)16914e4cbee9SChristoph Hellwig static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone,
16924e4cbee9SChristoph Hellwig blk_status_t *error)
169376e33fe4SMike Snitzer {
169414ef1e48SChristoph Hellwig struct multipath *m = ti->private;
169514ef1e48SChristoph Hellwig struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
169614ef1e48SChristoph Hellwig struct pgpath *pgpath = mpio->pgpath;
169776e33fe4SMike Snitzer unsigned long flags;
16981be56909SChristoph Hellwig int r = DM_ENDIO_DONE;
169976e33fe4SMike Snitzer
1700a1275677SKeith Busch if (!*error || !blk_path_error(*error))
170114ef1e48SChristoph Hellwig goto done;
170276e33fe4SMike Snitzer
170314ef1e48SChristoph Hellwig if (pgpath)
170414ef1e48SChristoph Hellwig fail_path(pgpath);
170576e33fe4SMike Snitzer
1706a271a89cSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) {
1707a271a89cSMike Snitzer spin_lock_irqsave(&m->lock, flags);
1708a271a89cSMike Snitzer if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
1709a862e4e2SMike Snitzer if (__must_push_back(m)) {
1710c1fd0abeSMike Snitzer r = DM_ENDIO_REQUEUE;
1711c1fd0abeSMike Snitzer } else {
171218a482f5SChristoph Hellwig dm_report_EIO(m);
17134e4cbee9SChristoph Hellwig *error = BLK_STS_IOERR;
1714c1fd0abeSMike Snitzer }
1715a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
171614ef1e48SChristoph Hellwig goto done;
171718a482f5SChristoph Hellwig }
171876e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
1719a271a89cSMike Snitzer }
172076e33fe4SMike Snitzer
1721f45f1186SMike Snitzer multipath_queue_bio(m, clone);
17221be56909SChristoph Hellwig r = DM_ENDIO_INCOMPLETE;
172314ef1e48SChristoph Hellwig done:
172476e33fe4SMike Snitzer if (pgpath) {
172514ef1e48SChristoph Hellwig struct path_selector *ps = &pgpath->pg->ps;
172614ef1e48SChristoph Hellwig
172776e33fe4SMike Snitzer if (ps->type->end_io)
1728087615bfSGabriel Krisman Bertazi ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes,
1729c06dfd12SGabriel Krisman Bertazi (mpio->start_time_ns ?:
1730c06dfd12SGabriel Krisman Bertazi dm_start_time_ns_from_clone(clone)));
173176e33fe4SMike Snitzer }
173276e33fe4SMike Snitzer
17331be56909SChristoph Hellwig return r;
173476e33fe4SMike Snitzer }
173576e33fe4SMike Snitzer
17361da177e4SLinus Torvalds /*
1737553ec94cSMike Snitzer * Suspend with flush can't complete until all the I/O is processed
1738553ec94cSMike Snitzer * so if the last path fails we must error any remaining I/O.
1739553ec94cSMike Snitzer * - Note that if the freeze_bdev fails while suspending, the
1740436d4108SAlasdair G Kergon * queue_if_no_path state is lost - userspace should reset it.
1741553ec94cSMike Snitzer * Otherwise, during noflush suspend, queue_if_no_path will not change.
17421da177e4SLinus Torvalds */
multipath_presuspend(struct dm_target * ti)17431da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti)
17441da177e4SLinus Torvalds {
17457943bd6dSMike Snitzer struct multipath *m = ti->private;
17461da177e4SLinus Torvalds
1747553ec94cSMike Snitzer /* FIXME: bio-based shouldn't need to always disable queue_if_no_path */
1748553ec94cSMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED || !dm_noflush_suspending(m->ti))
17494c3f4838SMike Snitzer queue_if_no_path(m, false, true, __func__);
17501da177e4SLinus Torvalds }
17511da177e4SLinus Torvalds
multipath_postsuspend(struct dm_target * ti)17526df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti)
17536df400abSKiyoshi Ueda {
17546380f26fSMike Anderson struct multipath *m = ti->private;
17556380f26fSMike Anderson
17566380f26fSMike Anderson mutex_lock(&m->work_mutex);
17572bded7bdSKiyoshi Ueda flush_multipath_work(m);
17586380f26fSMike Anderson mutex_unlock(&m->work_mutex);
17596df400abSKiyoshi Ueda }
17606df400abSKiyoshi Ueda
1761436d4108SAlasdair G Kergon /*
1762436d4108SAlasdair G Kergon * Restore the queue_if_no_path setting.
1763436d4108SAlasdair G Kergon */
multipath_resume(struct dm_target * ti)17641da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti)
17651da177e4SLinus Torvalds {
17667943bd6dSMike Snitzer struct multipath *m = ti->private;
17671814f2e3SMike Snitzer unsigned long flags;
17681da177e4SLinus Torvalds
17691814f2e3SMike Snitzer spin_lock_irqsave(&m->lock, flags);
1770553ec94cSMike Snitzer if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) {
1771553ec94cSMike Snitzer set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
1772553ec94cSMike Snitzer clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
1773553ec94cSMike Snitzer }
17744c3f4838SMike Snitzer
17754c3f4838SMike Snitzer DMDEBUG("%s: %s finished; QIFNP = %d; SQIFNP = %d",
1776d4a512edSMike Snitzer dm_table_device_name(m->ti->table), __func__,
17774c3f4838SMike Snitzer test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags),
17784c3f4838SMike Snitzer test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags));
17794c3f4838SMike Snitzer
17801814f2e3SMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
17811da177e4SLinus Torvalds }
17821da177e4SLinus Torvalds
17831da177e4SLinus Torvalds /*
17841da177e4SLinus Torvalds * Info output has the following format:
17851da177e4SLinus Torvalds * num_multipath_feature_args [multipath_feature_args]*
17861da177e4SLinus Torvalds * num_handler_status_args [handler_status_args]*
17871da177e4SLinus Torvalds * num_groups init_group_number
17881da177e4SLinus Torvalds * [A|D|E num_ps_status_args [ps_status_args]*
17891da177e4SLinus Torvalds * num_paths num_selector_args
17901da177e4SLinus Torvalds * [path_dev A|F fail_count [selector_args]* ]+ ]+
17911da177e4SLinus Torvalds *
17921da177e4SLinus Torvalds * Table output has the following format (identical to the constructor string):
17931da177e4SLinus Torvalds * num_feature_args [features_args]*
17941da177e4SLinus Torvalds * num_handler_args hw_handler [hw_handler_args]*
17951da177e4SLinus Torvalds * num_groups init_group_number
17961da177e4SLinus Torvalds * [priority selector-name num_ps_args [ps_args]*
17971da177e4SLinus Torvalds * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
17981da177e4SLinus Torvalds */
multipath_status(struct dm_target * ti,status_type_t type,unsigned int status_flags,char * result,unsigned int maxlen)1799fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type,
180086a3238cSHeinz Mauelshagen unsigned int status_flags, char *result, unsigned int maxlen)
18011da177e4SLinus Torvalds {
180233ace4caSTushar Sugandhi int sz = 0, pg_counter, pgpath_counter;
18031da177e4SLinus Torvalds unsigned long flags;
18047943bd6dSMike Snitzer struct multipath *m = ti->private;
18051da177e4SLinus Torvalds struct priority_group *pg;
18061da177e4SLinus Torvalds struct pgpath *p;
180786a3238cSHeinz Mauelshagen unsigned int pg_num;
18081da177e4SLinus Torvalds char state;
18091da177e4SLinus Torvalds
18101da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags);
18111da177e4SLinus Torvalds
18121da177e4SLinus Torvalds /* Features */
18131da177e4SLinus Torvalds if (type == STATUSTYPE_INFO)
181491e968aaSMike Snitzer DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
181591e968aaSMike Snitzer atomic_read(&m->pg_init_count));
1816c9e45581SDave Wysochanski else {
1817518257b1SMike Snitzer DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
18184e2d19e4SChandra Seetharaman (m->pg_init_retries > 0) * 2 +
1819a58a935dSMike Snitzer (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1820e83068a5SMike Snitzer test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
1821e83068a5SMike Snitzer (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
1822e83068a5SMike Snitzer
1823518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
1824c9e45581SDave Wysochanski DMEMIT("queue_if_no_path ");
1825c9e45581SDave Wysochanski if (m->pg_init_retries)
1826c9e45581SDave Wysochanski DMEMIT("pg_init_retries %u ", m->pg_init_retries);
18274e2d19e4SChandra Seetharaman if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
18284e2d19e4SChandra Seetharaman DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
1829518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
1830a58a935dSMike Snitzer DMEMIT("retain_attached_hw_handler ");
1831e83068a5SMike Snitzer if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
1832e83068a5SMike Snitzer switch (m->queue_mode) {
1833e83068a5SMike Snitzer case DM_TYPE_BIO_BASED:
1834e83068a5SMike Snitzer DMEMIT("queue_mode bio ");
1835e83068a5SMike Snitzer break;
18367e0d574fSBart Van Assche default:
18377e0d574fSBart Van Assche WARN_ON_ONCE(true);
18387e0d574fSBart Van Assche break;
1839e83068a5SMike Snitzer }
1840e83068a5SMike Snitzer }
1841c9e45581SDave Wysochanski }
18421da177e4SLinus Torvalds
1843cfae5c9bSChandra Seetharaman if (!m->hw_handler_name || type == STATUSTYPE_INFO)
18441da177e4SLinus Torvalds DMEMIT("0 ");
18451da177e4SLinus Torvalds else
1846cfae5c9bSChandra Seetharaman DMEMIT("1 %s ", m->hw_handler_name);
18471da177e4SLinus Torvalds
18481da177e4SLinus Torvalds DMEMIT("%u ", m->nr_priority_groups);
18491da177e4SLinus Torvalds
18501da177e4SLinus Torvalds if (m->next_pg)
18511da177e4SLinus Torvalds pg_num = m->next_pg->pg_num;
18521da177e4SLinus Torvalds else if (m->current_pg)
18531da177e4SLinus Torvalds pg_num = m->current_pg->pg_num;
18541da177e4SLinus Torvalds else
1855a490a07aSMike Snitzer pg_num = (m->nr_priority_groups ? 1 : 0);
18561da177e4SLinus Torvalds
18571da177e4SLinus Torvalds DMEMIT("%u ", pg_num);
18581da177e4SLinus Torvalds
18591da177e4SLinus Torvalds switch (type) {
18601da177e4SLinus Torvalds case STATUSTYPE_INFO:
18611da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) {
18621da177e4SLinus Torvalds if (pg->bypassed)
18631da177e4SLinus Torvalds state = 'D'; /* Disabled */
18641da177e4SLinus Torvalds else if (pg == m->current_pg)
18651da177e4SLinus Torvalds state = 'A'; /* Currently Active */
18661da177e4SLinus Torvalds else
18671da177e4SLinus Torvalds state = 'E'; /* Enabled */
18681da177e4SLinus Torvalds
18691da177e4SLinus Torvalds DMEMIT("%c ", state);
18701da177e4SLinus Torvalds
18711da177e4SLinus Torvalds if (pg->ps.type->status)
18721da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type,
18731da177e4SLinus Torvalds result + sz,
18741da177e4SLinus Torvalds maxlen - sz);
18751da177e4SLinus Torvalds else
18761da177e4SLinus Torvalds DMEMIT("0 ");
18771da177e4SLinus Torvalds
18781da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths,
18791da177e4SLinus Torvalds pg->ps.type->info_args);
18801da177e4SLinus Torvalds
18811da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) {
18821da177e4SLinus Torvalds DMEMIT("%s %s %u ", p->path.dev->name,
18836680073dSKiyoshi Ueda p->is_active ? "A" : "F",
18841da177e4SLinus Torvalds p->fail_count);
18851da177e4SLinus Torvalds if (pg->ps.type->status)
18861da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps,
18871da177e4SLinus Torvalds &p->path, type, result + sz,
18881da177e4SLinus Torvalds maxlen - sz);
18891da177e4SLinus Torvalds }
18901da177e4SLinus Torvalds }
18911da177e4SLinus Torvalds break;
18921da177e4SLinus Torvalds
18931da177e4SLinus Torvalds case STATUSTYPE_TABLE:
18941da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) {
18951da177e4SLinus Torvalds DMEMIT("%s ", pg->ps.type->name);
18961da177e4SLinus Torvalds
18971da177e4SLinus Torvalds if (pg->ps.type->status)
18981da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type,
18991da177e4SLinus Torvalds result + sz,
19001da177e4SLinus Torvalds maxlen - sz);
19011da177e4SLinus Torvalds else
19021da177e4SLinus Torvalds DMEMIT("0 ");
19031da177e4SLinus Torvalds
19041da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths,
19051da177e4SLinus Torvalds pg->ps.type->table_args);
19061da177e4SLinus Torvalds
19071da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) {
19081da177e4SLinus Torvalds DMEMIT("%s ", p->path.dev->name);
19091da177e4SLinus Torvalds if (pg->ps.type->status)
19101da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps,
19111da177e4SLinus Torvalds &p->path, type, result + sz,
19121da177e4SLinus Torvalds maxlen - sz);
19131da177e4SLinus Torvalds }
19141da177e4SLinus Torvalds }
19151da177e4SLinus Torvalds break;
19168ec45662STushar Sugandhi
19178ec45662STushar Sugandhi case STATUSTYPE_IMA:
191833ace4caSTushar Sugandhi sz = 0; /*reset the result pointer*/
191933ace4caSTushar Sugandhi
19208ec45662STushar Sugandhi DMEMIT_TARGET_NAME_VERSION(ti->type);
192133ace4caSTushar Sugandhi DMEMIT(",nr_priority_groups=%u", m->nr_priority_groups);
192233ace4caSTushar Sugandhi
192333ace4caSTushar Sugandhi pg_counter = 0;
19248ec45662STushar Sugandhi list_for_each_entry(pg, &m->priority_groups, list) {
19258ec45662STushar Sugandhi if (pg->bypassed)
19268ec45662STushar Sugandhi state = 'D'; /* Disabled */
19278ec45662STushar Sugandhi else if (pg == m->current_pg)
19288ec45662STushar Sugandhi state = 'A'; /* Currently Active */
19298ec45662STushar Sugandhi else
19308ec45662STushar Sugandhi state = 'E'; /* Enabled */
193133ace4caSTushar Sugandhi DMEMIT(",pg_state_%d=%c", pg_counter, state);
193233ace4caSTushar Sugandhi DMEMIT(",nr_pgpaths_%d=%u", pg_counter, pg->nr_pgpaths);
193333ace4caSTushar Sugandhi DMEMIT(",path_selector_name_%d=%s", pg_counter, pg->ps.type->name);
19348ec45662STushar Sugandhi
193533ace4caSTushar Sugandhi pgpath_counter = 0;
19368ec45662STushar Sugandhi list_for_each_entry(p, &pg->pgpaths, list) {
193733ace4caSTushar Sugandhi DMEMIT(",path_name_%d_%d=%s,is_active_%d_%d=%c,fail_count_%d_%d=%u",
193833ace4caSTushar Sugandhi pg_counter, pgpath_counter, p->path.dev->name,
193933ace4caSTushar Sugandhi pg_counter, pgpath_counter, p->is_active ? 'A' : 'F',
194033ace4caSTushar Sugandhi pg_counter, pgpath_counter, p->fail_count);
19418ec45662STushar Sugandhi if (pg->ps.type->status) {
194233ace4caSTushar Sugandhi DMEMIT(",path_selector_status_%d_%d=",
194333ace4caSTushar Sugandhi pg_counter, pgpath_counter);
19448ec45662STushar Sugandhi sz += pg->ps.type->status(&pg->ps, &p->path,
19458ec45662STushar Sugandhi type, result + sz,
19468ec45662STushar Sugandhi maxlen - sz);
19478ec45662STushar Sugandhi }
194833ace4caSTushar Sugandhi pgpath_counter++;
19498ec45662STushar Sugandhi }
195033ace4caSTushar Sugandhi pg_counter++;
19518ec45662STushar Sugandhi }
19528ec45662STushar Sugandhi DMEMIT(";");
19538ec45662STushar Sugandhi break;
19541da177e4SLinus Torvalds }
19551da177e4SLinus Torvalds
19561da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags);
19571da177e4SLinus Torvalds }
19581da177e4SLinus Torvalds
multipath_message(struct dm_target * ti,unsigned int argc,char ** argv,char * result,unsigned int maxlen)195986a3238cSHeinz Mauelshagen static int multipath_message(struct dm_target *ti, unsigned int argc, char **argv,
196086a3238cSHeinz Mauelshagen char *result, unsigned int maxlen)
19611da177e4SLinus Torvalds {
19626380f26fSMike Anderson int r = -EINVAL;
19631da177e4SLinus Torvalds struct dm_dev *dev;
19647943bd6dSMike Snitzer struct multipath *m = ti->private;
19651da177e4SLinus Torvalds action_fn action;
1966be240ff5SAnatol Pomazau unsigned long flags;
19671da177e4SLinus Torvalds
19686380f26fSMike Anderson mutex_lock(&m->work_mutex);
19696380f26fSMike Anderson
1970c2f3d24bSKiyoshi Ueda if (dm_suspended(ti)) {
1971c2f3d24bSKiyoshi Ueda r = -EBUSY;
1972c2f3d24bSKiyoshi Ueda goto out;
1973c2f3d24bSKiyoshi Ueda }
1974c2f3d24bSKiyoshi Ueda
19751da177e4SLinus Torvalds if (argc == 1) {
1976498f0103SMike Snitzer if (!strcasecmp(argv[0], "queue_if_no_path")) {
19774c3f4838SMike Snitzer r = queue_if_no_path(m, true, false, __func__);
1978be240ff5SAnatol Pomazau spin_lock_irqsave(&m->lock, flags);
1979be240ff5SAnatol Pomazau enable_nopath_timeout(m);
1980be240ff5SAnatol Pomazau spin_unlock_irqrestore(&m->lock, flags);
19816380f26fSMike Anderson goto out;
1982498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
19834c3f4838SMike Snitzer r = queue_if_no_path(m, false, false, __func__);
1984be240ff5SAnatol Pomazau disable_nopath_timeout(m);
19856380f26fSMike Anderson goto out;
19866380f26fSMike Anderson }
19871da177e4SLinus Torvalds }
19881da177e4SLinus Torvalds
19896380f26fSMike Anderson if (argc != 2) {
1990a356e426SJose Castillo DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
19916380f26fSMike Anderson goto out;
19926380f26fSMike Anderson }
19931da177e4SLinus Torvalds
1994498f0103SMike Snitzer if (!strcasecmp(argv[0], "disable_group")) {
1995be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], true);
19966380f26fSMike Anderson goto out;
1997498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "enable_group")) {
1998be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], false);
19996380f26fSMike Anderson goto out;
2000498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "switch_group")) {
20016380f26fSMike Anderson r = switch_pg_num(m, argv[1]);
20026380f26fSMike Anderson goto out;
2003498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "reinstate_path"))
20041da177e4SLinus Torvalds action = reinstate_path;
2005498f0103SMike Snitzer else if (!strcasecmp(argv[0], "fail_path"))
20061da177e4SLinus Torvalds action = fail_path;
20076380f26fSMike Anderson else {
2008a356e426SJose Castillo DMWARN("Unrecognised multipath message received: %s", argv[0]);
20096380f26fSMike Anderson goto out;
20106380f26fSMike Anderson }
20111da177e4SLinus Torvalds
20128215d6ecSNikanth Karthikesan r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
20131da177e4SLinus Torvalds if (r) {
201472d94861SAlasdair G Kergon DMWARN("message: error getting device %s",
20151da177e4SLinus Torvalds argv[1]);
20166380f26fSMike Anderson goto out;
20171da177e4SLinus Torvalds }
20181da177e4SLinus Torvalds
20191da177e4SLinus Torvalds r = action_dev(m, dev, action);
20201da177e4SLinus Torvalds
20211da177e4SLinus Torvalds dm_put_device(ti, dev);
20221da177e4SLinus Torvalds
20236380f26fSMike Anderson out:
20246380f26fSMike Anderson mutex_unlock(&m->work_mutex);
20251da177e4SLinus Torvalds return r;
20261da177e4SLinus Torvalds }
20271da177e4SLinus Torvalds
multipath_prepare_ioctl(struct dm_target * ti,struct block_device ** bdev)2028e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti,
20295bd5e8d8SMike Snitzer struct block_device **bdev)
20309af4aa30SMilan Broz {
203135991652SMikulas Patocka struct multipath *m = ti->private;
2032564dbb13SMike Snitzer struct pgpath *pgpath;
203369cea0d4SMike Snitzer unsigned long flags;
203435991652SMikulas Patocka int r;
203535991652SMikulas Patocka
2036564dbb13SMike Snitzer pgpath = READ_ONCE(m->current_pgpath);
2037374117adSMike Snitzer if (!pgpath || !mpath_double_check_test_bit(MPATHF_QUEUE_IO, m))
2038564dbb13SMike Snitzer pgpath = choose_pgpath(m, 0);
20399af4aa30SMilan Broz
2040564dbb13SMike Snitzer if (pgpath) {
2041374117adSMike Snitzer if (!mpath_double_check_test_bit(MPATHF_QUEUE_IO, m)) {
2042564dbb13SMike Snitzer *bdev = pgpath->path.dev->bdev;
204343e43c9eSJunichi Nomura r = 0;
204443e43c9eSJunichi Nomura } else {
204543e43c9eSJunichi Nomura /* pg_init has not started or completed */
20466c182cd8SHannes Reinecke r = -ENOTCONN;
204743e43c9eSJunichi Nomura }
204843e43c9eSJunichi Nomura } else {
204943e43c9eSJunichi Nomura /* No path is available */
2050a271a89cSMike Snitzer r = -EIO;
2051a271a89cSMike Snitzer spin_lock_irqsave(&m->lock, flags);
2052518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
205343e43c9eSJunichi Nomura r = -ENOTCONN;
2054a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
205543e43c9eSJunichi Nomura }
20569af4aa30SMilan Broz
20575bbbfdf6SJunichi Nomura if (r == -ENOTCONN) {
2058506458efSWill Deacon if (!READ_ONCE(m->current_pg)) {
20593e9f1be1SHannes Reinecke /* Path status changed, redo selection */
20602da1610aSMike Snitzer (void) choose_pgpath(m, 0);
20613e9f1be1SHannes Reinecke }
206269cea0d4SMike Snitzer spin_lock_irqsave(&m->lock, flags);
2063518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
206469cea0d4SMike Snitzer (void) __pg_init_all_paths(m);
206569cea0d4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
206663d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table);
20677e48c768SMike Snitzer process_queued_io_list(m);
20683e9f1be1SHannes Reinecke }
206935991652SMikulas Patocka
2070e56f81e0SChristoph Hellwig /*
2071e56f81e0SChristoph Hellwig * Only pass ioctls through if the device sizes match exactly.
2072e56f81e0SChristoph Hellwig */
20736dcbb52cSChristoph Hellwig if (!r && ti->len != bdev_nr_sectors((*bdev)))
2074e56f81e0SChristoph Hellwig return 1;
2075e56f81e0SChristoph Hellwig return r;
20769af4aa30SMilan Broz }
20779af4aa30SMilan Broz
multipath_iterate_devices(struct dm_target * ti,iterate_devices_callout_fn fn,void * data)2078af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti,
2079af4874e0SMike Snitzer iterate_devices_callout_fn fn, void *data)
2080af4874e0SMike Snitzer {
2081af4874e0SMike Snitzer struct multipath *m = ti->private;
2082af4874e0SMike Snitzer struct priority_group *pg;
2083af4874e0SMike Snitzer struct pgpath *p;
2084af4874e0SMike Snitzer int ret = 0;
2085af4874e0SMike Snitzer
2086af4874e0SMike Snitzer list_for_each_entry(pg, &m->priority_groups, list) {
2087af4874e0SMike Snitzer list_for_each_entry(p, &pg->pgpaths, list) {
20885dea271bSMike Snitzer ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
2089af4874e0SMike Snitzer if (ret)
2090af4874e0SMike Snitzer goto out;
2091af4874e0SMike Snitzer }
2092af4874e0SMike Snitzer }
2093af4874e0SMike Snitzer
2094af4874e0SMike Snitzer out:
2095af4874e0SMike Snitzer return ret;
2096af4874e0SMike Snitzer }
2097af4874e0SMike Snitzer
pgpath_busy(struct pgpath * pgpath)20989f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath)
2099f40c67f0SKiyoshi Ueda {
2100f40c67f0SKiyoshi Ueda struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
2101f40c67f0SKiyoshi Ueda
210252b09914SMike Snitzer return blk_lld_busy(q);
2103f40c67f0SKiyoshi Ueda }
2104f40c67f0SKiyoshi Ueda
2105f40c67f0SKiyoshi Ueda /*
2106f40c67f0SKiyoshi Ueda * We return "busy", only when we can map I/Os but underlying devices
2107f40c67f0SKiyoshi Ueda * are busy (so even if we map I/Os now, the I/Os will wait on
2108f40c67f0SKiyoshi Ueda * the underlying queue).
2109f40c67f0SKiyoshi Ueda * In other words, if we want to kill I/Os or queue them inside us
2110f40c67f0SKiyoshi Ueda * due to map unavailability, we don't return "busy". Otherwise,
2111f40c67f0SKiyoshi Ueda * dm core won't give us the I/Os and we can't do what we want.
2112f40c67f0SKiyoshi Ueda */
multipath_busy(struct dm_target * ti)2113f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti)
2114f40c67f0SKiyoshi Ueda {
2115be7d31ccSMike Snitzer bool busy = false, has_active = false;
2116f40c67f0SKiyoshi Ueda struct multipath *m = ti->private;
21172da1610aSMike Snitzer struct priority_group *pg, *next_pg;
2118f40c67f0SKiyoshi Ueda struct pgpath *pgpath;
2119f40c67f0SKiyoshi Ueda
2120b88efd43SMike Snitzer /* pg_init in progress */
2121b88efd43SMike Snitzer if (atomic_read(&m->pg_init_in_progress))
21222da1610aSMike Snitzer return true;
21232da1610aSMike Snitzer
2124b88efd43SMike Snitzer /* no paths available, for blk-mq: rely on IO mapping to delay requeue */
2125a271a89cSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) {
2126a271a89cSMike Snitzer unsigned long flags;
21270ef0b471SHeinz Mauelshagen
2128a271a89cSMike Snitzer spin_lock_irqsave(&m->lock, flags);
2129a271a89cSMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
2130a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
2131953923c0SMike Snitzer return (m->queue_mode != DM_TYPE_REQUEST_BASED);
2132a271a89cSMike Snitzer }
2133a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags);
2134a271a89cSMike Snitzer }
2135b88efd43SMike Snitzer
2136f40c67f0SKiyoshi Ueda /* Guess which priority_group will be used at next mapping time */
2137506458efSWill Deacon pg = READ_ONCE(m->current_pg);
2138506458efSWill Deacon next_pg = READ_ONCE(m->next_pg);
2139506458efSWill Deacon if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg))
21402da1610aSMike Snitzer pg = next_pg;
21412da1610aSMike Snitzer
21422da1610aSMike Snitzer if (!pg) {
2143f40c67f0SKiyoshi Ueda /*
2144f40c67f0SKiyoshi Ueda * We don't know which pg will be used at next mapping time.
21452da1610aSMike Snitzer * We don't call choose_pgpath() here to avoid to trigger
2146f40c67f0SKiyoshi Ueda * pg_init just by busy checking.
2147f40c67f0SKiyoshi Ueda * So we don't know whether underlying devices we will be using
2148f40c67f0SKiyoshi Ueda * at next mapping time are busy or not. Just try mapping.
2149f40c67f0SKiyoshi Ueda */
21502da1610aSMike Snitzer return busy;
21512da1610aSMike Snitzer }
2152f40c67f0SKiyoshi Ueda
2153f40c67f0SKiyoshi Ueda /*
2154f40c67f0SKiyoshi Ueda * If there is one non-busy active path at least, the path selector
2155f40c67f0SKiyoshi Ueda * will be able to select it. So we consider such a pg as not busy.
2156f40c67f0SKiyoshi Ueda */
2157be7d31ccSMike Snitzer busy = true;
21582da1610aSMike Snitzer list_for_each_entry(pgpath, &pg->pgpaths, list) {
2159f40c67f0SKiyoshi Ueda if (pgpath->is_active) {
2160be7d31ccSMike Snitzer has_active = true;
21619f54cec5SMike Snitzer if (!pgpath_busy(pgpath)) {
2162be7d31ccSMike Snitzer busy = false;
2163f40c67f0SKiyoshi Ueda break;
2164f40c67f0SKiyoshi Ueda }
2165f40c67f0SKiyoshi Ueda }
21662da1610aSMike Snitzer }
2167f40c67f0SKiyoshi Ueda
21682da1610aSMike Snitzer if (!has_active) {
2169f40c67f0SKiyoshi Ueda /*
2170f40c67f0SKiyoshi Ueda * No active path in this pg, so this pg won't be used and
2171f40c67f0SKiyoshi Ueda * the current_pg will be changed at next mapping time.
2172f40c67f0SKiyoshi Ueda * We need to try mapping to determine it.
2173f40c67f0SKiyoshi Ueda */
2174be7d31ccSMike Snitzer busy = false;
21752da1610aSMike Snitzer }
2176f40c67f0SKiyoshi Ueda
2177f40c67f0SKiyoshi Ueda return busy;
2178f40c67f0SKiyoshi Ueda }
2179f40c67f0SKiyoshi Ueda
2180a4a82ce3SHeinz Mauelshagen /*
2181a4a82ce3SHeinz Mauelshagen *---------------------------------------------------------------
21821da177e4SLinus Torvalds * Module setup
2183a4a82ce3SHeinz Mauelshagen *---------------------------------------------------------------
2184a4a82ce3SHeinz Mauelshagen */
21851da177e4SLinus Torvalds static struct target_type multipath_target = {
21861da177e4SLinus Torvalds .name = "multipath",
2187636be424SMike Snitzer .version = {1, 14, 0},
21888c5c1473SSteffen Maier .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE |
21898c5c1473SSteffen Maier DM_TARGET_PASSES_INTEGRITY,
21901da177e4SLinus Torvalds .module = THIS_MODULE,
21911da177e4SLinus Torvalds .ctr = multipath_ctr,
21921da177e4SLinus Torvalds .dtr = multipath_dtr,
2193e5863d9aSMike Snitzer .clone_and_map_rq = multipath_clone_and_map,
2194e5863d9aSMike Snitzer .release_clone_rq = multipath_release_clone,
2195f40c67f0SKiyoshi Ueda .rq_end_io = multipath_end_io,
219676e33fe4SMike Snitzer .map = multipath_map_bio,
219776e33fe4SMike Snitzer .end_io = multipath_end_io_bio,
219876e33fe4SMike Snitzer .presuspend = multipath_presuspend,
219976e33fe4SMike Snitzer .postsuspend = multipath_postsuspend,
220076e33fe4SMike Snitzer .resume = multipath_resume,
220176e33fe4SMike Snitzer .status = multipath_status,
220276e33fe4SMike Snitzer .message = multipath_message,
220376e33fe4SMike Snitzer .prepare_ioctl = multipath_prepare_ioctl,
220476e33fe4SMike Snitzer .iterate_devices = multipath_iterate_devices,
220576e33fe4SMike Snitzer .busy = multipath_busy,
220676e33fe4SMike Snitzer };
220776e33fe4SMike Snitzer
dm_multipath_init(void)22081da177e4SLinus Torvalds static int __init dm_multipath_init(void)
22091da177e4SLinus Torvalds {
2210a7e8f7fbSTetsuo Handa int r = -ENOMEM;
22111da177e4SLinus Torvalds
22124d4d66abSTejun Heo kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
2213c557308eSAlasdair G Kergon if (!kmultipathd) {
22140cd33124SAlasdair G Kergon DMERR("failed to create workqueue kmpathd");
2215ff658e9cSJohannes Thumshirn goto bad_alloc_kmultipathd;
2216c557308eSAlasdair G Kergon }
2217c557308eSAlasdair G Kergon
2218bab7cfc7SChandra Seetharaman /*
2219bab7cfc7SChandra Seetharaman * A separate workqueue is used to handle the device handlers
2220bab7cfc7SChandra Seetharaman * to avoid overloading existing workqueue. Overloading the
2221bab7cfc7SChandra Seetharaman * old workqueue would also create a bottleneck in the
2222bab7cfc7SChandra Seetharaman * path of the storage hardware device activation.
2223bab7cfc7SChandra Seetharaman */
22244d4d66abSTejun Heo kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
22254d4d66abSTejun Heo WQ_MEM_RECLAIM);
2226bab7cfc7SChandra Seetharaman if (!kmpath_handlerd) {
2227bab7cfc7SChandra Seetharaman DMERR("failed to create workqueue kmpath_handlerd");
2228ff658e9cSJohannes Thumshirn goto bad_alloc_kmpath_handlerd;
2229bab7cfc7SChandra Seetharaman }
2230bab7cfc7SChandra Seetharaman
2231a7e8f7fbSTetsuo Handa dm_mpath_wq = alloc_workqueue("dm_mpath_wq", 0, 0);
2232a7e8f7fbSTetsuo Handa if (!dm_mpath_wq) {
2233a7e8f7fbSTetsuo Handa DMERR("failed to create workqueue dm_mpath_wq");
2234a7e8f7fbSTetsuo Handa goto bad_alloc_dm_mpath_wq;
2235a7e8f7fbSTetsuo Handa }
2236a7e8f7fbSTetsuo Handa
22377e6358d2Smonty_pavel@sina.com r = dm_register_target(&multipath_target);
2238*b362c733SYangtao Li if (r < 0)
22397e6358d2Smonty_pavel@sina.com goto bad_register_target;
22407e6358d2Smonty_pavel@sina.com
2241ff658e9cSJohannes Thumshirn return 0;
2242ff658e9cSJohannes Thumshirn
22437e6358d2Smonty_pavel@sina.com bad_register_target:
2244a7e8f7fbSTetsuo Handa destroy_workqueue(dm_mpath_wq);
2245a7e8f7fbSTetsuo Handa bad_alloc_dm_mpath_wq:
22467e6358d2Smonty_pavel@sina.com destroy_workqueue(kmpath_handlerd);
2247ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd:
2248ff658e9cSJohannes Thumshirn destroy_workqueue(kmultipathd);
2249ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd:
22501da177e4SLinus Torvalds return r;
22511da177e4SLinus Torvalds }
22521da177e4SLinus Torvalds
dm_multipath_exit(void)22531da177e4SLinus Torvalds static void __exit dm_multipath_exit(void)
22541da177e4SLinus Torvalds {
2255a7e8f7fbSTetsuo Handa destroy_workqueue(dm_mpath_wq);
2256bab7cfc7SChandra Seetharaman destroy_workqueue(kmpath_handlerd);
2257c557308eSAlasdair G Kergon destroy_workqueue(kmultipathd);
2258c557308eSAlasdair G Kergon
225910d3bd09SMikulas Patocka dm_unregister_target(&multipath_target);
22601da177e4SLinus Torvalds }
22611da177e4SLinus Torvalds
22621da177e4SLinus Torvalds module_init(dm_multipath_init);
22631da177e4SLinus Torvalds module_exit(dm_multipath_exit);
22641da177e4SLinus Torvalds
22656a808034SHeinz Mauelshagen module_param_named(queue_if_no_path_timeout_secs, queue_if_no_path_timeout_secs, ulong, 0644);
2266be240ff5SAnatol Pomazau MODULE_PARM_DESC(queue_if_no_path_timeout_secs, "No available paths queue IO timeout in seconds");
2267be240ff5SAnatol Pomazau
22681da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target");
22691da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
22701da177e4SLinus Torvalds MODULE_LICENSE("GPL");
2271