blob: cfe55910699d91fa1fbf124dee8ec432adcc0d09 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Sistina Software Limited.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4 *
5 * This file is released under the GPL.
6 */
7
Mikulas Patocka586e80e2008-10-21 17:44:59 +01008#include <linux/device-mapper.h>
9
Mike Snitzerf4790822013-09-12 18:06:12 -040010#include "dm.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "dm-path-selector.h"
Mike Andersonb15546f2007-10-19 22:48:02 +010012#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Mike Snitzere5863d92014-12-17 21:08:12 -050014#include <linux/blkdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/ctype.h>
16#include <linux/init.h>
17#include <linux/mempool.h>
18#include <linux/module.h>
19#include <linux/pagemap.h>
20#include <linux/slab.h>
21#include <linux/time.h>
22#include <linux/workqueue.h>
Mikulas Patocka35991652012-06-03 00:29:58 +010023#include <linux/delay.h>
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070024#include <scsi/scsi_dh.h>
Arun Sharma600634972011-07-26 16:09:06 -070025#include <linux/atomic.h>
Mike Snitzer78ce23b2016-01-31 17:38:28 -050026#include <linux/blk-mq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Alasdair G Kergon72d94862006-06-26 00:27:35 -070028#define DM_MSG_PREFIX "multipath"
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000029#define DM_PG_INIT_DELAY_MSECS 2000
30#define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32/* Path properties */
33struct pgpath {
34 struct list_head list;
35
36 struct priority_group *pg; /* Owning PG */
Kiyoshi Ueda66800732008-10-10 13:36:58 +010037 unsigned is_active; /* Path status */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 unsigned fail_count; /* Cumulative failure count */
39
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080040 struct dm_path path;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000041 struct delayed_work activate_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042};
43
44#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
45
46/*
47 * Paths are grouped into Priority Groups and numbered from 1 upwards.
48 * Each has a path selector which controls which path gets used.
49 */
50struct priority_group {
51 struct list_head list;
52
53 struct multipath *m; /* Owning multipath instance */
54 struct path_selector ps;
55
56 unsigned pg_num; /* Reference number */
57 unsigned bypassed; /* Temporarily bypass this PG? */
58
59 unsigned nr_pgpaths; /* Number of paths in PG */
60 struct list_head pgpaths;
61};
62
63/* Multipath context */
64struct multipath {
65 struct list_head list;
66 struct dm_target *ti;
67
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070068 const char *hw_handler_name;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -070069 char *hw_handler_params;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000070
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010071 spinlock_t lock;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 unsigned nr_priority_groups;
74 struct list_head priority_groups;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000075
76 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 unsigned pg_init_required; /* pg_init needs calling? */
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -070079 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000080 unsigned pg_init_delay_retry; /* Delay pg_init retry? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 unsigned nr_valid_paths; /* Total number of usable paths */
83 struct pgpath *current_pgpath;
84 struct priority_group *current_pg;
85 struct priority_group *next_pg; /* Switch to this PG if set */
86 unsigned repeat_count; /* I/Os left before calling PS again */
87
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010088 unsigned queue_io:1; /* Must we queue all I/O? */
89 unsigned queue_if_no_path:1; /* Queue I/O if last path fails? */
90 unsigned saved_queue_if_no_path:1; /* Saved state during suspension */
Mike Snitzera58a9352012-07-27 15:08:04 +010091 unsigned retain_attached_hw_handler:1; /* If there's already a hw_handler present, don't change it. */
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +000092 unsigned pg_init_disabled:1; /* pg_init is not currently allowed */
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010093
Dave Wysochanskic9e45582007-10-19 22:47:53 +010094 unsigned pg_init_retries; /* Number of times to retry pg_init */
95 unsigned pg_init_count; /* Number of times pg_init called */
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000096 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 struct work_struct trigger_event;
99
100 /*
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100101 * We must use a mempool of dm_mpath_io structs so that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 * can resubmit bios on error.
103 */
104 mempool_t *mpio_pool;
Mike Anderson6380f262009-12-10 23:52:21 +0000105
106 struct mutex work_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109/*
110 * Context information attached to each bio we process.
111 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100112struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct pgpath *pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100114 size_t nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115};
116
117typedef int (*action_fn) (struct pgpath *pgpath);
118
Christoph Lametere18b8902006-12-06 20:33:20 -0800119static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700121static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000122static void trigger_event(struct work_struct *work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700123static void activate_path(struct work_struct *work);
Hannes Reineckee8099172014-02-28 15:33:44 +0100124static int __pgpath_busy(struct pgpath *pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126
127/*-----------------------------------------------
128 * Allocation routines
129 *-----------------------------------------------*/
130
131static struct pgpath *alloc_pgpath(void)
132{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700133 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Mike Anderson224cb3e2008-08-29 09:36:09 +0200135 if (pgpath) {
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100136 pgpath->is_active = 1;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000137 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 return pgpath;
141}
142
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100143static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 kfree(pgpath);
146}
147
148static struct priority_group *alloc_priority_group(void)
149{
150 struct priority_group *pg;
151
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700152 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700154 if (pg)
155 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 return pg;
158}
159
160static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
161{
162 struct pgpath *pgpath, *tmp;
163
164 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
165 list_del(&pgpath->list);
166 dm_put_device(ti, pgpath->path.dev);
167 free_pgpath(pgpath);
168 }
169}
170
171static void free_priority_group(struct priority_group *pg,
172 struct dm_target *ti)
173{
174 struct path_selector *ps = &pg->ps;
175
176 if (ps->type) {
177 ps->type->destroy(ps);
178 dm_put_path_selector(ps->type);
179 }
180
181 free_pgpaths(&pg->pgpaths, ti);
182 kfree(pg);
183}
184
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500185static struct multipath *alloc_multipath(struct dm_target *ti, bool use_blk_mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct multipath *m;
188
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700189 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 INIT_LIST_HEAD(&m->priority_groups);
192 spin_lock_init(&m->lock);
193 m->queue_io = 1;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000194 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
David Howellsc4028952006-11-22 14:57:56 +0000195 INIT_WORK(&m->trigger_event, trigger_event);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000196 init_waitqueue_head(&m->pg_init_wait);
Mike Anderson6380f262009-12-10 23:52:21 +0000197 mutex_init(&m->work_mutex);
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500198
199 m->mpio_pool = NULL;
200 if (!use_blk_mq) {
201 unsigned min_ios = dm_get_reserved_rq_based_ios();
202
203 m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache);
204 if (!m->mpio_pool) {
205 kfree(m);
206 return NULL;
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500209
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700210 m->ti = ti;
211 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
214 return m;
215}
216
217static void free_multipath(struct multipath *m)
218{
219 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
222 list_del(&pg->list);
223 free_priority_group(pg, m->ti);
224 }
225
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700226 kfree(m->hw_handler_name);
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700227 kfree(m->hw_handler_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 mempool_destroy(m->mpio_pool);
229 kfree(m);
230}
231
Mike Snitzer2eff1922016-02-03 09:13:14 -0500232static struct dm_mpath_io *get_mpio(union map_info *info)
233{
234 return info->ptr;
235}
236
237static struct dm_mpath_io *set_mpio(struct multipath *m, union map_info *info)
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100238{
239 struct dm_mpath_io *mpio;
240
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500241 if (!m->mpio_pool) {
242 /* Use blk-mq pdu memory requested via per_io_data_size */
Mike Snitzer2eff1922016-02-03 09:13:14 -0500243 mpio = get_mpio(info);
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500244 memset(mpio, 0, sizeof(*mpio));
245 return mpio;
246 }
247
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100248 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
249 if (!mpio)
Mike Snitzer2eff1922016-02-03 09:13:14 -0500250 return NULL;
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100251
252 memset(mpio, 0, sizeof(*mpio));
253 info->ptr = mpio;
254
Mike Snitzer2eff1922016-02-03 09:13:14 -0500255 return mpio;
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100256}
257
Mike Snitzer2eff1922016-02-03 09:13:14 -0500258static void clear_request_fn_mpio(struct multipath *m, union map_info *info)
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100259{
Mike Snitzer2eff1922016-02-03 09:13:14 -0500260 /* Only needed for non blk-mq (.request_fn) multipath */
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500261 if (m->mpio_pool) {
262 struct dm_mpath_io *mpio = info->ptr;
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100263
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500264 info->ptr = NULL;
265 mempool_free(mpio, m->mpio_pool);
266 }
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100267}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269/*-----------------------------------------------
270 * Path selection
271 *-----------------------------------------------*/
272
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100273static int __pg_init_all_paths(struct multipath *m)
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000274{
275 struct pgpath *pgpath;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000276 unsigned long pg_init_delay = 0;
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000277
Hannes Reinecke17f4ff42014-02-28 15:33:42 +0100278 if (m->pg_init_in_progress || m->pg_init_disabled)
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100279 return 0;
Hannes Reinecke17f4ff42014-02-28 15:33:42 +0100280
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000281 m->pg_init_count++;
282 m->pg_init_required = 0;
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100283
284 /* Check here to reset pg_init_required */
285 if (!m->current_pg)
286 return 0;
287
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000288 if (m->pg_init_delay_retry)
289 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
290 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000291 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
292 /* Skip failed paths */
293 if (!pgpath->is_active)
294 continue;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000295 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
296 pg_init_delay))
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000297 m->pg_init_in_progress++;
298 }
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +0100299 return m->pg_init_in_progress;
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000300}
301
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
303{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 m->current_pg = pgpath->pg;
305
306 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700307 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 m->pg_init_required = 1;
309 m->queue_io = 1;
310 } else {
311 m->pg_init_required = 0;
312 m->queue_io = 0;
313 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100314
315 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100318static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
319 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800321 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100323 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (!path)
325 return -ENXIO;
326
327 m->current_pgpath = path_to_pgpath(path);
328
329 if (m->current_pg != pg)
330 __switch_pg(m, m->current_pgpath);
331
332 return 0;
333}
334
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100335static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
337 struct priority_group *pg;
338 unsigned bypassed = 1;
339
Benjamin Marzinski1f271972014-08-13 13:53:42 -0500340 if (!m->nr_valid_paths) {
341 m->queue_io = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 goto failed;
Benjamin Marzinski1f271972014-08-13 13:53:42 -0500343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
345 /* Were we instructed to switch PG? */
346 if (m->next_pg) {
347 pg = m->next_pg;
348 m->next_pg = NULL;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100349 if (!__choose_path_in_pg(m, pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return;
351 }
352
353 /* Don't change PG until it has no remaining paths */
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100354 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 return;
356
357 /*
358 * Loop through priority groups until we find a valid path.
359 * First time we skip PGs marked 'bypassed'.
Mike Christief220fd42012-06-03 00:29:45 +0100360 * Second time we only try the ones we skipped, but set
361 * pg_init_delay_retry so we do not hammer controllers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 */
363 do {
364 list_for_each_entry(pg, &m->priority_groups, list) {
365 if (pg->bypassed == bypassed)
366 continue;
Mike Christief220fd42012-06-03 00:29:45 +0100367 if (!__choose_path_in_pg(m, pg, nr_bytes)) {
368 if (!bypassed)
369 m->pg_init_delay_retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return;
Mike Christief220fd42012-06-03 00:29:45 +0100371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373 } while (bypassed--);
374
375failed:
376 m->current_pgpath = NULL;
377 m->current_pg = NULL;
378}
379
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800380/*
381 * Check whether bios must be queued in the device-mapper core rather
382 * than here in the target.
383 *
384 * m->lock must be held on entry.
385 *
386 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
387 * same value then we are not between multipath_presuspend()
388 * and multipath_resume() calls and we have no need to check
389 * for the DMF_NOFLUSH_SUSPENDING flag.
390 */
391static int __must_push_back(struct multipath *m)
392{
Hannes Reineckee8099172014-02-28 15:33:44 +0100393 return (m->queue_if_no_path ||
394 (m->queue_if_no_path != m->saved_queue_if_no_path &&
395 dm_noflush_suspending(m->ti)));
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800396}
397
Hannes Reinecke36fcffc2014-02-28 15:33:47 +0100398/*
399 * Map cloned requests
400 */
Mike Snitzere5863d92014-12-17 21:08:12 -0500401static int __multipath_map(struct dm_target *ti, struct request *clone,
402 union map_info *map_context,
403 struct request *rq, struct request **__clone)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
Mike Snitzer7943bd62016-02-02 21:53:15 -0500405 struct multipath *m = ti->private;
Hannes Reineckee3bde042014-02-28 15:33:46 +0100406 int r = DM_MAPIO_REQUEUE;
Mike Snitzere5863d92014-12-17 21:08:12 -0500407 size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100409 struct block_device *bdev;
Hannes Reineckee3bde042014-02-28 15:33:46 +0100410 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600412 spin_lock_irq(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /* Do we need to select a new pgpath? */
415 if (!m->current_pgpath ||
416 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100417 __choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 pgpath = m->current_pgpath;
420
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100421 if (!pgpath) {
422 if (!__must_push_back(m))
423 r = -EIO; /* Failed */
424 goto out_unlock;
Mike Snitzer6afbc012014-07-08 11:55:09 -0400425 } else if (m->queue_io || m->pg_init_required) {
Hannes Reineckee3bde042014-02-28 15:33:46 +0100426 __pg_init_all_paths(m);
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100427 goto out_unlock;
428 }
Mike Snitzer6afbc012014-07-08 11:55:09 -0400429
Mike Snitzer2eff1922016-02-03 09:13:14 -0500430 mpio = set_mpio(m, map_context);
431 if (!mpio)
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100432 /* ENOMEM, requeue */
433 goto out_unlock;
434
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100435 mpio->pgpath = pgpath;
436 mpio->nr_bytes = nr_bytes;
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600437
438 bdev = pgpath->path.dev->bdev;
439
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600440 spin_unlock_irq(&m->lock);
441
Mike Snitzere5863d92014-12-17 21:08:12 -0500442 if (clone) {
Mike Snitzerc5248f72016-02-20 14:02:49 -0500443 /*
444 * Old request-based interface: allocated clone is passed in.
445 * Used by: .request_fn stacked on .request_fn path(s).
446 */
Mike Snitzere5863d92014-12-17 21:08:12 -0500447 clone->q = bdev_get_queue(bdev);
448 clone->rq_disk = bdev->bd_disk;
449 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
450 } else {
Mike Snitzereca7ee62016-02-20 13:45:38 -0500451 /*
452 * blk-mq request-based interface; used by both:
453 * .request_fn stacked on blk-mq path(s) and
454 * blk-mq stacked on blk-mq path(s).
455 */
Mike Snitzer78ce23b2016-01-31 17:38:28 -0500456 *__clone = blk_mq_alloc_request(bdev_get_queue(bdev),
457 rq_data_dir(rq), BLK_MQ_REQ_NOWAIT);
Mike Snitzer4c6dd532015-05-27 15:23:56 -0400458 if (IS_ERR(*__clone)) {
Mike Snitzere5863d92014-12-17 21:08:12 -0500459 /* ENOMEM, requeue */
Mike Snitzer2eff1922016-02-03 09:13:14 -0500460 clear_request_fn_mpio(m, map_context);
Mike Snitzere5863d92014-12-17 21:08:12 -0500461 return r;
Mike Snitzer4c6dd532015-05-27 15:23:56 -0400462 }
Mike Snitzere5863d92014-12-17 21:08:12 -0500463 (*__clone)->bio = (*__clone)->biotail = NULL;
464 (*__clone)->rq_disk = bdev->bd_disk;
465 (*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT;
466 }
467
Mike Snitzer9bf59a62014-02-28 15:33:48 +0100468 if (pgpath->pg->ps.type->start_io)
469 pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
470 &pgpath->path,
471 nr_bytes);
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600472 return DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Hannes Reineckee3bde042014-02-28 15:33:46 +0100474out_unlock:
Keith Busch2eb6e1e2014-10-17 17:46:36 -0600475 spin_unlock_irq(&m->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
477 return r;
478}
479
Mike Snitzere5863d92014-12-17 21:08:12 -0500480static int multipath_map(struct dm_target *ti, struct request *clone,
481 union map_info *map_context)
482{
483 return __multipath_map(ti, clone, map_context, NULL, NULL);
484}
485
486static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
487 union map_info *map_context,
488 struct request **clone)
489{
490 return __multipath_map(ti, NULL, map_context, rq, clone);
491}
492
493static void multipath_release_clone(struct request *clone)
494{
Mike Snitzer78ce23b2016-01-31 17:38:28 -0500495 blk_mq_free_request(clone);
Mike Snitzere5863d92014-12-17 21:08:12 -0500496}
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498/*
499 * If we run out of usable paths, should we queue I/O or error it?
500 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700501static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
502 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 unsigned long flags;
505
506 spin_lock_irqsave(&m->lock, flags);
507
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700508 if (save_old_value)
509 m->saved_queue_if_no_path = m->queue_if_no_path;
510 else
511 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 m->queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 spin_unlock_irqrestore(&m->lock, flags);
514
Hannes Reinecke63d832c2014-05-26 14:45:39 +0200515 if (!queue_if_no_path)
516 dm_table_run_md_queue_async(m->ti->table);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return 0;
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521/*
522 * An event is triggered whenever a path is taken out of use.
523 * Includes path failure and PG bypass.
524 */
David Howellsc4028952006-11-22 14:57:56 +0000525static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
David Howellsc4028952006-11-22 14:57:56 +0000527 struct multipath *m =
528 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 dm_table_event(m->ti->table);
531}
532
533/*-----------------------------------------------------------------
534 * Constructor/argument parsing:
535 * <#multipath feature args> [<arg>]*
536 * <#hw_handler args> [hw_handler [<arg>]*]
537 * <#priority groups>
538 * <initial priority group>
539 * [<selector> <#selector args> [<arg>]*
540 * <#paths> <#per-path selector args>
541 * [<path> [<arg>]* ]+ ]+
542 *---------------------------------------------------------------*/
Mike Snitzer498f0102011-08-02 12:32:04 +0100543static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct dm_target *ti)
545{
546 int r;
547 struct path_selector_type *pst;
548 unsigned ps_argc;
549
Mike Snitzer498f0102011-08-02 12:32:04 +0100550 static struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700551 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 };
553
Mike Snitzer498f0102011-08-02 12:32:04 +0100554 pst = dm_get_path_selector(dm_shift_arg(as));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700556 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -EINVAL;
558 }
559
Mike Snitzer498f0102011-08-02 12:32:04 +0100560 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100561 if (r) {
562 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 r = pst->create(&pg->ps, ps_argc, as->argv);
567 if (r) {
568 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700569 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return r;
571 }
572
573 pg->ps.type = pst;
Mike Snitzer498f0102011-08-02 12:32:04 +0100574 dm_consume_args(as, ps_argc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 return 0;
577}
578
Mike Snitzer498f0102011-08-02 12:32:04 +0100579static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct dm_target *ti)
581{
582 int r;
583 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700584 struct multipath *m = ti->private;
Mike Snitzera58a9352012-07-27 15:08:04 +0100585 struct request_queue *q = NULL;
586 const char *attached_handler_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* we need at least a path arg */
589 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700590 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100591 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
594 p = alloc_pgpath();
595 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100596 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Mike Snitzer498f0102011-08-02 12:32:04 +0100598 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000599 &p->path.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700601 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 goto bad;
603 }
604
Mike Snitzera58a9352012-07-27 15:08:04 +0100605 if (m->retain_attached_hw_handler || m->hw_handler_name)
606 q = bdev_get_queue(p->path.dev->bdev);
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100607
Mike Snitzera58a9352012-07-27 15:08:04 +0100608 if (m->retain_attached_hw_handler) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200609retain:
Mike Snitzera58a9352012-07-27 15:08:04 +0100610 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
611 if (attached_handler_name) {
612 /*
613 * Reset hw_handler_name to match the attached handler
614 * and clear any hw_handler_params associated with the
615 * ignored handler.
616 *
617 * NB. This modifies the table line to show the actual
618 * handler instead of the original table passed in.
619 */
620 kfree(m->hw_handler_name);
621 m->hw_handler_name = attached_handler_name;
622
623 kfree(m->hw_handler_params);
624 m->hw_handler_params = NULL;
625 }
626 }
627
628 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100629 r = scsi_dh_attach(q, m->hw_handler_name);
630 if (r == -EBUSY) {
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200631 char b[BDEVNAME_SIZE];
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100632
Christoph Hellwig1bab0de2015-08-27 14:16:54 +0200633 printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
634 bdevname(p->path.dev->bdev, b));
635 goto retain;
636 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700637 if (r < 0) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100638 ti->error = "error attaching hardware handler";
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700639 dm_put_device(ti, p->path.dev);
640 goto bad;
641 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700642
643 if (m->hw_handler_params) {
644 r = scsi_dh_set_params(q, m->hw_handler_params);
645 if (r < 0) {
646 ti->error = "unable to set hardware "
647 "handler parameters";
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700648 dm_put_device(ti, p->path.dev);
649 goto bad;
650 }
651 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700652 }
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
655 if (r) {
656 dm_put_device(ti, p->path.dev);
657 goto bad;
658 }
659
660 return p;
661
662 bad:
663 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100664 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Mike Snitzer498f0102011-08-02 12:32:04 +0100667static struct priority_group *parse_priority_group(struct dm_arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700668 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Mike Snitzer498f0102011-08-02 12:32:04 +0100670 static struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700671 {1, 1024, "invalid number of paths"},
672 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 };
674
675 int r;
Mike Snitzer498f0102011-08-02 12:32:04 +0100676 unsigned i, nr_selector_args, nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700678 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 if (as->argc < 2) {
681 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100682 ti->error = "not enough priority group arguments";
683 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685
686 pg = alloc_priority_group();
687 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700688 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100689 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691 pg->m = m;
692
693 r = parse_path_selector(as, pg, ti);
694 if (r)
695 goto bad;
696
697 /*
698 * read the paths
699 */
Mike Snitzer498f0102011-08-02 12:32:04 +0100700 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (r)
702 goto bad;
703
Mike Snitzer498f0102011-08-02 12:32:04 +0100704 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (r)
706 goto bad;
707
Mike Snitzer498f0102011-08-02 12:32:04 +0100708 nr_args = 1 + nr_selector_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 for (i = 0; i < pg->nr_pgpaths; i++) {
710 struct pgpath *pgpath;
Mike Snitzer498f0102011-08-02 12:32:04 +0100711 struct dm_arg_set path_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Mike Snitzer498f0102011-08-02 12:32:04 +0100713 if (as->argc < nr_args) {
Mikulas Patocka148acff2008-07-21 12:00:30 +0100714 ti->error = "not enough path parameters";
Alasdair G Kergon6bbf79a2010-08-12 04:13:49 +0100715 r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Mike Snitzer498f0102011-08-02 12:32:04 +0100719 path_args.argc = nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 path_args.argv = as->argv;
721
722 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100723 if (IS_ERR(pgpath)) {
724 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100726 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 pgpath->pg = pg;
729 list_add_tail(&pgpath->list, &pg->pgpaths);
Mike Snitzer498f0102011-08-02 12:32:04 +0100730 dm_consume_args(as, nr_args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732
733 return pg;
734
735 bad:
736 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100737 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738}
739
Mike Snitzer498f0102011-08-02 12:32:04 +0100740static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700743 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700744 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Mike Snitzer498f0102011-08-02 12:32:04 +0100746 static struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700747 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 };
749
Mike Snitzer498f0102011-08-02 12:32:04 +0100750 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return -EINVAL;
752
753 if (!hw_argc)
754 return 0;
755
Mike Snitzer498f0102011-08-02 12:32:04 +0100756 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
Chandra Seetharaman14e98c52008-11-13 23:39:06 +0000757
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700758 if (hw_argc > 1) {
759 char *p;
760 int i, j, len = 4;
761
762 for (i = 0; i <= hw_argc - 2; i++)
763 len += strlen(as->argv[i]) + 1;
764 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
765 if (!p) {
766 ti->error = "memory allocation failed";
767 ret = -ENOMEM;
768 goto fail;
769 }
770 j = sprintf(p, "%d", hw_argc - 1);
771 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
772 j = sprintf(p, "%s", as->argv[i]);
773 }
Mike Snitzer498f0102011-08-02 12:32:04 +0100774 dm_consume_args(as, hw_argc - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700777fail:
778 kfree(m->hw_handler_name);
779 m->hw_handler_name = NULL;
780 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Mike Snitzer498f0102011-08-02 12:32:04 +0100783static int parse_features(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 int r;
786 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700787 struct dm_target *ti = m->ti;
Mike Snitzer498f0102011-08-02 12:32:04 +0100788 const char *arg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Mike Snitzer498f0102011-08-02 12:32:04 +0100790 static struct dm_arg _args[] = {
Mike Snitzera58a9352012-07-27 15:08:04 +0100791 {0, 6, "invalid number of feature args"},
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100792 {1, 50, "pg_init_retries must be between 1 and 50"},
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000793 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 };
795
Mike Snitzer498f0102011-08-02 12:32:04 +0100796 r = dm_read_arg_group(_args, as, &argc, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (r)
798 return -EINVAL;
799
800 if (!argc)
801 return 0;
802
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100803 do {
Mike Snitzer498f0102011-08-02 12:32:04 +0100804 arg_name = dm_shift_arg(as);
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100805 argc--;
806
Mike Snitzer498f0102011-08-02 12:32:04 +0100807 if (!strcasecmp(arg_name, "queue_if_no_path")) {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100808 r = queue_if_no_path(m, 1, 0);
809 continue;
810 }
811
Mike Snitzera58a9352012-07-27 15:08:04 +0100812 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
813 m->retain_attached_hw_handler = 1;
814 continue;
815 }
816
Mike Snitzer498f0102011-08-02 12:32:04 +0100817 if (!strcasecmp(arg_name, "pg_init_retries") &&
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100818 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +0100819 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100820 argc--;
821 continue;
822 }
823
Mike Snitzer498f0102011-08-02 12:32:04 +0100824 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000825 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +0100826 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000827 argc--;
828 continue;
829 }
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100832 r = -EINVAL;
833 } while (argc && !r);
834
835 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
838static int multipath_ctr(struct dm_target *ti, unsigned int argc,
839 char **argv)
840{
Mike Snitzer498f0102011-08-02 12:32:04 +0100841 /* target arguments */
842 static struct dm_arg _args[] = {
Mike Snitzera490a072011-03-24 13:54:33 +0000843 {0, 1024, "invalid number of priority groups"},
844 {0, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 };
846
847 int r;
848 struct multipath *m;
Mike Snitzer498f0102011-08-02 12:32:04 +0100849 struct dm_arg_set as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 unsigned pg_count = 0;
851 unsigned next_pg_num;
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500852 bool use_blk_mq = dm_use_blk_mq(dm_table_get_md(ti->table));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 as.argc = argc;
855 as.argv = argv;
856
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500857 m = alloc_multipath(ti, use_blk_mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700859 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return -EINVAL;
861 }
862
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700863 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (r)
865 goto bad;
866
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700867 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (r)
869 goto bad;
870
Mike Snitzer498f0102011-08-02 12:32:04 +0100871 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (r)
873 goto bad;
874
Mike Snitzer498f0102011-08-02 12:32:04 +0100875 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (r)
877 goto bad;
878
Mike Snitzera490a072011-03-24 13:54:33 +0000879 if ((!m->nr_priority_groups && next_pg_num) ||
880 (m->nr_priority_groups && !next_pg_num)) {
881 ti->error = "invalid initial priority group";
882 r = -EINVAL;
883 goto bad;
884 }
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 /* parse the priority groups */
887 while (as.argc) {
888 struct priority_group *pg;
889
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700890 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100891 if (IS_ERR(pg)) {
892 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 goto bad;
894 }
895
896 m->nr_valid_paths += pg->nr_pgpaths;
897 list_add_tail(&pg->list, &m->priority_groups);
898 pg_count++;
899 pg->pg_num = pg_count;
900 if (!--next_pg_num)
901 m->next_pg = pg;
902 }
903
904 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700905 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 r = -EINVAL;
907 goto bad;
908 }
909
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000910 ti->num_flush_bios = 1;
911 ti->num_discard_bios = 1;
Mike Snitzer042bcef2013-05-10 14:37:16 +0100912 ti->num_write_same_bios = 1;
Mike Snitzer8637a6b2016-01-31 12:08:36 -0500913 if (use_blk_mq)
914 ti->per_io_data_size = sizeof(struct dm_mpath_io);
Mikulas Patocka86279212009-06-22 10:12:24 +0100915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return 0;
917
918 bad:
919 free_multipath(m);
920 return r;
921}
922
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000923static void multipath_wait_for_pg_init_completion(struct multipath *m)
924{
925 DECLARE_WAITQUEUE(wait, current);
926 unsigned long flags;
927
928 add_wait_queue(&m->pg_init_wait, &wait);
929
930 while (1) {
931 set_current_state(TASK_UNINTERRUPTIBLE);
932
933 spin_lock_irqsave(&m->lock, flags);
934 if (!m->pg_init_in_progress) {
935 spin_unlock_irqrestore(&m->lock, flags);
936 break;
937 }
938 spin_unlock_irqrestore(&m->lock, flags);
939
940 io_schedule();
941 }
942 set_current_state(TASK_RUNNING);
943
944 remove_wait_queue(&m->pg_init_wait, &wait);
945}
946
947static void flush_multipath_work(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +0000949 unsigned long flags;
950
951 spin_lock_irqsave(&m->lock, flags);
952 m->pg_init_disabled = 1;
953 spin_unlock_irqrestore(&m->lock, flags);
954
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700955 flush_workqueue(kmpath_handlerd);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000956 multipath_wait_for_pg_init_completion(m);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700957 flush_workqueue(kmultipathd);
Tejun Heo43829732012-08-20 14:51:24 -0700958 flush_work(&m->trigger_event);
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +0000959
960 spin_lock_irqsave(&m->lock, flags);
961 m->pg_init_disabled = 0;
962 spin_unlock_irqrestore(&m->lock, flags);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +0000963}
964
965static void multipath_dtr(struct dm_target *ti)
966{
967 struct multipath *m = ti->private;
968
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000969 flush_multipath_work(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 free_multipath(m);
971}
972
973/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 * Take a path out of use.
975 */
976static int fail_path(struct pgpath *pgpath)
977{
978 unsigned long flags;
979 struct multipath *m = pgpath->pg->m;
980
981 spin_lock_irqsave(&m->lock, flags);
982
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100983 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto out;
985
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700986 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100989 pgpath->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 pgpath->fail_count++;
991
992 m->nr_valid_paths--;
993
994 if (pgpath == m->current_pgpath)
995 m->current_pgpath = NULL;
996
Mike Andersonb15546f2007-10-19 22:48:02 +0100997 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
998 pgpath->path.dev->name, m->nr_valid_paths);
999
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001000 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
1002out:
1003 spin_unlock_irqrestore(&m->lock, flags);
1004
1005 return 0;
1006}
1007
1008/*
1009 * Reinstate a previously-failed path
1010 */
1011static int reinstate_path(struct pgpath *pgpath)
1012{
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001013 int r = 0, run_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 unsigned long flags;
1015 struct multipath *m = pgpath->pg->m;
1016
1017 spin_lock_irqsave(&m->lock, flags);
1018
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001019 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 goto out;
1021
Alasdair G Kergondef052d2008-07-21 12:00:31 +01001022 if (!pgpath->pg->ps.type->reinstate_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 DMWARN("Reinstate path not supported by path selector %s",
1024 pgpath->pg->ps.type->name);
1025 r = -EINVAL;
1026 goto out;
1027 }
1028
1029 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1030 if (r)
1031 goto out;
1032
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001033 pgpath->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Hannes Reineckee8099172014-02-28 15:33:44 +01001035 if (!m->nr_valid_paths++) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001036 m->current_pgpath = NULL;
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001037 run_queue = 1;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001038 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001039 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001040 m->pg_init_in_progress++;
1041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Mike Andersonb15546f2007-10-19 22:48:02 +01001043 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1044 pgpath->path.dev->name, m->nr_valid_paths);
1045
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001046 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048out:
1049 spin_unlock_irqrestore(&m->lock, flags);
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001050 if (run_queue)
1051 dm_table_run_md_queue_async(m->ti->table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 return r;
1054}
1055
1056/*
1057 * Fail or reinstate all paths that match the provided struct dm_dev.
1058 */
1059static int action_dev(struct multipath *m, struct dm_dev *dev,
1060 action_fn action)
1061{
Mike Snitzer19040c02011-03-24 13:54:31 +00001062 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 struct pgpath *pgpath;
1064 struct priority_group *pg;
1065
1066 list_for_each_entry(pg, &m->priority_groups, list) {
1067 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1068 if (pgpath->path.dev == dev)
1069 r = action(pgpath);
1070 }
1071 }
1072
1073 return r;
1074}
1075
1076/*
1077 * Temporarily try to avoid having to use the specified PG
1078 */
1079static void bypass_pg(struct multipath *m, struct priority_group *pg,
1080 int bypassed)
1081{
1082 unsigned long flags;
1083
1084 spin_lock_irqsave(&m->lock, flags);
1085
1086 pg->bypassed = bypassed;
1087 m->current_pgpath = NULL;
1088 m->current_pg = NULL;
1089
1090 spin_unlock_irqrestore(&m->lock, flags);
1091
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001092 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093}
1094
1095/*
1096 * Switch to using the specified PG from the next I/O that gets mapped
1097 */
1098static int switch_pg_num(struct multipath *m, const char *pgstr)
1099{
1100 struct priority_group *pg;
1101 unsigned pgnum;
1102 unsigned long flags;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001103 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001105 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 (pgnum > m->nr_priority_groups)) {
1107 DMWARN("invalid PG number supplied to switch_pg_num");
1108 return -EINVAL;
1109 }
1110
1111 spin_lock_irqsave(&m->lock, flags);
1112 list_for_each_entry(pg, &m->priority_groups, list) {
1113 pg->bypassed = 0;
1114 if (--pgnum)
1115 continue;
1116
1117 m->current_pgpath = NULL;
1118 m->current_pg = NULL;
1119 m->next_pg = pg;
1120 }
1121 spin_unlock_irqrestore(&m->lock, flags);
1122
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001123 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return 0;
1125}
1126
1127/*
1128 * Set/clear bypassed status of a PG.
1129 * PGs are numbered upwards from 1 in the order they were declared.
1130 */
1131static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1132{
1133 struct priority_group *pg;
1134 unsigned pgnum;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001135 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001137 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 (pgnum > m->nr_priority_groups)) {
1139 DMWARN("invalid PG number supplied to bypass_pg");
1140 return -EINVAL;
1141 }
1142
1143 list_for_each_entry(pg, &m->priority_groups, list) {
1144 if (!--pgnum)
1145 break;
1146 }
1147
1148 bypass_pg(m, pg, bypassed);
1149 return 0;
1150}
1151
1152/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001153 * Should we retry pg_init immediately?
1154 */
1155static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1156{
1157 unsigned long flags;
1158 int limit_reached = 0;
1159
1160 spin_lock_irqsave(&m->lock, flags);
1161
Shiva Krishna Merla954a73d2013-10-30 03:26:38 +00001162 if (m->pg_init_count <= m->pg_init_retries && !m->pg_init_disabled)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001163 m->pg_init_required = 1;
1164 else
1165 limit_reached = 1;
1166
1167 spin_unlock_irqrestore(&m->lock, flags);
1168
1169 return limit_reached;
1170}
1171
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001172static void pg_init_done(void *data, int errors)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001173{
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001174 struct pgpath *pgpath = data;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001175 struct priority_group *pg = pgpath->pg;
1176 struct multipath *m = pg->m;
1177 unsigned long flags;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001178 unsigned delay_retry = 0;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001179
1180 /* device or driver problems */
1181 switch (errors) {
1182 case SCSI_DH_OK:
1183 break;
1184 case SCSI_DH_NOSYS:
1185 if (!m->hw_handler_name) {
1186 errors = 0;
1187 break;
1188 }
Moger, Babuf7b934c2010-03-06 02:29:49 +00001189 DMERR("Could not failover the device: Handler scsi_dh_%s "
1190 "Error %d.", m->hw_handler_name, errors);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001191 /*
1192 * Fail path for now, so we do not ping pong
1193 */
1194 fail_path(pgpath);
1195 break;
1196 case SCSI_DH_DEV_TEMP_BUSY:
1197 /*
1198 * Probably doing something like FW upgrade on the
1199 * controller so try the other pg.
1200 */
1201 bypass_pg(m, pg, 1);
1202 break;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001203 case SCSI_DH_RETRY:
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001204 /* Wait before retrying. */
1205 delay_retry = 1;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001206 case SCSI_DH_IMM_RETRY:
1207 case SCSI_DH_RES_TEMP_UNAVAIL:
1208 if (pg_init_limit_reached(m, pgpath))
1209 fail_path(pgpath);
1210 errors = 0;
1211 break;
1212 default:
1213 /*
1214 * We probably do not want to fail the path for a device
1215 * error, but this is what the old dm did. In future
1216 * patches we can do more advanced handling.
1217 */
1218 fail_path(pgpath);
1219 }
1220
1221 spin_lock_irqsave(&m->lock, flags);
1222 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001223 if (pgpath == m->current_pgpath) {
1224 DMERR("Could not failover device. Error %d.", errors);
1225 m->current_pgpath = NULL;
1226 m->current_pg = NULL;
1227 }
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001228 } else if (!m->pg_init_required)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001229 pg->bypassed = 0;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001230
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001231 if (--m->pg_init_in_progress)
1232 /* Activations of other paths are still on going */
1233 goto out;
1234
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001235 if (m->pg_init_required) {
1236 m->pg_init_delay_retry = delay_retry;
1237 if (__pg_init_all_paths(m))
1238 goto out;
1239 }
1240 m->queue_io = 0;
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001241
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001242 /*
1243 * Wake up any thread waiting to suspend.
1244 */
1245 wake_up(&m->pg_init_wait);
1246
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001247out:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001248 spin_unlock_irqrestore(&m->lock, flags);
1249}
1250
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001251static void activate_path(struct work_struct *work)
1252{
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001253 struct pgpath *pgpath =
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001254 container_of(work, struct pgpath, activate_path.work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001255
Hannes Reinecke3a017502014-02-28 15:33:49 +01001256 if (pgpath->is_active)
1257 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
1258 pg_init_done, pgpath);
1259 else
1260 pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001261}
1262
Hannes Reinecke7e782af2013-07-01 15:16:26 +02001263static int noretry_error(int error)
1264{
1265 switch (error) {
1266 case -EOPNOTSUPP:
1267 case -EREMOTEIO:
1268 case -EILSEQ:
1269 case -ENODATA:
Jun'ichi Nomuracc9d3c32013-09-13 14:54:30 +09001270 case -ENOSPC:
Hannes Reinecke7e782af2013-07-01 15:16:26 +02001271 return 1;
1272 }
1273
1274 /* Anything else could be a path failure, so should be retried */
1275 return 0;
1276}
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278/*
1279 * end_io handling
1280 */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001281static int do_end_io(struct multipath *m, struct request *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001282 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283{
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001284 /*
1285 * We don't queue any clone request inside the multipath target
1286 * during end I/O handling, since those clone requests don't have
1287 * bio clones. If we queue them inside the multipath target,
1288 * we need to make bio clones, that requires memory allocation.
1289 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1290 * don't have bio clones.)
1291 * Instead of queueing the clone request here, we queue the original
1292 * request into dm core, which will remake a clone request and
1293 * clone bios for it and resubmit it later.
1294 */
1295 int r = DM_ENDIO_REQUEUE;
Stefan Bader640eb3b2005-11-21 21:32:35 -08001296 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001298 if (!error && !clone->errors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 return 0; /* I/O complete */
1300
Mike Snitzer7eee4ae2014-06-02 15:50:06 -04001301 if (noretry_error(error))
Mike Snitzer959eb4e2010-08-12 04:14:32 +01001302 return error;
1303
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001304 if (mpio->pgpath)
1305 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Stefan Bader640eb3b2005-11-21 21:32:35 -08001307 spin_lock_irqsave(&m->lock, flags);
Hannes Reinecke751b2a72011-01-18 10:13:12 +01001308 if (!m->nr_valid_paths) {
1309 if (!m->queue_if_no_path) {
1310 if (!__must_push_back(m))
1311 r = -EIO;
1312 } else {
1313 if (error == -EBADE)
1314 r = error;
1315 }
1316 }
Stefan Bader640eb3b2005-11-21 21:32:35 -08001317 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001319 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001322static int multipath_end_io(struct dm_target *ti, struct request *clone,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 int error, union map_info *map_context)
1324{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001325 struct multipath *m = ti->private;
Mike Snitzer2eff1922016-02-03 09:13:14 -05001326 struct dm_mpath_io *mpio = get_mpio(map_context);
Wei Yongjuna71a2612012-10-12 16:59:42 +01001327 struct pgpath *pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 struct path_selector *ps;
1329 int r;
1330
Jun'ichi Nomura466891f2012-03-28 18:41:25 +01001331 BUG_ON(!mpio);
1332
Mike Snitzer2eff1922016-02-03 09:13:14 -05001333 r = do_end_io(m, clone, error, mpio);
Wei Yongjuna71a2612012-10-12 16:59:42 +01001334 pgpath = mpio->pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 if (pgpath) {
1336 ps = &pgpath->pg->ps;
1337 if (ps->type->end_io)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001338 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
Mike Snitzer2eff1922016-02-03 09:13:14 -05001340 clear_request_fn_mpio(m, map_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 return r;
1343}
1344
1345/*
1346 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001347 * the last path fails we must error any remaining I/O.
1348 * Note that if the freeze_bdev fails while suspending, the
1349 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 */
1351static void multipath_presuspend(struct dm_target *ti)
1352{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001353 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001355 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001358static void multipath_postsuspend(struct dm_target *ti)
1359{
Mike Anderson6380f262009-12-10 23:52:21 +00001360 struct multipath *m = ti->private;
1361
1362 mutex_lock(&m->work_mutex);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001363 flush_multipath_work(m);
Mike Anderson6380f262009-12-10 23:52:21 +00001364 mutex_unlock(&m->work_mutex);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001365}
1366
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001367/*
1368 * Restore the queue_if_no_path setting.
1369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370static void multipath_resume(struct dm_target *ti)
1371{
Mike Snitzer7943bd62016-02-02 21:53:15 -05001372 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 unsigned long flags;
1374
1375 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001376 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 spin_unlock_irqrestore(&m->lock, flags);
1378}
1379
1380/*
1381 * Info output has the following format:
1382 * num_multipath_feature_args [multipath_feature_args]*
1383 * num_handler_status_args [handler_status_args]*
1384 * num_groups init_group_number
1385 * [A|D|E num_ps_status_args [ps_status_args]*
1386 * num_paths num_selector_args
1387 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1388 *
1389 * Table output has the following format (identical to the constructor string):
1390 * num_feature_args [features_args]*
1391 * num_handler_args hw_handler [hw_handler_args]*
1392 * num_groups init_group_number
1393 * [priority selector-name num_ps_args [ps_args]*
1394 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1395 */
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001396static void multipath_status(struct dm_target *ti, status_type_t type,
1397 unsigned status_flags, char *result, unsigned maxlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
1399 int sz = 0;
1400 unsigned long flags;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001401 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 struct priority_group *pg;
1403 struct pgpath *p;
1404 unsigned pg_num;
1405 char state;
1406
1407 spin_lock_irqsave(&m->lock, flags);
1408
1409 /* Features */
1410 if (type == STATUSTYPE_INFO)
Hannes Reineckee8099172014-02-28 15:33:44 +01001411 DMEMIT("2 %u %u ", m->queue_io, m->pg_init_count);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001412 else {
1413 DMEMIT("%u ", m->queue_if_no_path +
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001414 (m->pg_init_retries > 0) * 2 +
Mike Snitzera58a9352012-07-27 15:08:04 +01001415 (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
1416 m->retain_attached_hw_handler);
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001417 if (m->queue_if_no_path)
1418 DMEMIT("queue_if_no_path ");
1419 if (m->pg_init_retries)
1420 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001421 if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
1422 DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
Mike Snitzera58a9352012-07-27 15:08:04 +01001423 if (m->retain_attached_hw_handler)
1424 DMEMIT("retain_attached_hw_handler ");
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001427 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 DMEMIT("0 ");
1429 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001430 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 DMEMIT("%u ", m->nr_priority_groups);
1433
1434 if (m->next_pg)
1435 pg_num = m->next_pg->pg_num;
1436 else if (m->current_pg)
1437 pg_num = m->current_pg->pg_num;
1438 else
Mike Snitzera490a072011-03-24 13:54:33 +00001439 pg_num = (m->nr_priority_groups ? 1 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 DMEMIT("%u ", pg_num);
1442
1443 switch (type) {
1444 case STATUSTYPE_INFO:
1445 list_for_each_entry(pg, &m->priority_groups, list) {
1446 if (pg->bypassed)
1447 state = 'D'; /* Disabled */
1448 else if (pg == m->current_pg)
1449 state = 'A'; /* Currently Active */
1450 else
1451 state = 'E'; /* Enabled */
1452
1453 DMEMIT("%c ", state);
1454
1455 if (pg->ps.type->status)
1456 sz += pg->ps.type->status(&pg->ps, NULL, type,
1457 result + sz,
1458 maxlen - sz);
1459 else
1460 DMEMIT("0 ");
1461
1462 DMEMIT("%u %u ", pg->nr_pgpaths,
1463 pg->ps.type->info_args);
1464
1465 list_for_each_entry(p, &pg->pgpaths, list) {
1466 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001467 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 p->fail_count);
1469 if (pg->ps.type->status)
1470 sz += pg->ps.type->status(&pg->ps,
1471 &p->path, type, result + sz,
1472 maxlen - sz);
1473 }
1474 }
1475 break;
1476
1477 case STATUSTYPE_TABLE:
1478 list_for_each_entry(pg, &m->priority_groups, list) {
1479 DMEMIT("%s ", pg->ps.type->name);
1480
1481 if (pg->ps.type->status)
1482 sz += pg->ps.type->status(&pg->ps, NULL, type,
1483 result + sz,
1484 maxlen - sz);
1485 else
1486 DMEMIT("0 ");
1487
1488 DMEMIT("%u %u ", pg->nr_pgpaths,
1489 pg->ps.type->table_args);
1490
1491 list_for_each_entry(p, &pg->pgpaths, list) {
1492 DMEMIT("%s ", p->path.dev->name);
1493 if (pg->ps.type->status)
1494 sz += pg->ps.type->status(&pg->ps,
1495 &p->path, type, result + sz,
1496 maxlen - sz);
1497 }
1498 }
1499 break;
1500 }
1501
1502 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
1505static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1506{
Mike Anderson6380f262009-12-10 23:52:21 +00001507 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 struct dm_dev *dev;
Mike Snitzer7943bd62016-02-02 21:53:15 -05001509 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 action_fn action;
1511
Mike Anderson6380f262009-12-10 23:52:21 +00001512 mutex_lock(&m->work_mutex);
1513
Kiyoshi Uedac2f3d242009-12-10 23:52:27 +00001514 if (dm_suspended(ti)) {
1515 r = -EBUSY;
1516 goto out;
1517 }
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (argc == 1) {
Mike Snitzer498f0102011-08-02 12:32:04 +01001520 if (!strcasecmp(argv[0], "queue_if_no_path")) {
Mike Anderson6380f262009-12-10 23:52:21 +00001521 r = queue_if_no_path(m, 1, 0);
1522 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001523 } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
Mike Anderson6380f262009-12-10 23:52:21 +00001524 r = queue_if_no_path(m, 0, 0);
1525 goto out;
1526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
1528
Mike Anderson6380f262009-12-10 23:52:21 +00001529 if (argc != 2) {
Jose Castilloa356e422014-01-29 17:52:45 +01001530 DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
Mike Anderson6380f262009-12-10 23:52:21 +00001531 goto out;
1532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Mike Snitzer498f0102011-08-02 12:32:04 +01001534 if (!strcasecmp(argv[0], "disable_group")) {
Mike Anderson6380f262009-12-10 23:52:21 +00001535 r = bypass_pg_num(m, argv[1], 1);
1536 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001537 } else if (!strcasecmp(argv[0], "enable_group")) {
Mike Anderson6380f262009-12-10 23:52:21 +00001538 r = bypass_pg_num(m, argv[1], 0);
1539 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001540 } else if (!strcasecmp(argv[0], "switch_group")) {
Mike Anderson6380f262009-12-10 23:52:21 +00001541 r = switch_pg_num(m, argv[1]);
1542 goto out;
Mike Snitzer498f0102011-08-02 12:32:04 +01001543 } else if (!strcasecmp(argv[0], "reinstate_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 action = reinstate_path;
Mike Snitzer498f0102011-08-02 12:32:04 +01001545 else if (!strcasecmp(argv[0], "fail_path"))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 action = fail_path;
Mike Anderson6380f262009-12-10 23:52:21 +00001547 else {
Jose Castilloa356e422014-01-29 17:52:45 +01001548 DMWARN("Unrecognised multipath message received: %s", argv[0]);
Mike Anderson6380f262009-12-10 23:52:21 +00001549 goto out;
1550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00001552 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001554 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 argv[1]);
Mike Anderson6380f262009-12-10 23:52:21 +00001556 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558
1559 r = action_dev(m, dev, action);
1560
1561 dm_put_device(ti, dev);
1562
Mike Anderson6380f262009-12-10 23:52:21 +00001563out:
1564 mutex_unlock(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001568static int multipath_prepare_ioctl(struct dm_target *ti,
1569 struct block_device **bdev, fmode_t *mode)
Milan Broz9af4aa32006-10-03 01:15:20 -07001570{
Mikulas Patocka35991652012-06-03 00:29:58 +01001571 struct multipath *m = ti->private;
Milan Broz9af4aa32006-10-03 01:15:20 -07001572 unsigned long flags;
Mikulas Patocka35991652012-06-03 00:29:58 +01001573 int r;
1574
Milan Broz9af4aa32006-10-03 01:15:20 -07001575 spin_lock_irqsave(&m->lock, flags);
1576
1577 if (!m->current_pgpath)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001578 __choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001579
Junichi Nomura43e43c92015-11-17 09:36:56 +00001580 if (m->current_pgpath) {
1581 if (!m->queue_io) {
1582 *bdev = m->current_pgpath->path.dev->bdev;
1583 *mode = m->current_pgpath->path.dev->mode;
1584 r = 0;
1585 } else {
1586 /* pg_init has not started or completed */
1587 r = -ENOTCONN;
1588 }
1589 } else {
1590 /* No path is available */
1591 if (m->queue_if_no_path)
1592 r = -ENOTCONN;
1593 else
1594 r = -EIO;
Milan Broze90dae12006-10-03 01:15:22 -07001595 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001596
Milan Broz9af4aa32006-10-03 01:15:20 -07001597 spin_unlock_irqrestore(&m->lock, flags);
1598
Junichi Nomura5bbbfdf2015-11-17 09:39:26 +00001599 if (r == -ENOTCONN) {
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001600 spin_lock_irqsave(&m->lock, flags);
1601 if (!m->current_pg) {
1602 /* Path status changed, redo selection */
1603 __choose_pgpath(m, 0);
1604 }
1605 if (m->pg_init_required)
1606 __pg_init_all_paths(m);
Mike Snitzer4cdd2ad2014-05-13 13:49:39 -04001607 spin_unlock_irqrestore(&m->lock, flags);
Hannes Reinecke63d832c2014-05-26 14:45:39 +02001608 dm_table_run_md_queue_async(m->ti->table);
Hannes Reinecke3e9f1be2014-02-28 15:33:45 +01001609 }
Mikulas Patocka35991652012-06-03 00:29:58 +01001610
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001611 /*
1612 * Only pass ioctls through if the device sizes match exactly.
1613 */
1614 if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
1615 return 1;
1616 return r;
Milan Broz9af4aa32006-10-03 01:15:20 -07001617}
1618
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001619static int multipath_iterate_devices(struct dm_target *ti,
1620 iterate_devices_callout_fn fn, void *data)
1621{
1622 struct multipath *m = ti->private;
1623 struct priority_group *pg;
1624 struct pgpath *p;
1625 int ret = 0;
1626
1627 list_for_each_entry(pg, &m->priority_groups, list) {
1628 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01001629 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001630 if (ret)
1631 goto out;
1632 }
1633 }
1634
1635out:
1636 return ret;
1637}
1638
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001639static int __pgpath_busy(struct pgpath *pgpath)
1640{
1641 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1642
Mike Snitzer52b09912015-02-23 16:36:41 -05001643 return blk_lld_busy(q);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001644}
1645
1646/*
1647 * We return "busy", only when we can map I/Os but underlying devices
1648 * are busy (so even if we map I/Os now, the I/Os will wait on
1649 * the underlying queue).
1650 * In other words, if we want to kill I/Os or queue them inside us
1651 * due to map unavailability, we don't return "busy". Otherwise,
1652 * dm core won't give us the I/Os and we can't do what we want.
1653 */
1654static int multipath_busy(struct dm_target *ti)
1655{
1656 int busy = 0, has_active = 0;
1657 struct multipath *m = ti->private;
1658 struct priority_group *pg;
1659 struct pgpath *pgpath;
1660 unsigned long flags;
1661
1662 spin_lock_irqsave(&m->lock, flags);
1663
Jun'ichi Nomura7a7a3b42014-07-08 00:55:14 +00001664 /* pg_init in progress or no paths available */
1665 if (m->pg_init_in_progress ||
1666 (!m->nr_valid_paths && m->queue_if_no_path)) {
Hannes Reineckeb63349a2013-10-01 11:49:56 +02001667 busy = 1;
1668 goto out;
1669 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001670 /* Guess which priority_group will be used at next mapping time */
1671 if (unlikely(!m->current_pgpath && m->next_pg))
1672 pg = m->next_pg;
1673 else if (likely(m->current_pg))
1674 pg = m->current_pg;
1675 else
1676 /*
1677 * We don't know which pg will be used at next mapping time.
1678 * We don't call __choose_pgpath() here to avoid to trigger
1679 * pg_init just by busy checking.
1680 * So we don't know whether underlying devices we will be using
1681 * at next mapping time are busy or not. Just try mapping.
1682 */
1683 goto out;
1684
1685 /*
1686 * If there is one non-busy active path at least, the path selector
1687 * will be able to select it. So we consider such a pg as not busy.
1688 */
1689 busy = 1;
1690 list_for_each_entry(pgpath, &pg->pgpaths, list)
1691 if (pgpath->is_active) {
1692 has_active = 1;
1693
1694 if (!__pgpath_busy(pgpath)) {
1695 busy = 0;
1696 break;
1697 }
1698 }
1699
1700 if (!has_active)
1701 /*
1702 * No active path in this pg, so this pg won't be used and
1703 * the current_pg will be changed at next mapping time.
1704 * We need to try mapping to determine it.
1705 */
1706 busy = 0;
1707
1708out:
1709 spin_unlock_irqrestore(&m->lock, flags);
1710
1711 return busy;
1712}
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714/*-----------------------------------------------------------------
1715 * Module setup
1716 *---------------------------------------------------------------*/
1717static struct target_type multipath_target = {
1718 .name = "multipath",
Mike Snitzer16f12262016-01-31 17:22:27 -05001719 .version = {1, 11, 0},
1720 .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 .module = THIS_MODULE,
1722 .ctr = multipath_ctr,
1723 .dtr = multipath_dtr,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001724 .map_rq = multipath_map,
Mike Snitzere5863d92014-12-17 21:08:12 -05001725 .clone_and_map_rq = multipath_clone_and_map,
1726 .release_clone_rq = multipath_release_clone,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001727 .rq_end_io = multipath_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 .presuspend = multipath_presuspend,
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001729 .postsuspend = multipath_postsuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 .resume = multipath_resume,
1731 .status = multipath_status,
1732 .message = multipath_message,
Christoph Hellwige56f81e2015-10-15 14:10:50 +02001733 .prepare_ioctl = multipath_prepare_ioctl,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001734 .iterate_devices = multipath_iterate_devices,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001735 .busy = multipath_busy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736};
1737
1738static int __init dm_multipath_init(void)
1739{
1740 int r;
1741
1742 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001743 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 if (!_mpio_cache)
1745 return -ENOMEM;
1746
1747 r = dm_register_target(&multipath_target);
1748 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001749 DMERR("register failed %d", r);
Johannes Thumshirnff658e92015-01-11 12:45:23 +01001750 r = -EINVAL;
1751 goto bad_register_target;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 }
1753
Tejun Heo4d4d66a2011-01-13 19:59:57 +00001754 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001755 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001756 DMERR("failed to create workqueue kmpathd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01001757 r = -ENOMEM;
1758 goto bad_alloc_kmultipathd;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001759 }
1760
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001761 /*
1762 * A separate workqueue is used to handle the device handlers
1763 * to avoid overloading existing workqueue. Overloading the
1764 * old workqueue would also create a bottleneck in the
1765 * path of the storage hardware device activation.
1766 */
Tejun Heo4d4d66a2011-01-13 19:59:57 +00001767 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
1768 WQ_MEM_RECLAIM);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001769 if (!kmpath_handlerd) {
1770 DMERR("failed to create workqueue kmpath_handlerd");
Johannes Thumshirnff658e92015-01-11 12:45:23 +01001771 r = -ENOMEM;
1772 goto bad_alloc_kmpath_handlerd;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001773 }
1774
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001775 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 multipath_target.version[0], multipath_target.version[1],
1777 multipath_target.version[2]);
1778
Johannes Thumshirnff658e92015-01-11 12:45:23 +01001779 return 0;
1780
1781bad_alloc_kmpath_handlerd:
1782 destroy_workqueue(kmultipathd);
1783bad_alloc_kmultipathd:
1784 dm_unregister_target(&multipath_target);
1785bad_register_target:
1786 kmem_cache_destroy(_mpio_cache);
1787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 return r;
1789}
1790
1791static void __exit dm_multipath_exit(void)
1792{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001793 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001794 destroy_workqueue(kmultipathd);
1795
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001796 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 kmem_cache_destroy(_mpio_cache);
1798}
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800module_init(dm_multipath_init);
1801module_exit(dm_multipath_exit);
1802
1803MODULE_DESCRIPTION(DM_NAME " multipath target");
1804MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1805MODULE_LICENSE("GPL");