blob: 700154e2148371da991d71c6b01cd67b9b8e24cb [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 unsigned nr_valid_paths; /* Total number of usable paths */
74 struct pgpath *current_pgpath;
75 struct priority_group *current_pg;
76 struct priority_group *next_pg; /* Switch to this PG if set */
77 unsigned repeat_count; /* I/Os left before calling PS again */
78
79 unsigned queue_io; /* Must we queue all I/O? */
80 unsigned queue_if_no_path; /* Queue I/O if last path fails? */
Alasdair G Kergon436d4102005-07-12 15:53:03 -070081 unsigned saved_queue_if_no_path;/* Saved state during suspension */
Dave Wysochanskic9e45582007-10-19 22:47:53 +010082 unsigned pg_init_retries; /* Number of times to retry pg_init */
83 unsigned pg_init_count; /* Number of times pg_init called */
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 struct work_struct process_queued_ios;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +010086 struct list_head queued_ios;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned queue_size;
88
89 struct work_struct trigger_event;
90
91 /*
Alasdair G Kergon028867a2007-07-12 17:26:32 +010092 * We must use a mempool of dm_mpath_io structs so that we
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * can resubmit bios on error.
94 */
95 mempool_t *mpio_pool;
Mike Anderson6380f262009-12-10 23:52:21 +000096
97 struct mutex work_mutex;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098};
99
100/*
101 * Context information attached to each bio we process.
102 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100103struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 struct pgpath *pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100105 size_t nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106};
107
108typedef int (*action_fn) (struct pgpath *pgpath);
109
110#define MIN_IOS 256 /* Mempool size */
111
Christoph Lametere18b8902006-12-06 20:33:20 -0800112static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700114static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000115static void process_queued_ios(struct work_struct *work);
116static void trigger_event(struct work_struct *work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700117static void activate_path(struct work_struct *work);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200118static void deactivate_path(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120
121/*-----------------------------------------------
122 * Allocation routines
123 *-----------------------------------------------*/
124
125static struct pgpath *alloc_pgpath(void)
126{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700127 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Mike Anderson224cb3e2008-08-29 09:36:09 +0200129 if (pgpath) {
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100130 pgpath->is_active = 1;
Mike Anderson224cb3e2008-08-29 09:36:09 +0200131 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100132 INIT_WORK(&pgpath->activate_path, activate_path);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 return pgpath;
136}
137
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100138static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 kfree(pgpath);
141}
142
Mike Anderson224cb3e2008-08-29 09:36:09 +0200143static void deactivate_path(struct work_struct *work)
144{
145 struct pgpath *pgpath =
146 container_of(work, struct pgpath, deactivate_path);
147
148 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
149}
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static struct priority_group *alloc_priority_group(void)
152{
153 struct priority_group *pg;
154
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700155 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700157 if (pg)
158 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 return pg;
161}
162
163static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
164{
165 struct pgpath *pgpath, *tmp;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700166 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
169 list_del(&pgpath->list);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700170 if (m->hw_handler_name)
171 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 dm_put_device(ti, pgpath->path.dev);
173 free_pgpath(pgpath);
174 }
175}
176
177static void free_priority_group(struct priority_group *pg,
178 struct dm_target *ti)
179{
180 struct path_selector *ps = &pg->ps;
181
182 if (ps->type) {
183 ps->type->destroy(ps);
184 dm_put_path_selector(ps->type);
185 }
186
187 free_pgpaths(&pg->pgpaths, ti);
188 kfree(pg);
189}
190
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700191static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 struct multipath *m;
194
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700195 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 INIT_LIST_HEAD(&m->priority_groups);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100198 INIT_LIST_HEAD(&m->queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 spin_lock_init(&m->lock);
200 m->queue_io = 1;
David Howellsc4028952006-11-22 14:57:56 +0000201 INIT_WORK(&m->process_queued_ios, process_queued_ios);
202 INIT_WORK(&m->trigger_event, trigger_event);
Mike Anderson6380f262009-12-10 23:52:21 +0000203 mutex_init(&m->work_mutex);
Matthew Dobson93d23412006-03-26 01:37:50 -0800204 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (!m->mpio_pool) {
206 kfree(m);
207 return NULL;
208 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700209 m->ti = ti;
210 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
213 return m;
214}
215
216static void free_multipath(struct multipath *m)
217{
218 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
221 list_del(&pg->list);
222 free_priority_group(pg, m->ti);
223 }
224
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700225 kfree(m->hw_handler_name);
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700226 kfree(m->hw_handler_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 mempool_destroy(m->mpio_pool);
228 kfree(m);
229}
230
231
232/*-----------------------------------------------
233 * Path selection
234 *-----------------------------------------------*/
235
236static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
237{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 m->current_pg = pgpath->pg;
239
240 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700241 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 m->pg_init_required = 1;
243 m->queue_io = 1;
244 } else {
245 m->pg_init_required = 0;
246 m->queue_io = 0;
247 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100248
249 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100252static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
253 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800255 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100257 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (!path)
259 return -ENXIO;
260
261 m->current_pgpath = path_to_pgpath(path);
262
263 if (m->current_pg != pg)
264 __switch_pg(m, m->current_pgpath);
265
266 return 0;
267}
268
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100269static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 struct priority_group *pg;
272 unsigned bypassed = 1;
273
274 if (!m->nr_valid_paths)
275 goto failed;
276
277 /* Were we instructed to switch PG? */
278 if (m->next_pg) {
279 pg = m->next_pg;
280 m->next_pg = NULL;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100281 if (!__choose_path_in_pg(m, pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 return;
283 }
284
285 /* Don't change PG until it has no remaining paths */
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100286 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return;
288
289 /*
290 * Loop through priority groups until we find a valid path.
291 * First time we skip PGs marked 'bypassed'.
292 * Second time we only try the ones we skipped.
293 */
294 do {
295 list_for_each_entry(pg, &m->priority_groups, list) {
296 if (pg->bypassed == bypassed)
297 continue;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100298 if (!__choose_path_in_pg(m, pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return;
300 }
301 } while (bypassed--);
302
303failed:
304 m->current_pgpath = NULL;
305 m->current_pg = NULL;
306}
307
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800308/*
309 * Check whether bios must be queued in the device-mapper core rather
310 * than here in the target.
311 *
312 * m->lock must be held on entry.
313 *
314 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
315 * same value then we are not between multipath_presuspend()
316 * and multipath_resume() calls and we have no need to check
317 * for the DMF_NOFLUSH_SUSPENDING flag.
318 */
319static int __must_push_back(struct multipath *m)
320{
321 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
322 dm_noflush_suspending(m->ti));
323}
324
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100325static int map_io(struct multipath *m, struct request *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100326 struct dm_mpath_io *mpio, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800328 int r = DM_MAPIO_REMAPPED;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100329 size_t nr_bytes = blk_rq_bytes(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 unsigned long flags;
331 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100332 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 spin_lock_irqsave(&m->lock, flags);
335
336 /* Do we need to select a new pgpath? */
337 if (!m->current_pgpath ||
338 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100339 __choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341 pgpath = m->current_pgpath;
342
343 if (was_queued)
344 m->queue_size--;
345
346 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700347 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 /* Queue for the daemon to resubmit */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100349 list_add_tail(&clone->queuelist, &m->queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700351 if ((m->pg_init_required && !m->pg_init_in_progress) ||
352 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700353 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800355 r = DM_MAPIO_SUBMITTED;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100356 } else if (pgpath) {
357 bdev = pgpath->path.dev->bdev;
358 clone->q = bdev_get_queue(bdev);
359 clone->rq_disk = bdev->bd_disk;
360 } else if (__must_push_back(m))
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800361 r = DM_MAPIO_REQUEUE;
362 else
363 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 mpio->pgpath = pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100366 mpio->nr_bytes = nr_bytes;
367
368 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
369 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
370 nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 spin_unlock_irqrestore(&m->lock, flags);
373
374 return r;
375}
376
377/*
378 * If we run out of usable paths, should we queue I/O or error it?
379 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700380static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
381 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 unsigned long flags;
384
385 spin_lock_irqsave(&m->lock, flags);
386
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700387 if (save_old_value)
388 m->saved_queue_if_no_path = m->queue_if_no_path;
389 else
390 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700392 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700393 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
395 spin_unlock_irqrestore(&m->lock, flags);
396
397 return 0;
398}
399
400/*-----------------------------------------------------------------
401 * The multipath daemon is responsible for resubmitting queued ios.
402 *---------------------------------------------------------------*/
403
404static void dispatch_queued_ios(struct multipath *m)
405{
406 int r;
407 unsigned long flags;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100408 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 union map_info *info;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100410 struct request *clone, *n;
411 LIST_HEAD(cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 spin_lock_irqsave(&m->lock, flags);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100414 list_splice_init(&m->queued_ios, &cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 spin_unlock_irqrestore(&m->lock, flags);
416
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100417 list_for_each_entry_safe(clone, n, &cl, queuelist) {
418 list_del_init(&clone->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100420 info = dm_get_rq_mapinfo(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 mpio = info->ptr;
422
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100423 r = map_io(m, clone, mpio, 1);
424 if (r < 0) {
425 mempool_free(mpio, m->mpio_pool);
426 dm_kill_unmapped_request(clone, r);
427 } else if (r == DM_MAPIO_REMAPPED)
428 dm_dispatch_request(clone);
429 else if (r == DM_MAPIO_REQUEUE) {
430 mempool_free(mpio, m->mpio_pool);
431 dm_requeue_unmapped_request(clone);
432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434}
435
David Howellsc4028952006-11-22 14:57:56 +0000436static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
David Howellsc4028952006-11-22 14:57:56 +0000438 struct multipath *m =
439 container_of(work, struct multipath, process_queued_ios);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100440 struct pgpath *pgpath = NULL, *tmp;
441 unsigned must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 unsigned long flags;
443
444 spin_lock_irqsave(&m->lock, flags);
445
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700446 if (!m->queue_size)
447 goto out;
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (!m->current_pgpath)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100450 __choose_pgpath(m, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 pgpath = m->current_pgpath;
453
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700454 if ((pgpath && !m->queue_io) ||
455 (!pgpath && !m->queue_if_no_path))
456 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Chandra Seetharamanb81aa1c2008-11-13 23:39:00 +0000458 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100459 m->pg_init_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 m->pg_init_required = 0;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100461 list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
462 if (queue_work(kmpath_handlerd, &tmp->activate_path))
463 m->pg_init_in_progress++;
464 }
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700465 }
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700466out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!must_queue)
469 dispatch_queued_ios(m);
470}
471
472/*
473 * An event is triggered whenever a path is taken out of use.
474 * Includes path failure and PG bypass.
475 */
David Howellsc4028952006-11-22 14:57:56 +0000476static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
David Howellsc4028952006-11-22 14:57:56 +0000478 struct multipath *m =
479 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 dm_table_event(m->ti->table);
482}
483
484/*-----------------------------------------------------------------
485 * Constructor/argument parsing:
486 * <#multipath feature args> [<arg>]*
487 * <#hw_handler args> [hw_handler [<arg>]*]
488 * <#priority groups>
489 * <initial priority group>
490 * [<selector> <#selector args> [<arg>]*
491 * <#paths> <#per-path selector args>
492 * [<path> [<arg>]* ]+ ]+
493 *---------------------------------------------------------------*/
494struct param {
495 unsigned min;
496 unsigned max;
497 char *error;
498};
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500static int read_param(struct param *param, char *str, unsigned *v, char **error)
501{
502 if (!str ||
503 (sscanf(str, "%u", v) != 1) ||
504 (*v < param->min) ||
505 (*v > param->max)) {
506 *error = param->error;
507 return -EINVAL;
508 }
509
510 return 0;
511}
512
513struct arg_set {
514 unsigned argc;
515 char **argv;
516};
517
518static char *shift(struct arg_set *as)
519{
520 char *r;
521
522 if (as->argc) {
523 as->argc--;
524 r = *as->argv;
525 as->argv++;
526 return r;
527 }
528
529 return NULL;
530}
531
532static void consume(struct arg_set *as, unsigned n)
533{
534 BUG_ON (as->argc < n);
535 as->argc -= n;
536 as->argv += n;
537}
538
539static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
540 struct dm_target *ti)
541{
542 int r;
543 struct path_selector_type *pst;
544 unsigned ps_argc;
545
546 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700547 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 };
549
550 pst = dm_get_path_selector(shift(as));
551 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700552 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 return -EINVAL;
554 }
555
556 r = read_param(_params, shift(as), &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100557 if (r) {
558 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Mikulas Patocka0e0497c2009-06-22 10:08:02 +0100562 if (ps_argc > as->argc) {
563 dm_put_path_selector(pst);
564 ti->error = "not enough arguments for path selector";
565 return -EINVAL;
566 }
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 r = pst->create(&pg->ps, ps_argc, as->argv);
569 if (r) {
570 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700571 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return r;
573 }
574
575 pg->ps.type = pst;
576 consume(as, ps_argc);
577
578 return 0;
579}
580
581static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
582 struct dm_target *ti)
583{
584 int r;
585 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700586 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* we need at least a path arg */
589 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700590 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100591 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
594 p = alloc_pgpath();
595 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100596 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
599 dm_table_get_mode(ti->table), &p->path.dev);
600 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700601 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 goto bad;
603 }
604
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700605 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100606 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
607
608 r = scsi_dh_attach(q, m->hw_handler_name);
609 if (r == -EBUSY) {
610 /*
611 * Already attached to different hw_handler,
612 * try to reattach with correct one.
613 */
614 scsi_dh_detach(q);
615 r = scsi_dh_attach(q, m->hw_handler_name);
616 }
617
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700618 if (r < 0) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100619 ti->error = "error attaching hardware handler";
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700620 dm_put_device(ti, p->path.dev);
621 goto bad;
622 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700623
624 if (m->hw_handler_params) {
625 r = scsi_dh_set_params(q, m->hw_handler_params);
626 if (r < 0) {
627 ti->error = "unable to set hardware "
628 "handler parameters";
629 scsi_dh_detach(q);
630 dm_put_device(ti, p->path.dev);
631 goto bad;
632 }
633 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700634 }
635
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
637 if (r) {
638 dm_put_device(ti, p->path.dev);
639 goto bad;
640 }
641
642 return p;
643
644 bad:
645 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100646 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700650 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
652 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700653 {1, 1024, "invalid number of paths"},
654 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 };
656
657 int r;
658 unsigned i, nr_selector_args, nr_params;
659 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700660 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 if (as->argc < 2) {
663 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100664 ti->error = "not enough priority group arguments";
665 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
668 pg = alloc_priority_group();
669 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700670 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100671 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 }
673 pg->m = m;
674
675 r = parse_path_selector(as, pg, ti);
676 if (r)
677 goto bad;
678
679 /*
680 * read the paths
681 */
682 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
683 if (r)
684 goto bad;
685
686 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
687 if (r)
688 goto bad;
689
690 nr_params = 1 + nr_selector_args;
691 for (i = 0; i < pg->nr_pgpaths; i++) {
692 struct pgpath *pgpath;
693 struct arg_set path_args;
694
Mikulas Patocka148acff2008-07-21 12:00:30 +0100695 if (as->argc < nr_params) {
696 ti->error = "not enough path parameters";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100698 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
700 path_args.argc = nr_params;
701 path_args.argv = as->argv;
702
703 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100704 if (IS_ERR(pgpath)) {
705 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 pgpath->pg = pg;
710 list_add_tail(&pgpath->list, &pg->pgpaths);
711 consume(as, nr_params);
712 }
713
714 return pg;
715
716 bad:
717 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100718 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700721static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700724 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700725 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700728 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 };
730
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700731 if (read_param(_params, shift(as), &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 return -EINVAL;
733
734 if (!hw_argc)
735 return 0;
736
Mikulas Patockae094f4f2009-06-22 10:12:10 +0100737 if (hw_argc > as->argc) {
738 ti->error = "not enough arguments for hardware handler";
739 return -EINVAL;
740 }
741
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700742 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
743 request_module("scsi_dh_%s", m->hw_handler_name);
744 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700745 ti->error = "unknown hardware handler type";
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700746 ret = -EINVAL;
747 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
Chandra Seetharaman14e98c52008-11-13 23:39:06 +0000749
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700750 if (hw_argc > 1) {
751 char *p;
752 int i, j, len = 4;
753
754 for (i = 0; i <= hw_argc - 2; i++)
755 len += strlen(as->argv[i]) + 1;
756 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
757 if (!p) {
758 ti->error = "memory allocation failed";
759 ret = -ENOMEM;
760 goto fail;
761 }
762 j = sprintf(p, "%d", hw_argc - 1);
763 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
764 j = sprintf(p, "%s", as->argv[i]);
765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 consume(as, hw_argc - 1);
767
768 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700769fail:
770 kfree(m->hw_handler_name);
771 m->hw_handler_name = NULL;
772 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700775static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
777 int r;
778 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700779 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100780 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100783 {0, 3, "invalid number of feature args"},
784 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 };
786
787 r = read_param(_params, shift(as), &argc, &ti->error);
788 if (r)
789 return -EINVAL;
790
791 if (!argc)
792 return 0;
793
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100794 do {
795 param_name = shift(as);
796 argc--;
797
798 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
799 r = queue_if_no_path(m, 1, 0);
800 continue;
801 }
802
803 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
804 (argc >= 1)) {
805 r = read_param(_params + 1, shift(as),
806 &m->pg_init_retries, &ti->error);
807 argc--;
808 continue;
809 }
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100812 r = -EINVAL;
813 } while (argc && !r);
814
815 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818static int multipath_ctr(struct dm_target *ti, unsigned int argc,
819 char **argv)
820{
821 /* target parameters */
822 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700823 {1, 1024, "invalid number of priority groups"},
824 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 };
826
827 int r;
828 struct multipath *m;
829 struct arg_set as;
830 unsigned pg_count = 0;
831 unsigned next_pg_num;
832
833 as.argc = argc;
834 as.argv = argv;
835
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700836 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700838 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 return -EINVAL;
840 }
841
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700842 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 if (r)
844 goto bad;
845
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700846 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 if (r)
848 goto bad;
849
850 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
851 if (r)
852 goto bad;
853
854 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
855 if (r)
856 goto bad;
857
858 /* parse the priority groups */
859 while (as.argc) {
860 struct priority_group *pg;
861
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700862 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100863 if (IS_ERR(pg)) {
864 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 goto bad;
866 }
867
868 m->nr_valid_paths += pg->nr_pgpaths;
869 list_add_tail(&pg->list, &m->priority_groups);
870 pg_count++;
871 pg->pg_num = pg_count;
872 if (!--next_pg_num)
873 m->next_pg = pg;
874 }
875
876 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700877 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 r = -EINVAL;
879 goto bad;
880 }
881
Mikulas Patocka86279212009-06-22 10:12:24 +0100882 ti->num_flush_requests = 1;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 return 0;
885
886 bad:
887 free_multipath(m);
888 return r;
889}
890
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +0000891static void flush_multipath_work(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700893 flush_workqueue(kmpath_handlerd);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700894 flush_workqueue(kmultipathd);
Mikulas Patocka53b351f2009-06-22 10:12:13 +0100895 flush_scheduled_work();
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +0000896}
897
898static void multipath_dtr(struct dm_target *ti)
899{
900 struct multipath *m = ti->private;
901
902 flush_multipath_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 free_multipath(m);
904}
905
906/*
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100907 * Map cloned requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100909static int multipath_map(struct dm_target *ti, struct request *clone,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 union map_info *map_context)
911{
912 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100913 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 struct multipath *m = (struct multipath *) ti->private;
915
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100916 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
917 if (!mpio)
918 /* ENOMEM, requeue */
919 return DM_MAPIO_REQUEUE;
920 memset(mpio, 0, sizeof(*mpio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
922 map_context->ptr = mpio;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100923 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
924 r = map_io(m, clone, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800925 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 mempool_free(mpio, m->mpio_pool);
927
928 return r;
929}
930
931/*
932 * Take a path out of use.
933 */
934static int fail_path(struct pgpath *pgpath)
935{
936 unsigned long flags;
937 struct multipath *m = pgpath->pg->m;
938
939 spin_lock_irqsave(&m->lock, flags);
940
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100941 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 goto out;
943
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700944 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100947 pgpath->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 pgpath->fail_count++;
949
950 m->nr_valid_paths--;
951
952 if (pgpath == m->current_pgpath)
953 m->current_pgpath = NULL;
954
Mike Andersonb15546f2007-10-19 22:48:02 +0100955 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
956 pgpath->path.dev->name, m->nr_valid_paths);
957
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +0000958 schedule_work(&m->trigger_event);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200959 queue_work(kmultipathd, &pgpath->deactivate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961out:
962 spin_unlock_irqrestore(&m->lock, flags);
963
964 return 0;
965}
966
967/*
968 * Reinstate a previously-failed path
969 */
970static int reinstate_path(struct pgpath *pgpath)
971{
972 int r = 0;
973 unsigned long flags;
974 struct multipath *m = pgpath->pg->m;
975
976 spin_lock_irqsave(&m->lock, flags);
977
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100978 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 goto out;
980
Alasdair G Kergondef052d2008-07-21 12:00:31 +0100981 if (!pgpath->pg->ps.type->reinstate_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 DMWARN("Reinstate path not supported by path selector %s",
983 pgpath->pg->ps.type->name);
984 r = -EINVAL;
985 goto out;
986 }
987
988 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
989 if (r)
990 goto out;
991
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100992 pgpath->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100994 if (!m->nr_valid_paths++ && m->queue_size) {
995 m->current_pgpath = NULL;
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700996 queue_work(kmultipathd, &m->process_queued_ios);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100997 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
998 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
999 m->pg_init_in_progress++;
1000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Mike Andersonb15546f2007-10-19 22:48:02 +01001002 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1003 pgpath->path.dev->name, m->nr_valid_paths);
1004
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001005 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007out:
1008 spin_unlock_irqrestore(&m->lock, flags);
1009
1010 return r;
1011}
1012
1013/*
1014 * Fail or reinstate all paths that match the provided struct dm_dev.
1015 */
1016static int action_dev(struct multipath *m, struct dm_dev *dev,
1017 action_fn action)
1018{
1019 int r = 0;
1020 struct pgpath *pgpath;
1021 struct priority_group *pg;
1022
1023 list_for_each_entry(pg, &m->priority_groups, list) {
1024 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1025 if (pgpath->path.dev == dev)
1026 r = action(pgpath);
1027 }
1028 }
1029
1030 return r;
1031}
1032
1033/*
1034 * Temporarily try to avoid having to use the specified PG
1035 */
1036static void bypass_pg(struct multipath *m, struct priority_group *pg,
1037 int bypassed)
1038{
1039 unsigned long flags;
1040
1041 spin_lock_irqsave(&m->lock, flags);
1042
1043 pg->bypassed = bypassed;
1044 m->current_pgpath = NULL;
1045 m->current_pg = NULL;
1046
1047 spin_unlock_irqrestore(&m->lock, flags);
1048
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001049 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050}
1051
1052/*
1053 * Switch to using the specified PG from the next I/O that gets mapped
1054 */
1055static int switch_pg_num(struct multipath *m, const char *pgstr)
1056{
1057 struct priority_group *pg;
1058 unsigned pgnum;
1059 unsigned long flags;
1060
1061 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1062 (pgnum > m->nr_priority_groups)) {
1063 DMWARN("invalid PG number supplied to switch_pg_num");
1064 return -EINVAL;
1065 }
1066
1067 spin_lock_irqsave(&m->lock, flags);
1068 list_for_each_entry(pg, &m->priority_groups, list) {
1069 pg->bypassed = 0;
1070 if (--pgnum)
1071 continue;
1072
1073 m->current_pgpath = NULL;
1074 m->current_pg = NULL;
1075 m->next_pg = pg;
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 return 0;
1081}
1082
1083/*
1084 * Set/clear bypassed status of a PG.
1085 * PGs are numbered upwards from 1 in the order they were declared.
1086 */
1087static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1088{
1089 struct priority_group *pg;
1090 unsigned pgnum;
1091
1092 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1093 (pgnum > m->nr_priority_groups)) {
1094 DMWARN("invalid PG number supplied to bypass_pg");
1095 return -EINVAL;
1096 }
1097
1098 list_for_each_entry(pg, &m->priority_groups, list) {
1099 if (!--pgnum)
1100 break;
1101 }
1102
1103 bypass_pg(m, pg, bypassed);
1104 return 0;
1105}
1106
1107/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001108 * Should we retry pg_init immediately?
1109 */
1110static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1111{
1112 unsigned long flags;
1113 int limit_reached = 0;
1114
1115 spin_lock_irqsave(&m->lock, flags);
1116
1117 if (m->pg_init_count <= m->pg_init_retries)
1118 m->pg_init_required = 1;
1119 else
1120 limit_reached = 1;
1121
1122 spin_unlock_irqrestore(&m->lock, flags);
1123
1124 return limit_reached;
1125}
1126
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001127static void pg_init_done(void *data, int errors)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001128{
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001129 struct dm_path *path = data;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001130 struct pgpath *pgpath = path_to_pgpath(path);
1131 struct priority_group *pg = pgpath->pg;
1132 struct multipath *m = pg->m;
1133 unsigned long flags;
1134
1135 /* device or driver problems */
1136 switch (errors) {
1137 case SCSI_DH_OK:
1138 break;
1139 case SCSI_DH_NOSYS:
1140 if (!m->hw_handler_name) {
1141 errors = 0;
1142 break;
1143 }
1144 DMERR("Cannot failover device because scsi_dh_%s was not "
1145 "loaded.", m->hw_handler_name);
1146 /*
1147 * Fail path for now, so we do not ping pong
1148 */
1149 fail_path(pgpath);
1150 break;
1151 case SCSI_DH_DEV_TEMP_BUSY:
1152 /*
1153 * Probably doing something like FW upgrade on the
1154 * controller so try the other pg.
1155 */
1156 bypass_pg(m, pg, 1);
1157 break;
1158 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1159 case SCSI_DH_RETRY:
1160 case SCSI_DH_IMM_RETRY:
1161 case SCSI_DH_RES_TEMP_UNAVAIL:
1162 if (pg_init_limit_reached(m, pgpath))
1163 fail_path(pgpath);
1164 errors = 0;
1165 break;
1166 default:
1167 /*
1168 * We probably do not want to fail the path for a device
1169 * error, but this is what the old dm did. In future
1170 * patches we can do more advanced handling.
1171 */
1172 fail_path(pgpath);
1173 }
1174
1175 spin_lock_irqsave(&m->lock, flags);
1176 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001177 if (pgpath == m->current_pgpath) {
1178 DMERR("Could not failover device. Error %d.", errors);
1179 m->current_pgpath = NULL;
1180 m->current_pg = NULL;
1181 }
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001182 } else if (!m->pg_init_required) {
1183 m->queue_io = 0;
1184 pg->bypassed = 0;
1185 }
1186
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001187 m->pg_init_in_progress--;
1188 if (!m->pg_init_in_progress)
1189 queue_work(kmultipathd, &m->process_queued_ios);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001190 spin_unlock_irqrestore(&m->lock, flags);
1191}
1192
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001193static void activate_path(struct work_struct *work)
1194{
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001195 struct pgpath *pgpath =
1196 container_of(work, struct pgpath, activate_path);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001197
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001198 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
1199 pg_init_done, &pgpath->path);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001200}
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202/*
1203 * end_io handling
1204 */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001205static int do_end_io(struct multipath *m, struct request *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001206 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001208 /*
1209 * We don't queue any clone request inside the multipath target
1210 * during end I/O handling, since those clone requests don't have
1211 * bio clones. If we queue them inside the multipath target,
1212 * we need to make bio clones, that requires memory allocation.
1213 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1214 * don't have bio clones.)
1215 * Instead of queueing the clone request here, we queue the original
1216 * request into dm core, which will remake a clone request and
1217 * clone bios for it and resubmit it later.
1218 */
1219 int r = DM_ENDIO_REQUEUE;
Stefan Bader640eb3b2005-11-21 21:32:35 -08001220 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001222 if (!error && !clone->errors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return 0; /* I/O complete */
1224
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001225 if (error == -EOPNOTSUPP)
1226 return error;
1227
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001228 if (mpio->pgpath)
1229 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Stefan Bader640eb3b2005-11-21 21:32:35 -08001231 spin_lock_irqsave(&m->lock, flags);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001232 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1233 r = -EIO;
Stefan Bader640eb3b2005-11-21 21:32:35 -08001234 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001236 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001239static int multipath_end_io(struct dm_target *ti, struct request *clone,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 int error, union map_info *map_context)
1241{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001242 struct multipath *m = ti->private;
1243 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 struct pgpath *pgpath = mpio->pgpath;
1245 struct path_selector *ps;
1246 int r;
1247
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001248 r = do_end_io(m, clone, error, mpio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (pgpath) {
1250 ps = &pgpath->pg->ps;
1251 if (ps->type->end_io)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001252 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001254 mempool_free(mpio, m->mpio_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 return r;
1257}
1258
1259/*
1260 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001261 * the last path fails we must error any remaining I/O.
1262 * Note that if the freeze_bdev fails while suspending, the
1263 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 */
1265static void multipath_presuspend(struct dm_target *ti)
1266{
1267 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001269 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001272static void multipath_postsuspend(struct dm_target *ti)
1273{
Mike Anderson6380f262009-12-10 23:52:21 +00001274 struct multipath *m = ti->private;
1275
1276 mutex_lock(&m->work_mutex);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001277 flush_multipath_work();
Mike Anderson6380f262009-12-10 23:52:21 +00001278 mutex_unlock(&m->work_mutex);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001279}
1280
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001281/*
1282 * Restore the queue_if_no_path setting.
1283 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284static void multipath_resume(struct dm_target *ti)
1285{
1286 struct multipath *m = (struct multipath *) ti->private;
1287 unsigned long flags;
1288
1289 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001290 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 spin_unlock_irqrestore(&m->lock, flags);
1292}
1293
1294/*
1295 * Info output has the following format:
1296 * num_multipath_feature_args [multipath_feature_args]*
1297 * num_handler_status_args [handler_status_args]*
1298 * num_groups init_group_number
1299 * [A|D|E num_ps_status_args [ps_status_args]*
1300 * num_paths num_selector_args
1301 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1302 *
1303 * Table output has the following format (identical to the constructor string):
1304 * num_feature_args [features_args]*
1305 * num_handler_args hw_handler [hw_handler_args]*
1306 * num_groups init_group_number
1307 * [priority selector-name num_ps_args [ps_args]*
1308 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1309 */
1310static int multipath_status(struct dm_target *ti, status_type_t type,
1311 char *result, unsigned int maxlen)
1312{
1313 int sz = 0;
1314 unsigned long flags;
1315 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 struct priority_group *pg;
1317 struct pgpath *p;
1318 unsigned pg_num;
1319 char state;
1320
1321 spin_lock_irqsave(&m->lock, flags);
1322
1323 /* Features */
1324 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001325 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1326 else {
1327 DMEMIT("%u ", m->queue_if_no_path +
1328 (m->pg_init_retries > 0) * 2);
1329 if (m->queue_if_no_path)
1330 DMEMIT("queue_if_no_path ");
1331 if (m->pg_init_retries)
1332 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001335 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 DMEMIT("0 ");
1337 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001338 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339
1340 DMEMIT("%u ", m->nr_priority_groups);
1341
1342 if (m->next_pg)
1343 pg_num = m->next_pg->pg_num;
1344 else if (m->current_pg)
1345 pg_num = m->current_pg->pg_num;
1346 else
1347 pg_num = 1;
1348
1349 DMEMIT("%u ", pg_num);
1350
1351 switch (type) {
1352 case STATUSTYPE_INFO:
1353 list_for_each_entry(pg, &m->priority_groups, list) {
1354 if (pg->bypassed)
1355 state = 'D'; /* Disabled */
1356 else if (pg == m->current_pg)
1357 state = 'A'; /* Currently Active */
1358 else
1359 state = 'E'; /* Enabled */
1360
1361 DMEMIT("%c ", state);
1362
1363 if (pg->ps.type->status)
1364 sz += pg->ps.type->status(&pg->ps, NULL, type,
1365 result + sz,
1366 maxlen - sz);
1367 else
1368 DMEMIT("0 ");
1369
1370 DMEMIT("%u %u ", pg->nr_pgpaths,
1371 pg->ps.type->info_args);
1372
1373 list_for_each_entry(p, &pg->pgpaths, list) {
1374 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001375 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 p->fail_count);
1377 if (pg->ps.type->status)
1378 sz += pg->ps.type->status(&pg->ps,
1379 &p->path, type, result + sz,
1380 maxlen - sz);
1381 }
1382 }
1383 break;
1384
1385 case STATUSTYPE_TABLE:
1386 list_for_each_entry(pg, &m->priority_groups, list) {
1387 DMEMIT("%s ", pg->ps.type->name);
1388
1389 if (pg->ps.type->status)
1390 sz += pg->ps.type->status(&pg->ps, NULL, type,
1391 result + sz,
1392 maxlen - sz);
1393 else
1394 DMEMIT("0 ");
1395
1396 DMEMIT("%u %u ", pg->nr_pgpaths,
1397 pg->ps.type->table_args);
1398
1399 list_for_each_entry(p, &pg->pgpaths, list) {
1400 DMEMIT("%s ", p->path.dev->name);
1401 if (pg->ps.type->status)
1402 sz += pg->ps.type->status(&pg->ps,
1403 &p->path, type, result + sz,
1404 maxlen - sz);
1405 }
1406 }
1407 break;
1408 }
1409
1410 spin_unlock_irqrestore(&m->lock, flags);
1411
1412 return 0;
1413}
1414
1415static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1416{
Mike Anderson6380f262009-12-10 23:52:21 +00001417 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 struct dm_dev *dev;
1419 struct multipath *m = (struct multipath *) ti->private;
1420 action_fn action;
1421
Mike Anderson6380f262009-12-10 23:52:21 +00001422 mutex_lock(&m->work_mutex);
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (argc == 1) {
Mike Anderson6380f262009-12-10 23:52:21 +00001425 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path"))) {
1426 r = queue_if_no_path(m, 1, 0);
1427 goto out;
1428 } else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path"))) {
1429 r = queue_if_no_path(m, 0, 0);
1430 goto out;
1431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433
Mike Anderson6380f262009-12-10 23:52:21 +00001434 if (argc != 2) {
1435 DMWARN("Unrecognised multipath message received.");
1436 goto out;
1437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
Mike Anderson6380f262009-12-10 23:52:21 +00001439 if (!strnicmp(argv[0], MESG_STR("disable_group"))) {
1440 r = bypass_pg_num(m, argv[1], 1);
1441 goto out;
1442 } else if (!strnicmp(argv[0], MESG_STR("enable_group"))) {
1443 r = bypass_pg_num(m, argv[1], 0);
1444 goto out;
1445 } else if (!strnicmp(argv[0], MESG_STR("switch_group"))) {
1446 r = switch_pg_num(m, argv[1]);
1447 goto out;
1448 } else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 action = reinstate_path;
1450 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1451 action = fail_path;
Mike Anderson6380f262009-12-10 23:52:21 +00001452 else {
1453 DMWARN("Unrecognised multipath message received.");
1454 goto out;
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
1457 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1458 dm_table_get_mode(ti->table), &dev);
1459 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001460 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 argv[1]);
Mike Anderson6380f262009-12-10 23:52:21 +00001462 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 }
1464
1465 r = action_dev(m, dev, action);
1466
1467 dm_put_device(ti, dev);
1468
Mike Anderson6380f262009-12-10 23:52:21 +00001469out:
1470 mutex_unlock(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
Al Viro647b3d02007-08-28 22:15:59 -04001474static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
Milan Broz9af4aa32006-10-03 01:15:20 -07001475 unsigned long arg)
1476{
1477 struct multipath *m = (struct multipath *) ti->private;
1478 struct block_device *bdev = NULL;
Al Viro633a08b2007-08-29 20:34:12 -04001479 fmode_t mode = 0;
Milan Broz9af4aa32006-10-03 01:15:20 -07001480 unsigned long flags;
1481 int r = 0;
1482
1483 spin_lock_irqsave(&m->lock, flags);
1484
1485 if (!m->current_pgpath)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001486 __choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001487
Milan Broze90dae12006-10-03 01:15:22 -07001488 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001489 bdev = m->current_pgpath->path.dev->bdev;
Al Viro633a08b2007-08-29 20:34:12 -04001490 mode = m->current_pgpath->path.dev->mode;
Milan Broze90dae12006-10-03 01:15:22 -07001491 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001492
1493 if (m->queue_io)
1494 r = -EAGAIN;
1495 else if (!bdev)
1496 r = -EIO;
1497
1498 spin_unlock_irqrestore(&m->lock, flags);
1499
Al Viro633a08b2007-08-29 20:34:12 -04001500 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001501}
1502
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001503static int multipath_iterate_devices(struct dm_target *ti,
1504 iterate_devices_callout_fn fn, void *data)
1505{
1506 struct multipath *m = ti->private;
1507 struct priority_group *pg;
1508 struct pgpath *p;
1509 int ret = 0;
1510
1511 list_for_each_entry(pg, &m->priority_groups, list) {
1512 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01001513 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001514 if (ret)
1515 goto out;
1516 }
1517 }
1518
1519out:
1520 return ret;
1521}
1522
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001523static int __pgpath_busy(struct pgpath *pgpath)
1524{
1525 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1526
1527 return dm_underlying_device_busy(q);
1528}
1529
1530/*
1531 * We return "busy", only when we can map I/Os but underlying devices
1532 * are busy (so even if we map I/Os now, the I/Os will wait on
1533 * the underlying queue).
1534 * In other words, if we want to kill I/Os or queue them inside us
1535 * due to map unavailability, we don't return "busy". Otherwise,
1536 * dm core won't give us the I/Os and we can't do what we want.
1537 */
1538static int multipath_busy(struct dm_target *ti)
1539{
1540 int busy = 0, has_active = 0;
1541 struct multipath *m = ti->private;
1542 struct priority_group *pg;
1543 struct pgpath *pgpath;
1544 unsigned long flags;
1545
1546 spin_lock_irqsave(&m->lock, flags);
1547
1548 /* Guess which priority_group will be used at next mapping time */
1549 if (unlikely(!m->current_pgpath && m->next_pg))
1550 pg = m->next_pg;
1551 else if (likely(m->current_pg))
1552 pg = m->current_pg;
1553 else
1554 /*
1555 * We don't know which pg will be used at next mapping time.
1556 * We don't call __choose_pgpath() here to avoid to trigger
1557 * pg_init just by busy checking.
1558 * So we don't know whether underlying devices we will be using
1559 * at next mapping time are busy or not. Just try mapping.
1560 */
1561 goto out;
1562
1563 /*
1564 * If there is one non-busy active path at least, the path selector
1565 * will be able to select it. So we consider such a pg as not busy.
1566 */
1567 busy = 1;
1568 list_for_each_entry(pgpath, &pg->pgpaths, list)
1569 if (pgpath->is_active) {
1570 has_active = 1;
1571
1572 if (!__pgpath_busy(pgpath)) {
1573 busy = 0;
1574 break;
1575 }
1576 }
1577
1578 if (!has_active)
1579 /*
1580 * No active path in this pg, so this pg won't be used and
1581 * the current_pg will be changed at next mapping time.
1582 * We need to try mapping to determine it.
1583 */
1584 busy = 0;
1585
1586out:
1587 spin_unlock_irqrestore(&m->lock, flags);
1588
1589 return busy;
1590}
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592/*-----------------------------------------------------------------
1593 * Module setup
1594 *---------------------------------------------------------------*/
1595static struct target_type multipath_target = {
1596 .name = "multipath",
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001597 .version = {1, 1, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 .module = THIS_MODULE,
1599 .ctr = multipath_ctr,
1600 .dtr = multipath_dtr,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001601 .map_rq = multipath_map,
1602 .rq_end_io = multipath_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 .presuspend = multipath_presuspend,
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001604 .postsuspend = multipath_postsuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 .resume = multipath_resume,
1606 .status = multipath_status,
1607 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001608 .ioctl = multipath_ioctl,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001609 .iterate_devices = multipath_iterate_devices,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001610 .busy = multipath_busy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611};
1612
1613static int __init dm_multipath_init(void)
1614{
1615 int r;
1616
1617 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001618 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (!_mpio_cache)
1620 return -ENOMEM;
1621
1622 r = dm_register_target(&multipath_target);
1623 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001624 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 kmem_cache_destroy(_mpio_cache);
1626 return -EINVAL;
1627 }
1628
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001629 kmultipathd = create_workqueue("kmpathd");
1630 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001631 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001632 dm_unregister_target(&multipath_target);
1633 kmem_cache_destroy(_mpio_cache);
1634 return -ENOMEM;
1635 }
1636
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001637 /*
1638 * A separate workqueue is used to handle the device handlers
1639 * to avoid overloading existing workqueue. Overloading the
1640 * old workqueue would also create a bottleneck in the
1641 * path of the storage hardware device activation.
1642 */
1643 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1644 if (!kmpath_handlerd) {
1645 DMERR("failed to create workqueue kmpath_handlerd");
1646 destroy_workqueue(kmultipathd);
1647 dm_unregister_target(&multipath_target);
1648 kmem_cache_destroy(_mpio_cache);
1649 return -ENOMEM;
1650 }
1651
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001652 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 multipath_target.version[0], multipath_target.version[1],
1654 multipath_target.version[2]);
1655
1656 return r;
1657}
1658
1659static void __exit dm_multipath_exit(void)
1660{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001661 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001662 destroy_workqueue(kmultipathd);
1663
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001664 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 kmem_cache_destroy(_mpio_cache);
1666}
1667
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668module_init(dm_multipath_init);
1669module_exit(dm_multipath_exit);
1670
1671MODULE_DESCRIPTION(DM_NAME " multipath target");
1672MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1673MODULE_LICENSE("GPL");