blob: 3d3848132c6925ae711734d45efc2380c23c9784 [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 */
33 unsigned fail_count; /* Cumulative failure count */
34
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -080035 struct dm_path path;
Mike Anderson224cb3e2008-08-29 09:36:09 +020036 struct work_struct deactivate_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
39#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
40
41/*
42 * Paths are grouped into Priority Groups and numbered from 1 upwards.
43 * Each has a path selector which controls which path gets used.
44 */
45struct priority_group {
46 struct list_head list;
47
48 struct multipath *m; /* Owning multipath instance */
49 struct path_selector ps;
50
51 unsigned pg_num; /* Reference number */
52 unsigned bypassed; /* Temporarily bypass this PG? */
53
54 unsigned nr_pgpaths; /* Number of paths in PG */
55 struct list_head pgpaths;
56};
57
58/* Multipath context */
59struct multipath {
60 struct list_head list;
61 struct dm_target *ti;
62
63 spinlock_t lock;
64
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070065 const char *hw_handler_name;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -070066 struct work_struct activate_path;
Chandra Seetharaman7253a332008-10-01 14:39:27 +010067 struct pgpath *pgpath_to_activate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 unsigned nr_priority_groups;
69 struct list_head priority_groups;
70 unsigned pg_init_required; /* pg_init needs calling? */
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -070071 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 unsigned nr_valid_paths; /* Total number of usable paths */
74 struct pgpath *current_pgpath;
75 struct priority_group *current_pg;
76 struct priority_group *next_pg; /* Switch to this PG if set */
77 unsigned repeat_count; /* I/Os left before calling PS again */
78
79 unsigned queue_io; /* Must we queue all I/O? */
80 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
Alasdair G Kergon436d4102005-07-12 15:53:03 -070081 unsigned saved_queue_if_no_path;/* Saved state during suspension */
Dave Wysochanskic9e45582007-10-19 22:47:53 +010082 unsigned pg_init_retries; /* Number of times to retry pg_init */
83 unsigned pg_init_count; /* Number of times pg_init called */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 struct work_struct process_queued_ios;
86 struct bio_list queued_ios;
87 unsigned queue_size;
88
89 struct work_struct trigger_event;
90
91 /*
Alasdair G Kergon028867a2007-07-12 17:26:32 +010092 * We must use a mempool of dm_mpath_io structs so that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * can resubmit bios on error.
94 */
95 mempool_t *mpio_pool;
96};
97
98/*
99 * Context information attached to each bio we process.
100 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100101struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct pgpath *pgpath;
103 struct dm_bio_details details;
104};
105
106typedef int (*action_fn) (struct pgpath *pgpath);
107
108#define MIN_IOS 256 /* Mempool size */
109
Christoph Lametere18b8902006-12-06 20:33:20 -0800110static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700112static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000113static void process_queued_ios(struct work_struct *work);
114static void trigger_event(struct work_struct *work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700115static void activate_path(struct work_struct *work);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200116static void deactivate_path(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118
119/*-----------------------------------------------
120 * Allocation routines
121 *-----------------------------------------------*/
122
123static struct pgpath *alloc_pgpath(void)
124{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700125 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Mike Anderson224cb3e2008-08-29 09:36:09 +0200127 if (pgpath) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 pgpath->path.is_active = 1;
Mike Anderson224cb3e2008-08-29 09:36:09 +0200129 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 return pgpath;
133}
134
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100135static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 kfree(pgpath);
138}
139
Mike Anderson224cb3e2008-08-29 09:36:09 +0200140static void deactivate_path(struct work_struct *work)
141{
142 struct pgpath *pgpath =
143 container_of(work, struct pgpath, deactivate_path);
144
145 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
146}
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148static struct priority_group *alloc_priority_group(void)
149{
150 struct priority_group *pg;
151
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700152 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700154 if (pg)
155 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 return pg;
158}
159
160static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
161{
Chandra Seetharaman7253a332008-10-01 14:39:27 +0100162 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct pgpath *pgpath, *tmp;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700164 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
167 list_del(&pgpath->list);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700168 if (m->hw_handler_name)
169 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 dm_put_device(ti, pgpath->path.dev);
Chandra Seetharaman7253a332008-10-01 14:39:27 +0100171 spin_lock_irqsave(&m->lock, flags);
172 if (m->pgpath_to_activate == pgpath)
173 m->pgpath_to_activate = NULL;
174 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 free_pgpath(pgpath);
176 }
177}
178
179static void free_priority_group(struct priority_group *pg,
180 struct dm_target *ti)
181{
182 struct path_selector *ps = &pg->ps;
183
184 if (ps->type) {
185 ps->type->destroy(ps);
186 dm_put_path_selector(ps->type);
187 }
188
189 free_pgpaths(&pg->pgpaths, ti);
190 kfree(pg);
191}
192
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700193static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 struct multipath *m;
196
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700197 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 INIT_LIST_HEAD(&m->priority_groups);
200 spin_lock_init(&m->lock);
201 m->queue_io = 1;
David Howellsc4028952006-11-22 14:57:56 +0000202 INIT_WORK(&m->process_queued_ios, process_queued_ios);
203 INIT_WORK(&m->trigger_event, trigger_event);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700204 INIT_WORK(&m->activate_path, activate_path);
Matthew Dobson93d23412006-03-26 01:37:50 -0800205 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (!m->mpio_pool) {
207 kfree(m);
208 return NULL;
209 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700210 m->ti = ti;
211 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
214 return m;
215}
216
217static void free_multipath(struct multipath *m)
218{
219 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
222 list_del(&pg->list);
223 free_priority_group(pg, m->ti);
224 }
225
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700226 kfree(m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 mempool_destroy(m->mpio_pool);
228 kfree(m);
229}
230
231
232/*-----------------------------------------------
233 * Path selection
234 *-----------------------------------------------*/
235
236static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
237{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 m->current_pg = pgpath->pg;
239
240 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700241 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 m->pg_init_required = 1;
243 m->queue_io = 1;
244 } else {
245 m->pg_init_required = 0;
246 m->queue_io = 0;
247 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100248
249 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
252static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
253{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800254 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
257 if (!path)
258 return -ENXIO;
259
260 m->current_pgpath = path_to_pgpath(path);
261
262 if (m->current_pg != pg)
263 __switch_pg(m, m->current_pgpath);
264
265 return 0;
266}
267
268static void __choose_pgpath(struct multipath *m)
269{
270 struct priority_group *pg;
271 unsigned bypassed = 1;
272
273 if (!m->nr_valid_paths)
274 goto failed;
275
276 /* Were we instructed to switch PG? */
277 if (m->next_pg) {
278 pg = m->next_pg;
279 m->next_pg = NULL;
280 if (!__choose_path_in_pg(m, pg))
281 return;
282 }
283
284 /* Don't change PG until it has no remaining paths */
285 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
286 return;
287
288 /*
289 * Loop through priority groups until we find a valid path.
290 * First time we skip PGs marked 'bypassed'.
291 * Second time we only try the ones we skipped.
292 */
293 do {
294 list_for_each_entry(pg, &m->priority_groups, list) {
295 if (pg->bypassed == bypassed)
296 continue;
297 if (!__choose_path_in_pg(m, pg))
298 return;
299 }
300 } while (bypassed--);
301
302failed:
303 m->current_pgpath = NULL;
304 m->current_pg = NULL;
305}
306
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800307/*
308 * Check whether bios must be queued in the device-mapper core rather
309 * than here in the target.
310 *
311 * m->lock must be held on entry.
312 *
313 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
314 * same value then we are not between multipath_presuspend()
315 * and multipath_resume() calls and we have no need to check
316 * for the DMF_NOFLUSH_SUSPENDING flag.
317 */
318static int __must_push_back(struct multipath *m)
319{
320 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
321 dm_noflush_suspending(m->ti));
322}
323
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100324static int map_io(struct multipath *m, struct bio *bio,
325 struct dm_mpath_io *mpio, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800327 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 unsigned long flags;
329 struct pgpath *pgpath;
330
331 spin_lock_irqsave(&m->lock, flags);
332
333 /* Do we need to select a new pgpath? */
334 if (!m->current_pgpath ||
335 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
336 __choose_pgpath(m);
337
338 pgpath = m->current_pgpath;
339
340 if (was_queued)
341 m->queue_size--;
342
343 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700344 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /* Queue for the daemon to resubmit */
346 bio_list_add(&m->queued_ios, bio);
347 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700348 if ((m->pg_init_required && !m->pg_init_in_progress) ||
349 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700350 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800352 r = DM_MAPIO_SUBMITTED;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800353 } else if (pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 bio->bi_bdev = pgpath->path.dev->bdev;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800355 else if (__must_push_back(m))
356 r = DM_MAPIO_REQUEUE;
357 else
358 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 mpio->pgpath = pgpath;
361
362 spin_unlock_irqrestore(&m->lock, flags);
363
364 return r;
365}
366
367/*
368 * If we run out of usable paths, should we queue I/O or error it?
369 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700370static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
371 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372{
373 unsigned long flags;
374
375 spin_lock_irqsave(&m->lock, flags);
376
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700377 if (save_old_value)
378 m->saved_queue_if_no_path = m->queue_if_no_path;
379 else
380 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700382 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700383 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
385 spin_unlock_irqrestore(&m->lock, flags);
386
387 return 0;
388}
389
390/*-----------------------------------------------------------------
391 * The multipath daemon is responsible for resubmitting queued ios.
392 *---------------------------------------------------------------*/
393
394static void dispatch_queued_ios(struct multipath *m)
395{
396 int r;
397 unsigned long flags;
398 struct bio *bio = NULL, *next;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100399 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 union map_info *info;
401
402 spin_lock_irqsave(&m->lock, flags);
403 bio = bio_list_get(&m->queued_ios);
404 spin_unlock_irqrestore(&m->lock, flags);
405
406 while (bio) {
407 next = bio->bi_next;
408 bio->bi_next = NULL;
409
410 info = dm_get_mapinfo(bio);
411 mpio = info->ptr;
412
413 r = map_io(m, bio, mpio, 1);
414 if (r < 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200415 bio_endio(bio, r);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800416 else if (r == DM_MAPIO_REMAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 generic_make_request(bio);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800418 else if (r == DM_MAPIO_REQUEUE)
NeilBrown6712ecf2007-09-27 12:47:43 +0200419 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 bio = next;
422 }
423}
424
David Howellsc4028952006-11-22 14:57:56 +0000425static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
David Howellsc4028952006-11-22 14:57:56 +0000427 struct multipath *m =
428 container_of(work, struct multipath, process_queued_ios);
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700429 struct pgpath *pgpath = NULL;
430 unsigned init_required = 0, must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 unsigned long flags;
432
433 spin_lock_irqsave(&m->lock, flags);
434
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700435 if (!m->queue_size)
436 goto out;
437
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 if (!m->current_pgpath)
439 __choose_pgpath(m);
440
441 pgpath = m->current_pgpath;
Chandra Seetharaman7253a332008-10-01 14:39:27 +0100442 m->pgpath_to_activate = m->current_pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700444 if ((pgpath && !m->queue_io) ||
445 (!pgpath && !m->queue_if_no_path))
446 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700448 if (m->pg_init_required && !m->pg_init_in_progress) {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100449 m->pg_init_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 m->pg_init_required = 0;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700451 m->pg_init_in_progress = 1;
452 init_required = 1;
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700455out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 spin_unlock_irqrestore(&m->lock, flags);
457
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700458 if (init_required)
459 queue_work(kmpath_handlerd, &m->activate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461 if (!must_queue)
462 dispatch_queued_ios(m);
463}
464
465/*
466 * An event is triggered whenever a path is taken out of use.
467 * Includes path failure and PG bypass.
468 */
David Howellsc4028952006-11-22 14:57:56 +0000469static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
David Howellsc4028952006-11-22 14:57:56 +0000471 struct multipath *m =
472 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 dm_table_event(m->ti->table);
475}
476
477/*-----------------------------------------------------------------
478 * Constructor/argument parsing:
479 * <#multipath feature args> [<arg>]*
480 * <#hw_handler args> [hw_handler [<arg>]*]
481 * <#priority groups>
482 * <initial priority group>
483 * [<selector> <#selector args> [<arg>]*
484 * <#paths> <#per-path selector args>
485 * [<path> [<arg>]* ]+ ]+
486 *---------------------------------------------------------------*/
487struct param {
488 unsigned min;
489 unsigned max;
490 char *error;
491};
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static int read_param(struct param *param, char *str, unsigned *v, char **error)
494{
495 if (!str ||
496 (sscanf(str, "%u", v) != 1) ||
497 (*v < param->min) ||
498 (*v > param->max)) {
499 *error = param->error;
500 return -EINVAL;
501 }
502
503 return 0;
504}
505
506struct arg_set {
507 unsigned argc;
508 char **argv;
509};
510
511static char *shift(struct arg_set *as)
512{
513 char *r;
514
515 if (as->argc) {
516 as->argc--;
517 r = *as->argv;
518 as->argv++;
519 return r;
520 }
521
522 return NULL;
523}
524
525static void consume(struct arg_set *as, unsigned n)
526{
527 BUG_ON (as->argc < n);
528 as->argc -= n;
529 as->argv += n;
530}
531
532static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
533 struct dm_target *ti)
534{
535 int r;
536 struct path_selector_type *pst;
537 unsigned ps_argc;
538
539 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700540 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 };
542
543 pst = dm_get_path_selector(shift(as));
544 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700545 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return -EINVAL;
547 }
548
549 r = read_param(_params, shift(as), &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100550 if (r) {
551 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 r = pst->create(&pg->ps, ps_argc, as->argv);
556 if (r) {
557 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700558 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return r;
560 }
561
562 pg->ps.type = pst;
563 consume(as, ps_argc);
564
565 return 0;
566}
567
568static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
569 struct dm_target *ti)
570{
571 int r;
572 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700573 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /* we need at least a path arg */
576 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700577 ti->error = "no device given";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return NULL;
579 }
580
581 p = alloc_pgpath();
582 if (!p)
583 return NULL;
584
585 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
586 dm_table_get_mode(ti->table), &p->path.dev);
587 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700588 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto bad;
590 }
591
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700592 if (m->hw_handler_name) {
593 r = scsi_dh_attach(bdev_get_queue(p->path.dev->bdev),
594 m->hw_handler_name);
595 if (r < 0) {
596 dm_put_device(ti, p->path.dev);
597 goto bad;
598 }
599 }
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
602 if (r) {
603 dm_put_device(ti, p->path.dev);
604 goto bad;
605 }
606
607 return p;
608
609 bad:
610 free_pgpath(p);
611 return NULL;
612}
613
614static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700615 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
617 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700618 {1, 1024, "invalid number of paths"},
619 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 };
621
622 int r;
623 unsigned i, nr_selector_args, nr_params;
624 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700625 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 if (as->argc < 2) {
628 as->argc = 0;
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700629 ti->error = "not enough priority group aruments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return NULL;
631 }
632
633 pg = alloc_priority_group();
634 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700635 ti->error = "couldn't allocate priority group";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 return NULL;
637 }
638 pg->m = m;
639
640 r = parse_path_selector(as, pg, ti);
641 if (r)
642 goto bad;
643
644 /*
645 * read the paths
646 */
647 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
648 if (r)
649 goto bad;
650
651 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
652 if (r)
653 goto bad;
654
655 nr_params = 1 + nr_selector_args;
656 for (i = 0; i < pg->nr_pgpaths; i++) {
657 struct pgpath *pgpath;
658 struct arg_set path_args;
659
Mikulas Patocka148acff2008-07-21 12:00:30 +0100660 if (as->argc < nr_params) {
661 ti->error = "not enough path parameters";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 path_args.argc = nr_params;
666 path_args.argv = as->argv;
667
668 pgpath = parse_path(&path_args, &pg->ps, ti);
669 if (!pgpath)
670 goto bad;
671
672 pgpath->pg = pg;
673 list_add_tail(&pgpath->list, &pg->pgpaths);
674 consume(as, nr_params);
675 }
676
677 return pg;
678
679 bad:
680 free_priority_group(pg, ti);
681 return NULL;
682}
683
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700684static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 unsigned hw_argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700687 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700690 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 };
692
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700693 if (read_param(_params, shift(as), &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -EINVAL;
695
696 if (!hw_argc)
697 return 0;
698
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700699 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
700 request_module("scsi_dh_%s", m->hw_handler_name);
701 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700702 ti->error = "unknown hardware handler type";
Chandra Seetharamanfe9233f2008-05-23 18:16:40 -0700703 kfree(m->hw_handler_name);
704 m->hw_handler_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return -EINVAL;
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 consume(as, hw_argc - 1);
708
709 return 0;
710}
711
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700712static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 int r;
715 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700716 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100717 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
719 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100720 {0, 3, "invalid number of feature args"},
721 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 };
723
724 r = read_param(_params, shift(as), &argc, &ti->error);
725 if (r)
726 return -EINVAL;
727
728 if (!argc)
729 return 0;
730
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100731 do {
732 param_name = shift(as);
733 argc--;
734
735 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
736 r = queue_if_no_path(m, 1, 0);
737 continue;
738 }
739
740 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
741 (argc >= 1)) {
742 r = read_param(_params + 1, shift(as),
743 &m->pg_init_retries, &ti->error);
744 argc--;
745 continue;
746 }
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100749 r = -EINVAL;
750 } while (argc && !r);
751
752 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
755static int multipath_ctr(struct dm_target *ti, unsigned int argc,
756 char **argv)
757{
758 /* target parameters */
759 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700760 {1, 1024, "invalid number of priority groups"},
761 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 };
763
764 int r;
765 struct multipath *m;
766 struct arg_set as;
767 unsigned pg_count = 0;
768 unsigned next_pg_num;
769
770 as.argc = argc;
771 as.argv = argv;
772
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700773 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700775 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 return -EINVAL;
777 }
778
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700779 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (r)
781 goto bad;
782
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700783 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (r)
785 goto bad;
786
787 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
788 if (r)
789 goto bad;
790
791 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
792 if (r)
793 goto bad;
794
795 /* parse the priority groups */
796 while (as.argc) {
797 struct priority_group *pg;
798
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700799 pg = parse_priority_group(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (!pg) {
801 r = -EINVAL;
802 goto bad;
803 }
804
805 m->nr_valid_paths += pg->nr_pgpaths;
806 list_add_tail(&pg->list, &m->priority_groups);
807 pg_count++;
808 pg->pg_num = pg_count;
809 if (!--next_pg_num)
810 m->next_pg = pg;
811 }
812
813 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700814 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 r = -EINVAL;
816 goto bad;
817 }
818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 return 0;
820
821 bad:
822 free_multipath(m);
823 return r;
824}
825
826static void multipath_dtr(struct dm_target *ti)
827{
828 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700829
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700830 flush_workqueue(kmpath_handlerd);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700831 flush_workqueue(kmultipathd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 free_multipath(m);
833}
834
835/*
836 * Map bios, recording original fields for later in case we have to resubmit
837 */
838static int multipath_map(struct dm_target *ti, struct bio *bio,
839 union map_info *map_context)
840{
841 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100842 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct multipath *m = (struct multipath *) ti->private;
844
845 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
846 dm_bio_record(&mpio->details, bio);
847
848 map_context->ptr = mpio;
849 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
850 r = map_io(m, bio, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800851 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 mempool_free(mpio, m->mpio_pool);
853
854 return r;
855}
856
857/*
858 * Take a path out of use.
859 */
860static int fail_path(struct pgpath *pgpath)
861{
862 unsigned long flags;
863 struct multipath *m = pgpath->pg->m;
864
865 spin_lock_irqsave(&m->lock, flags);
866
867 if (!pgpath->path.is_active)
868 goto out;
869
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700870 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
873 pgpath->path.is_active = 0;
874 pgpath->fail_count++;
875
876 m->nr_valid_paths--;
877
878 if (pgpath == m->current_pgpath)
879 m->current_pgpath = NULL;
880
Mike Andersonb15546f2007-10-19 22:48:02 +0100881 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
882 pgpath->path.dev->name, m->nr_valid_paths);
883
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700884 queue_work(kmultipathd, &m->trigger_event);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200885 queue_work(kmultipathd, &pgpath->deactivate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887out:
888 spin_unlock_irqrestore(&m->lock, flags);
889
890 return 0;
891}
892
893/*
894 * Reinstate a previously-failed path
895 */
896static int reinstate_path(struct pgpath *pgpath)
897{
898 int r = 0;
899 unsigned long flags;
900 struct multipath *m = pgpath->pg->m;
901
902 spin_lock_irqsave(&m->lock, flags);
903
904 if (pgpath->path.is_active)
905 goto out;
906
Alasdair G Kergondef052d2008-07-21 12:00:31 +0100907 if (!pgpath->pg->ps.type->reinstate_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 DMWARN("Reinstate path not supported by path selector %s",
909 pgpath->pg->ps.type->name);
910 r = -EINVAL;
911 goto out;
912 }
913
914 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
915 if (r)
916 goto out;
917
918 pgpath->path.is_active = 1;
919
920 m->current_pgpath = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700921 if (!m->nr_valid_paths++ && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700922 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Mike Andersonb15546f2007-10-19 22:48:02 +0100924 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
925 pgpath->path.dev->name, m->nr_valid_paths);
926
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700927 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929out:
930 spin_unlock_irqrestore(&m->lock, flags);
931
932 return r;
933}
934
935/*
936 * Fail or reinstate all paths that match the provided struct dm_dev.
937 */
938static int action_dev(struct multipath *m, struct dm_dev *dev,
939 action_fn action)
940{
941 int r = 0;
942 struct pgpath *pgpath;
943 struct priority_group *pg;
944
945 list_for_each_entry(pg, &m->priority_groups, list) {
946 list_for_each_entry(pgpath, &pg->pgpaths, list) {
947 if (pgpath->path.dev == dev)
948 r = action(pgpath);
949 }
950 }
951
952 return r;
953}
954
955/*
956 * Temporarily try to avoid having to use the specified PG
957 */
958static void bypass_pg(struct multipath *m, struct priority_group *pg,
959 int bypassed)
960{
961 unsigned long flags;
962
963 spin_lock_irqsave(&m->lock, flags);
964
965 pg->bypassed = bypassed;
966 m->current_pgpath = NULL;
967 m->current_pg = NULL;
968
969 spin_unlock_irqrestore(&m->lock, flags);
970
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700971 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
974/*
975 * Switch to using the specified PG from the next I/O that gets mapped
976 */
977static int switch_pg_num(struct multipath *m, const char *pgstr)
978{
979 struct priority_group *pg;
980 unsigned pgnum;
981 unsigned long flags;
982
983 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
984 (pgnum > m->nr_priority_groups)) {
985 DMWARN("invalid PG number supplied to switch_pg_num");
986 return -EINVAL;
987 }
988
989 spin_lock_irqsave(&m->lock, flags);
990 list_for_each_entry(pg, &m->priority_groups, list) {
991 pg->bypassed = 0;
992 if (--pgnum)
993 continue;
994
995 m->current_pgpath = NULL;
996 m->current_pg = NULL;
997 m->next_pg = pg;
998 }
999 spin_unlock_irqrestore(&m->lock, flags);
1000
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001001 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return 0;
1003}
1004
1005/*
1006 * Set/clear bypassed status of a PG.
1007 * PGs are numbered upwards from 1 in the order they were declared.
1008 */
1009static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1010{
1011 struct priority_group *pg;
1012 unsigned pgnum;
1013
1014 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1015 (pgnum > m->nr_priority_groups)) {
1016 DMWARN("invalid PG number supplied to bypass_pg");
1017 return -EINVAL;
1018 }
1019
1020 list_for_each_entry(pg, &m->priority_groups, list) {
1021 if (!--pgnum)
1022 break;
1023 }
1024
1025 bypass_pg(m, pg, bypassed);
1026 return 0;
1027}
1028
1029/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001030 * Should we retry pg_init immediately?
1031 */
1032static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1033{
1034 unsigned long flags;
1035 int limit_reached = 0;
1036
1037 spin_lock_irqsave(&m->lock, flags);
1038
1039 if (m->pg_init_count <= m->pg_init_retries)
1040 m->pg_init_required = 1;
1041 else
1042 limit_reached = 1;
1043
1044 spin_unlock_irqrestore(&m->lock, flags);
1045
1046 return limit_reached;
1047}
1048
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001049static void pg_init_done(struct dm_path *path, int errors)
1050{
1051 struct pgpath *pgpath = path_to_pgpath(path);
1052 struct priority_group *pg = pgpath->pg;
1053 struct multipath *m = pg->m;
1054 unsigned long flags;
1055
1056 /* device or driver problems */
1057 switch (errors) {
1058 case SCSI_DH_OK:
1059 break;
1060 case SCSI_DH_NOSYS:
1061 if (!m->hw_handler_name) {
1062 errors = 0;
1063 break;
1064 }
1065 DMERR("Cannot failover device because scsi_dh_%s was not "
1066 "loaded.", m->hw_handler_name);
1067 /*
1068 * Fail path for now, so we do not ping pong
1069 */
1070 fail_path(pgpath);
1071 break;
1072 case SCSI_DH_DEV_TEMP_BUSY:
1073 /*
1074 * Probably doing something like FW upgrade on the
1075 * controller so try the other pg.
1076 */
1077 bypass_pg(m, pg, 1);
1078 break;
1079 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1080 case SCSI_DH_RETRY:
1081 case SCSI_DH_IMM_RETRY:
1082 case SCSI_DH_RES_TEMP_UNAVAIL:
1083 if (pg_init_limit_reached(m, pgpath))
1084 fail_path(pgpath);
1085 errors = 0;
1086 break;
1087 default:
1088 /*
1089 * We probably do not want to fail the path for a device
1090 * error, but this is what the old dm did. In future
1091 * patches we can do more advanced handling.
1092 */
1093 fail_path(pgpath);
1094 }
1095
1096 spin_lock_irqsave(&m->lock, flags);
1097 if (errors) {
1098 DMERR("Could not failover device. Error %d.", errors);
1099 m->current_pgpath = NULL;
1100 m->current_pg = NULL;
1101 } else if (!m->pg_init_required) {
1102 m->queue_io = 0;
1103 pg->bypassed = 0;
1104 }
1105
1106 m->pg_init_in_progress = 0;
1107 queue_work(kmultipathd, &m->process_queued_ios);
1108 spin_unlock_irqrestore(&m->lock, flags);
1109}
1110
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001111static void activate_path(struct work_struct *work)
1112{
1113 int ret;
1114 struct multipath *m =
1115 container_of(work, struct multipath, activate_path);
Chandra Seetharaman7253a332008-10-01 14:39:27 +01001116 struct dm_path *path;
1117 unsigned long flags;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001118
Chandra Seetharaman7253a332008-10-01 14:39:27 +01001119 spin_lock_irqsave(&m->lock, flags);
1120 path = &m->pgpath_to_activate->path;
1121 m->pgpath_to_activate = NULL;
1122 spin_unlock_irqrestore(&m->lock, flags);
1123 if (!path)
1124 return;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001125 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1126 pg_init_done(path, ret);
1127}
1128
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129/*
1130 * end_io handling
1131 */
1132static int do_end_io(struct multipath *m, struct bio *bio,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001133 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Stefan Bader640eb3b2005-11-21 21:32:35 -08001135 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 if (!error)
1138 return 0; /* I/O complete */
1139
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -07001140 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1141 return error;
1142
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001143 if (error == -EOPNOTSUPP)
1144 return error;
1145
Stefan Bader640eb3b2005-11-21 21:32:35 -08001146 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (!m->nr_valid_paths) {
Kiyoshi Ueda45e15722006-12-08 02:41:10 -08001148 if (__must_push_back(m)) {
1149 spin_unlock_irqrestore(&m->lock, flags);
1150 return DM_ENDIO_REQUEUE;
1151 } else if (!m->queue_if_no_path) {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001152 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 return -EIO;
1154 } else {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001155 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 goto requeue;
1157 }
1158 }
Stefan Bader640eb3b2005-11-21 21:32:35 -08001159 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001161 if (mpio->pgpath)
1162 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 requeue:
1165 dm_bio_restore(&mpio->details, bio);
1166
1167 /* queue for the daemon to resubmit or fail */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001168 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 bio_list_add(&m->queued_ios, bio);
1170 m->queue_size++;
1171 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001172 queue_work(kmultipathd, &m->process_queued_ios);
Stefan Bader640eb3b2005-11-21 21:32:35 -08001173 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001175 return DM_ENDIO_INCOMPLETE; /* io not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1179 int error, union map_info *map_context)
1180{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001181 struct multipath *m = ti->private;
1182 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct pgpath *pgpath = mpio->pgpath;
1184 struct path_selector *ps;
1185 int r;
1186
1187 r = do_end_io(m, bio, error, mpio);
1188 if (pgpath) {
1189 ps = &pgpath->pg->ps;
1190 if (ps->type->end_io)
1191 ps->type->end_io(ps, &pgpath->path);
1192 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001193 if (r != DM_ENDIO_INCOMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 mempool_free(mpio, m->mpio_pool);
1195
1196 return r;
1197}
1198
1199/*
1200 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001201 * the last path fails we must error any remaining I/O.
1202 * Note that if the freeze_bdev fails while suspending, the
1203 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 */
1205static void multipath_presuspend(struct dm_target *ti)
1206{
1207 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001209 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001212/*
1213 * Restore the queue_if_no_path setting.
1214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215static void multipath_resume(struct dm_target *ti)
1216{
1217 struct multipath *m = (struct multipath *) ti->private;
1218 unsigned long flags;
1219
1220 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001221 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 spin_unlock_irqrestore(&m->lock, flags);
1223}
1224
1225/*
1226 * Info output has the following format:
1227 * num_multipath_feature_args [multipath_feature_args]*
1228 * num_handler_status_args [handler_status_args]*
1229 * num_groups init_group_number
1230 * [A|D|E num_ps_status_args [ps_status_args]*
1231 * num_paths num_selector_args
1232 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1233 *
1234 * Table output has the following format (identical to the constructor string):
1235 * num_feature_args [features_args]*
1236 * num_handler_args hw_handler [hw_handler_args]*
1237 * num_groups init_group_number
1238 * [priority selector-name num_ps_args [ps_args]*
1239 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1240 */
1241static int multipath_status(struct dm_target *ti, status_type_t type,
1242 char *result, unsigned int maxlen)
1243{
1244 int sz = 0;
1245 unsigned long flags;
1246 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 struct priority_group *pg;
1248 struct pgpath *p;
1249 unsigned pg_num;
1250 char state;
1251
1252 spin_lock_irqsave(&m->lock, flags);
1253
1254 /* Features */
1255 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001256 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1257 else {
1258 DMEMIT("%u ", m->queue_if_no_path +
1259 (m->pg_init_retries > 0) * 2);
1260 if (m->queue_if_no_path)
1261 DMEMIT("queue_if_no_path ");
1262 if (m->pg_init_retries)
1263 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001266 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 DMEMIT("0 ");
1268 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001269 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
1271 DMEMIT("%u ", m->nr_priority_groups);
1272
1273 if (m->next_pg)
1274 pg_num = m->next_pg->pg_num;
1275 else if (m->current_pg)
1276 pg_num = m->current_pg->pg_num;
1277 else
1278 pg_num = 1;
1279
1280 DMEMIT("%u ", pg_num);
1281
1282 switch (type) {
1283 case STATUSTYPE_INFO:
1284 list_for_each_entry(pg, &m->priority_groups, list) {
1285 if (pg->bypassed)
1286 state = 'D'; /* Disabled */
1287 else if (pg == m->current_pg)
1288 state = 'A'; /* Currently Active */
1289 else
1290 state = 'E'; /* Enabled */
1291
1292 DMEMIT("%c ", state);
1293
1294 if (pg->ps.type->status)
1295 sz += pg->ps.type->status(&pg->ps, NULL, type,
1296 result + sz,
1297 maxlen - sz);
1298 else
1299 DMEMIT("0 ");
1300
1301 DMEMIT("%u %u ", pg->nr_pgpaths,
1302 pg->ps.type->info_args);
1303
1304 list_for_each_entry(p, &pg->pgpaths, list) {
1305 DMEMIT("%s %s %u ", p->path.dev->name,
1306 p->path.is_active ? "A" : "F",
1307 p->fail_count);
1308 if (pg->ps.type->status)
1309 sz += pg->ps.type->status(&pg->ps,
1310 &p->path, type, result + sz,
1311 maxlen - sz);
1312 }
1313 }
1314 break;
1315
1316 case STATUSTYPE_TABLE:
1317 list_for_each_entry(pg, &m->priority_groups, list) {
1318 DMEMIT("%s ", pg->ps.type->name);
1319
1320 if (pg->ps.type->status)
1321 sz += pg->ps.type->status(&pg->ps, NULL, type,
1322 result + sz,
1323 maxlen - sz);
1324 else
1325 DMEMIT("0 ");
1326
1327 DMEMIT("%u %u ", pg->nr_pgpaths,
1328 pg->ps.type->table_args);
1329
1330 list_for_each_entry(p, &pg->pgpaths, list) {
1331 DMEMIT("%s ", p->path.dev->name);
1332 if (pg->ps.type->status)
1333 sz += pg->ps.type->status(&pg->ps,
1334 &p->path, type, result + sz,
1335 maxlen - sz);
1336 }
1337 }
1338 break;
1339 }
1340
1341 spin_unlock_irqrestore(&m->lock, flags);
1342
1343 return 0;
1344}
1345
1346static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1347{
1348 int r;
1349 struct dm_dev *dev;
1350 struct multipath *m = (struct multipath *) ti->private;
1351 action_fn action;
1352
1353 if (argc == 1) {
1354 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001355 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001357 return queue_if_no_path(m, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359
1360 if (argc != 2)
1361 goto error;
1362
1363 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1364 return bypass_pg_num(m, argv[1], 1);
1365 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1366 return bypass_pg_num(m, argv[1], 0);
1367 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1368 return switch_pg_num(m, argv[1]);
1369 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1370 action = reinstate_path;
1371 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1372 action = fail_path;
1373 else
1374 goto error;
1375
1376 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1377 dm_table_get_mode(ti->table), &dev);
1378 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001379 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 argv[1]);
1381 return -EINVAL;
1382 }
1383
1384 r = action_dev(m, dev, action);
1385
1386 dm_put_device(ti, dev);
1387
1388 return r;
1389
1390error:
1391 DMWARN("Unrecognised multipath message received.");
1392 return -EINVAL;
1393}
1394
Milan Broz9af4aa32006-10-03 01:15:20 -07001395static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1396 struct file *filp, unsigned int cmd,
1397 unsigned long arg)
1398{
1399 struct multipath *m = (struct multipath *) ti->private;
1400 struct block_device *bdev = NULL;
1401 unsigned long flags;
Milan Broze90dae12006-10-03 01:15:22 -07001402 struct file fake_file = {};
1403 struct dentry fake_dentry = {};
Milan Broz9af4aa32006-10-03 01:15:20 -07001404 int r = 0;
1405
Josef Sipekc649bb92006-12-08 02:37:19 -08001406 fake_file.f_path.dentry = &fake_dentry;
Milan Broze90dae12006-10-03 01:15:22 -07001407
Milan Broz9af4aa32006-10-03 01:15:20 -07001408 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;
Milan Broze90dae12006-10-03 01:15:22 -07001415 fake_dentry.d_inode = bdev->bd_inode;
1416 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1417 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001418
1419 if (m->queue_io)
1420 r = -EAGAIN;
1421 else if (!bdev)
1422 r = -EIO;
1423
1424 spin_unlock_irqrestore(&m->lock, flags);
1425
Milan Broze90dae12006-10-03 01:15:22 -07001426 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1427 bdev->bd_disk, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001428}
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430/*-----------------------------------------------------------------
1431 * Module setup
1432 *---------------------------------------------------------------*/
1433static struct target_type multipath_target = {
1434 .name = "multipath",
Milan Broz9af4aa32006-10-03 01:15:20 -07001435 .version = {1, 0, 5},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 .module = THIS_MODULE,
1437 .ctr = multipath_ctr,
1438 .dtr = multipath_dtr,
1439 .map = multipath_map,
1440 .end_io = multipath_end_io,
1441 .presuspend = multipath_presuspend,
1442 .resume = multipath_resume,
1443 .status = multipath_status,
1444 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001445 .ioctl = multipath_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446};
1447
1448static int __init dm_multipath_init(void)
1449{
1450 int r;
1451
1452 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001453 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 if (!_mpio_cache)
1455 return -ENOMEM;
1456
1457 r = dm_register_target(&multipath_target);
1458 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001459 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 kmem_cache_destroy(_mpio_cache);
1461 return -EINVAL;
1462 }
1463
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001464 kmultipathd = create_workqueue("kmpathd");
1465 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001466 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001467 dm_unregister_target(&multipath_target);
1468 kmem_cache_destroy(_mpio_cache);
1469 return -ENOMEM;
1470 }
1471
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001472 /*
1473 * A separate workqueue is used to handle the device handlers
1474 * to avoid overloading existing workqueue. Overloading the
1475 * old workqueue would also create a bottleneck in the
1476 * path of the storage hardware device activation.
1477 */
1478 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1479 if (!kmpath_handlerd) {
1480 DMERR("failed to create workqueue kmpath_handlerd");
1481 destroy_workqueue(kmultipathd);
1482 dm_unregister_target(&multipath_target);
1483 kmem_cache_destroy(_mpio_cache);
1484 return -ENOMEM;
1485 }
1486
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001487 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 multipath_target.version[0], multipath_target.version[1],
1489 multipath_target.version[2]);
1490
1491 return r;
1492}
1493
1494static void __exit dm_multipath_exit(void)
1495{
1496 int r;
1497
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001498 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001499 destroy_workqueue(kmultipathd);
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 r = dm_unregister_target(&multipath_target);
1502 if (r < 0)
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001503 DMERR("target unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 kmem_cache_destroy(_mpio_cache);
1505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507module_init(dm_multipath_init);
1508module_exit(dm_multipath_exit);
1509
1510MODULE_DESCRIPTION(DM_NAME " multipath target");
1511MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1512MODULE_LICENSE("GPL");