blob: 58b1015260faa340cb26d4206b3b3cc615c74862 [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-list.h"
12#include "dm-bio-record.h"
Mike Andersonb15546f2007-10-19 22:48:02 +010013#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <linux/ctype.h>
16#include <linux/init.h>
17#include <linux/mempool.h>
18#include <linux/module.h>
19#include <linux/pagemap.h>
20#include <linux/slab.h>
21#include <linux/time.h>
22#include <linux/workqueue.h>
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070023#include <scsi/scsi_dh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/atomic.h>
25
Alasdair G Kergon72d94862006-06-26 00:27:35 -070026#define DM_MSG_PREFIX "multipath"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#define MESG_STR(x) x, sizeof(x)
28
29/* Path properties */
30struct pgpath {
31 struct list_head list;
32
33 struct priority_group *pg; /* Owning PG */
Kiyoshi Ueda66800732008-10-10 13:36:58 +010034 unsigned is_active; /* Path status */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 unsigned fail_count; /* Cumulative failure count */
36
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080037 struct dm_path path;
Mike Anderson224cb3e2008-08-29 09:36:09 +020038 struct work_struct deactivate_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039};
40
41#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
42
43/*
44 * Paths are grouped into Priority Groups and numbered from 1 upwards.
45 * Each has a path selector which controls which path gets used.
46 */
47struct priority_group {
48 struct list_head list;
49
50 struct multipath *m; /* Owning multipath instance */
51 struct path_selector ps;
52
53 unsigned pg_num; /* Reference number */
54 unsigned bypassed; /* Temporarily bypass this PG? */
55
56 unsigned nr_pgpaths; /* Number of paths in PG */
57 struct list_head pgpaths;
58};
59
60/* Multipath context */
61struct multipath {
62 struct list_head list;
63 struct dm_target *ti;
64
65 spinlock_t lock;
66
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070067 const char *hw_handler_name;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -070068 struct work_struct activate_path;
Chandra Seetharaman7253a332008-10-01 14:39:27 +010069 struct pgpath *pgpath_to_activate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 unsigned nr_priority_groups;
71 struct list_head priority_groups;
72 unsigned pg_init_required; /* pg_init needs calling? */
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -070073 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 unsigned nr_valid_paths; /* Total number of usable paths */
76 struct pgpath *current_pgpath;
77 struct priority_group *current_pg;
78 struct priority_group *next_pg; /* Switch to this PG if set */
79 unsigned repeat_count; /* I/Os left before calling PS again */
80
81 unsigned queue_io; /* Must we queue all I/O? */
82 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
Alasdair G Kergon436d4102005-07-12 15:53:03 -070083 unsigned saved_queue_if_no_path;/* Saved state during suspension */
Dave Wysochanskic9e45582007-10-19 22:47:53 +010084 unsigned pg_init_retries; /* Number of times to retry pg_init */
85 unsigned pg_init_count; /* Number of times pg_init called */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87 struct work_struct process_queued_ios;
88 struct bio_list queued_ios;
89 unsigned queue_size;
90
91 struct work_struct trigger_event;
92
93 /*
Alasdair G Kergon028867a2007-07-12 17:26:32 +010094 * We must use a mempool of dm_mpath_io structs so that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 * can resubmit bios on error.
96 */
97 mempool_t *mpio_pool;
98};
99
100/*
101 * Context information attached to each bio we process.
102 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100103struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct pgpath *pgpath;
105 struct dm_bio_details details;
106};
107
108typedef int (*action_fn) (struct pgpath *pgpath);
109
110#define MIN_IOS 256 /* Mempool size */
111
Christoph Lametere18b8902006-12-06 20:33:20 -0800112static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700114static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000115static void process_queued_ios(struct work_struct *work);
116static void trigger_event(struct work_struct *work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700117static void activate_path(struct work_struct *work);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200118static void deactivate_path(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120
121/*-----------------------------------------------
122 * Allocation routines
123 *-----------------------------------------------*/
124
125static struct pgpath *alloc_pgpath(void)
126{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700127 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Mike Anderson224cb3e2008-08-29 09:36:09 +0200129 if (pgpath) {
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100130 pgpath->is_active = 1;
Mike Anderson224cb3e2008-08-29 09:36:09 +0200131 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 return pgpath;
135}
136
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100137static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 kfree(pgpath);
140}
141
Mike Anderson224cb3e2008-08-29 09:36:09 +0200142static void deactivate_path(struct work_struct *work)
143{
144 struct pgpath *pgpath =
145 container_of(work, struct pgpath, deactivate_path);
146
147 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
148}
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static struct priority_group *alloc_priority_group(void)
151{
152 struct priority_group *pg;
153
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700154 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700156 if (pg)
157 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 return pg;
160}
161
162static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
163{
Chandra Seetharaman7253a332008-10-01 14:39:27 +0100164 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct pgpath *pgpath, *tmp;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700166 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
169 list_del(&pgpath->list);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700170 if (m->hw_handler_name)
171 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 dm_put_device(ti, pgpath->path.dev);
Chandra Seetharaman7253a332008-10-01 14:39:27 +0100173 spin_lock_irqsave(&m->lock, flags);
174 if (m->pgpath_to_activate == pgpath)
175 m->pgpath_to_activate = NULL;
176 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 free_pgpath(pgpath);
178 }
179}
180
181static void free_priority_group(struct priority_group *pg,
182 struct dm_target *ti)
183{
184 struct path_selector *ps = &pg->ps;
185
186 if (ps->type) {
187 ps->type->destroy(ps);
188 dm_put_path_selector(ps->type);
189 }
190
191 free_pgpaths(&pg->pgpaths, ti);
192 kfree(pg);
193}
194
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700195static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct multipath *m;
198
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700199 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 INIT_LIST_HEAD(&m->priority_groups);
202 spin_lock_init(&m->lock);
203 m->queue_io = 1;
David Howellsc4028952006-11-22 14:57:56 +0000204 INIT_WORK(&m->process_queued_ios, process_queued_ios);
205 INIT_WORK(&m->trigger_event, trigger_event);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700206 INIT_WORK(&m->activate_path, activate_path);
Matthew Dobson93d23412006-03-26 01:37:50 -0800207 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!m->mpio_pool) {
209 kfree(m);
210 return NULL;
211 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700212 m->ti = ti;
213 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
216 return m;
217}
218
219static void free_multipath(struct multipath *m)
220{
221 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
224 list_del(&pg->list);
225 free_priority_group(pg, m->ti);
226 }
227
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700228 kfree(m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 mempool_destroy(m->mpio_pool);
230 kfree(m);
231}
232
233
234/*-----------------------------------------------
235 * Path selection
236 *-----------------------------------------------*/
237
238static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
239{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 m->current_pg = pgpath->pg;
241
242 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700243 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 m->pg_init_required = 1;
245 m->queue_io = 1;
246 } else {
247 m->pg_init_required = 0;
248 m->queue_io = 0;
249 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100250
251 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
255{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800256 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
259 if (!path)
260 return -ENXIO;
261
262 m->current_pgpath = path_to_pgpath(path);
263
264 if (m->current_pg != pg)
265 __switch_pg(m, m->current_pgpath);
266
267 return 0;
268}
269
270static void __choose_pgpath(struct multipath *m)
271{
272 struct priority_group *pg;
273 unsigned bypassed = 1;
274
275 if (!m->nr_valid_paths)
276 goto failed;
277
278 /* Were we instructed to switch PG? */
279 if (m->next_pg) {
280 pg = m->next_pg;
281 m->next_pg = NULL;
282 if (!__choose_path_in_pg(m, pg))
283 return;
284 }
285
286 /* Don't change PG until it has no remaining paths */
287 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
288 return;
289
290 /*
291 * Loop through priority groups until we find a valid path.
292 * First time we skip PGs marked 'bypassed'.
293 * Second time we only try the ones we skipped.
294 */
295 do {
296 list_for_each_entry(pg, &m->priority_groups, list) {
297 if (pg->bypassed == bypassed)
298 continue;
299 if (!__choose_path_in_pg(m, pg))
300 return;
301 }
302 } while (bypassed--);
303
304failed:
305 m->current_pgpath = NULL;
306 m->current_pg = NULL;
307}
308
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800309/*
310 * Check whether bios must be queued in the device-mapper core rather
311 * than here in the target.
312 *
313 * m->lock must be held on entry.
314 *
315 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
316 * same value then we are not between multipath_presuspend()
317 * and multipath_resume() calls and we have no need to check
318 * for the DMF_NOFLUSH_SUSPENDING flag.
319 */
320static int __must_push_back(struct multipath *m)
321{
322 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
323 dm_noflush_suspending(m->ti));
324}
325
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100326static int map_io(struct multipath *m, struct bio *bio,
327 struct dm_mpath_io *mpio, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800329 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 unsigned long flags;
331 struct pgpath *pgpath;
332
333 spin_lock_irqsave(&m->lock, flags);
334
335 /* Do we need to select a new pgpath? */
336 if (!m->current_pgpath ||
337 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
338 __choose_pgpath(m);
339
340 pgpath = m->current_pgpath;
341
342 if (was_queued)
343 m->queue_size--;
344
345 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700346 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 /* Queue for the daemon to resubmit */
348 bio_list_add(&m->queued_ios, bio);
349 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700350 if ((m->pg_init_required && !m->pg_init_in_progress) ||
351 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700352 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800354 r = DM_MAPIO_SUBMITTED;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800355 } else if (pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 bio->bi_bdev = pgpath->path.dev->bdev;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800357 else if (__must_push_back(m))
358 r = DM_MAPIO_REQUEUE;
359 else
360 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 mpio->pgpath = pgpath;
363
364 spin_unlock_irqrestore(&m->lock, flags);
365
366 return r;
367}
368
369/*
370 * If we run out of usable paths, should we queue I/O or error it?
371 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700372static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
373 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 unsigned long flags;
376
377 spin_lock_irqsave(&m->lock, flags);
378
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700379 if (save_old_value)
380 m->saved_queue_if_no_path = m->queue_if_no_path;
381 else
382 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700384 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700385 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 spin_unlock_irqrestore(&m->lock, flags);
388
389 return 0;
390}
391
392/*-----------------------------------------------------------------
393 * The multipath daemon is responsible for resubmitting queued ios.
394 *---------------------------------------------------------------*/
395
396static void dispatch_queued_ios(struct multipath *m)
397{
398 int r;
399 unsigned long flags;
400 struct bio *bio = NULL, *next;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100401 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 union map_info *info;
403
404 spin_lock_irqsave(&m->lock, flags);
405 bio = bio_list_get(&m->queued_ios);
406 spin_unlock_irqrestore(&m->lock, flags);
407
408 while (bio) {
409 next = bio->bi_next;
410 bio->bi_next = NULL;
411
412 info = dm_get_mapinfo(bio);
413 mpio = info->ptr;
414
415 r = map_io(m, bio, mpio, 1);
416 if (r < 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200417 bio_endio(bio, r);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800418 else if (r == DM_MAPIO_REMAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 generic_make_request(bio);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800420 else if (r == DM_MAPIO_REQUEUE)
NeilBrown6712ecf2007-09-27 12:47:43 +0200421 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 bio = next;
424 }
425}
426
David Howellsc4028952006-11-22 14:57:56 +0000427static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
David Howellsc4028952006-11-22 14:57:56 +0000429 struct multipath *m =
430 container_of(work, struct multipath, process_queued_ios);
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700431 struct pgpath *pgpath = NULL;
432 unsigned init_required = 0, must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 unsigned long flags;
434
435 spin_lock_irqsave(&m->lock, flags);
436
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700437 if (!m->queue_size)
438 goto out;
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (!m->current_pgpath)
441 __choose_pgpath(m);
442
443 pgpath = m->current_pgpath;
444
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
Chandra Seetharamanb81aa1c2008-11-13 23:39:00 +0000449 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
450 m->pgpath_to_activate = pgpath;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100451 m->pg_init_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 m->pg_init_required = 0;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700453 m->pg_init_in_progress = 1;
454 init_required = 1;
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700457out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 spin_unlock_irqrestore(&m->lock, flags);
459
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700460 if (init_required)
461 queue_work(kmpath_handlerd, &m->activate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (!must_queue)
464 dispatch_queued_ios(m);
465}
466
467/*
468 * An event is triggered whenever a path is taken out of use.
469 * Includes path failure and PG bypass.
470 */
David Howellsc4028952006-11-22 14:57:56 +0000471static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
David Howellsc4028952006-11-22 14:57:56 +0000473 struct multipath *m =
474 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 dm_table_event(m->ti->table);
477}
478
479/*-----------------------------------------------------------------
480 * Constructor/argument parsing:
481 * <#multipath feature args> [<arg>]*
482 * <#hw_handler args> [hw_handler [<arg>]*]
483 * <#priority groups>
484 * <initial priority group>
485 * [<selector> <#selector args> [<arg>]*
486 * <#paths> <#per-path selector args>
487 * [<path> [<arg>]* ]+ ]+
488 *---------------------------------------------------------------*/
489struct param {
490 unsigned min;
491 unsigned max;
492 char *error;
493};
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static int read_param(struct param *param, char *str, unsigned *v, char **error)
496{
497 if (!str ||
498 (sscanf(str, "%u", v) != 1) ||
499 (*v < param->min) ||
500 (*v > param->max)) {
501 *error = param->error;
502 return -EINVAL;
503 }
504
505 return 0;
506}
507
508struct arg_set {
509 unsigned argc;
510 char **argv;
511};
512
513static char *shift(struct arg_set *as)
514{
515 char *r;
516
517 if (as->argc) {
518 as->argc--;
519 r = *as->argv;
520 as->argv++;
521 return r;
522 }
523
524 return NULL;
525}
526
527static void consume(struct arg_set *as, unsigned n)
528{
529 BUG_ON (as->argc < n);
530 as->argc -= n;
531 as->argv += n;
532}
533
534static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
535 struct dm_target *ti)
536{
537 int r;
538 struct path_selector_type *pst;
539 unsigned ps_argc;
540
541 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700542 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 };
544
545 pst = dm_get_path_selector(shift(as));
546 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700547 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return -EINVAL;
549 }
550
551 r = read_param(_params, shift(as), &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100552 if (r) {
553 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 r = pst->create(&pg->ps, ps_argc, as->argv);
558 if (r) {
559 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700560 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 return r;
562 }
563
564 pg->ps.type = pst;
565 consume(as, ps_argc);
566
567 return 0;
568}
569
570static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
571 struct dm_target *ti)
572{
573 int r;
574 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700575 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 /* we need at least a path arg */
578 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700579 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100580 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
583 p = alloc_pgpath();
584 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100585 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
588 dm_table_get_mode(ti->table), &p->path.dev);
589 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700590 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 goto bad;
592 }
593
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700594 if (m->hw_handler_name) {
595 r = scsi_dh_attach(bdev_get_queue(p->path.dev->bdev),
596 m->hw_handler_name);
597 if (r < 0) {
598 dm_put_device(ti, p->path.dev);
599 goto bad;
600 }
601 }
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
604 if (r) {
605 dm_put_device(ti, p->path.dev);
606 goto bad;
607 }
608
609 return p;
610
611 bad:
612 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100613 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700617 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700620 {1, 1024, "invalid number of paths"},
621 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 };
623
624 int r;
625 unsigned i, nr_selector_args, nr_params;
626 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700627 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 if (as->argc < 2) {
630 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100631 ti->error = "not enough priority group arguments";
632 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634
635 pg = alloc_priority_group();
636 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700637 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100638 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 }
640 pg->m = m;
641
642 r = parse_path_selector(as, pg, ti);
643 if (r)
644 goto bad;
645
646 /*
647 * read the paths
648 */
649 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
650 if (r)
651 goto bad;
652
653 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
654 if (r)
655 goto bad;
656
657 nr_params = 1 + nr_selector_args;
658 for (i = 0; i < pg->nr_pgpaths; i++) {
659 struct pgpath *pgpath;
660 struct arg_set path_args;
661
Mikulas Patocka148acff2008-07-21 12:00:30 +0100662 if (as->argc < nr_params) {
663 ti->error = "not enough path parameters";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 path_args.argc = nr_params;
668 path_args.argv = as->argv;
669
670 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100671 if (IS_ERR(pgpath)) {
672 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 pgpath->pg = pg;
677 list_add_tail(&pgpath->list, &pg->pgpaths);
678 consume(as, nr_params);
679 }
680
681 return pg;
682
683 bad:
684 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100685 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700688static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 unsigned hw_argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700691 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700694 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 };
696
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700697 if (read_param(_params, shift(as), &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 return -EINVAL;
699
700 if (!hw_argc)
701 return 0;
702
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700703 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
704 request_module("scsi_dh_%s", m->hw_handler_name);
705 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700706 ti->error = "unknown hardware handler type";
Chandra Seetharamanfe9233f2008-05-23 18:16:40 -0700707 kfree(m->hw_handler_name);
708 m->hw_handler_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return -EINVAL;
710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 consume(as, hw_argc - 1);
712
713 return 0;
714}
715
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700716static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 int r;
719 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700720 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100721 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100724 {0, 3, "invalid number of feature args"},
725 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 };
727
728 r = read_param(_params, shift(as), &argc, &ti->error);
729 if (r)
730 return -EINVAL;
731
732 if (!argc)
733 return 0;
734
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100735 do {
736 param_name = shift(as);
737 argc--;
738
739 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
740 r = queue_if_no_path(m, 1, 0);
741 continue;
742 }
743
744 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
745 (argc >= 1)) {
746 r = read_param(_params + 1, shift(as),
747 &m->pg_init_retries, &ti->error);
748 argc--;
749 continue;
750 }
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100753 r = -EINVAL;
754 } while (argc && !r);
755
756 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759static int multipath_ctr(struct dm_target *ti, unsigned int argc,
760 char **argv)
761{
762 /* target parameters */
763 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700764 {1, 1024, "invalid number of priority groups"},
765 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 };
767
768 int r;
769 struct multipath *m;
770 struct arg_set as;
771 unsigned pg_count = 0;
772 unsigned next_pg_num;
773
774 as.argc = argc;
775 as.argv = argv;
776
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700777 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700779 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 return -EINVAL;
781 }
782
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700783 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (r)
785 goto bad;
786
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700787 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (r)
789 goto bad;
790
791 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
792 if (r)
793 goto bad;
794
795 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
796 if (r)
797 goto bad;
798
799 /* parse the priority groups */
800 while (as.argc) {
801 struct priority_group *pg;
802
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700803 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100804 if (IS_ERR(pg)) {
805 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 goto bad;
807 }
808
809 m->nr_valid_paths += pg->nr_pgpaths;
810 list_add_tail(&pg->list, &m->priority_groups);
811 pg_count++;
812 pg->pg_num = pg_count;
813 if (!--next_pg_num)
814 m->next_pg = pg;
815 }
816
817 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700818 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 r = -EINVAL;
820 goto bad;
821 }
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return 0;
824
825 bad:
826 free_multipath(m);
827 return r;
828}
829
830static void multipath_dtr(struct dm_target *ti)
831{
832 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700833
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700834 flush_workqueue(kmpath_handlerd);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700835 flush_workqueue(kmultipathd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 free_multipath(m);
837}
838
839/*
840 * Map bios, recording original fields for later in case we have to resubmit
841 */
842static int multipath_map(struct dm_target *ti, struct bio *bio,
843 union map_info *map_context)
844{
845 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100846 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 struct multipath *m = (struct multipath *) ti->private;
848
849 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
850 dm_bio_record(&mpio->details, bio);
851
852 map_context->ptr = mpio;
Mike Christie6000a362008-08-19 18:45:30 -0500853 bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 r = map_io(m, bio, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800855 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 mempool_free(mpio, m->mpio_pool);
857
858 return r;
859}
860
861/*
862 * Take a path out of use.
863 */
864static int fail_path(struct pgpath *pgpath)
865{
866 unsigned long flags;
867 struct multipath *m = pgpath->pg->m;
868
869 spin_lock_irqsave(&m->lock, flags);
870
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100871 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 goto out;
873
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700874 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100877 pgpath->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 pgpath->fail_count++;
879
880 m->nr_valid_paths--;
881
882 if (pgpath == m->current_pgpath)
883 m->current_pgpath = NULL;
884
Mike Andersonb15546f2007-10-19 22:48:02 +0100885 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
886 pgpath->path.dev->name, m->nr_valid_paths);
887
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700888 queue_work(kmultipathd, &m->trigger_event);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200889 queue_work(kmultipathd, &pgpath->deactivate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891out:
892 spin_unlock_irqrestore(&m->lock, flags);
893
894 return 0;
895}
896
897/*
898 * Reinstate a previously-failed path
899 */
900static int reinstate_path(struct pgpath *pgpath)
901{
902 int r = 0;
903 unsigned long flags;
904 struct multipath *m = pgpath->pg->m;
905
906 spin_lock_irqsave(&m->lock, flags);
907
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100908 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 goto out;
910
Alasdair G Kergondef052d2008-07-21 12:00:31 +0100911 if (!pgpath->pg->ps.type->reinstate_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 DMWARN("Reinstate path not supported by path selector %s",
913 pgpath->pg->ps.type->name);
914 r = -EINVAL;
915 goto out;
916 }
917
918 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
919 if (r)
920 goto out;
921
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100922 pgpath->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 m->current_pgpath = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700925 if (!m->nr_valid_paths++ && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700926 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Mike Andersonb15546f2007-10-19 22:48:02 +0100928 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
929 pgpath->path.dev->name, m->nr_valid_paths);
930
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700931 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933out:
934 spin_unlock_irqrestore(&m->lock, flags);
935
936 return r;
937}
938
939/*
940 * Fail or reinstate all paths that match the provided struct dm_dev.
941 */
942static int action_dev(struct multipath *m, struct dm_dev *dev,
943 action_fn action)
944{
945 int r = 0;
946 struct pgpath *pgpath;
947 struct priority_group *pg;
948
949 list_for_each_entry(pg, &m->priority_groups, list) {
950 list_for_each_entry(pgpath, &pg->pgpaths, list) {
951 if (pgpath->path.dev == dev)
952 r = action(pgpath);
953 }
954 }
955
956 return r;
957}
958
959/*
960 * Temporarily try to avoid having to use the specified PG
961 */
962static void bypass_pg(struct multipath *m, struct priority_group *pg,
963 int bypassed)
964{
965 unsigned long flags;
966
967 spin_lock_irqsave(&m->lock, flags);
968
969 pg->bypassed = bypassed;
970 m->current_pgpath = NULL;
971 m->current_pg = NULL;
972
973 spin_unlock_irqrestore(&m->lock, flags);
974
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700975 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
977
978/*
979 * Switch to using the specified PG from the next I/O that gets mapped
980 */
981static int switch_pg_num(struct multipath *m, const char *pgstr)
982{
983 struct priority_group *pg;
984 unsigned pgnum;
985 unsigned long flags;
986
987 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
988 (pgnum > m->nr_priority_groups)) {
989 DMWARN("invalid PG number supplied to switch_pg_num");
990 return -EINVAL;
991 }
992
993 spin_lock_irqsave(&m->lock, flags);
994 list_for_each_entry(pg, &m->priority_groups, list) {
995 pg->bypassed = 0;
996 if (--pgnum)
997 continue;
998
999 m->current_pgpath = NULL;
1000 m->current_pg = NULL;
1001 m->next_pg = pg;
1002 }
1003 spin_unlock_irqrestore(&m->lock, flags);
1004
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001005 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return 0;
1007}
1008
1009/*
1010 * Set/clear bypassed status of a PG.
1011 * PGs are numbered upwards from 1 in the order they were declared.
1012 */
1013static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1014{
1015 struct priority_group *pg;
1016 unsigned pgnum;
1017
1018 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1019 (pgnum > m->nr_priority_groups)) {
1020 DMWARN("invalid PG number supplied to bypass_pg");
1021 return -EINVAL;
1022 }
1023
1024 list_for_each_entry(pg, &m->priority_groups, list) {
1025 if (!--pgnum)
1026 break;
1027 }
1028
1029 bypass_pg(m, pg, bypassed);
1030 return 0;
1031}
1032
1033/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001034 * Should we retry pg_init immediately?
1035 */
1036static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1037{
1038 unsigned long flags;
1039 int limit_reached = 0;
1040
1041 spin_lock_irqsave(&m->lock, flags);
1042
1043 if (m->pg_init_count <= m->pg_init_retries)
1044 m->pg_init_required = 1;
1045 else
1046 limit_reached = 1;
1047
1048 spin_unlock_irqrestore(&m->lock, flags);
1049
1050 return limit_reached;
1051}
1052
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001053static void pg_init_done(struct dm_path *path, int errors)
1054{
1055 struct pgpath *pgpath = path_to_pgpath(path);
1056 struct priority_group *pg = pgpath->pg;
1057 struct multipath *m = pg->m;
1058 unsigned long flags;
1059
1060 /* device or driver problems */
1061 switch (errors) {
1062 case SCSI_DH_OK:
1063 break;
1064 case SCSI_DH_NOSYS:
1065 if (!m->hw_handler_name) {
1066 errors = 0;
1067 break;
1068 }
1069 DMERR("Cannot failover device because scsi_dh_%s was not "
1070 "loaded.", m->hw_handler_name);
1071 /*
1072 * Fail path for now, so we do not ping pong
1073 */
1074 fail_path(pgpath);
1075 break;
1076 case SCSI_DH_DEV_TEMP_BUSY:
1077 /*
1078 * Probably doing something like FW upgrade on the
1079 * controller so try the other pg.
1080 */
1081 bypass_pg(m, pg, 1);
1082 break;
1083 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1084 case SCSI_DH_RETRY:
1085 case SCSI_DH_IMM_RETRY:
1086 case SCSI_DH_RES_TEMP_UNAVAIL:
1087 if (pg_init_limit_reached(m, pgpath))
1088 fail_path(pgpath);
1089 errors = 0;
1090 break;
1091 default:
1092 /*
1093 * We probably do not want to fail the path for a device
1094 * error, but this is what the old dm did. In future
1095 * patches we can do more advanced handling.
1096 */
1097 fail_path(pgpath);
1098 }
1099
1100 spin_lock_irqsave(&m->lock, flags);
1101 if (errors) {
1102 DMERR("Could not failover device. Error %d.", errors);
1103 m->current_pgpath = NULL;
1104 m->current_pg = NULL;
1105 } else if (!m->pg_init_required) {
1106 m->queue_io = 0;
1107 pg->bypassed = 0;
1108 }
1109
1110 m->pg_init_in_progress = 0;
1111 queue_work(kmultipathd, &m->process_queued_ios);
1112 spin_unlock_irqrestore(&m->lock, flags);
1113}
1114
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001115static void activate_path(struct work_struct *work)
1116{
1117 int ret;
1118 struct multipath *m =
1119 container_of(work, struct multipath, activate_path);
Chandra Seetharaman7253a332008-10-01 14:39:27 +01001120 struct dm_path *path;
1121 unsigned long flags;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001122
Chandra Seetharaman7253a332008-10-01 14:39:27 +01001123 spin_lock_irqsave(&m->lock, flags);
1124 path = &m->pgpath_to_activate->path;
1125 m->pgpath_to_activate = NULL;
1126 spin_unlock_irqrestore(&m->lock, flags);
1127 if (!path)
1128 return;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001129 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1130 pg_init_done(path, ret);
1131}
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133/*
1134 * end_io handling
1135 */
1136static int do_end_io(struct multipath *m, struct bio *bio,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001137 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Stefan Bader640eb3b2005-11-21 21:32:35 -08001139 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 if (!error)
1142 return 0; /* I/O complete */
1143
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -07001144 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1145 return error;
1146
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001147 if (error == -EOPNOTSUPP)
1148 return error;
1149
Stefan Bader640eb3b2005-11-21 21:32:35 -08001150 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 if (!m->nr_valid_paths) {
Kiyoshi Ueda45e15722006-12-08 02:41:10 -08001152 if (__must_push_back(m)) {
1153 spin_unlock_irqrestore(&m->lock, flags);
1154 return DM_ENDIO_REQUEUE;
1155 } else if (!m->queue_if_no_path) {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001156 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return -EIO;
1158 } else {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001159 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 goto requeue;
1161 }
1162 }
Stefan Bader640eb3b2005-11-21 21:32:35 -08001163 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001165 if (mpio->pgpath)
1166 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 requeue:
1169 dm_bio_restore(&mpio->details, bio);
1170
1171 /* queue for the daemon to resubmit or fail */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001172 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 bio_list_add(&m->queued_ios, bio);
1174 m->queue_size++;
1175 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001176 queue_work(kmultipathd, &m->process_queued_ios);
Stefan Bader640eb3b2005-11-21 21:32:35 -08001177 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001179 return DM_ENDIO_INCOMPLETE; /* io not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
1182static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1183 int error, union map_info *map_context)
1184{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001185 struct multipath *m = ti->private;
1186 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 struct pgpath *pgpath = mpio->pgpath;
1188 struct path_selector *ps;
1189 int r;
1190
1191 r = do_end_io(m, bio, error, mpio);
1192 if (pgpath) {
1193 ps = &pgpath->pg->ps;
1194 if (ps->type->end_io)
1195 ps->type->end_io(ps, &pgpath->path);
1196 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001197 if (r != DM_ENDIO_INCOMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 mempool_free(mpio, m->mpio_pool);
1199
1200 return r;
1201}
1202
1203/*
1204 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001205 * the last path fails we must error any remaining I/O.
1206 * Note that if the freeze_bdev fails while suspending, the
1207 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 */
1209static void multipath_presuspend(struct dm_target *ti)
1210{
1211 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001213 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001216/*
1217 * Restore the queue_if_no_path setting.
1218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219static void multipath_resume(struct dm_target *ti)
1220{
1221 struct multipath *m = (struct multipath *) ti->private;
1222 unsigned long flags;
1223
1224 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001225 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 spin_unlock_irqrestore(&m->lock, flags);
1227}
1228
1229/*
1230 * Info output has the following format:
1231 * num_multipath_feature_args [multipath_feature_args]*
1232 * num_handler_status_args [handler_status_args]*
1233 * num_groups init_group_number
1234 * [A|D|E num_ps_status_args [ps_status_args]*
1235 * num_paths num_selector_args
1236 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1237 *
1238 * Table output has the following format (identical to the constructor string):
1239 * num_feature_args [features_args]*
1240 * num_handler_args hw_handler [hw_handler_args]*
1241 * num_groups init_group_number
1242 * [priority selector-name num_ps_args [ps_args]*
1243 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1244 */
1245static int multipath_status(struct dm_target *ti, status_type_t type,
1246 char *result, unsigned int maxlen)
1247{
1248 int sz = 0;
1249 unsigned long flags;
1250 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 struct priority_group *pg;
1252 struct pgpath *p;
1253 unsigned pg_num;
1254 char state;
1255
1256 spin_lock_irqsave(&m->lock, flags);
1257
1258 /* Features */
1259 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001260 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1261 else {
1262 DMEMIT("%u ", m->queue_if_no_path +
1263 (m->pg_init_retries > 0) * 2);
1264 if (m->queue_if_no_path)
1265 DMEMIT("queue_if_no_path ");
1266 if (m->pg_init_retries)
1267 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001270 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 DMEMIT("0 ");
1272 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001273 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
1275 DMEMIT("%u ", m->nr_priority_groups);
1276
1277 if (m->next_pg)
1278 pg_num = m->next_pg->pg_num;
1279 else if (m->current_pg)
1280 pg_num = m->current_pg->pg_num;
1281 else
1282 pg_num = 1;
1283
1284 DMEMIT("%u ", pg_num);
1285
1286 switch (type) {
1287 case STATUSTYPE_INFO:
1288 list_for_each_entry(pg, &m->priority_groups, list) {
1289 if (pg->bypassed)
1290 state = 'D'; /* Disabled */
1291 else if (pg == m->current_pg)
1292 state = 'A'; /* Currently Active */
1293 else
1294 state = 'E'; /* Enabled */
1295
1296 DMEMIT("%c ", state);
1297
1298 if (pg->ps.type->status)
1299 sz += pg->ps.type->status(&pg->ps, NULL, type,
1300 result + sz,
1301 maxlen - sz);
1302 else
1303 DMEMIT("0 ");
1304
1305 DMEMIT("%u %u ", pg->nr_pgpaths,
1306 pg->ps.type->info_args);
1307
1308 list_for_each_entry(p, &pg->pgpaths, list) {
1309 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001310 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 p->fail_count);
1312 if (pg->ps.type->status)
1313 sz += pg->ps.type->status(&pg->ps,
1314 &p->path, type, result + sz,
1315 maxlen - sz);
1316 }
1317 }
1318 break;
1319
1320 case STATUSTYPE_TABLE:
1321 list_for_each_entry(pg, &m->priority_groups, list) {
1322 DMEMIT("%s ", pg->ps.type->name);
1323
1324 if (pg->ps.type->status)
1325 sz += pg->ps.type->status(&pg->ps, NULL, type,
1326 result + sz,
1327 maxlen - sz);
1328 else
1329 DMEMIT("0 ");
1330
1331 DMEMIT("%u %u ", pg->nr_pgpaths,
1332 pg->ps.type->table_args);
1333
1334 list_for_each_entry(p, &pg->pgpaths, list) {
1335 DMEMIT("%s ", p->path.dev->name);
1336 if (pg->ps.type->status)
1337 sz += pg->ps.type->status(&pg->ps,
1338 &p->path, type, result + sz,
1339 maxlen - sz);
1340 }
1341 }
1342 break;
1343 }
1344
1345 spin_unlock_irqrestore(&m->lock, flags);
1346
1347 return 0;
1348}
1349
1350static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1351{
1352 int r;
1353 struct dm_dev *dev;
1354 struct multipath *m = (struct multipath *) ti->private;
1355 action_fn action;
1356
1357 if (argc == 1) {
1358 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001359 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001361 return queue_if_no_path(m, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
1364 if (argc != 2)
1365 goto error;
1366
1367 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1368 return bypass_pg_num(m, argv[1], 1);
1369 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1370 return bypass_pg_num(m, argv[1], 0);
1371 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1372 return switch_pg_num(m, argv[1]);
1373 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1374 action = reinstate_path;
1375 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1376 action = fail_path;
1377 else
1378 goto error;
1379
1380 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1381 dm_table_get_mode(ti->table), &dev);
1382 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001383 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 argv[1]);
1385 return -EINVAL;
1386 }
1387
1388 r = action_dev(m, dev, action);
1389
1390 dm_put_device(ti, dev);
1391
1392 return r;
1393
1394error:
1395 DMWARN("Unrecognised multipath message received.");
1396 return -EINVAL;
1397}
1398
Al Viro647b3d02007-08-28 22:15:59 -04001399static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
Milan Broz9af4aa32006-10-03 01:15:20 -07001400 unsigned long arg)
1401{
1402 struct multipath *m = (struct multipath *) ti->private;
1403 struct block_device *bdev = NULL;
Al Viro633a08b2007-08-29 20:34:12 -04001404 fmode_t mode = 0;
Milan Broz9af4aa32006-10-03 01:15:20 -07001405 unsigned long flags;
1406 int r = 0;
1407
1408 spin_lock_irqsave(&m->lock, flags);
1409
1410 if (!m->current_pgpath)
1411 __choose_pgpath(m);
1412
Milan Broze90dae12006-10-03 01:15:22 -07001413 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001414 bdev = m->current_pgpath->path.dev->bdev;
Al Viro633a08b2007-08-29 20:34:12 -04001415 mode = m->current_pgpath->path.dev->mode;
Milan Broze90dae12006-10-03 01:15:22 -07001416 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001417
1418 if (m->queue_io)
1419 r = -EAGAIN;
1420 else if (!bdev)
1421 r = -EIO;
1422
1423 spin_unlock_irqrestore(&m->lock, flags);
1424
Al Viro633a08b2007-08-29 20:34:12 -04001425 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001426}
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428/*-----------------------------------------------------------------
1429 * Module setup
1430 *---------------------------------------------------------------*/
1431static struct target_type multipath_target = {
1432 .name = "multipath",
Milan Broz9af4aa32006-10-03 01:15:20 -07001433 .version = {1, 0, 5},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 .module = THIS_MODULE,
1435 .ctr = multipath_ctr,
1436 .dtr = multipath_dtr,
1437 .map = multipath_map,
1438 .end_io = multipath_end_io,
1439 .presuspend = multipath_presuspend,
1440 .resume = multipath_resume,
1441 .status = multipath_status,
1442 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001443 .ioctl = multipath_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444};
1445
1446static int __init dm_multipath_init(void)
1447{
1448 int r;
1449
1450 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001451 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 if (!_mpio_cache)
1453 return -ENOMEM;
1454
1455 r = dm_register_target(&multipath_target);
1456 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001457 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 kmem_cache_destroy(_mpio_cache);
1459 return -EINVAL;
1460 }
1461
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001462 kmultipathd = create_workqueue("kmpathd");
1463 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001464 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001465 dm_unregister_target(&multipath_target);
1466 kmem_cache_destroy(_mpio_cache);
1467 return -ENOMEM;
1468 }
1469
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001470 /*
1471 * A separate workqueue is used to handle the device handlers
1472 * to avoid overloading existing workqueue. Overloading the
1473 * old workqueue would also create a bottleneck in the
1474 * path of the storage hardware device activation.
1475 */
1476 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1477 if (!kmpath_handlerd) {
1478 DMERR("failed to create workqueue kmpath_handlerd");
1479 destroy_workqueue(kmultipathd);
1480 dm_unregister_target(&multipath_target);
1481 kmem_cache_destroy(_mpio_cache);
1482 return -ENOMEM;
1483 }
1484
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001485 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 multipath_target.version[0], multipath_target.version[1],
1487 multipath_target.version[2]);
1488
1489 return r;
1490}
1491
1492static void __exit dm_multipath_exit(void)
1493{
1494 int r;
1495
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001496 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001497 destroy_workqueue(kmultipathd);
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 r = dm_unregister_target(&multipath_target);
1500 if (r < 0)
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001501 DMERR("target unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 kmem_cache_destroy(_mpio_cache);
1503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505module_init(dm_multipath_init);
1506module_exit(dm_multipath_exit);
1507
1508MODULE_DESCRIPTION(DM_NAME " multipath target");
1509MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1510MODULE_LICENSE("GPL");