blob: d880299334e9dbe971d110a33877ac5f6f9f50dd [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
40#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
41
42/*
43 * Paths are grouped into Priority Groups and numbered from 1 upwards.
44 * Each has a path selector which controls which path gets used.
45 */
46struct priority_group {
47 struct list_head list;
48
49 struct multipath *m; /* Owning multipath instance */
50 struct path_selector ps;
51
52 unsigned pg_num; /* Reference number */
53 unsigned bypassed; /* Temporarily bypass this PG? */
54
55 unsigned nr_pgpaths; /* Number of paths in PG */
56 struct list_head pgpaths;
57};
58
59/* Multipath context */
60struct multipath {
61 struct list_head list;
62 struct dm_target *ti;
63
64 spinlock_t lock;
65
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070066 const char *hw_handler_name;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -070067 struct work_struct activate_path;
Chandra Seetharaman7253a332008-10-01 14:39:27 +010068 struct pgpath *pgpath_to_activate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 unsigned nr_priority_groups;
70 struct list_head priority_groups;
71 unsigned pg_init_required; /* pg_init needs calling? */
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -070072 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 unsigned nr_valid_paths; /* Total number of usable paths */
75 struct pgpath *current_pgpath;
76 struct priority_group *current_pg;
77 struct priority_group *next_pg; /* Switch to this PG if set */
78 unsigned repeat_count; /* I/Os left before calling PS again */
79
80 unsigned queue_io; /* Must we queue all I/O? */
81 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
Alasdair G Kergon436d4102005-07-12 15:53:03 -070082 unsigned saved_queue_if_no_path;/* Saved state during suspension */
Dave Wysochanskic9e45582007-10-19 22:47:53 +010083 unsigned pg_init_retries; /* Number of times to retry pg_init */
84 unsigned pg_init_count; /* Number of times pg_init called */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 struct work_struct process_queued_ios;
87 struct bio_list queued_ios;
88 unsigned queue_size;
89
90 struct work_struct trigger_event;
91
92 /*
Alasdair G Kergon028867a2007-07-12 17:26:32 +010093 * We must use a mempool of dm_mpath_io structs so that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * can resubmit bios on error.
95 */
96 mempool_t *mpio_pool;
97};
98
99/*
100 * Context information attached to each bio we process.
101 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100102struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct pgpath *pgpath;
104 struct dm_bio_details details;
105};
106
107typedef int (*action_fn) (struct pgpath *pgpath);
108
109#define MIN_IOS 256 /* Mempool size */
110
Christoph Lametere18b8902006-12-06 20:33:20 -0800111static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700113static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000114static void process_queued_ios(struct work_struct *work);
115static void trigger_event(struct work_struct *work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700116static void activate_path(struct work_struct *work);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200117static void deactivate_path(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119
120/*-----------------------------------------------
121 * Allocation routines
122 *-----------------------------------------------*/
123
124static struct pgpath *alloc_pgpath(void)
125{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700126 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Mike Anderson224cb3e2008-08-29 09:36:09 +0200128 if (pgpath) {
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100129 pgpath->is_active = 1;
Mike Anderson224cb3e2008-08-29 09:36:09 +0200130 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
131 }
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{
Chandra Seetharaman7253a332008-10-01 14:39:27 +0100163 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 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);
Chandra Seetharaman7253a332008-10-01 14:39:27 +0100172 spin_lock_irqsave(&m->lock, flags);
173 if (m->pgpath_to_activate == pgpath)
174 m->pgpath_to_activate = NULL;
175 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 free_pgpath(pgpath);
177 }
178}
179
180static void free_priority_group(struct priority_group *pg,
181 struct dm_target *ti)
182{
183 struct path_selector *ps = &pg->ps;
184
185 if (ps->type) {
186 ps->type->destroy(ps);
187 dm_put_path_selector(ps->type);
188 }
189
190 free_pgpaths(&pg->pgpaths, ti);
191 kfree(pg);
192}
193
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700194static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 struct multipath *m;
197
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700198 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 INIT_LIST_HEAD(&m->priority_groups);
201 spin_lock_init(&m->lock);
202 m->queue_io = 1;
David Howellsc4028952006-11-22 14:57:56 +0000203 INIT_WORK(&m->process_queued_ios, process_queued_ios);
204 INIT_WORK(&m->trigger_event, trigger_event);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700205 INIT_WORK(&m->activate_path, activate_path);
Matthew Dobson93d23412006-03-26 01:37:50 -0800206 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (!m->mpio_pool) {
208 kfree(m);
209 return NULL;
210 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700211 m->ti = ti;
212 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214
215 return m;
216}
217
218static void free_multipath(struct multipath *m)
219{
220 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
223 list_del(&pg->list);
224 free_priority_group(pg, m->ti);
225 }
226
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700227 kfree(m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 mempool_destroy(m->mpio_pool);
229 kfree(m);
230}
231
232
233/*-----------------------------------------------
234 * Path selection
235 *-----------------------------------------------*/
236
237static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
238{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 m->current_pg = pgpath->pg;
240
241 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700242 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 m->pg_init_required = 1;
244 m->queue_io = 1;
245 } else {
246 m->pg_init_required = 0;
247 m->queue_io = 0;
248 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100249
250 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
254{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800255 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
257 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
258 if (!path)
259 return -ENXIO;
260
261 m->current_pgpath = path_to_pgpath(path);
262
263 if (m->current_pg != pg)
264 __switch_pg(m, m->current_pgpath);
265
266 return 0;
267}
268
269static void __choose_pgpath(struct multipath *m)
270{
271 struct priority_group *pg;
272 unsigned bypassed = 1;
273
274 if (!m->nr_valid_paths)
275 goto failed;
276
277 /* Were we instructed to switch PG? */
278 if (m->next_pg) {
279 pg = m->next_pg;
280 m->next_pg = NULL;
281 if (!__choose_path_in_pg(m, pg))
282 return;
283 }
284
285 /* Don't change PG until it has no remaining paths */
286 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
287 return;
288
289 /*
290 * Loop through priority groups until we find a valid path.
291 * First time we skip PGs marked 'bypassed'.
292 * Second time we only try the ones we skipped.
293 */
294 do {
295 list_for_each_entry(pg, &m->priority_groups, list) {
296 if (pg->bypassed == bypassed)
297 continue;
298 if (!__choose_path_in_pg(m, pg))
299 return;
300 }
301 } while (bypassed--);
302
303failed:
304 m->current_pgpath = NULL;
305 m->current_pg = NULL;
306}
307
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800308/*
309 * Check whether bios must be queued in the device-mapper core rather
310 * than here in the target.
311 *
312 * m->lock must be held on entry.
313 *
314 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
315 * same value then we are not between multipath_presuspend()
316 * and multipath_resume() calls and we have no need to check
317 * for the DMF_NOFLUSH_SUSPENDING flag.
318 */
319static int __must_push_back(struct multipath *m)
320{
321 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
322 dm_noflush_suspending(m->ti));
323}
324
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100325static int map_io(struct multipath *m, struct bio *bio,
326 struct dm_mpath_io *mpio, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800328 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 unsigned long flags;
330 struct pgpath *pgpath;
331
332 spin_lock_irqsave(&m->lock, flags);
333
334 /* Do we need to select a new pgpath? */
335 if (!m->current_pgpath ||
336 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
337 __choose_pgpath(m);
338
339 pgpath = m->current_pgpath;
340
341 if (was_queued)
342 m->queue_size--;
343
344 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700345 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 /* Queue for the daemon to resubmit */
347 bio_list_add(&m->queued_ios, bio);
348 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700349 if ((m->pg_init_required && !m->pg_init_in_progress) ||
350 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700351 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800353 r = DM_MAPIO_SUBMITTED;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800354 } else if (pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 bio->bi_bdev = pgpath->path.dev->bdev;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800356 else if (__must_push_back(m))
357 r = DM_MAPIO_REQUEUE;
358 else
359 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361 mpio->pgpath = pgpath;
362
363 spin_unlock_irqrestore(&m->lock, flags);
364
365 return r;
366}
367
368/*
369 * If we run out of usable paths, should we queue I/O or error it?
370 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700371static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
372 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 unsigned long flags;
375
376 spin_lock_irqsave(&m->lock, flags);
377
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700378 if (save_old_value)
379 m->saved_queue_if_no_path = m->queue_if_no_path;
380 else
381 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700383 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700384 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386 spin_unlock_irqrestore(&m->lock, flags);
387
388 return 0;
389}
390
391/*-----------------------------------------------------------------
392 * The multipath daemon is responsible for resubmitting queued ios.
393 *---------------------------------------------------------------*/
394
395static void dispatch_queued_ios(struct multipath *m)
396{
397 int r;
398 unsigned long flags;
399 struct bio *bio = NULL, *next;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100400 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 union map_info *info;
402
403 spin_lock_irqsave(&m->lock, flags);
404 bio = bio_list_get(&m->queued_ios);
405 spin_unlock_irqrestore(&m->lock, flags);
406
407 while (bio) {
408 next = bio->bi_next;
409 bio->bi_next = NULL;
410
411 info = dm_get_mapinfo(bio);
412 mpio = info->ptr;
413
414 r = map_io(m, bio, mpio, 1);
415 if (r < 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200416 bio_endio(bio, r);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800417 else if (r == DM_MAPIO_REMAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 generic_make_request(bio);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800419 else if (r == DM_MAPIO_REQUEUE)
NeilBrown6712ecf2007-09-27 12:47:43 +0200420 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 bio = next;
423 }
424}
425
David Howellsc4028952006-11-22 14:57:56 +0000426static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
David Howellsc4028952006-11-22 14:57:56 +0000428 struct multipath *m =
429 container_of(work, struct multipath, process_queued_ios);
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700430 struct pgpath *pgpath = NULL;
431 unsigned init_required = 0, must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 unsigned long flags;
433
434 spin_lock_irqsave(&m->lock, flags);
435
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700436 if (!m->queue_size)
437 goto out;
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (!m->current_pgpath)
440 __choose_pgpath(m);
441
442 pgpath = m->current_pgpath;
443
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700444 if ((pgpath && !m->queue_io) ||
445 (!pgpath && !m->queue_if_no_path))
446 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Chandra Seetharamanb81aa1c2008-11-13 23:39:00 +0000448 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
449 m->pgpath_to_activate = pgpath;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100450 m->pg_init_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 m->pg_init_required = 0;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700452 m->pg_init_in_progress = 1;
453 init_required = 1;
454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700456out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 spin_unlock_irqrestore(&m->lock, flags);
458
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700459 if (init_required)
460 queue_work(kmpath_handlerd, &m->activate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 if (!must_queue)
463 dispatch_queued_ios(m);
464}
465
466/*
467 * An event is triggered whenever a path is taken out of use.
468 * Includes path failure and PG bypass.
469 */
David Howellsc4028952006-11-22 14:57:56 +0000470static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
David Howellsc4028952006-11-22 14:57:56 +0000472 struct multipath *m =
473 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 dm_table_event(m->ti->table);
476}
477
478/*-----------------------------------------------------------------
479 * Constructor/argument parsing:
480 * <#multipath feature args> [<arg>]*
481 * <#hw_handler args> [hw_handler [<arg>]*]
482 * <#priority groups>
483 * <initial priority group>
484 * [<selector> <#selector args> [<arg>]*
485 * <#paths> <#per-path selector args>
486 * [<path> [<arg>]* ]+ ]+
487 *---------------------------------------------------------------*/
488struct param {
489 unsigned min;
490 unsigned max;
491 char *error;
492};
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494static int read_param(struct param *param, char *str, unsigned *v, char **error)
495{
496 if (!str ||
497 (sscanf(str, "%u", v) != 1) ||
498 (*v < param->min) ||
499 (*v > param->max)) {
500 *error = param->error;
501 return -EINVAL;
502 }
503
504 return 0;
505}
506
507struct arg_set {
508 unsigned argc;
509 char **argv;
510};
511
512static char *shift(struct arg_set *as)
513{
514 char *r;
515
516 if (as->argc) {
517 as->argc--;
518 r = *as->argv;
519 as->argv++;
520 return r;
521 }
522
523 return NULL;
524}
525
526static void consume(struct arg_set *as, unsigned n)
527{
528 BUG_ON (as->argc < n);
529 as->argc -= n;
530 as->argv += n;
531}
532
533static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
534 struct dm_target *ti)
535{
536 int r;
537 struct path_selector_type *pst;
538 unsigned ps_argc;
539
540 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700541 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 };
543
544 pst = dm_get_path_selector(shift(as));
545 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700546 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return -EINVAL;
548 }
549
550 r = read_param(_params, shift(as), &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100551 if (r) {
552 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Mikulas Patocka0e0497c2009-06-22 10:08:02 +0100556 if (ps_argc > as->argc) {
557 dm_put_path_selector(pst);
558 ti->error = "not enough arguments for path selector";
559 return -EINVAL;
560 }
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 r = pst->create(&pg->ps, ps_argc, as->argv);
563 if (r) {
564 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700565 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return r;
567 }
568
569 pg->ps.type = pst;
570 consume(as, ps_argc);
571
572 return 0;
573}
574
575static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
576 struct dm_target *ti)
577{
578 int r;
579 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700580 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
582 /* we need at least a path arg */
583 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700584 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100585 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 }
587
588 p = alloc_pgpath();
589 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100590 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
593 dm_table_get_mode(ti->table), &p->path.dev);
594 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700595 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 goto bad;
597 }
598
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700599 if (m->hw_handler_name) {
600 r = scsi_dh_attach(bdev_get_queue(p->path.dev->bdev),
601 m->hw_handler_name);
602 if (r < 0) {
603 dm_put_device(ti, p->path.dev);
604 goto bad;
605 }
606 }
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
609 if (r) {
610 dm_put_device(ti, p->path.dev);
611 goto bad;
612 }
613
614 return p;
615
616 bad:
617 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100618 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
621static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700622 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700625 {1, 1024, "invalid number of paths"},
626 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 };
628
629 int r;
630 unsigned i, nr_selector_args, nr_params;
631 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700632 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (as->argc < 2) {
635 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100636 ti->error = "not enough priority group arguments";
637 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639
640 pg = alloc_priority_group();
641 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700642 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100643 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645 pg->m = m;
646
647 r = parse_path_selector(as, pg, ti);
648 if (r)
649 goto bad;
650
651 /*
652 * read the paths
653 */
654 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
655 if (r)
656 goto bad;
657
658 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
659 if (r)
660 goto bad;
661
662 nr_params = 1 + nr_selector_args;
663 for (i = 0; i < pg->nr_pgpaths; i++) {
664 struct pgpath *pgpath;
665 struct arg_set path_args;
666
Mikulas Patocka148acff2008-07-21 12:00:30 +0100667 if (as->argc < nr_params) {
668 ti->error = "not enough path parameters";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
672 path_args.argc = nr_params;
673 path_args.argv = as->argv;
674
675 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100676 if (IS_ERR(pgpath)) {
677 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
681 pgpath->pg = pg;
682 list_add_tail(&pgpath->list, &pg->pgpaths);
683 consume(as, nr_params);
684 }
685
686 return pg;
687
688 bad:
689 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100690 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691}
692
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700693static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 unsigned hw_argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700696 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
698 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700699 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 };
701
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700702 if (read_param(_params, shift(as), &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return -EINVAL;
704
705 if (!hw_argc)
706 return 0;
707
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700708 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
709 request_module("scsi_dh_%s", m->hw_handler_name);
710 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700711 ti->error = "unknown hardware handler type";
Chandra Seetharamanfe9233f2008-05-23 18:16:40 -0700712 kfree(m->hw_handler_name);
713 m->hw_handler_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return -EINVAL;
715 }
Chandra Seetharaman14e98c52008-11-13 23:39:06 +0000716
717 if (hw_argc > 1)
718 DMWARN("Ignoring user-specified arguments for "
719 "hardware handler \"%s\"", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 consume(as, hw_argc - 1);
721
722 return 0;
723}
724
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700725static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
727 int r;
728 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700729 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100730 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100733 {0, 3, "invalid number of feature args"},
734 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 };
736
737 r = read_param(_params, shift(as), &argc, &ti->error);
738 if (r)
739 return -EINVAL;
740
741 if (!argc)
742 return 0;
743
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100744 do {
745 param_name = shift(as);
746 argc--;
747
748 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
749 r = queue_if_no_path(m, 1, 0);
750 continue;
751 }
752
753 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
754 (argc >= 1)) {
755 r = read_param(_params + 1, shift(as),
756 &m->pg_init_retries, &ti->error);
757 argc--;
758 continue;
759 }
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100762 r = -EINVAL;
763 } while (argc && !r);
764
765 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768static int multipath_ctr(struct dm_target *ti, unsigned int argc,
769 char **argv)
770{
771 /* target parameters */
772 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700773 {1, 1024, "invalid number of priority groups"},
774 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 };
776
777 int r;
778 struct multipath *m;
779 struct arg_set as;
780 unsigned pg_count = 0;
781 unsigned next_pg_num;
782
783 as.argc = argc;
784 as.argv = argv;
785
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700786 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700788 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return -EINVAL;
790 }
791
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700792 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (r)
794 goto bad;
795
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700796 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (r)
798 goto bad;
799
800 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
801 if (r)
802 goto bad;
803
804 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
805 if (r)
806 goto bad;
807
808 /* parse the priority groups */
809 while (as.argc) {
810 struct priority_group *pg;
811
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700812 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100813 if (IS_ERR(pg)) {
814 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 goto bad;
816 }
817
818 m->nr_valid_paths += pg->nr_pgpaths;
819 list_add_tail(&pg->list, &m->priority_groups);
820 pg_count++;
821 pg->pg_num = pg_count;
822 if (!--next_pg_num)
823 m->next_pg = pg;
824 }
825
826 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700827 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 r = -EINVAL;
829 goto bad;
830 }
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 return 0;
833
834 bad:
835 free_multipath(m);
836 return r;
837}
838
839static void multipath_dtr(struct dm_target *ti)
840{
841 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700842
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700843 flush_workqueue(kmpath_handlerd);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700844 flush_workqueue(kmultipathd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 free_multipath(m);
846}
847
848/*
849 * Map bios, recording original fields for later in case we have to resubmit
850 */
851static int multipath_map(struct dm_target *ti, struct bio *bio,
852 union map_info *map_context)
853{
854 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100855 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 struct multipath *m = (struct multipath *) ti->private;
857
858 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
859 dm_bio_record(&mpio->details, bio);
860
861 map_context->ptr = mpio;
Mike Christie6000a362008-08-19 18:45:30 -0500862 bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 r = map_io(m, bio, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800864 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 mempool_free(mpio, m->mpio_pool);
866
867 return r;
868}
869
870/*
871 * Take a path out of use.
872 */
873static int fail_path(struct pgpath *pgpath)
874{
875 unsigned long flags;
876 struct multipath *m = pgpath->pg->m;
877
878 spin_lock_irqsave(&m->lock, flags);
879
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100880 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 goto out;
882
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700883 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100886 pgpath->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 pgpath->fail_count++;
888
889 m->nr_valid_paths--;
890
891 if (pgpath == m->current_pgpath)
892 m->current_pgpath = NULL;
893
Mike Andersonb15546f2007-10-19 22:48:02 +0100894 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
895 pgpath->path.dev->name, m->nr_valid_paths);
896
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +0000897 schedule_work(&m->trigger_event);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200898 queue_work(kmultipathd, &pgpath->deactivate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900out:
901 spin_unlock_irqrestore(&m->lock, flags);
902
903 return 0;
904}
905
906/*
907 * Reinstate a previously-failed path
908 */
909static int reinstate_path(struct pgpath *pgpath)
910{
911 int r = 0;
912 unsigned long flags;
913 struct multipath *m = pgpath->pg->m;
914
915 spin_lock_irqsave(&m->lock, flags);
916
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100917 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 goto out;
919
Alasdair G Kergondef052d2008-07-21 12:00:31 +0100920 if (!pgpath->pg->ps.type->reinstate_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 DMWARN("Reinstate path not supported by path selector %s",
922 pgpath->pg->ps.type->name);
923 r = -EINVAL;
924 goto out;
925 }
926
927 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
928 if (r)
929 goto out;
930
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100931 pgpath->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 m->current_pgpath = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700934 if (!m->nr_valid_paths++ && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700935 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Mike Andersonb15546f2007-10-19 22:48:02 +0100937 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
938 pgpath->path.dev->name, m->nr_valid_paths);
939
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +0000940 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942out:
943 spin_unlock_irqrestore(&m->lock, flags);
944
945 return r;
946}
947
948/*
949 * Fail or reinstate all paths that match the provided struct dm_dev.
950 */
951static int action_dev(struct multipath *m, struct dm_dev *dev,
952 action_fn action)
953{
954 int r = 0;
955 struct pgpath *pgpath;
956 struct priority_group *pg;
957
958 list_for_each_entry(pg, &m->priority_groups, list) {
959 list_for_each_entry(pgpath, &pg->pgpaths, list) {
960 if (pgpath->path.dev == dev)
961 r = action(pgpath);
962 }
963 }
964
965 return r;
966}
967
968/*
969 * Temporarily try to avoid having to use the specified PG
970 */
971static void bypass_pg(struct multipath *m, struct priority_group *pg,
972 int bypassed)
973{
974 unsigned long flags;
975
976 spin_lock_irqsave(&m->lock, flags);
977
978 pg->bypassed = bypassed;
979 m->current_pgpath = NULL;
980 m->current_pg = NULL;
981
982 spin_unlock_irqrestore(&m->lock, flags);
983
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +0000984 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987/*
988 * Switch to using the specified PG from the next I/O that gets mapped
989 */
990static int switch_pg_num(struct multipath *m, const char *pgstr)
991{
992 struct priority_group *pg;
993 unsigned pgnum;
994 unsigned long flags;
995
996 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
997 (pgnum > m->nr_priority_groups)) {
998 DMWARN("invalid PG number supplied to switch_pg_num");
999 return -EINVAL;
1000 }
1001
1002 spin_lock_irqsave(&m->lock, flags);
1003 list_for_each_entry(pg, &m->priority_groups, list) {
1004 pg->bypassed = 0;
1005 if (--pgnum)
1006 continue;
1007
1008 m->current_pgpath = NULL;
1009 m->current_pg = NULL;
1010 m->next_pg = pg;
1011 }
1012 spin_unlock_irqrestore(&m->lock, flags);
1013
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001014 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0;
1016}
1017
1018/*
1019 * Set/clear bypassed status of a PG.
1020 * PGs are numbered upwards from 1 in the order they were declared.
1021 */
1022static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1023{
1024 struct priority_group *pg;
1025 unsigned pgnum;
1026
1027 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1028 (pgnum > m->nr_priority_groups)) {
1029 DMWARN("invalid PG number supplied to bypass_pg");
1030 return -EINVAL;
1031 }
1032
1033 list_for_each_entry(pg, &m->priority_groups, list) {
1034 if (!--pgnum)
1035 break;
1036 }
1037
1038 bypass_pg(m, pg, bypassed);
1039 return 0;
1040}
1041
1042/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001043 * Should we retry pg_init immediately?
1044 */
1045static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1046{
1047 unsigned long flags;
1048 int limit_reached = 0;
1049
1050 spin_lock_irqsave(&m->lock, flags);
1051
1052 if (m->pg_init_count <= m->pg_init_retries)
1053 m->pg_init_required = 1;
1054 else
1055 limit_reached = 1;
1056
1057 spin_unlock_irqrestore(&m->lock, flags);
1058
1059 return limit_reached;
1060}
1061
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001062static void pg_init_done(struct dm_path *path, int errors)
1063{
1064 struct pgpath *pgpath = path_to_pgpath(path);
1065 struct priority_group *pg = pgpath->pg;
1066 struct multipath *m = pg->m;
1067 unsigned long flags;
1068
1069 /* device or driver problems */
1070 switch (errors) {
1071 case SCSI_DH_OK:
1072 break;
1073 case SCSI_DH_NOSYS:
1074 if (!m->hw_handler_name) {
1075 errors = 0;
1076 break;
1077 }
1078 DMERR("Cannot failover device because scsi_dh_%s was not "
1079 "loaded.", m->hw_handler_name);
1080 /*
1081 * Fail path for now, so we do not ping pong
1082 */
1083 fail_path(pgpath);
1084 break;
1085 case SCSI_DH_DEV_TEMP_BUSY:
1086 /*
1087 * Probably doing something like FW upgrade on the
1088 * controller so try the other pg.
1089 */
1090 bypass_pg(m, pg, 1);
1091 break;
1092 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1093 case SCSI_DH_RETRY:
1094 case SCSI_DH_IMM_RETRY:
1095 case SCSI_DH_RES_TEMP_UNAVAIL:
1096 if (pg_init_limit_reached(m, pgpath))
1097 fail_path(pgpath);
1098 errors = 0;
1099 break;
1100 default:
1101 /*
1102 * We probably do not want to fail the path for a device
1103 * error, but this is what the old dm did. In future
1104 * patches we can do more advanced handling.
1105 */
1106 fail_path(pgpath);
1107 }
1108
1109 spin_lock_irqsave(&m->lock, flags);
1110 if (errors) {
1111 DMERR("Could not failover device. Error %d.", errors);
1112 m->current_pgpath = NULL;
1113 m->current_pg = NULL;
1114 } else if (!m->pg_init_required) {
1115 m->queue_io = 0;
1116 pg->bypassed = 0;
1117 }
1118
1119 m->pg_init_in_progress = 0;
1120 queue_work(kmultipathd, &m->process_queued_ios);
1121 spin_unlock_irqrestore(&m->lock, flags);
1122}
1123
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001124static void activate_path(struct work_struct *work)
1125{
1126 int ret;
1127 struct multipath *m =
1128 container_of(work, struct multipath, activate_path);
Chandra Seetharaman7253a332008-10-01 14:39:27 +01001129 struct dm_path *path;
1130 unsigned long flags;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001131
Chandra Seetharaman7253a332008-10-01 14:39:27 +01001132 spin_lock_irqsave(&m->lock, flags);
1133 path = &m->pgpath_to_activate->path;
1134 m->pgpath_to_activate = NULL;
1135 spin_unlock_irqrestore(&m->lock, flags);
1136 if (!path)
1137 return;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001138 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1139 pg_init_done(path, ret);
1140}
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142/*
1143 * end_io handling
1144 */
1145static int do_end_io(struct multipath *m, struct bio *bio,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001146 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
Stefan Bader640eb3b2005-11-21 21:32:35 -08001148 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150 if (!error)
1151 return 0; /* I/O complete */
1152
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -07001153 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1154 return error;
1155
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001156 if (error == -EOPNOTSUPP)
1157 return error;
1158
Stefan Bader640eb3b2005-11-21 21:32:35 -08001159 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (!m->nr_valid_paths) {
Kiyoshi Ueda45e15722006-12-08 02:41:10 -08001161 if (__must_push_back(m)) {
1162 spin_unlock_irqrestore(&m->lock, flags);
1163 return DM_ENDIO_REQUEUE;
1164 } else if (!m->queue_if_no_path) {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001165 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return -EIO;
1167 } else {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001168 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 goto requeue;
1170 }
1171 }
Stefan Bader640eb3b2005-11-21 21:32:35 -08001172 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001174 if (mpio->pgpath)
1175 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 requeue:
1178 dm_bio_restore(&mpio->details, bio);
1179
1180 /* queue for the daemon to resubmit or fail */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001181 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 bio_list_add(&m->queued_ios, bio);
1183 m->queue_size++;
1184 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001185 queue_work(kmultipathd, &m->process_queued_ios);
Stefan Bader640eb3b2005-11-21 21:32:35 -08001186 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001188 return DM_ENDIO_INCOMPLETE; /* io not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189}
1190
1191static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1192 int error, union map_info *map_context)
1193{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001194 struct multipath *m = ti->private;
1195 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 struct pgpath *pgpath = mpio->pgpath;
1197 struct path_selector *ps;
1198 int r;
1199
1200 r = do_end_io(m, bio, error, mpio);
1201 if (pgpath) {
1202 ps = &pgpath->pg->ps;
1203 if (ps->type->end_io)
1204 ps->type->end_io(ps, &pgpath->path);
1205 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001206 if (r != DM_ENDIO_INCOMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 mempool_free(mpio, m->mpio_pool);
1208
1209 return r;
1210}
1211
1212/*
1213 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001214 * the last path fails we must error any remaining I/O.
1215 * Note that if the freeze_bdev fails while suspending, the
1216 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 */
1218static void multipath_presuspend(struct dm_target *ti)
1219{
1220 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001222 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001225/*
1226 * Restore the queue_if_no_path setting.
1227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228static void multipath_resume(struct dm_target *ti)
1229{
1230 struct multipath *m = (struct multipath *) ti->private;
1231 unsigned long flags;
1232
1233 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001234 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 spin_unlock_irqrestore(&m->lock, flags);
1236}
1237
1238/*
1239 * Info output has the following format:
1240 * num_multipath_feature_args [multipath_feature_args]*
1241 * num_handler_status_args [handler_status_args]*
1242 * num_groups init_group_number
1243 * [A|D|E num_ps_status_args [ps_status_args]*
1244 * num_paths num_selector_args
1245 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1246 *
1247 * Table output has the following format (identical to the constructor string):
1248 * num_feature_args [features_args]*
1249 * num_handler_args hw_handler [hw_handler_args]*
1250 * num_groups init_group_number
1251 * [priority selector-name num_ps_args [ps_args]*
1252 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1253 */
1254static int multipath_status(struct dm_target *ti, status_type_t type,
1255 char *result, unsigned int maxlen)
1256{
1257 int sz = 0;
1258 unsigned long flags;
1259 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 struct priority_group *pg;
1261 struct pgpath *p;
1262 unsigned pg_num;
1263 char state;
1264
1265 spin_lock_irqsave(&m->lock, flags);
1266
1267 /* Features */
1268 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001269 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1270 else {
1271 DMEMIT("%u ", m->queue_if_no_path +
1272 (m->pg_init_retries > 0) * 2);
1273 if (m->queue_if_no_path)
1274 DMEMIT("queue_if_no_path ");
1275 if (m->pg_init_retries)
1276 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001279 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 DMEMIT("0 ");
1281 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001282 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 DMEMIT("%u ", m->nr_priority_groups);
1285
1286 if (m->next_pg)
1287 pg_num = m->next_pg->pg_num;
1288 else if (m->current_pg)
1289 pg_num = m->current_pg->pg_num;
1290 else
1291 pg_num = 1;
1292
1293 DMEMIT("%u ", pg_num);
1294
1295 switch (type) {
1296 case STATUSTYPE_INFO:
1297 list_for_each_entry(pg, &m->priority_groups, list) {
1298 if (pg->bypassed)
1299 state = 'D'; /* Disabled */
1300 else if (pg == m->current_pg)
1301 state = 'A'; /* Currently Active */
1302 else
1303 state = 'E'; /* Enabled */
1304
1305 DMEMIT("%c ", state);
1306
1307 if (pg->ps.type->status)
1308 sz += pg->ps.type->status(&pg->ps, NULL, type,
1309 result + sz,
1310 maxlen - sz);
1311 else
1312 DMEMIT("0 ");
1313
1314 DMEMIT("%u %u ", pg->nr_pgpaths,
1315 pg->ps.type->info_args);
1316
1317 list_for_each_entry(p, &pg->pgpaths, list) {
1318 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001319 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 p->fail_count);
1321 if (pg->ps.type->status)
1322 sz += pg->ps.type->status(&pg->ps,
1323 &p->path, type, result + sz,
1324 maxlen - sz);
1325 }
1326 }
1327 break;
1328
1329 case STATUSTYPE_TABLE:
1330 list_for_each_entry(pg, &m->priority_groups, list) {
1331 DMEMIT("%s ", pg->ps.type->name);
1332
1333 if (pg->ps.type->status)
1334 sz += pg->ps.type->status(&pg->ps, NULL, type,
1335 result + sz,
1336 maxlen - sz);
1337 else
1338 DMEMIT("0 ");
1339
1340 DMEMIT("%u %u ", pg->nr_pgpaths,
1341 pg->ps.type->table_args);
1342
1343 list_for_each_entry(p, &pg->pgpaths, list) {
1344 DMEMIT("%s ", p->path.dev->name);
1345 if (pg->ps.type->status)
1346 sz += pg->ps.type->status(&pg->ps,
1347 &p->path, type, result + sz,
1348 maxlen - sz);
1349 }
1350 }
1351 break;
1352 }
1353
1354 spin_unlock_irqrestore(&m->lock, flags);
1355
1356 return 0;
1357}
1358
1359static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1360{
1361 int r;
1362 struct dm_dev *dev;
1363 struct multipath *m = (struct multipath *) ti->private;
1364 action_fn action;
1365
1366 if (argc == 1) {
1367 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001368 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001370 return queue_if_no_path(m, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372
1373 if (argc != 2)
1374 goto error;
1375
1376 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1377 return bypass_pg_num(m, argv[1], 1);
1378 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1379 return bypass_pg_num(m, argv[1], 0);
1380 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1381 return switch_pg_num(m, argv[1]);
1382 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1383 action = reinstate_path;
1384 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1385 action = fail_path;
1386 else
1387 goto error;
1388
1389 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1390 dm_table_get_mode(ti->table), &dev);
1391 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001392 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 argv[1]);
1394 return -EINVAL;
1395 }
1396
1397 r = action_dev(m, dev, action);
1398
1399 dm_put_device(ti, dev);
1400
1401 return r;
1402
1403error:
1404 DMWARN("Unrecognised multipath message received.");
1405 return -EINVAL;
1406}
1407
Al Viro647b3d02007-08-28 22:15:59 -04001408static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
Milan Broz9af4aa32006-10-03 01:15:20 -07001409 unsigned long arg)
1410{
1411 struct multipath *m = (struct multipath *) ti->private;
1412 struct block_device *bdev = NULL;
Al Viro633a08b2007-08-29 20:34:12 -04001413 fmode_t mode = 0;
Milan Broz9af4aa32006-10-03 01:15:20 -07001414 unsigned long flags;
1415 int r = 0;
1416
1417 spin_lock_irqsave(&m->lock, flags);
1418
1419 if (!m->current_pgpath)
1420 __choose_pgpath(m);
1421
Milan Broze90dae12006-10-03 01:15:22 -07001422 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001423 bdev = m->current_pgpath->path.dev->bdev;
Al Viro633a08b2007-08-29 20:34:12 -04001424 mode = m->current_pgpath->path.dev->mode;
Milan Broze90dae12006-10-03 01:15:22 -07001425 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001426
1427 if (m->queue_io)
1428 r = -EAGAIN;
1429 else if (!bdev)
1430 r = -EIO;
1431
1432 spin_unlock_irqrestore(&m->lock, flags);
1433
Al Viro633a08b2007-08-29 20:34:12 -04001434 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/*-----------------------------------------------------------------
1438 * Module setup
1439 *---------------------------------------------------------------*/
1440static struct target_type multipath_target = {
1441 .name = "multipath",
Milan Broz9af4aa32006-10-03 01:15:20 -07001442 .version = {1, 0, 5},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 .module = THIS_MODULE,
1444 .ctr = multipath_ctr,
1445 .dtr = multipath_dtr,
1446 .map = multipath_map,
1447 .end_io = multipath_end_io,
1448 .presuspend = multipath_presuspend,
1449 .resume = multipath_resume,
1450 .status = multipath_status,
1451 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001452 .ioctl = multipath_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453};
1454
1455static int __init dm_multipath_init(void)
1456{
1457 int r;
1458
1459 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001460 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 if (!_mpio_cache)
1462 return -ENOMEM;
1463
1464 r = dm_register_target(&multipath_target);
1465 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001466 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 kmem_cache_destroy(_mpio_cache);
1468 return -EINVAL;
1469 }
1470
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001471 kmultipathd = create_workqueue("kmpathd");
1472 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001473 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001474 dm_unregister_target(&multipath_target);
1475 kmem_cache_destroy(_mpio_cache);
1476 return -ENOMEM;
1477 }
1478
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001479 /*
1480 * A separate workqueue is used to handle the device handlers
1481 * to avoid overloading existing workqueue. Overloading the
1482 * old workqueue would also create a bottleneck in the
1483 * path of the storage hardware device activation.
1484 */
1485 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1486 if (!kmpath_handlerd) {
1487 DMERR("failed to create workqueue kmpath_handlerd");
1488 destroy_workqueue(kmultipathd);
1489 dm_unregister_target(&multipath_target);
1490 kmem_cache_destroy(_mpio_cache);
1491 return -ENOMEM;
1492 }
1493
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001494 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 multipath_target.version[0], multipath_target.version[1],
1496 multipath_target.version[2]);
1497
1498 return r;
1499}
1500
1501static void __exit dm_multipath_exit(void)
1502{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001503 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001504 destroy_workqueue(kmultipathd);
1505
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001506 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 kmem_cache_destroy(_mpio_cache);
1508}
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510module_init(dm_multipath_init);
1511module_exit(dm_multipath_exit);
1512
1513MODULE_DESCRIPTION(DM_NAME " multipath target");
1514MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1515MODULE_LICENSE("GPL");