blob: 030bc2a053ec98e15c8afe06a10a5251f5e61625 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 2003 Sistina Software Limited.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4 *
5 * This file is released under the GPL.
6 */
7
Mikulas Patocka586e80e2008-10-21 17:44:59 +01008#include <linux/device-mapper.h>
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "dm-path-selector.h"
Mike Andersonb15546f2007-10-19 22:48:02 +010011#include "dm-uevent.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#include <linux/ctype.h>
14#include <linux/init.h>
15#include <linux/mempool.h>
16#include <linux/module.h>
17#include <linux/pagemap.h>
18#include <linux/slab.h>
19#include <linux/time.h>
20#include <linux/workqueue.h>
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070021#include <scsi/scsi_dh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/atomic.h>
23
Alasdair G Kergon72d94862006-06-26 00:27:35 -070024#define DM_MSG_PREFIX "multipath"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#define MESG_STR(x) x, sizeof(x)
26
27/* Path properties */
28struct pgpath {
29 struct list_head list;
30
31 struct priority_group *pg; /* Owning PG */
Kiyoshi Ueda66800732008-10-10 13:36:58 +010032 unsigned is_active; /* Path status */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 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;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +010037 struct work_struct activate_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038};
39
40#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
41
42/*
43 * Paths are grouped into Priority Groups and numbered from 1 upwards.
44 * Each has a path selector which controls which path gets used.
45 */
46struct priority_group {
47 struct list_head list;
48
49 struct multipath *m; /* Owning multipath instance */
50 struct path_selector ps;
51
52 unsigned pg_num; /* Reference number */
53 unsigned bypassed; /* Temporarily bypass this PG? */
54
55 unsigned nr_pgpaths; /* Number of paths in PG */
56 struct list_head pgpaths;
57};
58
59/* Multipath context */
60struct multipath {
61 struct list_head list;
62 struct dm_target *ti;
63
64 spinlock_t lock;
65
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070066 const char *hw_handler_name;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -070067 char *hw_handler_params;
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 */
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +000072 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 unsigned nr_valid_paths; /* Total number of usable paths */
75 struct pgpath *current_pgpath;
76 struct priority_group *current_pg;
77 struct priority_group *next_pg; /* Switch to this PG if set */
78 unsigned repeat_count; /* I/Os left before calling PS again */
79
80 unsigned queue_io; /* Must we queue all I/O? */
81 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
Alasdair G Kergon436d4102005-07-12 15:53:03 -070082 unsigned saved_queue_if_no_path;/* Saved state during suspension */
Dave Wysochanskic9e45582007-10-19 22:47:53 +010083 unsigned pg_init_retries; /* Number of times to retry pg_init */
84 unsigned pg_init_count; /* Number of times pg_init called */
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 struct work_struct process_queued_ios;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +010087 struct list_head queued_ios;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 unsigned queue_size;
89
90 struct work_struct trigger_event;
91
92 /*
Alasdair G Kergon028867a2007-07-12 17:26:32 +010093 * We must use a mempool of dm_mpath_io structs so that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * can resubmit bios on error.
95 */
96 mempool_t *mpio_pool;
Mike Anderson6380f262009-12-10 23:52:21 +000097
98 struct mutex work_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
101/*
102 * Context information attached to each bio we process.
103 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100104struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct pgpath *pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100106 size_t nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109typedef int (*action_fn) (struct pgpath *pgpath);
110
111#define MIN_IOS 256 /* Mempool size */
112
Christoph Lametere18b8902006-12-06 20:33:20 -0800113static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700115static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000116static void process_queued_ios(struct work_struct *work);
117static void trigger_event(struct work_struct *work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700118static void activate_path(struct work_struct *work);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200119static void deactivate_path(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121
122/*-----------------------------------------------
123 * Allocation routines
124 *-----------------------------------------------*/
125
126static struct pgpath *alloc_pgpath(void)
127{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700128 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Mike Anderson224cb3e2008-08-29 09:36:09 +0200130 if (pgpath) {
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100131 pgpath->is_active = 1;
Mike Anderson224cb3e2008-08-29 09:36:09 +0200132 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100133 INIT_WORK(&pgpath->activate_path, activate_path);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 return pgpath;
137}
138
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100139static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 kfree(pgpath);
142}
143
Mike Anderson224cb3e2008-08-29 09:36:09 +0200144static void deactivate_path(struct work_struct *work)
145{
146 struct pgpath *pgpath =
147 container_of(work, struct pgpath, deactivate_path);
148
149 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static struct priority_group *alloc_priority_group(void)
153{
154 struct priority_group *pg;
155
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700156 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700158 if (pg)
159 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 return pg;
162}
163
164static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
165{
166 struct pgpath *pgpath, *tmp;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700167 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
170 list_del(&pgpath->list);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700171 if (m->hw_handler_name)
172 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 dm_put_device(ti, pgpath->path.dev);
174 free_pgpath(pgpath);
175 }
176}
177
178static void free_priority_group(struct priority_group *pg,
179 struct dm_target *ti)
180{
181 struct path_selector *ps = &pg->ps;
182
183 if (ps->type) {
184 ps->type->destroy(ps);
185 dm_put_path_selector(ps->type);
186 }
187
188 free_pgpaths(&pg->pgpaths, ti);
189 kfree(pg);
190}
191
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700192static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct multipath *m;
195
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700196 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 INIT_LIST_HEAD(&m->priority_groups);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100199 INIT_LIST_HEAD(&m->queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 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);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000204 init_waitqueue_head(&m->pg_init_wait);
Mike Anderson6380f262009-12-10 23:52:21 +0000205 mutex_init(&m->work_mutex);
Matthew Dobson93d23412006-03-26 01:37:50 -0800206 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (!m->mpio_pool) {
208 kfree(m);
209 return NULL;
210 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700211 m->ti = ti;
212 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214
215 return m;
216}
217
218static void free_multipath(struct multipath *m)
219{
220 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
223 list_del(&pg->list);
224 free_priority_group(pg, m->ti);
225 }
226
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700227 kfree(m->hw_handler_name);
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700228 kfree(m->hw_handler_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 mempool_destroy(m->mpio_pool);
230 kfree(m);
231}
232
233
234/*-----------------------------------------------
235 * Path selection
236 *-----------------------------------------------*/
237
238static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
239{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 m->current_pg = pgpath->pg;
241
242 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700243 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 m->pg_init_required = 1;
245 m->queue_io = 1;
246 } else {
247 m->pg_init_required = 0;
248 m->queue_io = 0;
249 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100250
251 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100254static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
255 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800257 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100259 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (!path)
261 return -ENXIO;
262
263 m->current_pgpath = path_to_pgpath(path);
264
265 if (m->current_pg != pg)
266 __switch_pg(m, m->current_pgpath);
267
268 return 0;
269}
270
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100271static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 struct priority_group *pg;
274 unsigned bypassed = 1;
275
276 if (!m->nr_valid_paths)
277 goto failed;
278
279 /* Were we instructed to switch PG? */
280 if (m->next_pg) {
281 pg = m->next_pg;
282 m->next_pg = NULL;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100283 if (!__choose_path_in_pg(m, pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return;
285 }
286
287 /* Don't change PG until it has no remaining paths */
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100288 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return;
290
291 /*
292 * Loop through priority groups until we find a valid path.
293 * First time we skip PGs marked 'bypassed'.
294 * Second time we only try the ones we skipped.
295 */
296 do {
297 list_for_each_entry(pg, &m->priority_groups, list) {
298 if (pg->bypassed == bypassed)
299 continue;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100300 if (!__choose_path_in_pg(m, pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return;
302 }
303 } while (bypassed--);
304
305failed:
306 m->current_pgpath = NULL;
307 m->current_pg = NULL;
308}
309
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800310/*
311 * Check whether bios must be queued in the device-mapper core rather
312 * than here in the target.
313 *
314 * m->lock must be held on entry.
315 *
316 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
317 * same value then we are not between multipath_presuspend()
318 * and multipath_resume() calls and we have no need to check
319 * for the DMF_NOFLUSH_SUSPENDING flag.
320 */
321static int __must_push_back(struct multipath *m)
322{
323 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
324 dm_noflush_suspending(m->ti));
325}
326
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100327static int map_io(struct multipath *m, struct request *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100328 struct dm_mpath_io *mpio, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800330 int r = DM_MAPIO_REMAPPED;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100331 size_t nr_bytes = blk_rq_bytes(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 unsigned long flags;
333 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100334 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 spin_lock_irqsave(&m->lock, flags);
337
338 /* Do we need to select a new pgpath? */
339 if (!m->current_pgpath ||
340 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100341 __choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 pgpath = m->current_pgpath;
344
345 if (was_queued)
346 m->queue_size--;
347
348 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700349 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 /* Queue for the daemon to resubmit */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100351 list_add_tail(&clone->queuelist, &m->queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700353 if ((m->pg_init_required && !m->pg_init_in_progress) ||
354 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700355 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800357 r = DM_MAPIO_SUBMITTED;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100358 } else if (pgpath) {
359 bdev = pgpath->path.dev->bdev;
360 clone->q = bdev_get_queue(bdev);
361 clone->rq_disk = bdev->bd_disk;
362 } else if (__must_push_back(m))
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800363 r = DM_MAPIO_REQUEUE;
364 else
365 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 mpio->pgpath = pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100368 mpio->nr_bytes = nr_bytes;
369
370 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
371 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
372 nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 spin_unlock_irqrestore(&m->lock, flags);
375
376 return r;
377}
378
379/*
380 * If we run out of usable paths, should we queue I/O or error it?
381 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700382static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
383 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 unsigned long flags;
386
387 spin_lock_irqsave(&m->lock, flags);
388
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700389 if (save_old_value)
390 m->saved_queue_if_no_path = m->queue_if_no_path;
391 else
392 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700394 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700395 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 spin_unlock_irqrestore(&m->lock, flags);
398
399 return 0;
400}
401
402/*-----------------------------------------------------------------
403 * The multipath daemon is responsible for resubmitting queued ios.
404 *---------------------------------------------------------------*/
405
406static void dispatch_queued_ios(struct multipath *m)
407{
408 int r;
409 unsigned long flags;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100410 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 union map_info *info;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100412 struct request *clone, *n;
413 LIST_HEAD(cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 spin_lock_irqsave(&m->lock, flags);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100416 list_splice_init(&m->queued_ios, &cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 spin_unlock_irqrestore(&m->lock, flags);
418
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100419 list_for_each_entry_safe(clone, n, &cl, queuelist) {
420 list_del_init(&clone->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100422 info = dm_get_rq_mapinfo(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 mpio = info->ptr;
424
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100425 r = map_io(m, clone, mpio, 1);
426 if (r < 0) {
427 mempool_free(mpio, m->mpio_pool);
428 dm_kill_unmapped_request(clone, r);
429 } else if (r == DM_MAPIO_REMAPPED)
430 dm_dispatch_request(clone);
431 else if (r == DM_MAPIO_REQUEUE) {
432 mempool_free(mpio, m->mpio_pool);
433 dm_requeue_unmapped_request(clone);
434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436}
437
David Howellsc4028952006-11-22 14:57:56 +0000438static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
David Howellsc4028952006-11-22 14:57:56 +0000440 struct multipath *m =
441 container_of(work, struct multipath, process_queued_ios);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100442 struct pgpath *pgpath = NULL, *tmp;
443 unsigned must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unsigned long flags;
445
446 spin_lock_irqsave(&m->lock, flags);
447
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700448 if (!m->queue_size)
449 goto out;
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (!m->current_pgpath)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100452 __choose_pgpath(m, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 pgpath = m->current_pgpath;
455
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700456 if ((pgpath && !m->queue_io) ||
457 (!pgpath && !m->queue_if_no_path))
458 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Chandra Seetharamanb81aa1c2008-11-13 23:39:00 +0000460 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100461 m->pg_init_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 m->pg_init_required = 0;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100463 list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
Moger, Babuf7b934c2010-03-06 02:29:49 +0000464 /* Skip failed paths */
465 if (!tmp->is_active)
466 continue;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100467 if (queue_work(kmpath_handlerd, &tmp->activate_path))
468 m->pg_init_in_progress++;
469 }
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700470 }
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700471out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (!must_queue)
474 dispatch_queued_ios(m);
475}
476
477/*
478 * An event is triggered whenever a path is taken out of use.
479 * Includes path failure and PG bypass.
480 */
David Howellsc4028952006-11-22 14:57:56 +0000481static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
David Howellsc4028952006-11-22 14:57:56 +0000483 struct multipath *m =
484 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 dm_table_event(m->ti->table);
487}
488
489/*-----------------------------------------------------------------
490 * Constructor/argument parsing:
491 * <#multipath feature args> [<arg>]*
492 * <#hw_handler args> [hw_handler [<arg>]*]
493 * <#priority groups>
494 * <initial priority group>
495 * [<selector> <#selector args> [<arg>]*
496 * <#paths> <#per-path selector args>
497 * [<path> [<arg>]* ]+ ]+
498 *---------------------------------------------------------------*/
499struct param {
500 unsigned min;
501 unsigned max;
502 char *error;
503};
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505static int read_param(struct param *param, char *str, unsigned *v, char **error)
506{
507 if (!str ||
508 (sscanf(str, "%u", v) != 1) ||
509 (*v < param->min) ||
510 (*v > param->max)) {
511 *error = param->error;
512 return -EINVAL;
513 }
514
515 return 0;
516}
517
518struct arg_set {
519 unsigned argc;
520 char **argv;
521};
522
523static char *shift(struct arg_set *as)
524{
525 char *r;
526
527 if (as->argc) {
528 as->argc--;
529 r = *as->argv;
530 as->argv++;
531 return r;
532 }
533
534 return NULL;
535}
536
537static void consume(struct arg_set *as, unsigned n)
538{
539 BUG_ON (as->argc < n);
540 as->argc -= n;
541 as->argv += n;
542}
543
544static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
545 struct dm_target *ti)
546{
547 int r;
548 struct path_selector_type *pst;
549 unsigned ps_argc;
550
551 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700552 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 };
554
555 pst = dm_get_path_selector(shift(as));
556 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700557 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return -EINVAL;
559 }
560
561 r = read_param(_params, shift(as), &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100562 if (r) {
563 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Mikulas Patocka0e0497c2009-06-22 10:08:02 +0100567 if (ps_argc > as->argc) {
568 dm_put_path_selector(pst);
569 ti->error = "not enough arguments for path selector";
570 return -EINVAL;
571 }
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 r = pst->create(&pg->ps, ps_argc, as->argv);
574 if (r) {
575 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700576 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return r;
578 }
579
580 pg->ps.type = pst;
581 consume(as, ps_argc);
582
583 return 0;
584}
585
586static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
587 struct dm_target *ti)
588{
589 int r;
590 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700591 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 /* we need at least a path arg */
594 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700595 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100596 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
599 p = alloc_pgpath();
600 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100601 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
604 dm_table_get_mode(ti->table), &p->path.dev);
605 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700606 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 goto bad;
608 }
609
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700610 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100611 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
612
613 r = scsi_dh_attach(q, m->hw_handler_name);
614 if (r == -EBUSY) {
615 /*
616 * Already attached to different hw_handler,
617 * try to reattach with correct one.
618 */
619 scsi_dh_detach(q);
620 r = scsi_dh_attach(q, m->hw_handler_name);
621 }
622
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700623 if (r < 0) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100624 ti->error = "error attaching hardware handler";
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700625 dm_put_device(ti, p->path.dev);
626 goto bad;
627 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700628
629 if (m->hw_handler_params) {
630 r = scsi_dh_set_params(q, m->hw_handler_params);
631 if (r < 0) {
632 ti->error = "unable to set hardware "
633 "handler parameters";
634 scsi_dh_detach(q);
635 dm_put_device(ti, p->path.dev);
636 goto bad;
637 }
638 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700639 }
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
642 if (r) {
643 dm_put_device(ti, p->path.dev);
644 goto bad;
645 }
646
647 return p;
648
649 bad:
650 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100651 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700655 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
657 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700658 {1, 1024, "invalid number of paths"},
659 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 };
661
662 int r;
663 unsigned i, nr_selector_args, nr_params;
664 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700665 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 if (as->argc < 2) {
668 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100669 ti->error = "not enough priority group arguments";
670 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
673 pg = alloc_priority_group();
674 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700675 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100676 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 pg->m = m;
679
680 r = parse_path_selector(as, pg, ti);
681 if (r)
682 goto bad;
683
684 /*
685 * read the paths
686 */
687 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
688 if (r)
689 goto bad;
690
691 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
692 if (r)
693 goto bad;
694
695 nr_params = 1 + nr_selector_args;
696 for (i = 0; i < pg->nr_pgpaths; i++) {
697 struct pgpath *pgpath;
698 struct arg_set path_args;
699
Mikulas Patocka148acff2008-07-21 12:00:30 +0100700 if (as->argc < nr_params) {
701 ti->error = "not enough path parameters";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 path_args.argc = nr_params;
706 path_args.argv = as->argv;
707
708 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100709 if (IS_ERR(pgpath)) {
710 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 pgpath->pg = pg;
715 list_add_tail(&pgpath->list, &pg->pgpaths);
716 consume(as, nr_params);
717 }
718
719 return pg;
720
721 bad:
722 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100723 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724}
725
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700726static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700729 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700730 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700733 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 };
735
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700736 if (read_param(_params, shift(as), &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 return -EINVAL;
738
739 if (!hw_argc)
740 return 0;
741
Mikulas Patockae094f4f2009-06-22 10:12:10 +0100742 if (hw_argc > as->argc) {
743 ti->error = "not enough arguments for hardware handler";
744 return -EINVAL;
745 }
746
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700747 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
748 request_module("scsi_dh_%s", m->hw_handler_name);
749 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700750 ti->error = "unknown hardware handler type";
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700751 ret = -EINVAL;
752 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 }
Chandra Seetharaman14e98c52008-11-13 23:39:06 +0000754
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700755 if (hw_argc > 1) {
756 char *p;
757 int i, j, len = 4;
758
759 for (i = 0; i <= hw_argc - 2; i++)
760 len += strlen(as->argv[i]) + 1;
761 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
762 if (!p) {
763 ti->error = "memory allocation failed";
764 ret = -ENOMEM;
765 goto fail;
766 }
767 j = sprintf(p, "%d", hw_argc - 1);
768 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
769 j = sprintf(p, "%s", as->argv[i]);
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 consume(as, hw_argc - 1);
772
773 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700774fail:
775 kfree(m->hw_handler_name);
776 m->hw_handler_name = NULL;
777 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700780static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 int r;
783 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700784 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100785 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100788 {0, 3, "invalid number of feature args"},
789 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 };
791
792 r = read_param(_params, shift(as), &argc, &ti->error);
793 if (r)
794 return -EINVAL;
795
796 if (!argc)
797 return 0;
798
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100799 do {
800 param_name = shift(as);
801 argc--;
802
803 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
804 r = queue_if_no_path(m, 1, 0);
805 continue;
806 }
807
808 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
809 (argc >= 1)) {
810 r = read_param(_params + 1, shift(as),
811 &m->pg_init_retries, &ti->error);
812 argc--;
813 continue;
814 }
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100817 r = -EINVAL;
818 } while (argc && !r);
819
820 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821}
822
823static int multipath_ctr(struct dm_target *ti, unsigned int argc,
824 char **argv)
825{
826 /* target parameters */
827 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700828 {1, 1024, "invalid number of priority groups"},
829 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 };
831
832 int r;
833 struct multipath *m;
834 struct arg_set as;
835 unsigned pg_count = 0;
836 unsigned next_pg_num;
837
838 as.argc = argc;
839 as.argv = argv;
840
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700841 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700843 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return -EINVAL;
845 }
846
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700847 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (r)
849 goto bad;
850
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700851 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 if (r)
853 goto bad;
854
855 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
856 if (r)
857 goto bad;
858
859 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
860 if (r)
861 goto bad;
862
863 /* parse the priority groups */
864 while (as.argc) {
865 struct priority_group *pg;
866
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700867 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100868 if (IS_ERR(pg)) {
869 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto bad;
871 }
872
873 m->nr_valid_paths += pg->nr_pgpaths;
874 list_add_tail(&pg->list, &m->priority_groups);
875 pg_count++;
876 pg->pg_num = pg_count;
877 if (!--next_pg_num)
878 m->next_pg = pg;
879 }
880
881 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700882 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 r = -EINVAL;
884 goto bad;
885 }
886
Mikulas Patocka86279212009-06-22 10:12:24 +0100887 ti->num_flush_requests = 1;
888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return 0;
890
891 bad:
892 free_multipath(m);
893 return r;
894}
895
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000896static void multipath_wait_for_pg_init_completion(struct multipath *m)
897{
898 DECLARE_WAITQUEUE(wait, current);
899 unsigned long flags;
900
901 add_wait_queue(&m->pg_init_wait, &wait);
902
903 while (1) {
904 set_current_state(TASK_UNINTERRUPTIBLE);
905
906 spin_lock_irqsave(&m->lock, flags);
907 if (!m->pg_init_in_progress) {
908 spin_unlock_irqrestore(&m->lock, flags);
909 break;
910 }
911 spin_unlock_irqrestore(&m->lock, flags);
912
913 io_schedule();
914 }
915 set_current_state(TASK_RUNNING);
916
917 remove_wait_queue(&m->pg_init_wait, &wait);
918}
919
920static void flush_multipath_work(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700922 flush_workqueue(kmpath_handlerd);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000923 multipath_wait_for_pg_init_completion(m);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700924 flush_workqueue(kmultipathd);
Mikulas Patocka53b351f2009-06-22 10:12:13 +0100925 flush_scheduled_work();
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +0000926}
927
928static void multipath_dtr(struct dm_target *ti)
929{
930 struct multipath *m = ti->private;
931
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000932 flush_multipath_work(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 free_multipath(m);
934}
935
936/*
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100937 * Map cloned requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100939static int multipath_map(struct dm_target *ti, struct request *clone,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 union map_info *map_context)
941{
942 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100943 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 struct multipath *m = (struct multipath *) ti->private;
945
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100946 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
947 if (!mpio)
948 /* ENOMEM, requeue */
949 return DM_MAPIO_REQUEUE;
950 memset(mpio, 0, sizeof(*mpio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 map_context->ptr = mpio;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100953 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
954 r = map_io(m, clone, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800955 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 mempool_free(mpio, m->mpio_pool);
957
958 return r;
959}
960
961/*
962 * Take a path out of use.
963 */
964static int fail_path(struct pgpath *pgpath)
965{
966 unsigned long flags;
967 struct multipath *m = pgpath->pg->m;
968
969 spin_lock_irqsave(&m->lock, flags);
970
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100971 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 goto out;
973
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700974 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
976 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100977 pgpath->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 pgpath->fail_count++;
979
980 m->nr_valid_paths--;
981
982 if (pgpath == m->current_pgpath)
983 m->current_pgpath = NULL;
984
Mike Andersonb15546f2007-10-19 22:48:02 +0100985 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
986 pgpath->path.dev->name, m->nr_valid_paths);
987
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +0000988 schedule_work(&m->trigger_event);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200989 queue_work(kmultipathd, &pgpath->deactivate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991out:
992 spin_unlock_irqrestore(&m->lock, flags);
993
994 return 0;
995}
996
997/*
998 * Reinstate a previously-failed path
999 */
1000static int reinstate_path(struct pgpath *pgpath)
1001{
1002 int r = 0;
1003 unsigned long flags;
1004 struct multipath *m = pgpath->pg->m;
1005
1006 spin_lock_irqsave(&m->lock, flags);
1007
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001008 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 goto out;
1010
Alasdair G Kergondef052d2008-07-21 12:00:31 +01001011 if (!pgpath->pg->ps.type->reinstate_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 DMWARN("Reinstate path not supported by path selector %s",
1013 pgpath->pg->ps.type->name);
1014 r = -EINVAL;
1015 goto out;
1016 }
1017
1018 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1019 if (r)
1020 goto out;
1021
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001022 pgpath->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001024 if (!m->nr_valid_paths++ && m->queue_size) {
1025 m->current_pgpath = NULL;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001026 queue_work(kmultipathd, &m->process_queued_ios);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001027 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
1028 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
1029 m->pg_init_in_progress++;
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Mike Andersonb15546f2007-10-19 22:48:02 +01001032 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1033 pgpath->path.dev->name, m->nr_valid_paths);
1034
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001035 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037out:
1038 spin_unlock_irqrestore(&m->lock, flags);
1039
1040 return r;
1041}
1042
1043/*
1044 * Fail or reinstate all paths that match the provided struct dm_dev.
1045 */
1046static int action_dev(struct multipath *m, struct dm_dev *dev,
1047 action_fn action)
1048{
1049 int r = 0;
1050 struct pgpath *pgpath;
1051 struct priority_group *pg;
1052
1053 list_for_each_entry(pg, &m->priority_groups, list) {
1054 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1055 if (pgpath->path.dev == dev)
1056 r = action(pgpath);
1057 }
1058 }
1059
1060 return r;
1061}
1062
1063/*
1064 * Temporarily try to avoid having to use the specified PG
1065 */
1066static void bypass_pg(struct multipath *m, struct priority_group *pg,
1067 int bypassed)
1068{
1069 unsigned long flags;
1070
1071 spin_lock_irqsave(&m->lock, flags);
1072
1073 pg->bypassed = bypassed;
1074 m->current_pgpath = NULL;
1075 m->current_pg = NULL;
1076
1077 spin_unlock_irqrestore(&m->lock, flags);
1078
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001079 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
1082/*
1083 * Switch to using the specified PG from the next I/O that gets mapped
1084 */
1085static int switch_pg_num(struct multipath *m, const char *pgstr)
1086{
1087 struct priority_group *pg;
1088 unsigned pgnum;
1089 unsigned long flags;
1090
1091 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1092 (pgnum > m->nr_priority_groups)) {
1093 DMWARN("invalid PG number supplied to switch_pg_num");
1094 return -EINVAL;
1095 }
1096
1097 spin_lock_irqsave(&m->lock, flags);
1098 list_for_each_entry(pg, &m->priority_groups, list) {
1099 pg->bypassed = 0;
1100 if (--pgnum)
1101 continue;
1102
1103 m->current_pgpath = NULL;
1104 m->current_pg = NULL;
1105 m->next_pg = pg;
1106 }
1107 spin_unlock_irqrestore(&m->lock, flags);
1108
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001109 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return 0;
1111}
1112
1113/*
1114 * Set/clear bypassed status of a PG.
1115 * PGs are numbered upwards from 1 in the order they were declared.
1116 */
1117static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1118{
1119 struct priority_group *pg;
1120 unsigned pgnum;
1121
1122 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1123 (pgnum > m->nr_priority_groups)) {
1124 DMWARN("invalid PG number supplied to bypass_pg");
1125 return -EINVAL;
1126 }
1127
1128 list_for_each_entry(pg, &m->priority_groups, list) {
1129 if (!--pgnum)
1130 break;
1131 }
1132
1133 bypass_pg(m, pg, bypassed);
1134 return 0;
1135}
1136
1137/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001138 * Should we retry pg_init immediately?
1139 */
1140static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1141{
1142 unsigned long flags;
1143 int limit_reached = 0;
1144
1145 spin_lock_irqsave(&m->lock, flags);
1146
1147 if (m->pg_init_count <= m->pg_init_retries)
1148 m->pg_init_required = 1;
1149 else
1150 limit_reached = 1;
1151
1152 spin_unlock_irqrestore(&m->lock, flags);
1153
1154 return limit_reached;
1155}
1156
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001157static void pg_init_done(void *data, int errors)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001158{
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001159 struct pgpath *pgpath = data;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001160 struct priority_group *pg = pgpath->pg;
1161 struct multipath *m = pg->m;
1162 unsigned long flags;
1163
1164 /* device or driver problems */
1165 switch (errors) {
1166 case SCSI_DH_OK:
1167 break;
1168 case SCSI_DH_NOSYS:
1169 if (!m->hw_handler_name) {
1170 errors = 0;
1171 break;
1172 }
Moger, Babuf7b934c2010-03-06 02:29:49 +00001173 DMERR("Could not failover the device: Handler scsi_dh_%s "
1174 "Error %d.", m->hw_handler_name, errors);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001175 /*
1176 * Fail path for now, so we do not ping pong
1177 */
1178 fail_path(pgpath);
1179 break;
1180 case SCSI_DH_DEV_TEMP_BUSY:
1181 /*
1182 * Probably doing something like FW upgrade on the
1183 * controller so try the other pg.
1184 */
1185 bypass_pg(m, pg, 1);
1186 break;
1187 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1188 case SCSI_DH_RETRY:
1189 case SCSI_DH_IMM_RETRY:
1190 case SCSI_DH_RES_TEMP_UNAVAIL:
1191 if (pg_init_limit_reached(m, pgpath))
1192 fail_path(pgpath);
1193 errors = 0;
1194 break;
1195 default:
1196 /*
1197 * We probably do not want to fail the path for a device
1198 * error, but this is what the old dm did. In future
1199 * patches we can do more advanced handling.
1200 */
1201 fail_path(pgpath);
1202 }
1203
1204 spin_lock_irqsave(&m->lock, flags);
1205 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001206 if (pgpath == m->current_pgpath) {
1207 DMERR("Could not failover device. Error %d.", errors);
1208 m->current_pgpath = NULL;
1209 m->current_pg = NULL;
1210 }
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001211 } else if (!m->pg_init_required)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001212 pg->bypassed = 0;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001213
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001214 if (--m->pg_init_in_progress)
1215 /* Activations of other paths are still on going */
1216 goto out;
1217
1218 if (!m->pg_init_required)
1219 m->queue_io = 0;
1220
1221 queue_work(kmultipathd, &m->process_queued_ios);
1222
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001223 /*
1224 * Wake up any thread waiting to suspend.
1225 */
1226 wake_up(&m->pg_init_wait);
1227
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001228out:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001229 spin_unlock_irqrestore(&m->lock, flags);
1230}
1231
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001232static void activate_path(struct work_struct *work)
1233{
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001234 struct pgpath *pgpath =
1235 container_of(work, struct pgpath, activate_path);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001236
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001237 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001238 pg_init_done, pgpath);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001239}
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241/*
1242 * end_io handling
1243 */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001244static int do_end_io(struct multipath *m, struct request *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001245 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246{
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001247 /*
1248 * We don't queue any clone request inside the multipath target
1249 * during end I/O handling, since those clone requests don't have
1250 * bio clones. If we queue them inside the multipath target,
1251 * we need to make bio clones, that requires memory allocation.
1252 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1253 * don't have bio clones.)
1254 * Instead of queueing the clone request here, we queue the original
1255 * request into dm core, which will remake a clone request and
1256 * clone bios for it and resubmit it later.
1257 */
1258 int r = DM_ENDIO_REQUEUE;
Stefan Bader640eb3b2005-11-21 21:32:35 -08001259 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001261 if (!error && !clone->errors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 return 0; /* I/O complete */
1263
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001264 if (error == -EOPNOTSUPP)
1265 return error;
1266
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001267 if (mpio->pgpath)
1268 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Stefan Bader640eb3b2005-11-21 21:32:35 -08001270 spin_lock_irqsave(&m->lock, flags);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001271 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1272 r = -EIO;
Stefan Bader640eb3b2005-11-21 21:32:35 -08001273 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001275 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001278static int multipath_end_io(struct dm_target *ti, struct request *clone,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int error, union map_info *map_context)
1280{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001281 struct multipath *m = ti->private;
1282 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 struct pgpath *pgpath = mpio->pgpath;
1284 struct path_selector *ps;
1285 int r;
1286
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001287 r = do_end_io(m, clone, error, mpio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (pgpath) {
1289 ps = &pgpath->pg->ps;
1290 if (ps->type->end_io)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001291 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001293 mempool_free(mpio, m->mpio_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 return r;
1296}
1297
1298/*
1299 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001300 * the last path fails we must error any remaining I/O.
1301 * Note that if the freeze_bdev fails while suspending, the
1302 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 */
1304static void multipath_presuspend(struct dm_target *ti)
1305{
1306 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001308 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001311static void multipath_postsuspend(struct dm_target *ti)
1312{
Mike Anderson6380f262009-12-10 23:52:21 +00001313 struct multipath *m = ti->private;
1314
1315 mutex_lock(&m->work_mutex);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001316 flush_multipath_work(m);
Mike Anderson6380f262009-12-10 23:52:21 +00001317 mutex_unlock(&m->work_mutex);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001318}
1319
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001320/*
1321 * Restore the queue_if_no_path setting.
1322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323static void multipath_resume(struct dm_target *ti)
1324{
1325 struct multipath *m = (struct multipath *) ti->private;
1326 unsigned long flags;
1327
1328 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001329 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 spin_unlock_irqrestore(&m->lock, flags);
1331}
1332
1333/*
1334 * Info output has the following format:
1335 * num_multipath_feature_args [multipath_feature_args]*
1336 * num_handler_status_args [handler_status_args]*
1337 * num_groups init_group_number
1338 * [A|D|E num_ps_status_args [ps_status_args]*
1339 * num_paths num_selector_args
1340 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1341 *
1342 * Table output has the following format (identical to the constructor string):
1343 * num_feature_args [features_args]*
1344 * num_handler_args hw_handler [hw_handler_args]*
1345 * num_groups init_group_number
1346 * [priority selector-name num_ps_args [ps_args]*
1347 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1348 */
1349static int multipath_status(struct dm_target *ti, status_type_t type,
1350 char *result, unsigned int maxlen)
1351{
1352 int sz = 0;
1353 unsigned long flags;
1354 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 struct priority_group *pg;
1356 struct pgpath *p;
1357 unsigned pg_num;
1358 char state;
1359
1360 spin_lock_irqsave(&m->lock, flags);
1361
1362 /* Features */
1363 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001364 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1365 else {
1366 DMEMIT("%u ", m->queue_if_no_path +
1367 (m->pg_init_retries > 0) * 2);
1368 if (m->queue_if_no_path)
1369 DMEMIT("queue_if_no_path ");
1370 if (m->pg_init_retries)
1371 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001374 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 DMEMIT("0 ");
1376 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001377 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 DMEMIT("%u ", m->nr_priority_groups);
1380
1381 if (m->next_pg)
1382 pg_num = m->next_pg->pg_num;
1383 else if (m->current_pg)
1384 pg_num = m->current_pg->pg_num;
1385 else
1386 pg_num = 1;
1387
1388 DMEMIT("%u ", pg_num);
1389
1390 switch (type) {
1391 case STATUSTYPE_INFO:
1392 list_for_each_entry(pg, &m->priority_groups, list) {
1393 if (pg->bypassed)
1394 state = 'D'; /* Disabled */
1395 else if (pg == m->current_pg)
1396 state = 'A'; /* Currently Active */
1397 else
1398 state = 'E'; /* Enabled */
1399
1400 DMEMIT("%c ", state);
1401
1402 if (pg->ps.type->status)
1403 sz += pg->ps.type->status(&pg->ps, NULL, type,
1404 result + sz,
1405 maxlen - sz);
1406 else
1407 DMEMIT("0 ");
1408
1409 DMEMIT("%u %u ", pg->nr_pgpaths,
1410 pg->ps.type->info_args);
1411
1412 list_for_each_entry(p, &pg->pgpaths, list) {
1413 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001414 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 p->fail_count);
1416 if (pg->ps.type->status)
1417 sz += pg->ps.type->status(&pg->ps,
1418 &p->path, type, result + sz,
1419 maxlen - sz);
1420 }
1421 }
1422 break;
1423
1424 case STATUSTYPE_TABLE:
1425 list_for_each_entry(pg, &m->priority_groups, list) {
1426 DMEMIT("%s ", pg->ps.type->name);
1427
1428 if (pg->ps.type->status)
1429 sz += pg->ps.type->status(&pg->ps, NULL, type,
1430 result + sz,
1431 maxlen - sz);
1432 else
1433 DMEMIT("0 ");
1434
1435 DMEMIT("%u %u ", pg->nr_pgpaths,
1436 pg->ps.type->table_args);
1437
1438 list_for_each_entry(p, &pg->pgpaths, list) {
1439 DMEMIT("%s ", p->path.dev->name);
1440 if (pg->ps.type->status)
1441 sz += pg->ps.type->status(&pg->ps,
1442 &p->path, type, result + sz,
1443 maxlen - sz);
1444 }
1445 }
1446 break;
1447 }
1448
1449 spin_unlock_irqrestore(&m->lock, flags);
1450
1451 return 0;
1452}
1453
1454static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1455{
Mike Anderson6380f262009-12-10 23:52:21 +00001456 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 struct dm_dev *dev;
1458 struct multipath *m = (struct multipath *) ti->private;
1459 action_fn action;
1460
Mike Anderson6380f262009-12-10 23:52:21 +00001461 mutex_lock(&m->work_mutex);
1462
Kiyoshi Uedac2f3d242009-12-10 23:52:27 +00001463 if (dm_suspended(ti)) {
1464 r = -EBUSY;
1465 goto out;
1466 }
1467
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 if (argc == 1) {
Mike Anderson6380f262009-12-10 23:52:21 +00001469 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path"))) {
1470 r = queue_if_no_path(m, 1, 0);
1471 goto out;
1472 } else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path"))) {
1473 r = queue_if_no_path(m, 0, 0);
1474 goto out;
1475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477
Mike Anderson6380f262009-12-10 23:52:21 +00001478 if (argc != 2) {
1479 DMWARN("Unrecognised multipath message received.");
1480 goto out;
1481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Mike Anderson6380f262009-12-10 23:52:21 +00001483 if (!strnicmp(argv[0], MESG_STR("disable_group"))) {
1484 r = bypass_pg_num(m, argv[1], 1);
1485 goto out;
1486 } else if (!strnicmp(argv[0], MESG_STR("enable_group"))) {
1487 r = bypass_pg_num(m, argv[1], 0);
1488 goto out;
1489 } else if (!strnicmp(argv[0], MESG_STR("switch_group"))) {
1490 r = switch_pg_num(m, argv[1]);
1491 goto out;
1492 } else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 action = reinstate_path;
1494 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1495 action = fail_path;
Mike Anderson6380f262009-12-10 23:52:21 +00001496 else {
1497 DMWARN("Unrecognised multipath message received.");
1498 goto out;
1499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1502 dm_table_get_mode(ti->table), &dev);
1503 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001504 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 argv[1]);
Mike Anderson6380f262009-12-10 23:52:21 +00001506 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 }
1508
1509 r = action_dev(m, dev, action);
1510
1511 dm_put_device(ti, dev);
1512
Mike Anderson6380f262009-12-10 23:52:21 +00001513out:
1514 mutex_unlock(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516}
1517
Al Viro647b3d02007-08-28 22:15:59 -04001518static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
Milan Broz9af4aa32006-10-03 01:15:20 -07001519 unsigned long arg)
1520{
1521 struct multipath *m = (struct multipath *) ti->private;
1522 struct block_device *bdev = NULL;
Al Viro633a08b2007-08-29 20:34:12 -04001523 fmode_t mode = 0;
Milan Broz9af4aa32006-10-03 01:15:20 -07001524 unsigned long flags;
1525 int r = 0;
1526
1527 spin_lock_irqsave(&m->lock, flags);
1528
1529 if (!m->current_pgpath)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001530 __choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001531
Milan Broze90dae12006-10-03 01:15:22 -07001532 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001533 bdev = m->current_pgpath->path.dev->bdev;
Al Viro633a08b2007-08-29 20:34:12 -04001534 mode = m->current_pgpath->path.dev->mode;
Milan Broze90dae12006-10-03 01:15:22 -07001535 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001536
1537 if (m->queue_io)
1538 r = -EAGAIN;
1539 else if (!bdev)
1540 r = -EIO;
1541
1542 spin_unlock_irqrestore(&m->lock, flags);
1543
Al Viro633a08b2007-08-29 20:34:12 -04001544 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001545}
1546
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001547static int multipath_iterate_devices(struct dm_target *ti,
1548 iterate_devices_callout_fn fn, void *data)
1549{
1550 struct multipath *m = ti->private;
1551 struct priority_group *pg;
1552 struct pgpath *p;
1553 int ret = 0;
1554
1555 list_for_each_entry(pg, &m->priority_groups, list) {
1556 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01001557 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001558 if (ret)
1559 goto out;
1560 }
1561 }
1562
1563out:
1564 return ret;
1565}
1566
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001567static int __pgpath_busy(struct pgpath *pgpath)
1568{
1569 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1570
1571 return dm_underlying_device_busy(q);
1572}
1573
1574/*
1575 * We return "busy", only when we can map I/Os but underlying devices
1576 * are busy (so even if we map I/Os now, the I/Os will wait on
1577 * the underlying queue).
1578 * In other words, if we want to kill I/Os or queue them inside us
1579 * due to map unavailability, we don't return "busy". Otherwise,
1580 * dm core won't give us the I/Os and we can't do what we want.
1581 */
1582static int multipath_busy(struct dm_target *ti)
1583{
1584 int busy = 0, has_active = 0;
1585 struct multipath *m = ti->private;
1586 struct priority_group *pg;
1587 struct pgpath *pgpath;
1588 unsigned long flags;
1589
1590 spin_lock_irqsave(&m->lock, flags);
1591
1592 /* Guess which priority_group will be used at next mapping time */
1593 if (unlikely(!m->current_pgpath && m->next_pg))
1594 pg = m->next_pg;
1595 else if (likely(m->current_pg))
1596 pg = m->current_pg;
1597 else
1598 /*
1599 * We don't know which pg will be used at next mapping time.
1600 * We don't call __choose_pgpath() here to avoid to trigger
1601 * pg_init just by busy checking.
1602 * So we don't know whether underlying devices we will be using
1603 * at next mapping time are busy or not. Just try mapping.
1604 */
1605 goto out;
1606
1607 /*
1608 * If there is one non-busy active path at least, the path selector
1609 * will be able to select it. So we consider such a pg as not busy.
1610 */
1611 busy = 1;
1612 list_for_each_entry(pgpath, &pg->pgpaths, list)
1613 if (pgpath->is_active) {
1614 has_active = 1;
1615
1616 if (!__pgpath_busy(pgpath)) {
1617 busy = 0;
1618 break;
1619 }
1620 }
1621
1622 if (!has_active)
1623 /*
1624 * No active path in this pg, so this pg won't be used and
1625 * the current_pg will be changed at next mapping time.
1626 * We need to try mapping to determine it.
1627 */
1628 busy = 0;
1629
1630out:
1631 spin_unlock_irqrestore(&m->lock, flags);
1632
1633 return busy;
1634}
1635
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636/*-----------------------------------------------------------------
1637 * Module setup
1638 *---------------------------------------------------------------*/
1639static struct target_type multipath_target = {
1640 .name = "multipath",
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001641 .version = {1, 1, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 .module = THIS_MODULE,
1643 .ctr = multipath_ctr,
1644 .dtr = multipath_dtr,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001645 .map_rq = multipath_map,
1646 .rq_end_io = multipath_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 .presuspend = multipath_presuspend,
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001648 .postsuspend = multipath_postsuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 .resume = multipath_resume,
1650 .status = multipath_status,
1651 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001652 .ioctl = multipath_ioctl,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001653 .iterate_devices = multipath_iterate_devices,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001654 .busy = multipath_busy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655};
1656
1657static int __init dm_multipath_init(void)
1658{
1659 int r;
1660
1661 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001662 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (!_mpio_cache)
1664 return -ENOMEM;
1665
1666 r = dm_register_target(&multipath_target);
1667 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001668 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 kmem_cache_destroy(_mpio_cache);
1670 return -EINVAL;
1671 }
1672
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001673 kmultipathd = create_workqueue("kmpathd");
1674 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001675 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001676 dm_unregister_target(&multipath_target);
1677 kmem_cache_destroy(_mpio_cache);
1678 return -ENOMEM;
1679 }
1680
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001681 /*
1682 * A separate workqueue is used to handle the device handlers
1683 * to avoid overloading existing workqueue. Overloading the
1684 * old workqueue would also create a bottleneck in the
1685 * path of the storage hardware device activation.
1686 */
1687 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1688 if (!kmpath_handlerd) {
1689 DMERR("failed to create workqueue kmpath_handlerd");
1690 destroy_workqueue(kmultipathd);
1691 dm_unregister_target(&multipath_target);
1692 kmem_cache_destroy(_mpio_cache);
1693 return -ENOMEM;
1694 }
1695
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001696 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 multipath_target.version[0], multipath_target.version[1],
1698 multipath_target.version[2]);
1699
1700 return r;
1701}
1702
1703static void __exit dm_multipath_exit(void)
1704{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001705 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001706 destroy_workqueue(kmultipathd);
1707
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001708 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 kmem_cache_destroy(_mpio_cache);
1710}
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712module_init(dm_multipath_init);
1713module_exit(dm_multipath_exit);
1714
1715MODULE_DESCRIPTION(DM_NAME " multipath target");
1716MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1717MODULE_LICENSE("GPL");