blob: c2fcf28b4c7022b2ec66e0c9d661e8a87b5279c8 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036};
37
38#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
39
40/*
41 * Paths are grouped into Priority Groups and numbered from 1 upwards.
42 * Each has a path selector which controls which path gets used.
43 */
44struct priority_group {
45 struct list_head list;
46
47 struct multipath *m; /* Owning multipath instance */
48 struct path_selector ps;
49
50 unsigned pg_num; /* Reference number */
51 unsigned bypassed; /* Temporarily bypass this PG? */
52
53 unsigned nr_pgpaths; /* Number of paths in PG */
54 struct list_head pgpaths;
55};
56
57/* Multipath context */
58struct multipath {
59 struct list_head list;
60 struct dm_target *ti;
61
62 spinlock_t lock;
63
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070064 const char *hw_handler_name;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -070065 struct work_struct activate_path;
Chandra Seetharaman7253a332008-10-01 14:39:27 +010066 struct pgpath *pgpath_to_activate;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 unsigned nr_priority_groups;
68 struct list_head priority_groups;
69 unsigned pg_init_required; /* pg_init needs calling? */
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -070070 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 unsigned nr_valid_paths; /* Total number of usable paths */
73 struct pgpath *current_pgpath;
74 struct priority_group *current_pg;
75 struct priority_group *next_pg; /* Switch to this PG if set */
76 unsigned repeat_count; /* I/Os left before calling PS again */
77
78 unsigned queue_io; /* Must we queue all I/O? */
79 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
Alasdair G Kergon436d4102005-07-12 15:53:03 -070080 unsigned saved_queue_if_no_path;/* Saved state during suspension */
Dave Wysochanskic9e45582007-10-19 22:47:53 +010081 unsigned pg_init_retries; /* Number of times to retry pg_init */
82 unsigned pg_init_count; /* Number of times pg_init called */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 struct work_struct process_queued_ios;
85 struct bio_list queued_ios;
86 unsigned queue_size;
87
88 struct work_struct trigger_event;
89
90 /*
Alasdair G Kergon028867a2007-07-12 17:26:32 +010091 * We must use a mempool of dm_mpath_io structs so that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * can resubmit bios on error.
93 */
94 mempool_t *mpio_pool;
95};
96
97/*
98 * Context information attached to each bio we process.
99 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100100struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 struct pgpath *pgpath;
102 struct dm_bio_details details;
103};
104
105typedef int (*action_fn) (struct pgpath *pgpath);
106
107#define MIN_IOS 256 /* Mempool size */
108
Christoph Lametere18b8902006-12-06 20:33:20 -0800109static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700111static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000112static void process_queued_ios(struct work_struct *work);
113static void trigger_event(struct work_struct *work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700114static void activate_path(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116
117/*-----------------------------------------------
118 * Allocation routines
119 *-----------------------------------------------*/
120
121static struct pgpath *alloc_pgpath(void)
122{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700123 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700125 if (pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 pgpath->path.is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 return pgpath;
129}
130
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100131static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
133 kfree(pgpath);
134}
135
136static struct priority_group *alloc_priority_group(void)
137{
138 struct priority_group *pg;
139
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700140 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700142 if (pg)
143 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 return pg;
146}
147
148static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
149{
Chandra Seetharaman7253a332008-10-01 14:39:27 +0100150 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct pgpath *pgpath, *tmp;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700152 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
154 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
155 list_del(&pgpath->list);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700156 if (m->hw_handler_name)
157 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 dm_put_device(ti, pgpath->path.dev);
Chandra Seetharaman7253a332008-10-01 14:39:27 +0100159 spin_lock_irqsave(&m->lock, flags);
160 if (m->pgpath_to_activate == pgpath)
161 m->pgpath_to_activate = NULL;
162 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 free_pgpath(pgpath);
164 }
165}
166
167static void free_priority_group(struct priority_group *pg,
168 struct dm_target *ti)
169{
170 struct path_selector *ps = &pg->ps;
171
172 if (ps->type) {
173 ps->type->destroy(ps);
174 dm_put_path_selector(ps->type);
175 }
176
177 free_pgpaths(&pg->pgpaths, ti);
178 kfree(pg);
179}
180
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700181static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 struct multipath *m;
184
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700185 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 INIT_LIST_HEAD(&m->priority_groups);
188 spin_lock_init(&m->lock);
189 m->queue_io = 1;
David Howellsc4028952006-11-22 14:57:56 +0000190 INIT_WORK(&m->process_queued_ios, process_queued_ios);
191 INIT_WORK(&m->trigger_event, trigger_event);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700192 INIT_WORK(&m->activate_path, activate_path);
Matthew Dobson93d23412006-03-26 01:37:50 -0800193 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (!m->mpio_pool) {
195 kfree(m);
196 return NULL;
197 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700198 m->ti = ti;
199 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
202 return m;
203}
204
205static void free_multipath(struct multipath *m)
206{
207 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
210 list_del(&pg->list);
211 free_priority_group(pg, m->ti);
212 }
213
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700214 kfree(m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 mempool_destroy(m->mpio_pool);
216 kfree(m);
217}
218
219
220/*-----------------------------------------------
221 * Path selection
222 *-----------------------------------------------*/
223
224static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
225{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 m->current_pg = pgpath->pg;
227
228 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700229 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 m->pg_init_required = 1;
231 m->queue_io = 1;
232 } else {
233 m->pg_init_required = 0;
234 m->queue_io = 0;
235 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100236
237 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
241{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800242 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
245 if (!path)
246 return -ENXIO;
247
248 m->current_pgpath = path_to_pgpath(path);
249
250 if (m->current_pg != pg)
251 __switch_pg(m, m->current_pgpath);
252
253 return 0;
254}
255
256static void __choose_pgpath(struct multipath *m)
257{
258 struct priority_group *pg;
259 unsigned bypassed = 1;
260
261 if (!m->nr_valid_paths)
262 goto failed;
263
264 /* Were we instructed to switch PG? */
265 if (m->next_pg) {
266 pg = m->next_pg;
267 m->next_pg = NULL;
268 if (!__choose_path_in_pg(m, pg))
269 return;
270 }
271
272 /* Don't change PG until it has no remaining paths */
273 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
274 return;
275
276 /*
277 * Loop through priority groups until we find a valid path.
278 * First time we skip PGs marked 'bypassed'.
279 * Second time we only try the ones we skipped.
280 */
281 do {
282 list_for_each_entry(pg, &m->priority_groups, list) {
283 if (pg->bypassed == bypassed)
284 continue;
285 if (!__choose_path_in_pg(m, pg))
286 return;
287 }
288 } while (bypassed--);
289
290failed:
291 m->current_pgpath = NULL;
292 m->current_pg = NULL;
293}
294
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800295/*
296 * Check whether bios must be queued in the device-mapper core rather
297 * than here in the target.
298 *
299 * m->lock must be held on entry.
300 *
301 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
302 * same value then we are not between multipath_presuspend()
303 * and multipath_resume() calls and we have no need to check
304 * for the DMF_NOFLUSH_SUSPENDING flag.
305 */
306static int __must_push_back(struct multipath *m)
307{
308 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
309 dm_noflush_suspending(m->ti));
310}
311
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100312static int map_io(struct multipath *m, struct bio *bio,
313 struct dm_mpath_io *mpio, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800315 int r = DM_MAPIO_REMAPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 unsigned long flags;
317 struct pgpath *pgpath;
318
319 spin_lock_irqsave(&m->lock, flags);
320
321 /* Do we need to select a new pgpath? */
322 if (!m->current_pgpath ||
323 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
324 __choose_pgpath(m);
325
326 pgpath = m->current_pgpath;
327
328 if (was_queued)
329 m->queue_size--;
330
331 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700332 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 /* Queue for the daemon to resubmit */
334 bio_list_add(&m->queued_ios, bio);
335 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700336 if ((m->pg_init_required && !m->pg_init_in_progress) ||
337 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700338 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800340 r = DM_MAPIO_SUBMITTED;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800341 } else if (pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 bio->bi_bdev = pgpath->path.dev->bdev;
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800343 else if (__must_push_back(m))
344 r = DM_MAPIO_REQUEUE;
345 else
346 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 mpio->pgpath = pgpath;
349
350 spin_unlock_irqrestore(&m->lock, flags);
351
352 return r;
353}
354
355/*
356 * If we run out of usable paths, should we queue I/O or error it?
357 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700358static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
359 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 unsigned long flags;
362
363 spin_lock_irqsave(&m->lock, flags);
364
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700365 if (save_old_value)
366 m->saved_queue_if_no_path = m->queue_if_no_path;
367 else
368 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700370 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700371 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 spin_unlock_irqrestore(&m->lock, flags);
374
375 return 0;
376}
377
378/*-----------------------------------------------------------------
379 * The multipath daemon is responsible for resubmitting queued ios.
380 *---------------------------------------------------------------*/
381
382static void dispatch_queued_ios(struct multipath *m)
383{
384 int r;
385 unsigned long flags;
386 struct bio *bio = NULL, *next;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100387 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 union map_info *info;
389
390 spin_lock_irqsave(&m->lock, flags);
391 bio = bio_list_get(&m->queued_ios);
392 spin_unlock_irqrestore(&m->lock, flags);
393
394 while (bio) {
395 next = bio->bi_next;
396 bio->bi_next = NULL;
397
398 info = dm_get_mapinfo(bio);
399 mpio = info->ptr;
400
401 r = map_io(m, bio, mpio, 1);
402 if (r < 0)
NeilBrown6712ecf2007-09-27 12:47:43 +0200403 bio_endio(bio, r);
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800404 else if (r == DM_MAPIO_REMAPPED)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 generic_make_request(bio);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800406 else if (r == DM_MAPIO_REQUEUE)
NeilBrown6712ecf2007-09-27 12:47:43 +0200407 bio_endio(bio, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 bio = next;
410 }
411}
412
David Howellsc4028952006-11-22 14:57:56 +0000413static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
David Howellsc4028952006-11-22 14:57:56 +0000415 struct multipath *m =
416 container_of(work, struct multipath, process_queued_ios);
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700417 struct pgpath *pgpath = NULL;
418 unsigned init_required = 0, must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 unsigned long flags;
420
421 spin_lock_irqsave(&m->lock, flags);
422
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700423 if (!m->queue_size)
424 goto out;
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!m->current_pgpath)
427 __choose_pgpath(m);
428
429 pgpath = m->current_pgpath;
Chandra Seetharaman7253a332008-10-01 14:39:27 +0100430 m->pgpath_to_activate = m->current_pgpath;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700432 if ((pgpath && !m->queue_io) ||
433 (!pgpath && !m->queue_if_no_path))
434 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700436 if (m->pg_init_required && !m->pg_init_in_progress) {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100437 m->pg_init_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 m->pg_init_required = 0;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700439 m->pg_init_in_progress = 1;
440 init_required = 1;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700443out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 spin_unlock_irqrestore(&m->lock, flags);
445
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700446 if (init_required)
447 queue_work(kmpath_handlerd, &m->activate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 if (!must_queue)
450 dispatch_queued_ios(m);
451}
452
453/*
454 * An event is triggered whenever a path is taken out of use.
455 * Includes path failure and PG bypass.
456 */
David Howellsc4028952006-11-22 14:57:56 +0000457static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
David Howellsc4028952006-11-22 14:57:56 +0000459 struct multipath *m =
460 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 dm_table_event(m->ti->table);
463}
464
465/*-----------------------------------------------------------------
466 * Constructor/argument parsing:
467 * <#multipath feature args> [<arg>]*
468 * <#hw_handler args> [hw_handler [<arg>]*]
469 * <#priority groups>
470 * <initial priority group>
471 * [<selector> <#selector args> [<arg>]*
472 * <#paths> <#per-path selector args>
473 * [<path> [<arg>]* ]+ ]+
474 *---------------------------------------------------------------*/
475struct param {
476 unsigned min;
477 unsigned max;
478 char *error;
479};
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481static int read_param(struct param *param, char *str, unsigned *v, char **error)
482{
483 if (!str ||
484 (sscanf(str, "%u", v) != 1) ||
485 (*v < param->min) ||
486 (*v > param->max)) {
487 *error = param->error;
488 return -EINVAL;
489 }
490
491 return 0;
492}
493
494struct arg_set {
495 unsigned argc;
496 char **argv;
497};
498
499static char *shift(struct arg_set *as)
500{
501 char *r;
502
503 if (as->argc) {
504 as->argc--;
505 r = *as->argv;
506 as->argv++;
507 return r;
508 }
509
510 return NULL;
511}
512
513static void consume(struct arg_set *as, unsigned n)
514{
515 BUG_ON (as->argc < n);
516 as->argc -= n;
517 as->argv += n;
518}
519
520static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
521 struct dm_target *ti)
522{
523 int r;
524 struct path_selector_type *pst;
525 unsigned ps_argc;
526
527 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700528 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 };
530
531 pst = dm_get_path_selector(shift(as));
532 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700533 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 return -EINVAL;
535 }
536
537 r = read_param(_params, shift(as), &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100538 if (r) {
539 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 r = pst->create(&pg->ps, ps_argc, as->argv);
544 if (r) {
545 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700546 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return r;
548 }
549
550 pg->ps.type = pst;
551 consume(as, ps_argc);
552
553 return 0;
554}
555
556static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
557 struct dm_target *ti)
558{
559 int r;
560 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700561 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 /* we need at least a path arg */
564 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700565 ti->error = "no device given";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return NULL;
567 }
568
569 p = alloc_pgpath();
570 if (!p)
571 return NULL;
572
573 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
574 dm_table_get_mode(ti->table), &p->path.dev);
575 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700576 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 goto bad;
578 }
579
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700580 if (m->hw_handler_name) {
581 r = scsi_dh_attach(bdev_get_queue(p->path.dev->bdev),
582 m->hw_handler_name);
583 if (r < 0) {
584 dm_put_device(ti, p->path.dev);
585 goto bad;
586 }
587 }
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
590 if (r) {
591 dm_put_device(ti, p->path.dev);
592 goto bad;
593 }
594
595 return p;
596
597 bad:
598 free_pgpath(p);
599 return NULL;
600}
601
602static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700603 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
605 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700606 {1, 1024, "invalid number of paths"},
607 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 };
609
610 int r;
611 unsigned i, nr_selector_args, nr_params;
612 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700613 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 if (as->argc < 2) {
616 as->argc = 0;
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700617 ti->error = "not enough priority group aruments";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return NULL;
619 }
620
621 pg = alloc_priority_group();
622 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700623 ti->error = "couldn't allocate priority group";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return NULL;
625 }
626 pg->m = m;
627
628 r = parse_path_selector(as, pg, ti);
629 if (r)
630 goto bad;
631
632 /*
633 * read the paths
634 */
635 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
636 if (r)
637 goto bad;
638
639 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
640 if (r)
641 goto bad;
642
643 nr_params = 1 + nr_selector_args;
644 for (i = 0; i < pg->nr_pgpaths; i++) {
645 struct pgpath *pgpath;
646 struct arg_set path_args;
647
Mikulas Patocka148acff2008-07-21 12:00:30 +0100648 if (as->argc < nr_params) {
649 ti->error = "not enough path parameters";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 path_args.argc = nr_params;
654 path_args.argv = as->argv;
655
656 pgpath = parse_path(&path_args, &pg->ps, ti);
657 if (!pgpath)
658 goto bad;
659
660 pgpath->pg = pg;
661 list_add_tail(&pgpath->list, &pg->pgpaths);
662 consume(as, nr_params);
663 }
664
665 return pg;
666
667 bad:
668 free_priority_group(pg, ti);
669 return NULL;
670}
671
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700672static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 unsigned hw_argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700675 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
677 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700678 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 };
680
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700681 if (read_param(_params, shift(as), &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -EINVAL;
683
684 if (!hw_argc)
685 return 0;
686
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700687 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
688 request_module("scsi_dh_%s", m->hw_handler_name);
689 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700690 ti->error = "unknown hardware handler type";
Chandra Seetharamanfe9233f2008-05-23 18:16:40 -0700691 kfree(m->hw_handler_name);
692 m->hw_handler_name = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return -EINVAL;
694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 consume(as, hw_argc - 1);
696
697 return 0;
698}
699
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700700static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
702 int r;
703 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700704 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100705 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100708 {0, 3, "invalid number of feature args"},
709 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 };
711
712 r = read_param(_params, shift(as), &argc, &ti->error);
713 if (r)
714 return -EINVAL;
715
716 if (!argc)
717 return 0;
718
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100719 do {
720 param_name = shift(as);
721 argc--;
722
723 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
724 r = queue_if_no_path(m, 1, 0);
725 continue;
726 }
727
728 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
729 (argc >= 1)) {
730 r = read_param(_params + 1, shift(as),
731 &m->pg_init_retries, &ti->error);
732 argc--;
733 continue;
734 }
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100737 r = -EINVAL;
738 } while (argc && !r);
739
740 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743static int multipath_ctr(struct dm_target *ti, unsigned int argc,
744 char **argv)
745{
746 /* target parameters */
747 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700748 {1, 1024, "invalid number of priority groups"},
749 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 };
751
752 int r;
753 struct multipath *m;
754 struct arg_set as;
755 unsigned pg_count = 0;
756 unsigned next_pg_num;
757
758 as.argc = argc;
759 as.argv = argv;
760
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700761 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700763 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 return -EINVAL;
765 }
766
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700767 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (r)
769 goto bad;
770
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700771 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (r)
773 goto bad;
774
775 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
776 if (r)
777 goto bad;
778
779 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
780 if (r)
781 goto bad;
782
783 /* parse the priority groups */
784 while (as.argc) {
785 struct priority_group *pg;
786
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700787 pg = parse_priority_group(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (!pg) {
789 r = -EINVAL;
790 goto bad;
791 }
792
793 m->nr_valid_paths += pg->nr_pgpaths;
794 list_add_tail(&pg->list, &m->priority_groups);
795 pg_count++;
796 pg->pg_num = pg_count;
797 if (!--next_pg_num)
798 m->next_pg = pg;
799 }
800
801 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700802 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 r = -EINVAL;
804 goto bad;
805 }
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return 0;
808
809 bad:
810 free_multipath(m);
811 return r;
812}
813
814static void multipath_dtr(struct dm_target *ti)
815{
816 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700817
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700818 flush_workqueue(kmpath_handlerd);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700819 flush_workqueue(kmultipathd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 free_multipath(m);
821}
822
823/*
824 * Map bios, recording original fields for later in case we have to resubmit
825 */
826static int multipath_map(struct dm_target *ti, struct bio *bio,
827 union map_info *map_context)
828{
829 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100830 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 struct multipath *m = (struct multipath *) ti->private;
832
833 mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
834 dm_bio_record(&mpio->details, bio);
835
836 map_context->ptr = mpio;
837 bio->bi_rw |= (1 << BIO_RW_FAILFAST);
838 r = map_io(m, bio, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800839 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 mempool_free(mpio, m->mpio_pool);
841
842 return r;
843}
844
845/*
846 * Take a path out of use.
847 */
848static int fail_path(struct pgpath *pgpath)
849{
850 unsigned long flags;
851 struct multipath *m = pgpath->pg->m;
852
853 spin_lock_irqsave(&m->lock, flags);
854
855 if (!pgpath->path.is_active)
856 goto out;
857
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700858 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
861 pgpath->path.is_active = 0;
862 pgpath->fail_count++;
863
864 m->nr_valid_paths--;
865
866 if (pgpath == m->current_pgpath)
867 m->current_pgpath = NULL;
868
Mike Andersonb15546f2007-10-19 22:48:02 +0100869 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
870 pgpath->path.dev->name, m->nr_valid_paths);
871
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700872 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874out:
875 spin_unlock_irqrestore(&m->lock, flags);
876
877 return 0;
878}
879
880/*
881 * Reinstate a previously-failed path
882 */
883static int reinstate_path(struct pgpath *pgpath)
884{
885 int r = 0;
886 unsigned long flags;
887 struct multipath *m = pgpath->pg->m;
888
889 spin_lock_irqsave(&m->lock, flags);
890
891 if (pgpath->path.is_active)
892 goto out;
893
Alasdair G Kergondef052d2008-07-21 12:00:31 +0100894 if (!pgpath->pg->ps.type->reinstate_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 DMWARN("Reinstate path not supported by path selector %s",
896 pgpath->pg->ps.type->name);
897 r = -EINVAL;
898 goto out;
899 }
900
901 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
902 if (r)
903 goto out;
904
905 pgpath->path.is_active = 1;
906
907 m->current_pgpath = NULL;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700908 if (!m->nr_valid_paths++ && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700909 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Mike Andersonb15546f2007-10-19 22:48:02 +0100911 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
912 pgpath->path.dev->name, m->nr_valid_paths);
913
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700914 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916out:
917 spin_unlock_irqrestore(&m->lock, flags);
918
919 return r;
920}
921
922/*
923 * Fail or reinstate all paths that match the provided struct dm_dev.
924 */
925static int action_dev(struct multipath *m, struct dm_dev *dev,
926 action_fn action)
927{
928 int r = 0;
929 struct pgpath *pgpath;
930 struct priority_group *pg;
931
932 list_for_each_entry(pg, &m->priority_groups, list) {
933 list_for_each_entry(pgpath, &pg->pgpaths, list) {
934 if (pgpath->path.dev == dev)
935 r = action(pgpath);
936 }
937 }
938
939 return r;
940}
941
942/*
943 * Temporarily try to avoid having to use the specified PG
944 */
945static void bypass_pg(struct multipath *m, struct priority_group *pg,
946 int bypassed)
947{
948 unsigned long flags;
949
950 spin_lock_irqsave(&m->lock, flags);
951
952 pg->bypassed = bypassed;
953 m->current_pgpath = NULL;
954 m->current_pg = NULL;
955
956 spin_unlock_irqrestore(&m->lock, flags);
957
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700958 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959}
960
961/*
962 * Switch to using the specified PG from the next I/O that gets mapped
963 */
964static int switch_pg_num(struct multipath *m, const char *pgstr)
965{
966 struct priority_group *pg;
967 unsigned pgnum;
968 unsigned long flags;
969
970 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
971 (pgnum > m->nr_priority_groups)) {
972 DMWARN("invalid PG number supplied to switch_pg_num");
973 return -EINVAL;
974 }
975
976 spin_lock_irqsave(&m->lock, flags);
977 list_for_each_entry(pg, &m->priority_groups, list) {
978 pg->bypassed = 0;
979 if (--pgnum)
980 continue;
981
982 m->current_pgpath = NULL;
983 m->current_pg = NULL;
984 m->next_pg = pg;
985 }
986 spin_unlock_irqrestore(&m->lock, flags);
987
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700988 queue_work(kmultipathd, &m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 return 0;
990}
991
992/*
993 * Set/clear bypassed status of a PG.
994 * PGs are numbered upwards from 1 in the order they were declared.
995 */
996static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
997{
998 struct priority_group *pg;
999 unsigned pgnum;
1000
1001 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1002 (pgnum > m->nr_priority_groups)) {
1003 DMWARN("invalid PG number supplied to bypass_pg");
1004 return -EINVAL;
1005 }
1006
1007 list_for_each_entry(pg, &m->priority_groups, list) {
1008 if (!--pgnum)
1009 break;
1010 }
1011
1012 bypass_pg(m, pg, bypassed);
1013 return 0;
1014}
1015
1016/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001017 * Should we retry pg_init immediately?
1018 */
1019static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1020{
1021 unsigned long flags;
1022 int limit_reached = 0;
1023
1024 spin_lock_irqsave(&m->lock, flags);
1025
1026 if (m->pg_init_count <= m->pg_init_retries)
1027 m->pg_init_required = 1;
1028 else
1029 limit_reached = 1;
1030
1031 spin_unlock_irqrestore(&m->lock, flags);
1032
1033 return limit_reached;
1034}
1035
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001036static void pg_init_done(struct dm_path *path, int errors)
1037{
1038 struct pgpath *pgpath = path_to_pgpath(path);
1039 struct priority_group *pg = pgpath->pg;
1040 struct multipath *m = pg->m;
1041 unsigned long flags;
1042
1043 /* device or driver problems */
1044 switch (errors) {
1045 case SCSI_DH_OK:
1046 break;
1047 case SCSI_DH_NOSYS:
1048 if (!m->hw_handler_name) {
1049 errors = 0;
1050 break;
1051 }
1052 DMERR("Cannot failover device because scsi_dh_%s was not "
1053 "loaded.", m->hw_handler_name);
1054 /*
1055 * Fail path for now, so we do not ping pong
1056 */
1057 fail_path(pgpath);
1058 break;
1059 case SCSI_DH_DEV_TEMP_BUSY:
1060 /*
1061 * Probably doing something like FW upgrade on the
1062 * controller so try the other pg.
1063 */
1064 bypass_pg(m, pg, 1);
1065 break;
1066 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1067 case SCSI_DH_RETRY:
1068 case SCSI_DH_IMM_RETRY:
1069 case SCSI_DH_RES_TEMP_UNAVAIL:
1070 if (pg_init_limit_reached(m, pgpath))
1071 fail_path(pgpath);
1072 errors = 0;
1073 break;
1074 default:
1075 /*
1076 * We probably do not want to fail the path for a device
1077 * error, but this is what the old dm did. In future
1078 * patches we can do more advanced handling.
1079 */
1080 fail_path(pgpath);
1081 }
1082
1083 spin_lock_irqsave(&m->lock, flags);
1084 if (errors) {
1085 DMERR("Could not failover device. Error %d.", errors);
1086 m->current_pgpath = NULL;
1087 m->current_pg = NULL;
1088 } else if (!m->pg_init_required) {
1089 m->queue_io = 0;
1090 pg->bypassed = 0;
1091 }
1092
1093 m->pg_init_in_progress = 0;
1094 queue_work(kmultipathd, &m->process_queued_ios);
1095 spin_unlock_irqrestore(&m->lock, flags);
1096}
1097
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001098static void activate_path(struct work_struct *work)
1099{
1100 int ret;
1101 struct multipath *m =
1102 container_of(work, struct multipath, activate_path);
Chandra Seetharaman7253a332008-10-01 14:39:27 +01001103 struct dm_path *path;
1104 unsigned long flags;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001105
Chandra Seetharaman7253a332008-10-01 14:39:27 +01001106 spin_lock_irqsave(&m->lock, flags);
1107 path = &m->pgpath_to_activate->path;
1108 m->pgpath_to_activate = NULL;
1109 spin_unlock_irqrestore(&m->lock, flags);
1110 if (!path)
1111 return;
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001112 ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
1113 pg_init_done(path, ret);
1114}
1115
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116/*
1117 * end_io handling
1118 */
1119static int do_end_io(struct multipath *m, struct bio *bio,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001120 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
Stefan Bader640eb3b2005-11-21 21:32:35 -08001122 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
1124 if (!error)
1125 return 0; /* I/O complete */
1126
Lars Marowsky-Bree4f588022005-06-08 15:50:31 -07001127 if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
1128 return error;
1129
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001130 if (error == -EOPNOTSUPP)
1131 return error;
1132
Stefan Bader640eb3b2005-11-21 21:32:35 -08001133 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (!m->nr_valid_paths) {
Kiyoshi Ueda45e15722006-12-08 02:41:10 -08001135 if (__must_push_back(m)) {
1136 spin_unlock_irqrestore(&m->lock, flags);
1137 return DM_ENDIO_REQUEUE;
1138 } else if (!m->queue_if_no_path) {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001139 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 return -EIO;
1141 } else {
Stefan Bader640eb3b2005-11-21 21:32:35 -08001142 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 goto requeue;
1144 }
1145 }
Stefan Bader640eb3b2005-11-21 21:32:35 -08001146 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001148 if (mpio->pgpath)
1149 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 requeue:
1152 dm_bio_restore(&mpio->details, bio);
1153
1154 /* queue for the daemon to resubmit or fail */
Stefan Bader640eb3b2005-11-21 21:32:35 -08001155 spin_lock_irqsave(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 bio_list_add(&m->queued_ios, bio);
1157 m->queue_size++;
1158 if (!m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001159 queue_work(kmultipathd, &m->process_queued_ios);
Stefan Bader640eb3b2005-11-21 21:32:35 -08001160 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001162 return DM_ENDIO_INCOMPLETE; /* io not complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163}
1164
1165static int multipath_end_io(struct dm_target *ti, struct bio *bio,
1166 int error, union map_info *map_context)
1167{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001168 struct multipath *m = ti->private;
1169 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 struct pgpath *pgpath = mpio->pgpath;
1171 struct path_selector *ps;
1172 int r;
1173
1174 r = do_end_io(m, bio, error, mpio);
1175 if (pgpath) {
1176 ps = &pgpath->pg->ps;
1177 if (ps->type->end_io)
1178 ps->type->end_io(ps, &pgpath->path);
1179 }
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -08001180 if (r != DM_ENDIO_INCOMPLETE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 mempool_free(mpio, m->mpio_pool);
1182
1183 return r;
1184}
1185
1186/*
1187 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001188 * the last path fails we must error any remaining I/O.
1189 * Note that if the freeze_bdev fails while suspending, the
1190 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 */
1192static void multipath_presuspend(struct dm_target *ti)
1193{
1194 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001196 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001199/*
1200 * Restore the queue_if_no_path setting.
1201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202static void multipath_resume(struct dm_target *ti)
1203{
1204 struct multipath *m = (struct multipath *) ti->private;
1205 unsigned long flags;
1206
1207 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001208 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 spin_unlock_irqrestore(&m->lock, flags);
1210}
1211
1212/*
1213 * Info output has the following format:
1214 * num_multipath_feature_args [multipath_feature_args]*
1215 * num_handler_status_args [handler_status_args]*
1216 * num_groups init_group_number
1217 * [A|D|E num_ps_status_args [ps_status_args]*
1218 * num_paths num_selector_args
1219 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1220 *
1221 * Table output has the following format (identical to the constructor string):
1222 * num_feature_args [features_args]*
1223 * num_handler_args hw_handler [hw_handler_args]*
1224 * num_groups init_group_number
1225 * [priority selector-name num_ps_args [ps_args]*
1226 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1227 */
1228static int multipath_status(struct dm_target *ti, status_type_t type,
1229 char *result, unsigned int maxlen)
1230{
1231 int sz = 0;
1232 unsigned long flags;
1233 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 struct priority_group *pg;
1235 struct pgpath *p;
1236 unsigned pg_num;
1237 char state;
1238
1239 spin_lock_irqsave(&m->lock, flags);
1240
1241 /* Features */
1242 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001243 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1244 else {
1245 DMEMIT("%u ", m->queue_if_no_path +
1246 (m->pg_init_retries > 0) * 2);
1247 if (m->queue_if_no_path)
1248 DMEMIT("queue_if_no_path ");
1249 if (m->pg_init_retries)
1250 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001253 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 DMEMIT("0 ");
1255 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001256 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 DMEMIT("%u ", m->nr_priority_groups);
1259
1260 if (m->next_pg)
1261 pg_num = m->next_pg->pg_num;
1262 else if (m->current_pg)
1263 pg_num = m->current_pg->pg_num;
1264 else
1265 pg_num = 1;
1266
1267 DMEMIT("%u ", pg_num);
1268
1269 switch (type) {
1270 case STATUSTYPE_INFO:
1271 list_for_each_entry(pg, &m->priority_groups, list) {
1272 if (pg->bypassed)
1273 state = 'D'; /* Disabled */
1274 else if (pg == m->current_pg)
1275 state = 'A'; /* Currently Active */
1276 else
1277 state = 'E'; /* Enabled */
1278
1279 DMEMIT("%c ", state);
1280
1281 if (pg->ps.type->status)
1282 sz += pg->ps.type->status(&pg->ps, NULL, type,
1283 result + sz,
1284 maxlen - sz);
1285 else
1286 DMEMIT("0 ");
1287
1288 DMEMIT("%u %u ", pg->nr_pgpaths,
1289 pg->ps.type->info_args);
1290
1291 list_for_each_entry(p, &pg->pgpaths, list) {
1292 DMEMIT("%s %s %u ", p->path.dev->name,
1293 p->path.is_active ? "A" : "F",
1294 p->fail_count);
1295 if (pg->ps.type->status)
1296 sz += pg->ps.type->status(&pg->ps,
1297 &p->path, type, result + sz,
1298 maxlen - sz);
1299 }
1300 }
1301 break;
1302
1303 case STATUSTYPE_TABLE:
1304 list_for_each_entry(pg, &m->priority_groups, list) {
1305 DMEMIT("%s ", pg->ps.type->name);
1306
1307 if (pg->ps.type->status)
1308 sz += pg->ps.type->status(&pg->ps, NULL, type,
1309 result + sz,
1310 maxlen - sz);
1311 else
1312 DMEMIT("0 ");
1313
1314 DMEMIT("%u %u ", pg->nr_pgpaths,
1315 pg->ps.type->table_args);
1316
1317 list_for_each_entry(p, &pg->pgpaths, list) {
1318 DMEMIT("%s ", p->path.dev->name);
1319 if (pg->ps.type->status)
1320 sz += pg->ps.type->status(&pg->ps,
1321 &p->path, type, result + sz,
1322 maxlen - sz);
1323 }
1324 }
1325 break;
1326 }
1327
1328 spin_unlock_irqrestore(&m->lock, flags);
1329
1330 return 0;
1331}
1332
1333static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1334{
1335 int r;
1336 struct dm_dev *dev;
1337 struct multipath *m = (struct multipath *) ti->private;
1338 action_fn action;
1339
1340 if (argc == 1) {
1341 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001342 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001344 return queue_if_no_path(m, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346
1347 if (argc != 2)
1348 goto error;
1349
1350 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1351 return bypass_pg_num(m, argv[1], 1);
1352 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1353 return bypass_pg_num(m, argv[1], 0);
1354 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1355 return switch_pg_num(m, argv[1]);
1356 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1357 action = reinstate_path;
1358 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1359 action = fail_path;
1360 else
1361 goto error;
1362
1363 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1364 dm_table_get_mode(ti->table), &dev);
1365 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001366 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 argv[1]);
1368 return -EINVAL;
1369 }
1370
1371 r = action_dev(m, dev, action);
1372
1373 dm_put_device(ti, dev);
1374
1375 return r;
1376
1377error:
1378 DMWARN("Unrecognised multipath message received.");
1379 return -EINVAL;
1380}
1381
Milan Broz9af4aa32006-10-03 01:15:20 -07001382static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
1383 struct file *filp, unsigned int cmd,
1384 unsigned long arg)
1385{
1386 struct multipath *m = (struct multipath *) ti->private;
1387 struct block_device *bdev = NULL;
1388 unsigned long flags;
Milan Broze90dae12006-10-03 01:15:22 -07001389 struct file fake_file = {};
1390 struct dentry fake_dentry = {};
Milan Broz9af4aa32006-10-03 01:15:20 -07001391 int r = 0;
1392
Josef Sipekc649bb92006-12-08 02:37:19 -08001393 fake_file.f_path.dentry = &fake_dentry;
Milan Broze90dae12006-10-03 01:15:22 -07001394
Milan Broz9af4aa32006-10-03 01:15:20 -07001395 spin_lock_irqsave(&m->lock, flags);
1396
1397 if (!m->current_pgpath)
1398 __choose_pgpath(m);
1399
Milan Broze90dae12006-10-03 01:15:22 -07001400 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001401 bdev = m->current_pgpath->path.dev->bdev;
Milan Broze90dae12006-10-03 01:15:22 -07001402 fake_dentry.d_inode = bdev->bd_inode;
1403 fake_file.f_mode = m->current_pgpath->path.dev->mode;
1404 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001405
1406 if (m->queue_io)
1407 r = -EAGAIN;
1408 else if (!bdev)
1409 r = -EIO;
1410
1411 spin_unlock_irqrestore(&m->lock, flags);
1412
Milan Broze90dae12006-10-03 01:15:22 -07001413 return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
1414 bdev->bd_disk, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001415}
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417/*-----------------------------------------------------------------
1418 * Module setup
1419 *---------------------------------------------------------------*/
1420static struct target_type multipath_target = {
1421 .name = "multipath",
Milan Broz9af4aa32006-10-03 01:15:20 -07001422 .version = {1, 0, 5},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 .module = THIS_MODULE,
1424 .ctr = multipath_ctr,
1425 .dtr = multipath_dtr,
1426 .map = multipath_map,
1427 .end_io = multipath_end_io,
1428 .presuspend = multipath_presuspend,
1429 .resume = multipath_resume,
1430 .status = multipath_status,
1431 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001432 .ioctl = multipath_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433};
1434
1435static int __init dm_multipath_init(void)
1436{
1437 int r;
1438
1439 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001440 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (!_mpio_cache)
1442 return -ENOMEM;
1443
1444 r = dm_register_target(&multipath_target);
1445 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001446 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 kmem_cache_destroy(_mpio_cache);
1448 return -EINVAL;
1449 }
1450
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001451 kmultipathd = create_workqueue("kmpathd");
1452 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001453 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001454 dm_unregister_target(&multipath_target);
1455 kmem_cache_destroy(_mpio_cache);
1456 return -ENOMEM;
1457 }
1458
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001459 /*
1460 * A separate workqueue is used to handle the device handlers
1461 * to avoid overloading existing workqueue. Overloading the
1462 * old workqueue would also create a bottleneck in the
1463 * path of the storage hardware device activation.
1464 */
1465 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1466 if (!kmpath_handlerd) {
1467 DMERR("failed to create workqueue kmpath_handlerd");
1468 destroy_workqueue(kmultipathd);
1469 dm_unregister_target(&multipath_target);
1470 kmem_cache_destroy(_mpio_cache);
1471 return -ENOMEM;
1472 }
1473
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001474 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 multipath_target.version[0], multipath_target.version[1],
1476 multipath_target.version[2]);
1477
1478 return r;
1479}
1480
1481static void __exit dm_multipath_exit(void)
1482{
1483 int r;
1484
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001485 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001486 destroy_workqueue(kmultipathd);
1487
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 r = dm_unregister_target(&multipath_target);
1489 if (r < 0)
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001490 DMERR("target unregister failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 kmem_cache_destroy(_mpio_cache);
1492}
1493
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494module_init(dm_multipath_init);
1495module_exit(dm_multipath_exit);
1496
1497MODULE_DESCRIPTION(DM_NAME " multipath target");
1498MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1499MODULE_LICENSE("GPL");