blob: 92bdcbb7c9356c94d15ca8458168718415502fd6 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "dm-bio-record.h"
Mike Andersonb15546f2007-10-19 22:48:02 +010012#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#include <linux/ctype.h>
15#include <linux/init.h>
16#include <linux/mempool.h>
17#include <linux/module.h>
18#include <linux/pagemap.h>
19#include <linux/slab.h>
20#include <linux/time.h>
21#include <linux/workqueue.h>
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070022#include <scsi/scsi_dh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/atomic.h>
24
Alasdair G Kergon72d94862006-06-26 00:27:35 -070025#define DM_MSG_PREFIX "multipath"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#define MESG_STR(x) x, sizeof(x)
27
28/* Path properties */
29struct pgpath {
30 struct list_head list;
31
32 struct priority_group *pg; /* Owning PG */
Kiyoshi Ueda66800732008-10-10 13:36:58 +010033 unsigned is_active; /* Path status */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 unsigned fail_count; /* Cumulative failure count */
35
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080036 struct dm_path path;
Mike Anderson224cb3e2008-08-29 09:36:09 +020037 struct work_struct deactivate_path;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +010038 struct work_struct 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
65 spinlock_t lock;
66
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070067 const char *hw_handler_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned nr_priority_groups;
69 struct list_head priority_groups;
70 unsigned pg_init_required; /* pg_init needs calling? */
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -070071 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 unsigned nr_valid_paths; /* Total number of usable paths */
74 struct pgpath *current_pgpath;
75 struct priority_group *current_pg;
76 struct priority_group *next_pg; /* Switch to this PG if set */
77 unsigned repeat_count; /* I/Os left before calling PS again */
78
79 unsigned queue_io; /* Must we queue all I/O? */
80 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
Alasdair G Kergon436d4102005-07-12 15:53:03 -070081 unsigned saved_queue_if_no_path;/* Saved state during suspension */
Dave Wysochanskic9e45582007-10-19 22:47:53 +010082 unsigned pg_init_retries; /* Number of times to retry pg_init */
83 unsigned pg_init_count; /* Number of times pg_init called */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 struct work_struct process_queued_ios;
86 struct bio_list queued_ios;
87 unsigned queue_size;
88
89 struct work_struct trigger_event;
90
91 /*
Alasdair G Kergon028867a2007-07-12 17:26:32 +010092 * We must use a mempool of dm_mpath_io structs so that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * can resubmit bios on error.
94 */
95 mempool_t *mpio_pool;
96};
97
98/*
99 * Context information attached to each bio we process.
100 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100101struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct pgpath *pgpath;
103 struct dm_bio_details details;
104};
105
106typedef int (*action_fn) (struct pgpath *pgpath);
107
108#define MIN_IOS 256 /* Mempool size */
109
Christoph Lametere18b8902006-12-06 20:33:20 -0800110static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700112static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000113static void process_queued_ios(struct work_struct *work);
114static void trigger_event(struct work_struct *work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700115static void activate_path(struct work_struct *work);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200116static void deactivate_path(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118
119/*-----------------------------------------------
120 * Allocation routines
121 *-----------------------------------------------*/
122
123static struct pgpath *alloc_pgpath(void)
124{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700125 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Mike Anderson224cb3e2008-08-29 09:36:09 +0200127 if (pgpath) {
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100128 pgpath->is_active = 1;
Mike Anderson224cb3e2008-08-29 09:36:09 +0200129 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100130 INIT_WORK(&pgpath->activate_path, activate_path);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 return pgpath;
134}
135
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100136static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 kfree(pgpath);
139}
140
Mike Anderson224cb3e2008-08-29 09:36:09 +0200141static void deactivate_path(struct work_struct *work)
142{
143 struct pgpath *pgpath =
144 container_of(work, struct pgpath, deactivate_path);
145
146 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static struct priority_group *alloc_priority_group(void)
150{
151 struct priority_group *pg;
152
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700153 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700155 if (pg)
156 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 return pg;
159}
160
161static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
162{
163 struct pgpath *pgpath, *tmp;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700164 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
167 list_del(&pgpath->list);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700168 if (m->hw_handler_name)
169 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 dm_put_device(ti, pgpath->path.dev);
171 free_pgpath(pgpath);
172 }
173}
174
175static void free_priority_group(struct priority_group *pg,
176 struct dm_target *ti)
177{
178 struct path_selector *ps = &pg->ps;
179
180 if (ps->type) {
181 ps->type->destroy(ps);
182 dm_put_path_selector(ps->type);
183 }
184
185 free_pgpaths(&pg->pgpaths, ti);
186 kfree(pg);
187}
188
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700189static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct multipath *m;
192
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700193 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 INIT_LIST_HEAD(&m->priority_groups);
196 spin_lock_init(&m->lock);
197 m->queue_io = 1;
David Howellsc4028952006-11-22 14:57:56 +0000198 INIT_WORK(&m->process_queued_ios, process_queued_ios);
199 INIT_WORK(&m->trigger_event, trigger_event);
Matthew Dobson93d23412006-03-26 01:37:50 -0800200 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (!m->mpio_pool) {
202 kfree(m);
203 return NULL;
204 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700205 m->ti = ti;
206 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
209 return m;
210}
211
212static void free_multipath(struct multipath *m)
213{
214 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
217 list_del(&pg->list);
218 free_priority_group(pg, m->ti);
219 }
220
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700221 kfree(m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 mempool_destroy(m->mpio_pool);
223 kfree(m);
224}
225
226
227/*-----------------------------------------------
228 * Path selection
229 *-----------------------------------------------*/
230
231static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
232{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 m->current_pg = pgpath->pg;
234
235 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700236 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 m->pg_init_required = 1;
238 m->queue_io = 1;
239 } else {
240 m->pg_init_required = 0;
241 m->queue_io = 0;
242 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100243
244 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
248{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800249 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
252 if (!path)
253 return -ENXIO;
254
255 m->current_pgpath = path_to_pgpath(path);
256
257 if (m->current_pg != pg)
258 __switch_pg(m, m->current_pgpath);
259
260 return 0;
261}
262
263static void __choose_pgpath(struct multipath *m)
264{
265 struct priority_group *pg;
266 unsigned bypassed = 1;
267
268 if (!m->nr_valid_paths)
269 goto failed;
270
271 /* Were we instructed to switch PG? */
272 if (m->next_pg) {
273 pg = m->next_pg;
274 m->next_pg = NULL;
275 if (!__choose_path_in_pg(m, pg))
276 return;
277 }
278
279 /* Don't change PG until it has no remaining paths */
280 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
281 return;
282
283 /*
284 * Loop through priority groups until we find a valid path.
285 * First time we skip PGs marked 'bypassed'.
286 * Second time we only try the ones we skipped.
287 */
288 do {
289 list_for_each_entry(pg, &m->priority_groups, list) {
290 if (pg->bypassed == bypassed)
291 continue;
292 if (!__choose_path_in_pg(m, pg))
293 return;
294 }
295 } while (bypassed--);
296
297failed:
298 m->current_pgpath = NULL;
299 m->current_pg = NULL;
300}
301
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800302/*
303 * Check whether bios must be queued in the device-mapper core rather
304 * than here in the target.
305 *
306 * m->lock must be held on entry.
307 *
308 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
309 * same value then we are not between multipath_presuspend()
310 * and multipath_resume() calls and we have no need to check
311 * for the DMF_NOFLUSH_SUSPENDING flag.
312 */
313static int __must_push_back(struct multipath *m)
314{
315 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
316 dm_noflush_suspending(m->ti));
317}
318
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100319static int map_io(struct multipath *m, struct bio *bio,
320 struct dm_mpath_io *mpio, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800322 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned long flags;
324 struct pgpath *pgpath;
325
326 spin_lock_irqsave(&m->lock, flags);
327
328 /* Do we need to select a new pgpath? */
329 if (!m->current_pgpath ||
330 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
331 __choose_pgpath(m);
332
333 pgpath = m->current_pgpath;
334
335 if (was_queued)
336 m->queue_size--;
337
338 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700339 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 /* Queue for the daemon to resubmit */
341 bio_list_add(&m->queued_ios, bio);
342 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700343 if ((m->pg_init_required && !m->pg_init_in_progress) ||
344 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700345 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800347 r = DM_MAPIO_SUBMITTED;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800348 } else if (pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 bio->bi_bdev = pgpath->path.dev->bdev;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800350 else if (__must_push_back(m))
351 r = DM_MAPIO_REQUEUE;
352 else
353 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 mpio->pgpath = pgpath;
356
357 spin_unlock_irqrestore(&m->lock, flags);
358
359 return r;
360}
361
362/*
363 * If we run out of usable paths, should we queue I/O or error it?
364 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700365static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
366 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 unsigned long flags;
369
370 spin_lock_irqsave(&m->lock, flags);
371
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700372 if (save_old_value)
373 m->saved_queue_if_no_path = m->queue_if_no_path;
374 else
375 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700377 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700378 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380 spin_unlock_irqrestore(&m->lock, flags);
381
382 return 0;
383}
384
385/*-----------------------------------------------------------------
386 * The multipath daemon is responsible for resubmitting queued ios.
387 *---------------------------------------------------------------*/
388
389static void dispatch_queued_ios(struct multipath *m)
390{
391 int r;
392 unsigned long flags;
393 struct bio *bio = NULL, *next;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100394 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 union map_info *info;
396
397 spin_lock_irqsave(&m->lock, flags);
398 bio = bio_list_get(&m->queued_ios);
399 spin_unlock_irqrestore(&m->lock, flags);
400
401 while (bio) {
402 next = bio->bi_next;
403 bio->bi_next = NULL;
404
405 info = dm_get_mapinfo(bio);
406 mpio = info->ptr;
407
408 r = map_io(m, bio, mpio, 1);
409 if (r < 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200410 bio_endio(bio, r);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800411 else if (r == DM_MAPIO_REMAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 generic_make_request(bio);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800413 else if (r == DM_MAPIO_REQUEUE)
NeilBrown6712ecf2007-09-27 12:47:43 +0200414 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 bio = next;
417 }
418}
419
David Howellsc4028952006-11-22 14:57:56 +0000420static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
David Howellsc4028952006-11-22 14:57:56 +0000422 struct multipath *m =
423 container_of(work, struct multipath, process_queued_ios);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100424 struct pgpath *pgpath = NULL, *tmp;
425 unsigned must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 unsigned long flags;
427
428 spin_lock_irqsave(&m->lock, flags);
429
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700430 if (!m->queue_size)
431 goto out;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (!m->current_pgpath)
434 __choose_pgpath(m);
435
436 pgpath = m->current_pgpath;
437
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700438 if ((pgpath && !m->queue_io) ||
439 (!pgpath && !m->queue_if_no_path))
440 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Chandra Seetharamanb81aa1c2008-11-13 23:39:00 +0000442 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100443 m->pg_init_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 m->pg_init_required = 0;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100445 list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
446 if (queue_work(kmpath_handlerd, &tmp->activate_path))
447 m->pg_init_in_progress++;
448 }
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700449 }
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700450out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (!must_queue)
453 dispatch_queued_ios(m);
454}
455
456/*
457 * An event is triggered whenever a path is taken out of use.
458 * Includes path failure and PG bypass.
459 */
David Howellsc4028952006-11-22 14:57:56 +0000460static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
David Howellsc4028952006-11-22 14:57:56 +0000462 struct multipath *m =
463 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
465 dm_table_event(m->ti->table);
466}
467
468/*-----------------------------------------------------------------
469 * Constructor/argument parsing:
470 * <#multipath feature args> [<arg>]*
471 * <#hw_handler args> [hw_handler [<arg>]*]
472 * <#priority groups>
473 * <initial priority group>
474 * [<selector> <#selector args> [<arg>]*
475 * <#paths> <#per-path selector args>
476 * [<path> [<arg>]* ]+ ]+
477 *---------------------------------------------------------------*/
478struct param {
479 unsigned min;
480 unsigned max;
481 char *error;
482};
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484static int read_param(struct param *param, char *str, unsigned *v, char **error)
485{
486 if (!str ||
487 (sscanf(str, "%u", v) != 1) ||
488 (*v < param->min) ||
489 (*v > param->max)) {
490 *error = param->error;
491 return -EINVAL;
492 }
493
494 return 0;
495}
496
497struct arg_set {
498 unsigned argc;
499 char **argv;
500};
501
502static char *shift(struct arg_set *as)
503{
504 char *r;
505
506 if (as->argc) {
507 as->argc--;
508 r = *as->argv;
509 as->argv++;
510 return r;
511 }
512
513 return NULL;
514}
515
516static void consume(struct arg_set *as, unsigned n)
517{
518 BUG_ON (as->argc < n);
519 as->argc -= n;
520 as->argv += n;
521}
522
523static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
524 struct dm_target *ti)
525{
526 int r;
527 struct path_selector_type *pst;
528 unsigned ps_argc;
529
530 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700531 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 };
533
534 pst = dm_get_path_selector(shift(as));
535 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700536 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return -EINVAL;
538 }
539
540 r = read_param(_params, shift(as), &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100541 if (r) {
542 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Mikulas Patocka0e0497c2009-06-22 10:08:02 +0100546 if (ps_argc > as->argc) {
547 dm_put_path_selector(pst);
548 ti->error = "not enough arguments for path selector";
549 return -EINVAL;
550 }
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 r = pst->create(&pg->ps, ps_argc, as->argv);
553 if (r) {
554 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700555 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return r;
557 }
558
559 pg->ps.type = pst;
560 consume(as, ps_argc);
561
562 return 0;
563}
564
565static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
566 struct dm_target *ti)
567{
568 int r;
569 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700570 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /* we need at least a path arg */
573 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700574 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100575 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 p = alloc_pgpath();
579 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100580 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
583 dm_table_get_mode(ti->table), &p->path.dev);
584 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700585 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 goto bad;
587 }
588
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700589 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100590 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
591
592 r = scsi_dh_attach(q, m->hw_handler_name);
593 if (r == -EBUSY) {
594 /*
595 * Already attached to different hw_handler,
596 * try to reattach with correct one.
597 */
598 scsi_dh_detach(q);
599 r = scsi_dh_attach(q, m->hw_handler_name);
600 }
601
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700602 if (r < 0) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100603 ti->error = "error attaching hardware handler";
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700604 dm_put_device(ti, p->path.dev);
605 goto bad;
606 }
607 }
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
610 if (r) {
611 dm_put_device(ti, p->path.dev);
612 goto bad;
613 }
614
615 return p;
616
617 bad:
618 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100619 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700623 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700626 {1, 1024, "invalid number of paths"},
627 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 };
629
630 int r;
631 unsigned i, nr_selector_args, nr_params;
632 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700633 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635 if (as->argc < 2) {
636 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100637 ti->error = "not enough priority group arguments";
638 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640
641 pg = alloc_priority_group();
642 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700643 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100644 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646 pg->m = m;
647
648 r = parse_path_selector(as, pg, ti);
649 if (r)
650 goto bad;
651
652 /*
653 * read the paths
654 */
655 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
656 if (r)
657 goto bad;
658
659 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
660 if (r)
661 goto bad;
662
663 nr_params = 1 + nr_selector_args;
664 for (i = 0; i < pg->nr_pgpaths; i++) {
665 struct pgpath *pgpath;
666 struct arg_set path_args;
667
Mikulas Patocka148acff2008-07-21 12:00:30 +0100668 if (as->argc < nr_params) {
669 ti->error = "not enough path parameters";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 path_args.argc = nr_params;
674 path_args.argv = as->argv;
675
676 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100677 if (IS_ERR(pgpath)) {
678 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 pgpath->pg = pg;
683 list_add_tail(&pgpath->list, &pg->pgpaths);
684 consume(as, nr_params);
685 }
686
687 return pg;
688
689 bad:
690 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100691 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700694static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 unsigned hw_argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700697 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700700 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 };
702
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700703 if (read_param(_params, shift(as), &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return -EINVAL;
705
706 if (!hw_argc)
707 return 0;
708
Mikulas Patockae094f4f2009-06-22 10:12:10 +0100709 if (hw_argc > as->argc) {
710 ti->error = "not enough arguments for hardware handler";
711 return -EINVAL;
712 }
713
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700714 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
715 request_module("scsi_dh_%s", m->hw_handler_name);
716 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700717 ti->error = "unknown hardware handler type";
Chandra Seetharamanfe9233f2008-05-23 18:16:40 -0700718 kfree(m->hw_handler_name);
719 m->hw_handler_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return -EINVAL;
721 }
Chandra Seetharaman14e98c52008-11-13 23:39:06 +0000722
723 if (hw_argc > 1)
724 DMWARN("Ignoring user-specified arguments for "
725 "hardware handler \"%s\"", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 consume(as, hw_argc - 1);
727
728 return 0;
729}
730
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700731static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 int r;
734 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700735 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100736 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100739 {0, 3, "invalid number of feature args"},
740 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 };
742
743 r = read_param(_params, shift(as), &argc, &ti->error);
744 if (r)
745 return -EINVAL;
746
747 if (!argc)
748 return 0;
749
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100750 do {
751 param_name = shift(as);
752 argc--;
753
754 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
755 r = queue_if_no_path(m, 1, 0);
756 continue;
757 }
758
759 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
760 (argc >= 1)) {
761 r = read_param(_params + 1, shift(as),
762 &m->pg_init_retries, &ti->error);
763 argc--;
764 continue;
765 }
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100768 r = -EINVAL;
769 } while (argc && !r);
770
771 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772}
773
774static int multipath_ctr(struct dm_target *ti, unsigned int argc,
775 char **argv)
776{
777 /* target parameters */
778 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700779 {1, 1024, "invalid number of priority groups"},
780 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 };
782
783 int r;
784 struct multipath *m;
785 struct arg_set as;
786 unsigned pg_count = 0;
787 unsigned next_pg_num;
788
789 as.argc = argc;
790 as.argv = argv;
791
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700792 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700794 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return -EINVAL;
796 }
797
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700798 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (r)
800 goto bad;
801
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700802 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (r)
804 goto bad;
805
806 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
807 if (r)
808 goto bad;
809
810 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
811 if (r)
812 goto bad;
813
814 /* parse the priority groups */
815 while (as.argc) {
816 struct priority_group *pg;
817
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700818 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100819 if (IS_ERR(pg)) {
820 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 goto bad;
822 }
823
824 m->nr_valid_paths += pg->nr_pgpaths;
825 list_add_tail(&pg->list, &m->priority_groups);
826 pg_count++;
827 pg->pg_num = pg_count;
828 if (!--next_pg_num)
829 m->next_pg = pg;
830 }
831
832 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700833 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 r = -EINVAL;
835 goto bad;
836 }
837
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return 0;
839
840 bad:
841 free_multipath(m);
842 return r;
843}
844
845static void multipath_dtr(struct dm_target *ti)
846{
847 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700848
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700849 flush_workqueue(kmpath_handlerd);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700850 flush_workqueue(kmultipathd);
Mikulas Patocka53b351f2009-06-22 10:12:13 +0100851 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 free_multipath(m);
853}
854
855/*
856 * Map bios, recording original fields for later in case we have to resubmit
857 */
858static int multipath_map(struct dm_target *ti, struct bio *bio,
859 union map_info *map_context)
860{
861 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100862 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 struct multipath *m = (struct multipath *) ti->private;
864
865 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
866 dm_bio_record(&mpio->details, bio);
867
868 map_context->ptr = mpio;
Mike Christie6000a362008-08-19 18:45:30 -0500869 bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 r = map_io(m, bio, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800871 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 mempool_free(mpio, m->mpio_pool);
873
874 return r;
875}
876
877/*
878 * Take a path out of use.
879 */
880static int fail_path(struct pgpath *pgpath)
881{
882 unsigned long flags;
883 struct multipath *m = pgpath->pg->m;
884
885 spin_lock_irqsave(&m->lock, flags);
886
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100887 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto out;
889
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700890 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100893 pgpath->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 pgpath->fail_count++;
895
896 m->nr_valid_paths--;
897
898 if (pgpath == m->current_pgpath)
899 m->current_pgpath = NULL;
900
Mike Andersonb15546f2007-10-19 22:48:02 +0100901 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
902 pgpath->path.dev->name, m->nr_valid_paths);
903
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +0000904 schedule_work(&m->trigger_event);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200905 queue_work(kmultipathd, &pgpath->deactivate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907out:
908 spin_unlock_irqrestore(&m->lock, flags);
909
910 return 0;
911}
912
913/*
914 * Reinstate a previously-failed path
915 */
916static int reinstate_path(struct pgpath *pgpath)
917{
918 int r = 0;
919 unsigned long flags;
920 struct multipath *m = pgpath->pg->m;
921
922 spin_lock_irqsave(&m->lock, flags);
923
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100924 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 goto out;
926
Alasdair G Kergondef052d2008-07-21 12:00:31 +0100927 if (!pgpath->pg->ps.type->reinstate_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 DMWARN("Reinstate path not supported by path selector %s",
929 pgpath->pg->ps.type->name);
930 r = -EINVAL;
931 goto out;
932 }
933
934 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
935 if (r)
936 goto out;
937
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100938 pgpath->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100940 if (!m->nr_valid_paths++ && m->queue_size) {
941 m->current_pgpath = NULL;
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700942 queue_work(kmultipathd, &m->process_queued_ios);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100943 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
944 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
945 m->pg_init_in_progress++;
946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
Mike Andersonb15546f2007-10-19 22:48:02 +0100948 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
949 pgpath->path.dev->name, m->nr_valid_paths);
950
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +0000951 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953out:
954 spin_unlock_irqrestore(&m->lock, flags);
955
956 return r;
957}
958
959/*
960 * Fail or reinstate all paths that match the provided struct dm_dev.
961 */
962static int action_dev(struct multipath *m, struct dm_dev *dev,
963 action_fn action)
964{
965 int r = 0;
966 struct pgpath *pgpath;
967 struct priority_group *pg;
968
969 list_for_each_entry(pg, &m->priority_groups, list) {
970 list_for_each_entry(pgpath, &pg->pgpaths, list) {
971 if (pgpath->path.dev == dev)
972 r = action(pgpath);
973 }
974 }
975
976 return r;
977}
978
979/*
980 * Temporarily try to avoid having to use the specified PG
981 */
982static void bypass_pg(struct multipath *m, struct priority_group *pg,
983 int bypassed)
984{
985 unsigned long flags;
986
987 spin_lock_irqsave(&m->lock, flags);
988
989 pg->bypassed = bypassed;
990 m->current_pgpath = NULL;
991 m->current_pg = NULL;
992
993 spin_unlock_irqrestore(&m->lock, flags);
994
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +0000995 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996}
997
998/*
999 * Switch to using the specified PG from the next I/O that gets mapped
1000 */
1001static int switch_pg_num(struct multipath *m, const char *pgstr)
1002{
1003 struct priority_group *pg;
1004 unsigned pgnum;
1005 unsigned long flags;
1006
1007 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1008 (pgnum > m->nr_priority_groups)) {
1009 DMWARN("invalid PG number supplied to switch_pg_num");
1010 return -EINVAL;
1011 }
1012
1013 spin_lock_irqsave(&m->lock, flags);
1014 list_for_each_entry(pg, &m->priority_groups, list) {
1015 pg->bypassed = 0;
1016 if (--pgnum)
1017 continue;
1018
1019 m->current_pgpath = NULL;
1020 m->current_pg = NULL;
1021 m->next_pg = pg;
1022 }
1023 spin_unlock_irqrestore(&m->lock, flags);
1024
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001025 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return 0;
1027}
1028
1029/*
1030 * Set/clear bypassed status of a PG.
1031 * PGs are numbered upwards from 1 in the order they were declared.
1032 */
1033static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1034{
1035 struct priority_group *pg;
1036 unsigned pgnum;
1037
1038 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1039 (pgnum > m->nr_priority_groups)) {
1040 DMWARN("invalid PG number supplied to bypass_pg");
1041 return -EINVAL;
1042 }
1043
1044 list_for_each_entry(pg, &m->priority_groups, list) {
1045 if (!--pgnum)
1046 break;
1047 }
1048
1049 bypass_pg(m, pg, bypassed);
1050 return 0;
1051}
1052
1053/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001054 * Should we retry pg_init immediately?
1055 */
1056static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1057{
1058 unsigned long flags;
1059 int limit_reached = 0;
1060
1061 spin_lock_irqsave(&m->lock, flags);
1062
1063 if (m->pg_init_count <= m->pg_init_retries)
1064 m->pg_init_required = 1;
1065 else
1066 limit_reached = 1;
1067
1068 spin_unlock_irqrestore(&m->lock, flags);
1069
1070 return limit_reached;
1071}
1072
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001073static void pg_init_done(struct dm_path *path, int errors)
1074{
1075 struct pgpath *pgpath = path_to_pgpath(path);
1076 struct priority_group *pg = pgpath->pg;
1077 struct multipath *m = pg->m;
1078 unsigned long flags;
1079
1080 /* device or driver problems */
1081 switch (errors) {
1082 case SCSI_DH_OK:
1083 break;
1084 case SCSI_DH_NOSYS:
1085 if (!m->hw_handler_name) {
1086 errors = 0;
1087 break;
1088 }
1089 DMERR("Cannot failover device because scsi_dh_%s was not "
1090 "loaded.", m->hw_handler_name);
1091 /*
1092 * Fail path for now, so we do not ping pong
1093 */
1094 fail_path(pgpath);
1095 break;
1096 case SCSI_DH_DEV_TEMP_BUSY:
1097 /*
1098 * Probably doing something like FW upgrade on the
1099 * controller so try the other pg.
1100 */
1101 bypass_pg(m, pg, 1);
1102 break;
1103 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1104 case SCSI_DH_RETRY:
1105 case SCSI_DH_IMM_RETRY:
1106 case SCSI_DH_RES_TEMP_UNAVAIL:
1107 if (pg_init_limit_reached(m, pgpath))
1108 fail_path(pgpath);
1109 errors = 0;
1110 break;
1111 default:
1112 /*
1113 * We probably do not want to fail the path for a device
1114 * error, but this is what the old dm did. In future
1115 * patches we can do more advanced handling.
1116 */
1117 fail_path(pgpath);
1118 }
1119
1120 spin_lock_irqsave(&m->lock, flags);
1121 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001122 if (pgpath == m->current_pgpath) {
1123 DMERR("Could not failover device. Error %d.", errors);
1124 m->current_pgpath = NULL;
1125 m->current_pg = NULL;
1126 }
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001127 } else if (!m->pg_init_required) {
1128 m->queue_io = 0;
1129 pg->bypassed = 0;
1130 }
1131
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001132 m->pg_init_in_progress--;
1133 if (!m->pg_init_in_progress)
1134 queue_work(kmultipathd, &m->process_queued_ios);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001135 spin_unlock_irqrestore(&m->lock, flags);
1136}
1137
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001138static void activate_path(struct work_struct *work)
1139{
1140 int ret;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001141 struct pgpath *pgpath =
1142 container_of(work, struct pgpath, activate_path);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001143
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001144 ret = scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev));
1145 pg_init_done(&pgpath->path, ret);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148/*
1149 * end_io handling
1150 */
1151static int do_end_io(struct multipath *m, struct bio *bio,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001152 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Stefan Bader640eb3b2005-11-21 21:32:35 -08001154 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 if (!error)
1157 return 0; /* I/O complete */
1158
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -07001159 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1160 return error;
1161
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001162 if (error == -EOPNOTSUPP)
1163 return error;
1164
Stefan Bader640eb3b2005-11-21 21:32:35 -08001165 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 if (!m->nr_valid_paths) {
Kiyoshi Ueda45e15722006-12-08 02:41:10 -08001167 if (__must_push_back(m)) {
1168 spin_unlock_irqrestore(&m->lock, flags);
1169 return DM_ENDIO_REQUEUE;
1170 } else if (!m->queue_if_no_path) {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001171 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return -EIO;
1173 } else {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001174 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 goto requeue;
1176 }
1177 }
Stefan Bader640eb3b2005-11-21 21:32:35 -08001178 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001180 if (mpio->pgpath)
1181 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
1183 requeue:
1184 dm_bio_restore(&mpio->details, bio);
1185
1186 /* queue for the daemon to resubmit or fail */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001187 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 bio_list_add(&m->queued_ios, bio);
1189 m->queue_size++;
1190 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001191 queue_work(kmultipathd, &m->process_queued_ios);
Stefan Bader640eb3b2005-11-21 21:32:35 -08001192 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001194 return DM_ENDIO_INCOMPLETE; /* io not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1198 int error, union map_info *map_context)
1199{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001200 struct multipath *m = ti->private;
1201 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 struct pgpath *pgpath = mpio->pgpath;
1203 struct path_selector *ps;
1204 int r;
1205
1206 r = do_end_io(m, bio, error, mpio);
1207 if (pgpath) {
1208 ps = &pgpath->pg->ps;
1209 if (ps->type->end_io)
1210 ps->type->end_io(ps, &pgpath->path);
1211 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001212 if (r != DM_ENDIO_INCOMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 mempool_free(mpio, m->mpio_pool);
1214
1215 return r;
1216}
1217
1218/*
1219 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001220 * the last path fails we must error any remaining I/O.
1221 * Note that if the freeze_bdev fails while suspending, the
1222 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 */
1224static void multipath_presuspend(struct dm_target *ti)
1225{
1226 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001228 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001231/*
1232 * Restore the queue_if_no_path setting.
1233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234static void multipath_resume(struct dm_target *ti)
1235{
1236 struct multipath *m = (struct multipath *) ti->private;
1237 unsigned long flags;
1238
1239 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001240 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 spin_unlock_irqrestore(&m->lock, flags);
1242}
1243
1244/*
1245 * Info output has the following format:
1246 * num_multipath_feature_args [multipath_feature_args]*
1247 * num_handler_status_args [handler_status_args]*
1248 * num_groups init_group_number
1249 * [A|D|E num_ps_status_args [ps_status_args]*
1250 * num_paths num_selector_args
1251 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1252 *
1253 * Table output has the following format (identical to the constructor string):
1254 * num_feature_args [features_args]*
1255 * num_handler_args hw_handler [hw_handler_args]*
1256 * num_groups init_group_number
1257 * [priority selector-name num_ps_args [ps_args]*
1258 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1259 */
1260static int multipath_status(struct dm_target *ti, status_type_t type,
1261 char *result, unsigned int maxlen)
1262{
1263 int sz = 0;
1264 unsigned long flags;
1265 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 struct priority_group *pg;
1267 struct pgpath *p;
1268 unsigned pg_num;
1269 char state;
1270
1271 spin_lock_irqsave(&m->lock, flags);
1272
1273 /* Features */
1274 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001275 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1276 else {
1277 DMEMIT("%u ", m->queue_if_no_path +
1278 (m->pg_init_retries > 0) * 2);
1279 if (m->queue_if_no_path)
1280 DMEMIT("queue_if_no_path ");
1281 if (m->pg_init_retries)
1282 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001285 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 DMEMIT("0 ");
1287 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001288 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 DMEMIT("%u ", m->nr_priority_groups);
1291
1292 if (m->next_pg)
1293 pg_num = m->next_pg->pg_num;
1294 else if (m->current_pg)
1295 pg_num = m->current_pg->pg_num;
1296 else
1297 pg_num = 1;
1298
1299 DMEMIT("%u ", pg_num);
1300
1301 switch (type) {
1302 case STATUSTYPE_INFO:
1303 list_for_each_entry(pg, &m->priority_groups, list) {
1304 if (pg->bypassed)
1305 state = 'D'; /* Disabled */
1306 else if (pg == m->current_pg)
1307 state = 'A'; /* Currently Active */
1308 else
1309 state = 'E'; /* Enabled */
1310
1311 DMEMIT("%c ", state);
1312
1313 if (pg->ps.type->status)
1314 sz += pg->ps.type->status(&pg->ps, NULL, type,
1315 result + sz,
1316 maxlen - sz);
1317 else
1318 DMEMIT("0 ");
1319
1320 DMEMIT("%u %u ", pg->nr_pgpaths,
1321 pg->ps.type->info_args);
1322
1323 list_for_each_entry(p, &pg->pgpaths, list) {
1324 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001325 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 p->fail_count);
1327 if (pg->ps.type->status)
1328 sz += pg->ps.type->status(&pg->ps,
1329 &p->path, type, result + sz,
1330 maxlen - sz);
1331 }
1332 }
1333 break;
1334
1335 case STATUSTYPE_TABLE:
1336 list_for_each_entry(pg, &m->priority_groups, list) {
1337 DMEMIT("%s ", pg->ps.type->name);
1338
1339 if (pg->ps.type->status)
1340 sz += pg->ps.type->status(&pg->ps, NULL, type,
1341 result + sz,
1342 maxlen - sz);
1343 else
1344 DMEMIT("0 ");
1345
1346 DMEMIT("%u %u ", pg->nr_pgpaths,
1347 pg->ps.type->table_args);
1348
1349 list_for_each_entry(p, &pg->pgpaths, list) {
1350 DMEMIT("%s ", p->path.dev->name);
1351 if (pg->ps.type->status)
1352 sz += pg->ps.type->status(&pg->ps,
1353 &p->path, type, result + sz,
1354 maxlen - sz);
1355 }
1356 }
1357 break;
1358 }
1359
1360 spin_unlock_irqrestore(&m->lock, flags);
1361
1362 return 0;
1363}
1364
1365static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1366{
1367 int r;
1368 struct dm_dev *dev;
1369 struct multipath *m = (struct multipath *) ti->private;
1370 action_fn action;
1371
1372 if (argc == 1) {
1373 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001374 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001376 return queue_if_no_path(m, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
1379 if (argc != 2)
1380 goto error;
1381
1382 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1383 return bypass_pg_num(m, argv[1], 1);
1384 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1385 return bypass_pg_num(m, argv[1], 0);
1386 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1387 return switch_pg_num(m, argv[1]);
1388 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1389 action = reinstate_path;
1390 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1391 action = fail_path;
1392 else
1393 goto error;
1394
1395 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1396 dm_table_get_mode(ti->table), &dev);
1397 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001398 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 argv[1]);
1400 return -EINVAL;
1401 }
1402
1403 r = action_dev(m, dev, action);
1404
1405 dm_put_device(ti, dev);
1406
1407 return r;
1408
1409error:
1410 DMWARN("Unrecognised multipath message received.");
1411 return -EINVAL;
1412}
1413
Al Viro647b3d02007-08-28 22:15:59 -04001414static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
Milan Broz9af4aa32006-10-03 01:15:20 -07001415 unsigned long arg)
1416{
1417 struct multipath *m = (struct multipath *) ti->private;
1418 struct block_device *bdev = NULL;
Al Viro633a08b2007-08-29 20:34:12 -04001419 fmode_t mode = 0;
Milan Broz9af4aa32006-10-03 01:15:20 -07001420 unsigned long flags;
1421 int r = 0;
1422
1423 spin_lock_irqsave(&m->lock, flags);
1424
1425 if (!m->current_pgpath)
1426 __choose_pgpath(m);
1427
Milan Broze90dae12006-10-03 01:15:22 -07001428 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001429 bdev = m->current_pgpath->path.dev->bdev;
Al Viro633a08b2007-08-29 20:34:12 -04001430 mode = m->current_pgpath->path.dev->mode;
Milan Broze90dae12006-10-03 01:15:22 -07001431 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001432
1433 if (m->queue_io)
1434 r = -EAGAIN;
1435 else if (!bdev)
1436 r = -EIO;
1437
1438 spin_unlock_irqrestore(&m->lock, flags);
1439
Al Viro633a08b2007-08-29 20:34:12 -04001440 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001441}
1442
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443/*-----------------------------------------------------------------
1444 * Module setup
1445 *---------------------------------------------------------------*/
1446static struct target_type multipath_target = {
1447 .name = "multipath",
Milan Broz9af4aa32006-10-03 01:15:20 -07001448 .version = {1, 0, 5},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 .module = THIS_MODULE,
1450 .ctr = multipath_ctr,
1451 .dtr = multipath_dtr,
1452 .map = multipath_map,
1453 .end_io = multipath_end_io,
1454 .presuspend = multipath_presuspend,
1455 .resume = multipath_resume,
1456 .status = multipath_status,
1457 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001458 .ioctl = multipath_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459};
1460
1461static int __init dm_multipath_init(void)
1462{
1463 int r;
1464
1465 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001466 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 if (!_mpio_cache)
1468 return -ENOMEM;
1469
1470 r = dm_register_target(&multipath_target);
1471 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001472 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 kmem_cache_destroy(_mpio_cache);
1474 return -EINVAL;
1475 }
1476
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001477 kmultipathd = create_workqueue("kmpathd");
1478 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001479 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001480 dm_unregister_target(&multipath_target);
1481 kmem_cache_destroy(_mpio_cache);
1482 return -ENOMEM;
1483 }
1484
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001485 /*
1486 * A separate workqueue is used to handle the device handlers
1487 * to avoid overloading existing workqueue. Overloading the
1488 * old workqueue would also create a bottleneck in the
1489 * path of the storage hardware device activation.
1490 */
1491 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1492 if (!kmpath_handlerd) {
1493 DMERR("failed to create workqueue kmpath_handlerd");
1494 destroy_workqueue(kmultipathd);
1495 dm_unregister_target(&multipath_target);
1496 kmem_cache_destroy(_mpio_cache);
1497 return -ENOMEM;
1498 }
1499
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001500 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 multipath_target.version[0], multipath_target.version[1],
1502 multipath_target.version[2]);
1503
1504 return r;
1505}
1506
1507static void __exit dm_multipath_exit(void)
1508{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001509 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001510 destroy_workqueue(kmultipathd);
1511
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001512 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 kmem_cache_destroy(_mpio_cache);
1514}
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516module_init(dm_multipath_init);
1517module_exit(dm_multipath_exit);
1518
1519MODULE_DESCRIPTION(DM_NAME " multipath target");
1520MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1521MODULE_LICENSE("GPL");