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