blob: b759a127f9c3718bbfffe2d16ca258fe4afe15e2 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "dm-path-selector.h"
Mike Andersonb15546f2007-10-19 22:48:02 +010011#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include <linux/ctype.h>
14#include <linux/init.h>
15#include <linux/mempool.h>
16#include <linux/module.h>
17#include <linux/pagemap.h>
18#include <linux/slab.h>
19#include <linux/time.h>
20#include <linux/workqueue.h>
Mikulas Patocka35991652012-06-03 00:29:58 +010021#include <linux/delay.h>
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070022#include <scsi/scsi_dh.h>
Arun Sharma600634972011-07-26 16:09:06 -070023#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "multipath"
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000026#define DM_PG_INIT_DELAY_MSECS 2000
27#define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/* Path properties */
30struct pgpath {
31 struct list_head list;
32
33 struct priority_group *pg; /* Owning PG */
Kiyoshi Ueda66800732008-10-10 13:36:58 +010034 unsigned is_active; /* Path status */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 unsigned fail_count; /* Cumulative failure count */
36
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080037 struct dm_path path;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000038 struct delayed_work activate_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
41#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
42
43/*
44 * Paths are grouped into Priority Groups and numbered from 1 upwards.
45 * Each has a path selector which controls which path gets used.
46 */
47struct priority_group {
48 struct list_head list;
49
50 struct multipath *m; /* Owning multipath instance */
51 struct path_selector ps;
52
53 unsigned pg_num; /* Reference number */
54 unsigned bypassed; /* Temporarily bypass this PG? */
55
56 unsigned nr_pgpaths; /* Number of paths in PG */
57 struct list_head pgpaths;
58};
59
60/* Multipath context */
61struct multipath {
62 struct list_head list;
63 struct dm_target *ti;
64
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070065 const char *hw_handler_name;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -070066 char *hw_handler_params;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000067
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010068 spinlock_t lock;
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 unsigned nr_priority_groups;
71 struct list_head priority_groups;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000072
73 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned pg_init_required; /* pg_init needs calling? */
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -070076 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000077 unsigned pg_init_delay_retry; /* Delay pg_init retry? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 unsigned nr_valid_paths; /* Total number of usable paths */
80 struct pgpath *current_pgpath;
81 struct priority_group *current_pg;
82 struct priority_group *next_pg; /* Switch to this PG if set */
83 unsigned repeat_count; /* I/Os left before calling PS again */
84
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010085 unsigned queue_io:1; /* Must we queue all I/O? */
86 unsigned queue_if_no_path:1; /* Queue I/O if last path fails? */
87 unsigned saved_queue_if_no_path:1; /* Saved state during suspension */
Mike Snitzera58a9352012-07-27 15:08:04 +010088 unsigned retain_attached_hw_handler:1; /* If there's already a hw_handler present, don't change it. */
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010089
Dave Wysochanskic9e45582007-10-19 22:47:53 +010090 unsigned pg_init_retries; /* Number of times to retry pg_init */
91 unsigned pg_init_count; /* Number of times pg_init called */
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +000092 unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Mike Snitzer1fbdd2b2012-06-03 00:29:43 +010094 unsigned queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct work_struct process_queued_ios;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +010096 struct list_head queued_ios;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 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
119#define MIN_IOS 256 /* Mempool size */
120
Christoph Lametere18b8902006-12-06 20:33:20 -0800121static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700123static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000124static void process_queued_ios(struct work_struct *work);
125static void trigger_event(struct work_struct *work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700126static void activate_path(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128
129/*-----------------------------------------------
130 * Allocation routines
131 *-----------------------------------------------*/
132
133static struct pgpath *alloc_pgpath(void)
134{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700135 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Mike Anderson224cb3e2008-08-29 09:36:09 +0200137 if (pgpath) {
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100138 pgpath->is_active = 1;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000139 INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 return pgpath;
143}
144
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100145static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 kfree(pgpath);
148}
149
150static struct priority_group *alloc_priority_group(void)
151{
152 struct priority_group *pg;
153
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700154 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700156 if (pg)
157 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 return pg;
160}
161
162static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
163{
164 struct pgpath *pgpath, *tmp;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700165 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
168 list_del(&pgpath->list);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700169 if (m->hw_handler_name)
170 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 dm_put_device(ti, pgpath->path.dev);
172 free_pgpath(pgpath);
173 }
174}
175
176static void free_priority_group(struct priority_group *pg,
177 struct dm_target *ti)
178{
179 struct path_selector *ps = &pg->ps;
180
181 if (ps->type) {
182 ps->type->destroy(ps);
183 dm_put_path_selector(ps->type);
184 }
185
186 free_pgpaths(&pg->pgpaths, ti);
187 kfree(pg);
188}
189
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700190static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 struct multipath *m;
193
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700194 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 INIT_LIST_HEAD(&m->priority_groups);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100197 INIT_LIST_HEAD(&m->queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 spin_lock_init(&m->lock);
199 m->queue_io = 1;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000200 m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
David Howellsc4028952006-11-22 14:57:56 +0000201 INIT_WORK(&m->process_queued_ios, process_queued_ios);
202 INIT_WORK(&m->trigger_event, trigger_event);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000203 init_waitqueue_head(&m->pg_init_wait);
Mike Anderson6380f262009-12-10 23:52:21 +0000204 mutex_init(&m->work_mutex);
Matthew Dobson93d23412006-03-26 01:37:50 -0800205 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (!m->mpio_pool) {
207 kfree(m);
208 return NULL;
209 }
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
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100232static int set_mapinfo(struct multipath *m, union map_info *info)
233{
234 struct dm_mpath_io *mpio;
235
236 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
237 if (!mpio)
238 return -ENOMEM;
239
240 memset(mpio, 0, sizeof(*mpio));
241 info->ptr = mpio;
242
243 return 0;
244}
245
246static void clear_mapinfo(struct multipath *m, union map_info *info)
247{
248 struct dm_mpath_io *mpio = info->ptr;
249
250 info->ptr = NULL;
251 mempool_free(mpio, m->mpio_pool);
252}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254/*-----------------------------------------------
255 * Path selection
256 *-----------------------------------------------*/
257
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000258static void __pg_init_all_paths(struct multipath *m)
259{
260 struct pgpath *pgpath;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000261 unsigned long pg_init_delay = 0;
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000262
263 m->pg_init_count++;
264 m->pg_init_required = 0;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000265 if (m->pg_init_delay_retry)
266 pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
267 m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000268 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
269 /* Skip failed paths */
270 if (!pgpath->is_active)
271 continue;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000272 if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
273 pg_init_delay))
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000274 m->pg_init_in_progress++;
275 }
276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
279{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 m->current_pg = pgpath->pg;
281
282 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700283 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 m->pg_init_required = 1;
285 m->queue_io = 1;
286 } else {
287 m->pg_init_required = 0;
288 m->queue_io = 0;
289 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100290
291 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100294static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
295 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800297 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100299 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (!path)
301 return -ENXIO;
302
303 m->current_pgpath = path_to_pgpath(path);
304
305 if (m->current_pg != pg)
306 __switch_pg(m, m->current_pgpath);
307
308 return 0;
309}
310
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100311static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 struct priority_group *pg;
314 unsigned bypassed = 1;
315
316 if (!m->nr_valid_paths)
317 goto failed;
318
319 /* Were we instructed to switch PG? */
320 if (m->next_pg) {
321 pg = m->next_pg;
322 m->next_pg = NULL;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100323 if (!__choose_path_in_pg(m, pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return;
325 }
326
327 /* Don't change PG until it has no remaining paths */
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100328 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return;
330
331 /*
332 * Loop through priority groups until we find a valid path.
333 * First time we skip PGs marked 'bypassed'.
Mike Christief220fd42012-06-03 00:29:45 +0100334 * Second time we only try the ones we skipped, but set
335 * pg_init_delay_retry so we do not hammer controllers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
337 do {
338 list_for_each_entry(pg, &m->priority_groups, list) {
339 if (pg->bypassed == bypassed)
340 continue;
Mike Christief220fd42012-06-03 00:29:45 +0100341 if (!__choose_path_in_pg(m, pg, nr_bytes)) {
342 if (!bypassed)
343 m->pg_init_delay_retry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return;
Mike Christief220fd42012-06-03 00:29:45 +0100345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347 } while (bypassed--);
348
349failed:
350 m->current_pgpath = NULL;
351 m->current_pg = NULL;
352}
353
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800354/*
355 * Check whether bios must be queued in the device-mapper core rather
356 * than here in the target.
357 *
358 * m->lock must be held on entry.
359 *
360 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
361 * same value then we are not between multipath_presuspend()
362 * and multipath_resume() calls and we have no need to check
363 * for the DMF_NOFLUSH_SUSPENDING flag.
364 */
365static int __must_push_back(struct multipath *m)
366{
367 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
368 dm_noflush_suspending(m->ti));
369}
370
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100371static int map_io(struct multipath *m, struct request *clone,
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100372 union map_info *map_context, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800374 int r = DM_MAPIO_REMAPPED;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100375 size_t nr_bytes = blk_rq_bytes(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 unsigned long flags;
377 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100378 struct block_device *bdev;
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100379 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 spin_lock_irqsave(&m->lock, flags);
382
383 /* Do we need to select a new pgpath? */
384 if (!m->current_pgpath ||
385 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100386 __choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 pgpath = m->current_pgpath;
389
390 if (was_queued)
391 m->queue_size--;
392
393 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700394 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* Queue for the daemon to resubmit */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100396 list_add_tail(&clone->queuelist, &m->queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700398 if ((m->pg_init_required && !m->pg_init_in_progress) ||
399 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700400 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800402 r = DM_MAPIO_SUBMITTED;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100403 } else if (pgpath) {
404 bdev = pgpath->path.dev->bdev;
405 clone->q = bdev_get_queue(bdev);
406 clone->rq_disk = bdev->bd_disk;
407 } else if (__must_push_back(m))
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800408 r = DM_MAPIO_REQUEUE;
409 else
410 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 mpio->pgpath = pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100413 mpio->nr_bytes = nr_bytes;
414
415 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
416 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
417 nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 spin_unlock_irqrestore(&m->lock, flags);
420
421 return r;
422}
423
424/*
425 * If we run out of usable paths, should we queue I/O or error it?
426 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700427static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
428 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 unsigned long flags;
431
432 spin_lock_irqsave(&m->lock, flags);
433
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700434 if (save_old_value)
435 m->saved_queue_if_no_path = m->queue_if_no_path;
436 else
437 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700439 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700440 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 spin_unlock_irqrestore(&m->lock, flags);
443
444 return 0;
445}
446
447/*-----------------------------------------------------------------
448 * The multipath daemon is responsible for resubmitting queued ios.
449 *---------------------------------------------------------------*/
450
451static void dispatch_queued_ios(struct multipath *m)
452{
453 int r;
454 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 union map_info *info;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100456 struct request *clone, *n;
457 LIST_HEAD(cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 spin_lock_irqsave(&m->lock, flags);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100460 list_splice_init(&m->queued_ios, &cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 spin_unlock_irqrestore(&m->lock, flags);
462
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100463 list_for_each_entry_safe(clone, n, &cl, queuelist) {
464 list_del_init(&clone->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100466 info = dm_get_rq_mapinfo(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100468 r = map_io(m, clone, info, 1);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100469 if (r < 0) {
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100470 clear_mapinfo(m, info);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100471 dm_kill_unmapped_request(clone, r);
472 } else if (r == DM_MAPIO_REMAPPED)
473 dm_dispatch_request(clone);
474 else if (r == DM_MAPIO_REQUEUE) {
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100475 clear_mapinfo(m, info);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100476 dm_requeue_unmapped_request(clone);
477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
479}
480
David Howellsc4028952006-11-22 14:57:56 +0000481static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
David Howellsc4028952006-11-22 14:57:56 +0000483 struct multipath *m =
484 container_of(work, struct multipath, process_queued_ios);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000485 struct pgpath *pgpath = NULL;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100486 unsigned must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 unsigned long flags;
488
489 spin_lock_irqsave(&m->lock, flags);
490
491 if (!m->current_pgpath)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100492 __choose_pgpath(m, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 pgpath = m->current_pgpath;
495
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700496 if ((pgpath && !m->queue_io) ||
497 (!pgpath && !m->queue_if_no_path))
498 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000500 if (m->pg_init_required && !m->pg_init_in_progress && pgpath)
501 __pg_init_all_paths(m);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (!must_queue)
505 dispatch_queued_ios(m);
506}
507
508/*
509 * An event is triggered whenever a path is taken out of use.
510 * Includes path failure and PG bypass.
511 */
David Howellsc4028952006-11-22 14:57:56 +0000512static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
David Howellsc4028952006-11-22 14:57:56 +0000514 struct multipath *m =
515 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 dm_table_event(m->ti->table);
518}
519
520/*-----------------------------------------------------------------
521 * Constructor/argument parsing:
522 * <#multipath feature args> [<arg>]*
523 * <#hw_handler args> [hw_handler [<arg>]*]
524 * <#priority groups>
525 * <initial priority group>
526 * [<selector> <#selector args> [<arg>]*
527 * <#paths> <#per-path selector args>
528 * [<path> [<arg>]* ]+ ]+
529 *---------------------------------------------------------------*/
Mike Snitzer498f0102011-08-02 12:32:04 +0100530static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 struct dm_target *ti)
532{
533 int r;
534 struct path_selector_type *pst;
535 unsigned ps_argc;
536
Mike Snitzer498f0102011-08-02 12:32:04 +0100537 static struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700538 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 };
540
Mike Snitzer498f0102011-08-02 12:32:04 +0100541 pst = dm_get_path_selector(dm_shift_arg(as));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700543 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return -EINVAL;
545 }
546
Mike Snitzer498f0102011-08-02 12:32:04 +0100547 r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100548 if (r) {
549 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 r = pst->create(&pg->ps, ps_argc, as->argv);
554 if (r) {
555 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700556 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return r;
558 }
559
560 pg->ps.type = pst;
Mike Snitzer498f0102011-08-02 12:32:04 +0100561 dm_consume_args(as, ps_argc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 return 0;
564}
565
Mike Snitzer498f0102011-08-02 12:32:04 +0100566static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct dm_target *ti)
568{
569 int r;
570 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700571 struct multipath *m = ti->private;
Mike Snitzera58a9352012-07-27 15:08:04 +0100572 struct request_queue *q = NULL;
573 const char *attached_handler_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /* we need at least a path arg */
576 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700577 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100578 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
581 p = alloc_pgpath();
582 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100583 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Mike Snitzer498f0102011-08-02 12:32:04 +0100585 r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000586 &p->path.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700588 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto bad;
590 }
591
Mike Snitzera58a9352012-07-27 15:08:04 +0100592 if (m->retain_attached_hw_handler || m->hw_handler_name)
593 q = bdev_get_queue(p->path.dev->bdev);
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100594
Mike Snitzera58a9352012-07-27 15:08:04 +0100595 if (m->retain_attached_hw_handler) {
596 attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
597 if (attached_handler_name) {
598 /*
599 * Reset hw_handler_name to match the attached handler
600 * and clear any hw_handler_params associated with the
601 * ignored handler.
602 *
603 * NB. This modifies the table line to show the actual
604 * handler instead of the original table passed in.
605 */
606 kfree(m->hw_handler_name);
607 m->hw_handler_name = attached_handler_name;
608
609 kfree(m->hw_handler_params);
610 m->hw_handler_params = NULL;
611 }
612 }
613
614 if (m->hw_handler_name) {
615 /*
616 * Increments scsi_dh reference, even when using an
617 * already-attached handler.
618 */
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100619 r = scsi_dh_attach(q, m->hw_handler_name);
620 if (r == -EBUSY) {
621 /*
Mike Snitzera58a9352012-07-27 15:08:04 +0100622 * Already attached to different hw_handler:
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100623 * try to reattach with correct one.
624 */
625 scsi_dh_detach(q);
626 r = scsi_dh_attach(q, m->hw_handler_name);
627 }
628
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700629 if (r < 0) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100630 ti->error = "error attaching hardware handler";
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700631 dm_put_device(ti, p->path.dev);
632 goto bad;
633 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700634
635 if (m->hw_handler_params) {
636 r = scsi_dh_set_params(q, m->hw_handler_params);
637 if (r < 0) {
638 ti->error = "unable to set hardware "
639 "handler parameters";
640 scsi_dh_detach(q);
641 dm_put_device(ti, p->path.dev);
642 goto bad;
643 }
644 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700645 }
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
648 if (r) {
649 dm_put_device(ti, p->path.dev);
650 goto bad;
651 }
652
653 return p;
654
655 bad:
656 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100657 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Mike Snitzer498f0102011-08-02 12:32:04 +0100660static struct priority_group *parse_priority_group(struct dm_arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700661 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Mike Snitzer498f0102011-08-02 12:32:04 +0100663 static struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700664 {1, 1024, "invalid number of paths"},
665 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 };
667
668 int r;
Mike Snitzer498f0102011-08-02 12:32:04 +0100669 unsigned i, nr_selector_args, nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700671 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (as->argc < 2) {
674 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100675 ti->error = "not enough priority group arguments";
676 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
679 pg = alloc_priority_group();
680 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700681 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100682 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
684 pg->m = m;
685
686 r = parse_path_selector(as, pg, ti);
687 if (r)
688 goto bad;
689
690 /*
691 * read the paths
692 */
Mike Snitzer498f0102011-08-02 12:32:04 +0100693 r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (r)
695 goto bad;
696
Mike Snitzer498f0102011-08-02 12:32:04 +0100697 r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (r)
699 goto bad;
700
Mike Snitzer498f0102011-08-02 12:32:04 +0100701 nr_args = 1 + nr_selector_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 for (i = 0; i < pg->nr_pgpaths; i++) {
703 struct pgpath *pgpath;
Mike Snitzer498f0102011-08-02 12:32:04 +0100704 struct dm_arg_set path_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Mike Snitzer498f0102011-08-02 12:32:04 +0100706 if (as->argc < nr_args) {
Mikulas Patocka148acff2008-07-21 12:00:30 +0100707 ti->error = "not enough path parameters";
Alasdair G Kergon6bbf79a2010-08-12 04:13:49 +0100708 r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Mike Snitzer498f0102011-08-02 12:32:04 +0100712 path_args.argc = nr_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 path_args.argv = as->argv;
714
715 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100716 if (IS_ERR(pgpath)) {
717 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 pgpath->pg = pg;
722 list_add_tail(&pgpath->list, &pg->pgpaths);
Mike Snitzer498f0102011-08-02 12:32:04 +0100723 dm_consume_args(as, nr_args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725
726 return pg;
727
728 bad:
729 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100730 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Mike Snitzer498f0102011-08-02 12:32:04 +0100733static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700736 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700737 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Mike Snitzer498f0102011-08-02 12:32:04 +0100739 static struct dm_arg _args[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700740 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 };
742
Mike Snitzer498f0102011-08-02 12:32:04 +0100743 if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return -EINVAL;
745
746 if (!hw_argc)
747 return 0;
748
Mike Snitzer498f0102011-08-02 12:32:04 +0100749 m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
Mike Snitzer510193a2012-05-12 01:43:21 +0100750 if (!try_then_request_module(scsi_dh_handler_exist(m->hw_handler_name),
751 "scsi_dh_%s", m->hw_handler_name)) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700752 ti->error = "unknown hardware handler type";
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700753 ret = -EINVAL;
754 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
Chandra Seetharaman14e98c52008-11-13 23:39:06 +0000756
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700757 if (hw_argc > 1) {
758 char *p;
759 int i, j, len = 4;
760
761 for (i = 0; i <= hw_argc - 2; i++)
762 len += strlen(as->argv[i]) + 1;
763 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
764 if (!p) {
765 ti->error = "memory allocation failed";
766 ret = -ENOMEM;
767 goto fail;
768 }
769 j = sprintf(p, "%d", hw_argc - 1);
770 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
771 j = sprintf(p, "%s", as->argv[i]);
772 }
Mike Snitzer498f0102011-08-02 12:32:04 +0100773 dm_consume_args(as, hw_argc - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700776fail:
777 kfree(m->hw_handler_name);
778 m->hw_handler_name = NULL;
779 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Mike Snitzer498f0102011-08-02 12:32:04 +0100782static int parse_features(struct dm_arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783{
784 int r;
785 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700786 struct dm_target *ti = m->ti;
Mike Snitzer498f0102011-08-02 12:32:04 +0100787 const char *arg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Mike Snitzer498f0102011-08-02 12:32:04 +0100789 static struct dm_arg _args[] = {
Mike Snitzera58a9352012-07-27 15:08:04 +0100790 {0, 6, "invalid number of feature args"},
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100791 {1, 50, "pg_init_retries must be between 1 and 50"},
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000792 {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 };
794
Mike Snitzer498f0102011-08-02 12:32:04 +0100795 r = dm_read_arg_group(_args, as, &argc, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (r)
797 return -EINVAL;
798
799 if (!argc)
800 return 0;
801
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100802 do {
Mike Snitzer498f0102011-08-02 12:32:04 +0100803 arg_name = dm_shift_arg(as);
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100804 argc--;
805
Mike Snitzer498f0102011-08-02 12:32:04 +0100806 if (!strcasecmp(arg_name, "queue_if_no_path")) {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100807 r = queue_if_no_path(m, 1, 0);
808 continue;
809 }
810
Mike Snitzera58a9352012-07-27 15:08:04 +0100811 if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
812 m->retain_attached_hw_handler = 1;
813 continue;
814 }
815
Mike Snitzer498f0102011-08-02 12:32:04 +0100816 if (!strcasecmp(arg_name, "pg_init_retries") &&
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100817 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +0100818 r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100819 argc--;
820 continue;
821 }
822
Mike Snitzer498f0102011-08-02 12:32:04 +0100823 if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000824 (argc >= 1)) {
Mike Snitzer498f0102011-08-02 12:32:04 +0100825 r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +0000826 argc--;
827 continue;
828 }
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100831 r = -EINVAL;
832 } while (argc && !r);
833
834 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
837static int multipath_ctr(struct dm_target *ti, unsigned int argc,
838 char **argv)
839{
Mike Snitzer498f0102011-08-02 12:32:04 +0100840 /* target arguments */
841 static struct dm_arg _args[] = {
Mike Snitzera490a072011-03-24 13:54:33 +0000842 {0, 1024, "invalid number of priority groups"},
843 {0, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 };
845
846 int r;
847 struct multipath *m;
Mike Snitzer498f0102011-08-02 12:32:04 +0100848 struct dm_arg_set as;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 unsigned pg_count = 0;
850 unsigned next_pg_num;
851
852 as.argc = argc;
853 as.argv = argv;
854
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700855 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700857 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -EINVAL;
859 }
860
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700861 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (r)
863 goto bad;
864
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700865 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (r)
867 goto bad;
868
Mike Snitzer498f0102011-08-02 12:32:04 +0100869 r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (r)
871 goto bad;
872
Mike Snitzer498f0102011-08-02 12:32:04 +0100873 r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (r)
875 goto bad;
876
Mike Snitzera490a072011-03-24 13:54:33 +0000877 if ((!m->nr_priority_groups && next_pg_num) ||
878 (m->nr_priority_groups && !next_pg_num)) {
879 ti->error = "invalid initial priority group";
880 r = -EINVAL;
881 goto bad;
882 }
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 /* parse the priority groups */
885 while (as.argc) {
886 struct priority_group *pg;
887
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700888 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100889 if (IS_ERR(pg)) {
890 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 goto bad;
892 }
893
894 m->nr_valid_paths += pg->nr_pgpaths;
895 list_add_tail(&pg->list, &m->priority_groups);
896 pg_count++;
897 pg->pg_num = pg_count;
898 if (!--next_pg_num)
899 m->next_pg = pg;
900 }
901
902 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700903 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 r = -EINVAL;
905 goto bad;
906 }
907
Alasdair G Kergon55a62ee2013-03-01 22:45:47 +0000908 ti->num_flush_bios = 1;
909 ti->num_discard_bios = 1;
Mike Snitzer042bcef2013-05-10 14:37:16 +0100910 ti->num_write_same_bios = 1;
Mikulas Patocka86279212009-06-22 10:12:24 +0100911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return 0;
913
914 bad:
915 free_multipath(m);
916 return r;
917}
918
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000919static void multipath_wait_for_pg_init_completion(struct multipath *m)
920{
921 DECLARE_WAITQUEUE(wait, current);
922 unsigned long flags;
923
924 add_wait_queue(&m->pg_init_wait, &wait);
925
926 while (1) {
927 set_current_state(TASK_UNINTERRUPTIBLE);
928
929 spin_lock_irqsave(&m->lock, flags);
930 if (!m->pg_init_in_progress) {
931 spin_unlock_irqrestore(&m->lock, flags);
932 break;
933 }
934 spin_unlock_irqrestore(&m->lock, flags);
935
936 io_schedule();
937 }
938 set_current_state(TASK_RUNNING);
939
940 remove_wait_queue(&m->pg_init_wait, &wait);
941}
942
943static void flush_multipath_work(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700945 flush_workqueue(kmpath_handlerd);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000946 multipath_wait_for_pg_init_completion(m);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700947 flush_workqueue(kmultipathd);
Tejun Heo43829732012-08-20 14:51:24 -0700948 flush_work(&m->trigger_event);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +0000949}
950
951static void multipath_dtr(struct dm_target *ti)
952{
953 struct multipath *m = ti->private;
954
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000955 flush_multipath_work(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 free_multipath(m);
957}
958
959/*
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100960 * Map cloned requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100962static int multipath_map(struct dm_target *ti, struct request *clone,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 union map_info *map_context)
964{
965 int r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 struct multipath *m = (struct multipath *) ti->private;
967
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100968 if (set_mapinfo(m, map_context) < 0)
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100969 /* ENOMEM, requeue */
970 return DM_MAPIO_REQUEUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100972 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100973 r = map_io(m, clone, map_context, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800974 if (r < 0 || r == DM_MAPIO_REQUEUE)
Jun'ichi Nomura466891f2012-03-28 18:41:25 +0100975 clear_mapinfo(m, map_context);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 return r;
978}
979
980/*
981 * Take a path out of use.
982 */
983static int fail_path(struct pgpath *pgpath)
984{
985 unsigned long flags;
986 struct multipath *m = pgpath->pg->m;
987
988 spin_lock_irqsave(&m->lock, flags);
989
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100990 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto out;
992
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700993 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100996 pgpath->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 pgpath->fail_count++;
998
999 m->nr_valid_paths--;
1000
1001 if (pgpath == m->current_pgpath)
1002 m->current_pgpath = NULL;
1003
Mike Andersonb15546f2007-10-19 22:48:02 +01001004 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
1005 pgpath->path.dev->name, m->nr_valid_paths);
1006
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001007 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009out:
1010 spin_unlock_irqrestore(&m->lock, flags);
1011
1012 return 0;
1013}
1014
1015/*
1016 * Reinstate a previously-failed path
1017 */
1018static int reinstate_path(struct pgpath *pgpath)
1019{
1020 int r = 0;
1021 unsigned long flags;
1022 struct multipath *m = pgpath->pg->m;
1023
1024 spin_lock_irqsave(&m->lock, flags);
1025
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001026 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 goto out;
1028
Alasdair G Kergondef052d2008-07-21 12:00:31 +01001029 if (!pgpath->pg->ps.type->reinstate_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 DMWARN("Reinstate path not supported by path selector %s",
1031 pgpath->pg->ps.type->name);
1032 r = -EINVAL;
1033 goto out;
1034 }
1035
1036 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1037 if (r)
1038 goto out;
1039
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001040 pgpath->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001042 if (!m->nr_valid_paths++ && m->queue_size) {
1043 m->current_pgpath = NULL;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001044 queue_work(kmultipathd, &m->process_queued_ios);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001045 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001046 if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001047 m->pg_init_in_progress++;
1048 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Mike Andersonb15546f2007-10-19 22:48:02 +01001050 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1051 pgpath->path.dev->name, m->nr_valid_paths);
1052
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001053 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055out:
1056 spin_unlock_irqrestore(&m->lock, flags);
1057
1058 return r;
1059}
1060
1061/*
1062 * Fail or reinstate all paths that match the provided struct dm_dev.
1063 */
1064static int action_dev(struct multipath *m, struct dm_dev *dev,
1065 action_fn action)
1066{
Mike Snitzer19040c02011-03-24 13:54:31 +00001067 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 struct pgpath *pgpath;
1069 struct priority_group *pg;
1070
1071 list_for_each_entry(pg, &m->priority_groups, list) {
1072 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1073 if (pgpath->path.dev == dev)
1074 r = action(pgpath);
1075 }
1076 }
1077
1078 return r;
1079}
1080
1081/*
1082 * Temporarily try to avoid having to use the specified PG
1083 */
1084static void bypass_pg(struct multipath *m, struct priority_group *pg,
1085 int bypassed)
1086{
1087 unsigned long flags;
1088
1089 spin_lock_irqsave(&m->lock, flags);
1090
1091 pg->bypassed = bypassed;
1092 m->current_pgpath = NULL;
1093 m->current_pg = NULL;
1094
1095 spin_unlock_irqrestore(&m->lock, flags);
1096
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001097 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
1100/*
1101 * Switch to using the specified PG from the next I/O that gets mapped
1102 */
1103static int switch_pg_num(struct multipath *m, const char *pgstr)
1104{
1105 struct priority_group *pg;
1106 unsigned pgnum;
1107 unsigned long flags;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001108 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001110 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 (pgnum > m->nr_priority_groups)) {
1112 DMWARN("invalid PG number supplied to switch_pg_num");
1113 return -EINVAL;
1114 }
1115
1116 spin_lock_irqsave(&m->lock, flags);
1117 list_for_each_entry(pg, &m->priority_groups, list) {
1118 pg->bypassed = 0;
1119 if (--pgnum)
1120 continue;
1121
1122 m->current_pgpath = NULL;
1123 m->current_pg = NULL;
1124 m->next_pg = pg;
1125 }
1126 spin_unlock_irqrestore(&m->lock, flags);
1127
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001128 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 return 0;
1130}
1131
1132/*
1133 * Set/clear bypassed status of a PG.
1134 * PGs are numbered upwards from 1 in the order they were declared.
1135 */
1136static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1137{
1138 struct priority_group *pg;
1139 unsigned pgnum;
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001140 char dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Mikulas Patocka31998ef2012-03-28 18:41:26 +01001142 if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 (pgnum > m->nr_priority_groups)) {
1144 DMWARN("invalid PG number supplied to bypass_pg");
1145 return -EINVAL;
1146 }
1147
1148 list_for_each_entry(pg, &m->priority_groups, list) {
1149 if (!--pgnum)
1150 break;
1151 }
1152
1153 bypass_pg(m, pg, bypassed);
1154 return 0;
1155}
1156
1157/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001158 * Should we retry pg_init immediately?
1159 */
1160static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1161{
1162 unsigned long flags;
1163 int limit_reached = 0;
1164
1165 spin_lock_irqsave(&m->lock, flags);
1166
1167 if (m->pg_init_count <= m->pg_init_retries)
1168 m->pg_init_required = 1;
1169 else
1170 limit_reached = 1;
1171
1172 spin_unlock_irqrestore(&m->lock, flags);
1173
1174 return limit_reached;
1175}
1176
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001177static void pg_init_done(void *data, int errors)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001178{
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001179 struct pgpath *pgpath = data;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001180 struct priority_group *pg = pgpath->pg;
1181 struct multipath *m = pg->m;
1182 unsigned long flags;
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001183 unsigned delay_retry = 0;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001184
1185 /* device or driver problems */
1186 switch (errors) {
1187 case SCSI_DH_OK:
1188 break;
1189 case SCSI_DH_NOSYS:
1190 if (!m->hw_handler_name) {
1191 errors = 0;
1192 break;
1193 }
Moger, Babuf7b934c2010-03-06 02:29:49 +00001194 DMERR("Could not failover the device: Handler scsi_dh_%s "
1195 "Error %d.", m->hw_handler_name, errors);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001196 /*
1197 * Fail path for now, so we do not ping pong
1198 */
1199 fail_path(pgpath);
1200 break;
1201 case SCSI_DH_DEV_TEMP_BUSY:
1202 /*
1203 * Probably doing something like FW upgrade on the
1204 * controller so try the other pg.
1205 */
1206 bypass_pg(m, pg, 1);
1207 break;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001208 case SCSI_DH_RETRY:
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001209 /* Wait before retrying. */
1210 delay_retry = 1;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001211 case SCSI_DH_IMM_RETRY:
1212 case SCSI_DH_RES_TEMP_UNAVAIL:
1213 if (pg_init_limit_reached(m, pgpath))
1214 fail_path(pgpath);
1215 errors = 0;
1216 break;
1217 default:
1218 /*
1219 * We probably do not want to fail the path for a device
1220 * error, but this is what the old dm did. In future
1221 * patches we can do more advanced handling.
1222 */
1223 fail_path(pgpath);
1224 }
1225
1226 spin_lock_irqsave(&m->lock, flags);
1227 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001228 if (pgpath == m->current_pgpath) {
1229 DMERR("Could not failover device. Error %d.", errors);
1230 m->current_pgpath = NULL;
1231 m->current_pg = NULL;
1232 }
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001233 } else if (!m->pg_init_required)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001234 pg->bypassed = 0;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001235
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001236 if (--m->pg_init_in_progress)
1237 /* Activations of other paths are still on going */
1238 goto out;
1239
1240 if (!m->pg_init_required)
1241 m->queue_io = 0;
1242
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001243 m->pg_init_delay_retry = delay_retry;
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001244 queue_work(kmultipathd, &m->process_queued_ios);
1245
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001246 /*
1247 * Wake up any thread waiting to suspend.
1248 */
1249 wake_up(&m->pg_init_wait);
1250
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001251out:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001252 spin_unlock_irqrestore(&m->lock, flags);
1253}
1254
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001255static void activate_path(struct work_struct *work)
1256{
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001257 struct pgpath *pgpath =
Chandra Seetharaman4e2d19e2011-01-13 20:00:01 +00001258 container_of(work, struct pgpath, activate_path.work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001259
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001260 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001261 pg_init_done, pgpath);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001262}
1263
Hannes Reinecke7e782af2013-07-01 15:16:26 +02001264static int noretry_error(int error)
1265{
1266 switch (error) {
1267 case -EOPNOTSUPP:
1268 case -EREMOTEIO:
1269 case -EILSEQ:
1270 case -ENODATA:
1271 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
Hannes Reinecke7e782af2013-07-01 15:16:26 +02001301 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;
1326 struct dm_mpath_io *mpio = map_context->ptr;
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
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001333 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 }
Jun'ichi Nomura466891f2012-03-28 18:41:25 +01001340 clear_mapinfo(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{
1353 struct multipath *m = (struct multipath *) 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{
1372 struct multipath *m = (struct multipath *) ti->private;
1373 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;
1401 struct multipath *m = (struct multipath *) 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)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001411 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1412 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;
1509 struct multipath *m = (struct multipath *) ti->private;
1510 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) {
1530 DMWARN("Unrecognised multipath message received.");
1531 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 {
1548 DMWARN("Unrecognised multipath message received.");
1549 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
Al Viro647b3d02007-08-28 22:15:59 -04001568static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
Milan Broz9af4aa32006-10-03 01:15:20 -07001569 unsigned long arg)
1570{
Mikulas Patocka35991652012-06-03 00:29:58 +01001571 struct multipath *m = ti->private;
Mike Snitzer7ba10aa2012-09-26 23:45:41 +01001572 struct pgpath *pgpath;
Mikulas Patocka35991652012-06-03 00:29:58 +01001573 struct block_device *bdev;
1574 fmode_t mode;
Milan Broz9af4aa32006-10-03 01:15:20 -07001575 unsigned long flags;
Mikulas Patocka35991652012-06-03 00:29:58 +01001576 int r;
1577
Mikulas Patocka35991652012-06-03 00:29:58 +01001578 bdev = NULL;
1579 mode = 0;
1580 r = 0;
Milan Broz9af4aa32006-10-03 01:15:20 -07001581
1582 spin_lock_irqsave(&m->lock, flags);
1583
1584 if (!m->current_pgpath)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001585 __choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001586
Mike Snitzer7ba10aa2012-09-26 23:45:41 +01001587 pgpath = m->current_pgpath;
1588
1589 if (pgpath) {
1590 bdev = pgpath->path.dev->bdev;
1591 mode = pgpath->path.dev->mode;
Milan Broze90dae12006-10-03 01:15:22 -07001592 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001593
Mike Snitzer7ba10aa2012-09-26 23:45:41 +01001594 if ((pgpath && m->queue_io) || (!pgpath && m->queue_if_no_path))
Hannes Reinecke6c182cd2013-07-10 23:41:15 +01001595 r = -ENOTCONN;
Milan Broz9af4aa32006-10-03 01:15:20 -07001596 else if (!bdev)
1597 r = -EIO;
1598
1599 spin_unlock_irqrestore(&m->lock, flags);
1600
Paolo Bonziniec8013b2012-01-12 16:01:29 +01001601 /*
1602 * Only pass ioctls through if the device sizes match exactly.
1603 */
1604 if (!r && ti->len != i_size_read(bdev->bd_inode) >> SECTOR_SHIFT)
1605 r = scsi_verify_blk_ioctl(NULL, cmd);
1606
Hannes Reinecke6c182cd2013-07-10 23:41:15 +01001607 if (r == -ENOTCONN && !fatal_signal_pending(current))
Mikulas Patocka35991652012-06-03 00:29:58 +01001608 queue_work(kmultipathd, &m->process_queued_ios);
Mikulas Patocka35991652012-06-03 00:29:58 +01001609
Al Viro633a08b2007-08-29 20:34:12 -04001610 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001611}
1612
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001613static int multipath_iterate_devices(struct dm_target *ti,
1614 iterate_devices_callout_fn fn, void *data)
1615{
1616 struct multipath *m = ti->private;
1617 struct priority_group *pg;
1618 struct pgpath *p;
1619 int ret = 0;
1620
1621 list_for_each_entry(pg, &m->priority_groups, list) {
1622 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01001623 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001624 if (ret)
1625 goto out;
1626 }
1627 }
1628
1629out:
1630 return ret;
1631}
1632
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001633static int __pgpath_busy(struct pgpath *pgpath)
1634{
1635 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1636
1637 return dm_underlying_device_busy(q);
1638}
1639
1640/*
1641 * We return "busy", only when we can map I/Os but underlying devices
1642 * are busy (so even if we map I/Os now, the I/Os will wait on
1643 * the underlying queue).
1644 * In other words, if we want to kill I/Os or queue them inside us
1645 * due to map unavailability, we don't return "busy". Otherwise,
1646 * dm core won't give us the I/Os and we can't do what we want.
1647 */
1648static int multipath_busy(struct dm_target *ti)
1649{
1650 int busy = 0, has_active = 0;
1651 struct multipath *m = ti->private;
1652 struct priority_group *pg;
1653 struct pgpath *pgpath;
1654 unsigned long flags;
1655
1656 spin_lock_irqsave(&m->lock, flags);
1657
1658 /* Guess which priority_group will be used at next mapping time */
1659 if (unlikely(!m->current_pgpath && m->next_pg))
1660 pg = m->next_pg;
1661 else if (likely(m->current_pg))
1662 pg = m->current_pg;
1663 else
1664 /*
1665 * We don't know which pg will be used at next mapping time.
1666 * We don't call __choose_pgpath() here to avoid to trigger
1667 * pg_init just by busy checking.
1668 * So we don't know whether underlying devices we will be using
1669 * at next mapping time are busy or not. Just try mapping.
1670 */
1671 goto out;
1672
1673 /*
1674 * If there is one non-busy active path at least, the path selector
1675 * will be able to select it. So we consider such a pg as not busy.
1676 */
1677 busy = 1;
1678 list_for_each_entry(pgpath, &pg->pgpaths, list)
1679 if (pgpath->is_active) {
1680 has_active = 1;
1681
1682 if (!__pgpath_busy(pgpath)) {
1683 busy = 0;
1684 break;
1685 }
1686 }
1687
1688 if (!has_active)
1689 /*
1690 * No active path in this pg, so this pg won't be used and
1691 * the current_pg will be changed at next mapping time.
1692 * We need to try mapping to determine it.
1693 */
1694 busy = 0;
1695
1696out:
1697 spin_unlock_irqrestore(&m->lock, flags);
1698
1699 return busy;
1700}
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702/*-----------------------------------------------------------------
1703 * Module setup
1704 *---------------------------------------------------------------*/
1705static struct target_type multipath_target = {
1706 .name = "multipath",
Mikulas Patockafd7c0922013-03-01 22:45:44 +00001707 .version = {1, 5, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 .module = THIS_MODULE,
1709 .ctr = multipath_ctr,
1710 .dtr = multipath_dtr,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001711 .map_rq = multipath_map,
1712 .rq_end_io = multipath_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 .presuspend = multipath_presuspend,
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001714 .postsuspend = multipath_postsuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 .resume = multipath_resume,
1716 .status = multipath_status,
1717 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001718 .ioctl = multipath_ioctl,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001719 .iterate_devices = multipath_iterate_devices,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001720 .busy = multipath_busy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721};
1722
1723static int __init dm_multipath_init(void)
1724{
1725 int r;
1726
1727 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001728 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 if (!_mpio_cache)
1730 return -ENOMEM;
1731
1732 r = dm_register_target(&multipath_target);
1733 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001734 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 kmem_cache_destroy(_mpio_cache);
1736 return -EINVAL;
1737 }
1738
Tejun Heo4d4d66a2011-01-13 19:59:57 +00001739 kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001740 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001741 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001742 dm_unregister_target(&multipath_target);
1743 kmem_cache_destroy(_mpio_cache);
1744 return -ENOMEM;
1745 }
1746
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001747 /*
1748 * A separate workqueue is used to handle the device handlers
1749 * to avoid overloading existing workqueue. Overloading the
1750 * old workqueue would also create a bottleneck in the
1751 * path of the storage hardware device activation.
1752 */
Tejun Heo4d4d66a2011-01-13 19:59:57 +00001753 kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
1754 WQ_MEM_RECLAIM);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001755 if (!kmpath_handlerd) {
1756 DMERR("failed to create workqueue kmpath_handlerd");
1757 destroy_workqueue(kmultipathd);
1758 dm_unregister_target(&multipath_target);
1759 kmem_cache_destroy(_mpio_cache);
1760 return -ENOMEM;
1761 }
1762
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001763 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 multipath_target.version[0], multipath_target.version[1],
1765 multipath_target.version[2]);
1766
1767 return r;
1768}
1769
1770static void __exit dm_multipath_exit(void)
1771{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001772 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001773 destroy_workqueue(kmultipathd);
1774
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001775 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 kmem_cache_destroy(_mpio_cache);
1777}
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779module_init(dm_multipath_init);
1780module_exit(dm_multipath_exit);
1781
1782MODULE_DESCRIPTION(DM_NAME " multipath target");
1783MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1784MODULE_LICENSE("GPL");