blob: c681d5e5f45c05b70b20fe7468a54c53269dfaf3 [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
8#include "dm.h"
9#include "dm-path-selector.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "dm-bio-list.h"
11#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;
Chandra Seetharaman7253a332008-10-01 14:39:27 +0100443 m->pgpath_to_activate = m->current_pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700445 if ((pgpath && !m->queue_io) ||
446 (!pgpath && !m->queue_if_no_path))
447 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700449 if (m->pg_init_required && !m->pg_init_in_progress) {
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
556 r = pst->create(&pg->ps, ps_argc, as->argv);
557 if (r) {
558 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700559 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return r;
561 }
562
563 pg->ps.type = pst;
564 consume(as, ps_argc);
565
566 return 0;
567}
568
569static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
570 struct dm_target *ti)
571{
572 int r;
573 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700574 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 /* we need at least a path arg */
577 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700578 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100579 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
582 p = alloc_pgpath();
583 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100584 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
587 dm_table_get_mode(ti->table), &p->path.dev);
588 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700589 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 goto bad;
591 }
592
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700593 if (m->hw_handler_name) {
594 r = scsi_dh_attach(bdev_get_queue(p->path.dev->bdev),
595 m->hw_handler_name);
596 if (r < 0) {
597 dm_put_device(ti, p->path.dev);
598 goto bad;
599 }
600 }
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
603 if (r) {
604 dm_put_device(ti, p->path.dev);
605 goto bad;
606 }
607
608 return p;
609
610 bad:
611 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100612 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
615static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700616 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
618 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700619 {1, 1024, "invalid number of paths"},
620 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 };
622
623 int r;
624 unsigned i, nr_selector_args, nr_params;
625 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700626 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (as->argc < 2) {
629 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100630 ti->error = "not enough priority group arguments";
631 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 }
633
634 pg = alloc_priority_group();
635 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700636 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100637 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
639 pg->m = m;
640
641 r = parse_path_selector(as, pg, ti);
642 if (r)
643 goto bad;
644
645 /*
646 * read the paths
647 */
648 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
649 if (r)
650 goto bad;
651
652 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
653 if (r)
654 goto bad;
655
656 nr_params = 1 + nr_selector_args;
657 for (i = 0; i < pg->nr_pgpaths; i++) {
658 struct pgpath *pgpath;
659 struct arg_set path_args;
660
Mikulas Patocka148acff2008-07-21 12:00:30 +0100661 if (as->argc < nr_params) {
662 ti->error = "not enough path parameters";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 path_args.argc = nr_params;
667 path_args.argv = as->argv;
668
669 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100670 if (IS_ERR(pgpath)) {
671 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 pgpath->pg = pg;
676 list_add_tail(&pgpath->list, &pg->pgpaths);
677 consume(as, nr_params);
678 }
679
680 return pg;
681
682 bad:
683 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100684 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700687static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 unsigned hw_argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700690 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700693 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 };
695
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700696 if (read_param(_params, shift(as), &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return -EINVAL;
698
699 if (!hw_argc)
700 return 0;
701
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700702 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
703 request_module("scsi_dh_%s", m->hw_handler_name);
704 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700705 ti->error = "unknown hardware handler type";
Chandra Seetharamanfe9233f2008-05-23 18:16:40 -0700706 kfree(m->hw_handler_name);
707 m->hw_handler_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 return -EINVAL;
709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 consume(as, hw_argc - 1);
711
712 return 0;
713}
714
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700715static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
717 int r;
718 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700719 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100720 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100723 {0, 3, "invalid number of feature args"},
724 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 };
726
727 r = read_param(_params, shift(as), &argc, &ti->error);
728 if (r)
729 return -EINVAL;
730
731 if (!argc)
732 return 0;
733
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100734 do {
735 param_name = shift(as);
736 argc--;
737
738 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
739 r = queue_if_no_path(m, 1, 0);
740 continue;
741 }
742
743 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
744 (argc >= 1)) {
745 r = read_param(_params + 1, shift(as),
746 &m->pg_init_retries, &ti->error);
747 argc--;
748 continue;
749 }
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100752 r = -EINVAL;
753 } while (argc && !r);
754
755 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758static int multipath_ctr(struct dm_target *ti, unsigned int argc,
759 char **argv)
760{
761 /* target parameters */
762 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700763 {1, 1024, "invalid number of priority groups"},
764 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 };
766
767 int r;
768 struct multipath *m;
769 struct arg_set as;
770 unsigned pg_count = 0;
771 unsigned next_pg_num;
772
773 as.argc = argc;
774 as.argv = argv;
775
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700776 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700778 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return -EINVAL;
780 }
781
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700782 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (r)
784 goto bad;
785
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700786 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (r)
788 goto bad;
789
790 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
791 if (r)
792 goto bad;
793
794 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
795 if (r)
796 goto bad;
797
798 /* parse the priority groups */
799 while (as.argc) {
800 struct priority_group *pg;
801
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700802 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100803 if (IS_ERR(pg)) {
804 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goto bad;
806 }
807
808 m->nr_valid_paths += pg->nr_pgpaths;
809 list_add_tail(&pg->list, &m->priority_groups);
810 pg_count++;
811 pg->pg_num = pg_count;
812 if (!--next_pg_num)
813 m->next_pg = pg;
814 }
815
816 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700817 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 r = -EINVAL;
819 goto bad;
820 }
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 return 0;
823
824 bad:
825 free_multipath(m);
826 return r;
827}
828
829static void multipath_dtr(struct dm_target *ti)
830{
831 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700832
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700833 flush_workqueue(kmpath_handlerd);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700834 flush_workqueue(kmultipathd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 free_multipath(m);
836}
837
838/*
839 * Map bios, recording original fields for later in case we have to resubmit
840 */
841static int multipath_map(struct dm_target *ti, struct bio *bio,
842 union map_info *map_context)
843{
844 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100845 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 struct multipath *m = (struct multipath *) ti->private;
847
848 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
849 dm_bio_record(&mpio->details, bio);
850
851 map_context->ptr = mpio;
Mike Christie6000a362008-08-19 18:45:30 -0500852 bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 r = map_io(m, bio, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800854 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 mempool_free(mpio, m->mpio_pool);
856
857 return r;
858}
859
860/*
861 * Take a path out of use.
862 */
863static int fail_path(struct pgpath *pgpath)
864{
865 unsigned long flags;
866 struct multipath *m = pgpath->pg->m;
867
868 spin_lock_irqsave(&m->lock, flags);
869
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100870 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 goto out;
872
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700873 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100876 pgpath->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 pgpath->fail_count++;
878
879 m->nr_valid_paths--;
880
881 if (pgpath == m->current_pgpath)
882 m->current_pgpath = NULL;
883
Mike Andersonb15546f2007-10-19 22:48:02 +0100884 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
885 pgpath->path.dev->name, m->nr_valid_paths);
886
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700887 queue_work(kmultipathd, &m->trigger_event);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200888 queue_work(kmultipathd, &pgpath->deactivate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890out:
891 spin_unlock_irqrestore(&m->lock, flags);
892
893 return 0;
894}
895
896/*
897 * Reinstate a previously-failed path
898 */
899static int reinstate_path(struct pgpath *pgpath)
900{
901 int r = 0;
902 unsigned long flags;
903 struct multipath *m = pgpath->pg->m;
904
905 spin_lock_irqsave(&m->lock, flags);
906
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100907 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 goto out;
909
Alasdair G Kergondef052d2008-07-21 12:00:31 +0100910 if (!pgpath->pg->ps.type->reinstate_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 DMWARN("Reinstate path not supported by path selector %s",
912 pgpath->pg->ps.type->name);
913 r = -EINVAL;
914 goto out;
915 }
916
917 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
918 if (r)
919 goto out;
920
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100921 pgpath->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 m->current_pgpath = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700924 if (!m->nr_valid_paths++ && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700925 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Mike Andersonb15546f2007-10-19 22:48:02 +0100927 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
928 pgpath->path.dev->name, m->nr_valid_paths);
929
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700930 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932out:
933 spin_unlock_irqrestore(&m->lock, flags);
934
935 return r;
936}
937
938/*
939 * Fail or reinstate all paths that match the provided struct dm_dev.
940 */
941static int action_dev(struct multipath *m, struct dm_dev *dev,
942 action_fn action)
943{
944 int r = 0;
945 struct pgpath *pgpath;
946 struct priority_group *pg;
947
948 list_for_each_entry(pg, &m->priority_groups, list) {
949 list_for_each_entry(pgpath, &pg->pgpaths, list) {
950 if (pgpath->path.dev == dev)
951 r = action(pgpath);
952 }
953 }
954
955 return r;
956}
957
958/*
959 * Temporarily try to avoid having to use the specified PG
960 */
961static void bypass_pg(struct multipath *m, struct priority_group *pg,
962 int bypassed)
963{
964 unsigned long flags;
965
966 spin_lock_irqsave(&m->lock, flags);
967
968 pg->bypassed = bypassed;
969 m->current_pgpath = NULL;
970 m->current_pg = NULL;
971
972 spin_unlock_irqrestore(&m->lock, flags);
973
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700974 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975}
976
977/*
978 * Switch to using the specified PG from the next I/O that gets mapped
979 */
980static int switch_pg_num(struct multipath *m, const char *pgstr)
981{
982 struct priority_group *pg;
983 unsigned pgnum;
984 unsigned long flags;
985
986 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
987 (pgnum > m->nr_priority_groups)) {
988 DMWARN("invalid PG number supplied to switch_pg_num");
989 return -EINVAL;
990 }
991
992 spin_lock_irqsave(&m->lock, flags);
993 list_for_each_entry(pg, &m->priority_groups, list) {
994 pg->bypassed = 0;
995 if (--pgnum)
996 continue;
997
998 m->current_pgpath = NULL;
999 m->current_pg = NULL;
1000 m->next_pg = pg;
1001 }
1002 spin_unlock_irqrestore(&m->lock, flags);
1003
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001004 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return 0;
1006}
1007
1008/*
1009 * Set/clear bypassed status of a PG.
1010 * PGs are numbered upwards from 1 in the order they were declared.
1011 */
1012static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1013{
1014 struct priority_group *pg;
1015 unsigned pgnum;
1016
1017 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1018 (pgnum > m->nr_priority_groups)) {
1019 DMWARN("invalid PG number supplied to bypass_pg");
1020 return -EINVAL;
1021 }
1022
1023 list_for_each_entry(pg, &m->priority_groups, list) {
1024 if (!--pgnum)
1025 break;
1026 }
1027
1028 bypass_pg(m, pg, bypassed);
1029 return 0;
1030}
1031
1032/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001033 * Should we retry pg_init immediately?
1034 */
1035static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1036{
1037 unsigned long flags;
1038 int limit_reached = 0;
1039
1040 spin_lock_irqsave(&m->lock, flags);
1041
1042 if (m->pg_init_count <= m->pg_init_retries)
1043 m->pg_init_required = 1;
1044 else
1045 limit_reached = 1;
1046
1047 spin_unlock_irqrestore(&m->lock, flags);
1048
1049 return limit_reached;
1050}
1051
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001052static void pg_init_done(struct dm_path *path, int errors)
1053{
1054 struct pgpath *pgpath = path_to_pgpath(path);
1055 struct priority_group *pg = pgpath->pg;
1056 struct multipath *m = pg->m;
1057 unsigned long flags;
1058
1059 /* device or driver problems */
1060 switch (errors) {
1061 case SCSI_DH_OK:
1062 break;
1063 case SCSI_DH_NOSYS:
1064 if (!m->hw_handler_name) {
1065 errors = 0;
1066 break;
1067 }
1068 DMERR("Cannot failover device because scsi_dh_%s was not "
1069 "loaded.", m->hw_handler_name);
1070 /*
1071 * Fail path for now, so we do not ping pong
1072 */
1073 fail_path(pgpath);
1074 break;
1075 case SCSI_DH_DEV_TEMP_BUSY:
1076 /*
1077 * Probably doing something like FW upgrade on the
1078 * controller so try the other pg.
1079 */
1080 bypass_pg(m, pg, 1);
1081 break;
1082 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1083 case SCSI_DH_RETRY:
1084 case SCSI_DH_IMM_RETRY:
1085 case SCSI_DH_RES_TEMP_UNAVAIL:
1086 if (pg_init_limit_reached(m, pgpath))
1087 fail_path(pgpath);
1088 errors = 0;
1089 break;
1090 default:
1091 /*
1092 * We probably do not want to fail the path for a device
1093 * error, but this is what the old dm did. In future
1094 * patches we can do more advanced handling.
1095 */
1096 fail_path(pgpath);
1097 }
1098
1099 spin_lock_irqsave(&m->lock, flags);
1100 if (errors) {
1101 DMERR("Could not failover device. Error %d.", errors);
1102 m->current_pgpath = NULL;
1103 m->current_pg = NULL;
1104 } else if (!m->pg_init_required) {
1105 m->queue_io = 0;
1106 pg->bypassed = 0;
1107 }
1108
1109 m->pg_init_in_progress = 0;
1110 queue_work(kmultipathd, &m->process_queued_ios);
1111 spin_unlock_irqrestore(&m->lock, flags);
1112}
1113
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001114static void activate_path(struct work_struct *work)
1115{
1116 int ret;
1117 struct multipath *m =
1118 container_of(work, struct multipath, activate_path);
Chandra Seetharaman7253a332008-10-01 14:39:27 +01001119 struct dm_path *path;
1120 unsigned long flags;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001121
Chandra Seetharaman7253a332008-10-01 14:39:27 +01001122 spin_lock_irqsave(&m->lock, flags);
1123 path = &m->pgpath_to_activate->path;
1124 m->pgpath_to_activate = NULL;
1125 spin_unlock_irqrestore(&m->lock, flags);
1126 if (!path)
1127 return;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001128 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1129 pg_init_done(path, ret);
1130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132/*
1133 * end_io handling
1134 */
1135static int do_end_io(struct multipath *m, struct bio *bio,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001136 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Stefan Bader640eb3b2005-11-21 21:32:35 -08001138 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 if (!error)
1141 return 0; /* I/O complete */
1142
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -07001143 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1144 return error;
1145
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001146 if (error == -EOPNOTSUPP)
1147 return error;
1148
Stefan Bader640eb3b2005-11-21 21:32:35 -08001149 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 if (!m->nr_valid_paths) {
Kiyoshi Ueda45e15722006-12-08 02:41:10 -08001151 if (__must_push_back(m)) {
1152 spin_unlock_irqrestore(&m->lock, flags);
1153 return DM_ENDIO_REQUEUE;
1154 } else if (!m->queue_if_no_path) {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001155 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 return -EIO;
1157 } else {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001158 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 goto requeue;
1160 }
1161 }
Stefan Bader640eb3b2005-11-21 21:32:35 -08001162 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001164 if (mpio->pgpath)
1165 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 requeue:
1168 dm_bio_restore(&mpio->details, bio);
1169
1170 /* queue for the daemon to resubmit or fail */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001171 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 bio_list_add(&m->queued_ios, bio);
1173 m->queue_size++;
1174 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001175 queue_work(kmultipathd, &m->process_queued_ios);
Stefan Bader640eb3b2005-11-21 21:32:35 -08001176 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001178 return DM_ENDIO_INCOMPLETE; /* io not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
1181static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1182 int error, union map_info *map_context)
1183{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001184 struct multipath *m = ti->private;
1185 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 struct pgpath *pgpath = mpio->pgpath;
1187 struct path_selector *ps;
1188 int r;
1189
1190 r = do_end_io(m, bio, error, mpio);
1191 if (pgpath) {
1192 ps = &pgpath->pg->ps;
1193 if (ps->type->end_io)
1194 ps->type->end_io(ps, &pgpath->path);
1195 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001196 if (r != DM_ENDIO_INCOMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 mempool_free(mpio, m->mpio_pool);
1198
1199 return r;
1200}
1201
1202/*
1203 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001204 * the last path fails we must error any remaining I/O.
1205 * Note that if the freeze_bdev fails while suspending, the
1206 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
1208static void multipath_presuspend(struct dm_target *ti)
1209{
1210 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001212 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001215/*
1216 * Restore the queue_if_no_path setting.
1217 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218static void multipath_resume(struct dm_target *ti)
1219{
1220 struct multipath *m = (struct multipath *) ti->private;
1221 unsigned long flags;
1222
1223 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001224 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 spin_unlock_irqrestore(&m->lock, flags);
1226}
1227
1228/*
1229 * Info output has the following format:
1230 * num_multipath_feature_args [multipath_feature_args]*
1231 * num_handler_status_args [handler_status_args]*
1232 * num_groups init_group_number
1233 * [A|D|E num_ps_status_args [ps_status_args]*
1234 * num_paths num_selector_args
1235 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1236 *
1237 * Table output has the following format (identical to the constructor string):
1238 * num_feature_args [features_args]*
1239 * num_handler_args hw_handler [hw_handler_args]*
1240 * num_groups init_group_number
1241 * [priority selector-name num_ps_args [ps_args]*
1242 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1243 */
1244static int multipath_status(struct dm_target *ti, status_type_t type,
1245 char *result, unsigned int maxlen)
1246{
1247 int sz = 0;
1248 unsigned long flags;
1249 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 struct priority_group *pg;
1251 struct pgpath *p;
1252 unsigned pg_num;
1253 char state;
1254
1255 spin_lock_irqsave(&m->lock, flags);
1256
1257 /* Features */
1258 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001259 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1260 else {
1261 DMEMIT("%u ", m->queue_if_no_path +
1262 (m->pg_init_retries > 0) * 2);
1263 if (m->queue_if_no_path)
1264 DMEMIT("queue_if_no_path ");
1265 if (m->pg_init_retries)
1266 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1267 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001269 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 DMEMIT("0 ");
1271 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001272 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 DMEMIT("%u ", m->nr_priority_groups);
1275
1276 if (m->next_pg)
1277 pg_num = m->next_pg->pg_num;
1278 else if (m->current_pg)
1279 pg_num = m->current_pg->pg_num;
1280 else
1281 pg_num = 1;
1282
1283 DMEMIT("%u ", pg_num);
1284
1285 switch (type) {
1286 case STATUSTYPE_INFO:
1287 list_for_each_entry(pg, &m->priority_groups, list) {
1288 if (pg->bypassed)
1289 state = 'D'; /* Disabled */
1290 else if (pg == m->current_pg)
1291 state = 'A'; /* Currently Active */
1292 else
1293 state = 'E'; /* Enabled */
1294
1295 DMEMIT("%c ", state);
1296
1297 if (pg->ps.type->status)
1298 sz += pg->ps.type->status(&pg->ps, NULL, type,
1299 result + sz,
1300 maxlen - sz);
1301 else
1302 DMEMIT("0 ");
1303
1304 DMEMIT("%u %u ", pg->nr_pgpaths,
1305 pg->ps.type->info_args);
1306
1307 list_for_each_entry(p, &pg->pgpaths, list) {
1308 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001309 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 p->fail_count);
1311 if (pg->ps.type->status)
1312 sz += pg->ps.type->status(&pg->ps,
1313 &p->path, type, result + sz,
1314 maxlen - sz);
1315 }
1316 }
1317 break;
1318
1319 case STATUSTYPE_TABLE:
1320 list_for_each_entry(pg, &m->priority_groups, list) {
1321 DMEMIT("%s ", pg->ps.type->name);
1322
1323 if (pg->ps.type->status)
1324 sz += pg->ps.type->status(&pg->ps, NULL, type,
1325 result + sz,
1326 maxlen - sz);
1327 else
1328 DMEMIT("0 ");
1329
1330 DMEMIT("%u %u ", pg->nr_pgpaths,
1331 pg->ps.type->table_args);
1332
1333 list_for_each_entry(p, &pg->pgpaths, list) {
1334 DMEMIT("%s ", p->path.dev->name);
1335 if (pg->ps.type->status)
1336 sz += pg->ps.type->status(&pg->ps,
1337 &p->path, type, result + sz,
1338 maxlen - sz);
1339 }
1340 }
1341 break;
1342 }
1343
1344 spin_unlock_irqrestore(&m->lock, flags);
1345
1346 return 0;
1347}
1348
1349static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1350{
1351 int r;
1352 struct dm_dev *dev;
1353 struct multipath *m = (struct multipath *) ti->private;
1354 action_fn action;
1355
1356 if (argc == 1) {
1357 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001358 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001360 return queue_if_no_path(m, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
1362
1363 if (argc != 2)
1364 goto error;
1365
1366 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1367 return bypass_pg_num(m, argv[1], 1);
1368 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1369 return bypass_pg_num(m, argv[1], 0);
1370 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1371 return switch_pg_num(m, argv[1]);
1372 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1373 action = reinstate_path;
1374 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1375 action = fail_path;
1376 else
1377 goto error;
1378
1379 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1380 dm_table_get_mode(ti->table), &dev);
1381 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001382 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 argv[1]);
1384 return -EINVAL;
1385 }
1386
1387 r = action_dev(m, dev, action);
1388
1389 dm_put_device(ti, dev);
1390
1391 return r;
1392
1393error:
1394 DMWARN("Unrecognised multipath message received.");
1395 return -EINVAL;
1396}
1397
Al Viro647b3d02007-08-28 22:15:59 -04001398static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
Milan Broz9af4aa32006-10-03 01:15:20 -07001399 unsigned long arg)
1400{
1401 struct multipath *m = (struct multipath *) ti->private;
1402 struct block_device *bdev = NULL;
1403 unsigned long flags;
Milan Broze90dae12006-10-03 01:15:22 -07001404 struct file fake_file = {};
1405 struct dentry fake_dentry = {};
Milan Broz9af4aa32006-10-03 01:15:20 -07001406 int r = 0;
1407
Josef Sipekc649bb92006-12-08 02:37:19 -08001408 fake_file.f_path.dentry = &fake_dentry;
Milan Broze90dae12006-10-03 01:15:22 -07001409
Milan Broz9af4aa32006-10-03 01:15:20 -07001410 spin_lock_irqsave(&m->lock, flags);
1411
1412 if (!m->current_pgpath)
1413 __choose_pgpath(m);
1414
Milan Broze90dae12006-10-03 01:15:22 -07001415 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001416 bdev = m->current_pgpath->path.dev->bdev;
Milan Broze90dae12006-10-03 01:15:22 -07001417 fake_dentry.d_inode = bdev->bd_inode;
1418 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1419 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001420
1421 if (m->queue_io)
1422 r = -EAGAIN;
1423 else if (!bdev)
1424 r = -EIO;
1425
1426 spin_unlock_irqrestore(&m->lock, flags);
1427
Milan Broze90dae12006-10-03 01:15:22 -07001428 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1429 bdev->bd_disk, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001430}
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432/*-----------------------------------------------------------------
1433 * Module setup
1434 *---------------------------------------------------------------*/
1435static struct target_type multipath_target = {
1436 .name = "multipath",
Milan Broz9af4aa32006-10-03 01:15:20 -07001437 .version = {1, 0, 5},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 .module = THIS_MODULE,
1439 .ctr = multipath_ctr,
1440 .dtr = multipath_dtr,
1441 .map = multipath_map,
1442 .end_io = multipath_end_io,
1443 .presuspend = multipath_presuspend,
1444 .resume = multipath_resume,
1445 .status = multipath_status,
1446 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001447 .ioctl = multipath_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448};
1449
1450static int __init dm_multipath_init(void)
1451{
1452 int r;
1453
1454 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001455 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 if (!_mpio_cache)
1457 return -ENOMEM;
1458
1459 r = dm_register_target(&multipath_target);
1460 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001461 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 kmem_cache_destroy(_mpio_cache);
1463 return -EINVAL;
1464 }
1465
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001466 kmultipathd = create_workqueue("kmpathd");
1467 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001468 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001469 dm_unregister_target(&multipath_target);
1470 kmem_cache_destroy(_mpio_cache);
1471 return -ENOMEM;
1472 }
1473
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001474 /*
1475 * A separate workqueue is used to handle the device handlers
1476 * to avoid overloading existing workqueue. Overloading the
1477 * old workqueue would also create a bottleneck in the
1478 * path of the storage hardware device activation.
1479 */
1480 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1481 if (!kmpath_handlerd) {
1482 DMERR("failed to create workqueue kmpath_handlerd");
1483 destroy_workqueue(kmultipathd);
1484 dm_unregister_target(&multipath_target);
1485 kmem_cache_destroy(_mpio_cache);
1486 return -ENOMEM;
1487 }
1488
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001489 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 multipath_target.version[0], multipath_target.version[1],
1491 multipath_target.version[2]);
1492
1493 return r;
1494}
1495
1496static void __exit dm_multipath_exit(void)
1497{
1498 int r;
1499
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001500 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001501 destroy_workqueue(kmultipathd);
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 r = dm_unregister_target(&multipath_target);
1504 if (r < 0)
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001505 DMERR("target unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 kmem_cache_destroy(_mpio_cache);
1507}
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509module_init(dm_multipath_init);
1510module_exit(dm_multipath_exit);
1511
1512MODULE_DESCRIPTION(DM_NAME " multipath target");
1513MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1514MODULE_LICENSE("GPL");