blob: 32d0b878ecccb5ac3b7878d68adc677ebc6aa880 [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;
96};
97
98/*
99 * Context information attached to each bio we process.
100 */
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100101struct dm_mpath_io {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 struct pgpath *pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100103 size_t nr_bytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
106typedef int (*action_fn) (struct pgpath *pgpath);
107
108#define MIN_IOS 256 /* Mempool size */
109
Christoph Lametere18b8902006-12-06 20:33:20 -0800110static struct kmem_cache *_mpio_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700112static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
David Howellsc4028952006-11-22 14:57:56 +0000113static void process_queued_ios(struct work_struct *work);
114static void trigger_event(struct work_struct *work);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700115static void activate_path(struct work_struct *work);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200116static void deactivate_path(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118
119/*-----------------------------------------------
120 * Allocation routines
121 *-----------------------------------------------*/
122
123static struct pgpath *alloc_pgpath(void)
124{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700125 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Mike Anderson224cb3e2008-08-29 09:36:09 +0200127 if (pgpath) {
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100128 pgpath->is_active = 1;
Mike Anderson224cb3e2008-08-29 09:36:09 +0200129 INIT_WORK(&pgpath->deactivate_path, deactivate_path);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100130 INIT_WORK(&pgpath->activate_path, activate_path);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 return pgpath;
134}
135
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100136static void free_pgpath(struct pgpath *pgpath)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 kfree(pgpath);
139}
140
Mike Anderson224cb3e2008-08-29 09:36:09 +0200141static void deactivate_path(struct work_struct *work)
142{
143 struct pgpath *pgpath =
144 container_of(work, struct pgpath, deactivate_path);
145
146 blk_abort_queue(pgpath->path.dev->bdev->bd_disk->queue);
147}
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static struct priority_group *alloc_priority_group(void)
150{
151 struct priority_group *pg;
152
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700153 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700155 if (pg)
156 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 return pg;
159}
160
161static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
162{
163 struct pgpath *pgpath, *tmp;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700164 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
167 list_del(&pgpath->list);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700168 if (m->hw_handler_name)
169 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 dm_put_device(ti, pgpath->path.dev);
171 free_pgpath(pgpath);
172 }
173}
174
175static void free_priority_group(struct priority_group *pg,
176 struct dm_target *ti)
177{
178 struct path_selector *ps = &pg->ps;
179
180 if (ps->type) {
181 ps->type->destroy(ps);
182 dm_put_path_selector(ps->type);
183 }
184
185 free_pgpaths(&pg->pgpaths, ti);
186 kfree(pg);
187}
188
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700189static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct multipath *m;
192
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700193 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 INIT_LIST_HEAD(&m->priority_groups);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100196 INIT_LIST_HEAD(&m->queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 spin_lock_init(&m->lock);
198 m->queue_io = 1;
David Howellsc4028952006-11-22 14:57:56 +0000199 INIT_WORK(&m->process_queued_ios, process_queued_ios);
200 INIT_WORK(&m->trigger_event, trigger_event);
Matthew Dobson93d23412006-03-26 01:37:50 -0800201 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (!m->mpio_pool) {
203 kfree(m);
204 return NULL;
205 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700206 m->ti = ti;
207 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
210 return m;
211}
212
213static void free_multipath(struct multipath *m)
214{
215 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
218 list_del(&pg->list);
219 free_priority_group(pg, m->ti);
220 }
221
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700222 kfree(m->hw_handler_name);
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700223 kfree(m->hw_handler_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 mempool_destroy(m->mpio_pool);
225 kfree(m);
226}
227
228
229/*-----------------------------------------------
230 * Path selection
231 *-----------------------------------------------*/
232
233static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
234{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 m->current_pg = pgpath->pg;
236
237 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700238 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 m->pg_init_required = 1;
240 m->queue_io = 1;
241 } else {
242 m->pg_init_required = 0;
243 m->queue_io = 0;
244 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100245
246 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100249static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
250 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800252 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100254 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (!path)
256 return -ENXIO;
257
258 m->current_pgpath = path_to_pgpath(path);
259
260 if (m->current_pg != pg)
261 __switch_pg(m, m->current_pgpath);
262
263 return 0;
264}
265
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100266static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 struct priority_group *pg;
269 unsigned bypassed = 1;
270
271 if (!m->nr_valid_paths)
272 goto failed;
273
274 /* Were we instructed to switch PG? */
275 if (m->next_pg) {
276 pg = m->next_pg;
277 m->next_pg = NULL;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100278 if (!__choose_path_in_pg(m, pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return;
280 }
281
282 /* Don't change PG until it has no remaining paths */
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100283 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return;
285
286 /*
287 * Loop through priority groups until we find a valid path.
288 * First time we skip PGs marked 'bypassed'.
289 * Second time we only try the ones we skipped.
290 */
291 do {
292 list_for_each_entry(pg, &m->priority_groups, list) {
293 if (pg->bypassed == bypassed)
294 continue;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100295 if (!__choose_path_in_pg(m, pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 return;
297 }
298 } while (bypassed--);
299
300failed:
301 m->current_pgpath = NULL;
302 m->current_pg = NULL;
303}
304
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800305/*
306 * Check whether bios must be queued in the device-mapper core rather
307 * than here in the target.
308 *
309 * m->lock must be held on entry.
310 *
311 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
312 * same value then we are not between multipath_presuspend()
313 * and multipath_resume() calls and we have no need to check
314 * for the DMF_NOFLUSH_SUSPENDING flag.
315 */
316static int __must_push_back(struct multipath *m)
317{
318 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
319 dm_noflush_suspending(m->ti));
320}
321
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100322static int map_io(struct multipath *m, struct request *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100323 struct dm_mpath_io *mpio, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800325 int r = DM_MAPIO_REMAPPED;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100326 size_t nr_bytes = blk_rq_bytes(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 unsigned long flags;
328 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100329 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 spin_lock_irqsave(&m->lock, flags);
332
333 /* Do we need to select a new pgpath? */
334 if (!m->current_pgpath ||
335 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100336 __choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 pgpath = m->current_pgpath;
339
340 if (was_queued)
341 m->queue_size--;
342
343 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700344 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 /* Queue for the daemon to resubmit */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100346 list_add_tail(&clone->queuelist, &m->queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700348 if ((m->pg_init_required && !m->pg_init_in_progress) ||
349 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700350 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800352 r = DM_MAPIO_SUBMITTED;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100353 } else if (pgpath) {
354 bdev = pgpath->path.dev->bdev;
355 clone->q = bdev_get_queue(bdev);
356 clone->rq_disk = bdev->bd_disk;
357 } else if (__must_push_back(m))
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800358 r = DM_MAPIO_REQUEUE;
359 else
360 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 mpio->pgpath = pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100363 mpio->nr_bytes = nr_bytes;
364
365 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
366 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
367 nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 spin_unlock_irqrestore(&m->lock, flags);
370
371 return r;
372}
373
374/*
375 * If we run out of usable paths, should we queue I/O or error it?
376 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700377static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
378 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 unsigned long flags;
381
382 spin_lock_irqsave(&m->lock, flags);
383
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700384 if (save_old_value)
385 m->saved_queue_if_no_path = m->queue_if_no_path;
386 else
387 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700389 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700390 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 spin_unlock_irqrestore(&m->lock, flags);
393
394 return 0;
395}
396
397/*-----------------------------------------------------------------
398 * The multipath daemon is responsible for resubmitting queued ios.
399 *---------------------------------------------------------------*/
400
401static void dispatch_queued_ios(struct multipath *m)
402{
403 int r;
404 unsigned long flags;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100405 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 union map_info *info;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100407 struct request *clone, *n;
408 LIST_HEAD(cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 spin_lock_irqsave(&m->lock, flags);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100411 list_splice_init(&m->queued_ios, &cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 spin_unlock_irqrestore(&m->lock, flags);
413
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100414 list_for_each_entry_safe(clone, n, &cl, queuelist) {
415 list_del_init(&clone->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100417 info = dm_get_rq_mapinfo(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 mpio = info->ptr;
419
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100420 r = map_io(m, clone, mpio, 1);
421 if (r < 0) {
422 mempool_free(mpio, m->mpio_pool);
423 dm_kill_unmapped_request(clone, r);
424 } else if (r == DM_MAPIO_REMAPPED)
425 dm_dispatch_request(clone);
426 else if (r == DM_MAPIO_REQUEUE) {
427 mempool_free(mpio, m->mpio_pool);
428 dm_requeue_unmapped_request(clone);
429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431}
432
David Howellsc4028952006-11-22 14:57:56 +0000433static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
David Howellsc4028952006-11-22 14:57:56 +0000435 struct multipath *m =
436 container_of(work, struct multipath, process_queued_ios);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100437 struct pgpath *pgpath = NULL, *tmp;
438 unsigned must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 unsigned long flags;
440
441 spin_lock_irqsave(&m->lock, flags);
442
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700443 if (!m->queue_size)
444 goto out;
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (!m->current_pgpath)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100447 __choose_pgpath(m, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 pgpath = m->current_pgpath;
450
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700451 if ((pgpath && !m->queue_io) ||
452 (!pgpath && !m->queue_if_no_path))
453 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Chandra Seetharamanb81aa1c2008-11-13 23:39:00 +0000455 if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100456 m->pg_init_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 m->pg_init_required = 0;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100458 list_for_each_entry(tmp, &pgpath->pg->pgpaths, list) {
459 if (queue_work(kmpath_handlerd, &tmp->activate_path))
460 m->pg_init_in_progress++;
461 }
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700462 }
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700463out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (!must_queue)
466 dispatch_queued_ios(m);
467}
468
469/*
470 * An event is triggered whenever a path is taken out of use.
471 * Includes path failure and PG bypass.
472 */
David Howellsc4028952006-11-22 14:57:56 +0000473static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
David Howellsc4028952006-11-22 14:57:56 +0000475 struct multipath *m =
476 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 dm_table_event(m->ti->table);
479}
480
481/*-----------------------------------------------------------------
482 * Constructor/argument parsing:
483 * <#multipath feature args> [<arg>]*
484 * <#hw_handler args> [hw_handler [<arg>]*]
485 * <#priority groups>
486 * <initial priority group>
487 * [<selector> <#selector args> [<arg>]*
488 * <#paths> <#per-path selector args>
489 * [<path> [<arg>]* ]+ ]+
490 *---------------------------------------------------------------*/
491struct param {
492 unsigned min;
493 unsigned max;
494 char *error;
495};
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497static int read_param(struct param *param, char *str, unsigned *v, char **error)
498{
499 if (!str ||
500 (sscanf(str, "%u", v) != 1) ||
501 (*v < param->min) ||
502 (*v > param->max)) {
503 *error = param->error;
504 return -EINVAL;
505 }
506
507 return 0;
508}
509
510struct arg_set {
511 unsigned argc;
512 char **argv;
513};
514
515static char *shift(struct arg_set *as)
516{
517 char *r;
518
519 if (as->argc) {
520 as->argc--;
521 r = *as->argv;
522 as->argv++;
523 return r;
524 }
525
526 return NULL;
527}
528
529static void consume(struct arg_set *as, unsigned n)
530{
531 BUG_ON (as->argc < n);
532 as->argc -= n;
533 as->argv += n;
534}
535
536static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
537 struct dm_target *ti)
538{
539 int r;
540 struct path_selector_type *pst;
541 unsigned ps_argc;
542
543 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700544 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 };
546
547 pst = dm_get_path_selector(shift(as));
548 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700549 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return -EINVAL;
551 }
552
553 r = read_param(_params, shift(as), &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100554 if (r) {
555 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Mikulas Patocka0e0497c2009-06-22 10:08:02 +0100559 if (ps_argc > as->argc) {
560 dm_put_path_selector(pst);
561 ti->error = "not enough arguments for path selector";
562 return -EINVAL;
563 }
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 r = pst->create(&pg->ps, ps_argc, as->argv);
566 if (r) {
567 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700568 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return r;
570 }
571
572 pg->ps.type = pst;
573 consume(as, ps_argc);
574
575 return 0;
576}
577
578static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
579 struct dm_target *ti)
580{
581 int r;
582 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700583 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* we need at least a path arg */
586 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700587 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100588 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
591 p = alloc_pgpath();
592 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100593 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 r = dm_get_device(ti, shift(as), ti->begin, ti->len,
596 dm_table_get_mode(ti->table), &p->path.dev);
597 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700598 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 goto bad;
600 }
601
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700602 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100603 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
604
605 r = scsi_dh_attach(q, m->hw_handler_name);
606 if (r == -EBUSY) {
607 /*
608 * Already attached to different hw_handler,
609 * try to reattach with correct one.
610 */
611 scsi_dh_detach(q);
612 r = scsi_dh_attach(q, m->hw_handler_name);
613 }
614
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700615 if (r < 0) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100616 ti->error = "error attaching hardware handler";
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700617 dm_put_device(ti, p->path.dev);
618 goto bad;
619 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700620
621 if (m->hw_handler_params) {
622 r = scsi_dh_set_params(q, m->hw_handler_params);
623 if (r < 0) {
624 ti->error = "unable to set hardware "
625 "handler parameters";
626 scsi_dh_detach(q);
627 dm_put_device(ti, p->path.dev);
628 goto bad;
629 }
630 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700631 }
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
634 if (r) {
635 dm_put_device(ti, p->path.dev);
636 goto bad;
637 }
638
639 return p;
640
641 bad:
642 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100643 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
646static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700647 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
649 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700650 {1, 1024, "invalid number of paths"},
651 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 };
653
654 int r;
655 unsigned i, nr_selector_args, nr_params;
656 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700657 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 if (as->argc < 2) {
660 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100661 ti->error = "not enough priority group arguments";
662 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
665 pg = alloc_priority_group();
666 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700667 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100668 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670 pg->m = m;
671
672 r = parse_path_selector(as, pg, ti);
673 if (r)
674 goto bad;
675
676 /*
677 * read the paths
678 */
679 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
680 if (r)
681 goto bad;
682
683 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
684 if (r)
685 goto bad;
686
687 nr_params = 1 + nr_selector_args;
688 for (i = 0; i < pg->nr_pgpaths; i++) {
689 struct pgpath *pgpath;
690 struct arg_set path_args;
691
Mikulas Patocka148acff2008-07-21 12:00:30 +0100692 if (as->argc < nr_params) {
693 ti->error = "not enough path parameters";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 path_args.argc = nr_params;
698 path_args.argv = as->argv;
699
700 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100701 if (IS_ERR(pgpath)) {
702 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 pgpath->pg = pg;
707 list_add_tail(&pgpath->list, &pg->pgpaths);
708 consume(as, nr_params);
709 }
710
711 return pg;
712
713 bad:
714 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100715 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700718static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700721 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700722 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700725 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 };
727
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700728 if (read_param(_params, shift(as), &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return -EINVAL;
730
731 if (!hw_argc)
732 return 0;
733
Mikulas Patockae094f4f2009-06-22 10:12:10 +0100734 if (hw_argc > as->argc) {
735 ti->error = "not enough arguments for hardware handler";
736 return -EINVAL;
737 }
738
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700739 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
740 request_module("scsi_dh_%s", m->hw_handler_name);
741 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700742 ti->error = "unknown hardware handler type";
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700743 ret = -EINVAL;
744 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Chandra Seetharaman14e98c52008-11-13 23:39:06 +0000746
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700747 if (hw_argc > 1) {
748 char *p;
749 int i, j, len = 4;
750
751 for (i = 0; i <= hw_argc - 2; i++)
752 len += strlen(as->argv[i]) + 1;
753 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
754 if (!p) {
755 ti->error = "memory allocation failed";
756 ret = -ENOMEM;
757 goto fail;
758 }
759 j = sprintf(p, "%d", hw_argc - 1);
760 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
761 j = sprintf(p, "%s", as->argv[i]);
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 consume(as, hw_argc - 1);
764
765 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700766fail:
767 kfree(m->hw_handler_name);
768 m->hw_handler_name = NULL;
769 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700772static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
774 int r;
775 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700776 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100777 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100780 {0, 3, "invalid number of feature args"},
781 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 };
783
784 r = read_param(_params, shift(as), &argc, &ti->error);
785 if (r)
786 return -EINVAL;
787
788 if (!argc)
789 return 0;
790
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100791 do {
792 param_name = shift(as);
793 argc--;
794
795 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
796 r = queue_if_no_path(m, 1, 0);
797 continue;
798 }
799
800 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
801 (argc >= 1)) {
802 r = read_param(_params + 1, shift(as),
803 &m->pg_init_retries, &ti->error);
804 argc--;
805 continue;
806 }
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100809 r = -EINVAL;
810 } while (argc && !r);
811
812 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813}
814
815static int multipath_ctr(struct dm_target *ti, unsigned int argc,
816 char **argv)
817{
818 /* target parameters */
819 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700820 {1, 1024, "invalid number of priority groups"},
821 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 };
823
824 int r;
825 struct multipath *m;
826 struct arg_set as;
827 unsigned pg_count = 0;
828 unsigned next_pg_num;
829
830 as.argc = argc;
831 as.argv = argv;
832
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700833 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700835 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 return -EINVAL;
837 }
838
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700839 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 if (r)
841 goto bad;
842
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700843 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 if (r)
845 goto bad;
846
847 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
848 if (r)
849 goto bad;
850
851 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
852 if (r)
853 goto bad;
854
855 /* parse the priority groups */
856 while (as.argc) {
857 struct priority_group *pg;
858
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700859 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100860 if (IS_ERR(pg)) {
861 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 goto bad;
863 }
864
865 m->nr_valid_paths += pg->nr_pgpaths;
866 list_add_tail(&pg->list, &m->priority_groups);
867 pg_count++;
868 pg->pg_num = pg_count;
869 if (!--next_pg_num)
870 m->next_pg = pg;
871 }
872
873 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700874 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 r = -EINVAL;
876 goto bad;
877 }
878
Mikulas Patocka86279212009-06-22 10:12:24 +0100879 ti->num_flush_requests = 1;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return 0;
882
883 bad:
884 free_multipath(m);
885 return r;
886}
887
888static void multipath_dtr(struct dm_target *ti)
889{
890 struct multipath *m = (struct multipath *) ti->private;
Alasdair G Kergona044d012005-07-12 15:53:02 -0700891
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700892 flush_workqueue(kmpath_handlerd);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700893 flush_workqueue(kmultipathd);
Mikulas Patocka53b351f2009-06-22 10:12:13 +0100894 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 free_multipath(m);
896}
897
898/*
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100899 * Map cloned requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100901static int multipath_map(struct dm_target *ti, struct request *clone,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 union map_info *map_context)
903{
904 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100905 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 struct multipath *m = (struct multipath *) ti->private;
907
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100908 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
909 if (!mpio)
910 /* ENOMEM, requeue */
911 return DM_MAPIO_REQUEUE;
912 memset(mpio, 0, sizeof(*mpio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 map_context->ptr = mpio;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100915 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
916 r = map_io(m, clone, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800917 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 mempool_free(mpio, m->mpio_pool);
919
920 return r;
921}
922
923/*
924 * Take a path out of use.
925 */
926static int fail_path(struct pgpath *pgpath)
927{
928 unsigned long flags;
929 struct multipath *m = pgpath->pg->m;
930
931 spin_lock_irqsave(&m->lock, flags);
932
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100933 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 goto out;
935
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700936 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
938 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100939 pgpath->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 pgpath->fail_count++;
941
942 m->nr_valid_paths--;
943
944 if (pgpath == m->current_pgpath)
945 m->current_pgpath = NULL;
946
Mike Andersonb15546f2007-10-19 22:48:02 +0100947 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
948 pgpath->path.dev->name, m->nr_valid_paths);
949
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +0000950 schedule_work(&m->trigger_event);
Mike Anderson224cb3e2008-08-29 09:36:09 +0200951 queue_work(kmultipathd, &pgpath->deactivate_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953out:
954 spin_unlock_irqrestore(&m->lock, flags);
955
956 return 0;
957}
958
959/*
960 * Reinstate a previously-failed path
961 */
962static int reinstate_path(struct pgpath *pgpath)
963{
964 int r = 0;
965 unsigned long flags;
966 struct multipath *m = pgpath->pg->m;
967
968 spin_lock_irqsave(&m->lock, flags);
969
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100970 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 goto out;
972
Alasdair G Kergondef052d2008-07-21 12:00:31 +0100973 if (!pgpath->pg->ps.type->reinstate_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 DMWARN("Reinstate path not supported by path selector %s",
975 pgpath->pg->ps.type->name);
976 r = -EINVAL;
977 goto out;
978 }
979
980 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
981 if (r)
982 goto out;
983
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100984 pgpath->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100986 if (!m->nr_valid_paths++ && m->queue_size) {
987 m->current_pgpath = NULL;
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700988 queue_work(kmultipathd, &m->process_queued_ios);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100989 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
990 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
991 m->pg_init_in_progress++;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Mike Andersonb15546f2007-10-19 22:48:02 +0100994 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
995 pgpath->path.dev->name, m->nr_valid_paths);
996
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +0000997 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999out:
1000 spin_unlock_irqrestore(&m->lock, flags);
1001
1002 return r;
1003}
1004
1005/*
1006 * Fail or reinstate all paths that match the provided struct dm_dev.
1007 */
1008static int action_dev(struct multipath *m, struct dm_dev *dev,
1009 action_fn action)
1010{
1011 int r = 0;
1012 struct pgpath *pgpath;
1013 struct priority_group *pg;
1014
1015 list_for_each_entry(pg, &m->priority_groups, list) {
1016 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1017 if (pgpath->path.dev == dev)
1018 r = action(pgpath);
1019 }
1020 }
1021
1022 return r;
1023}
1024
1025/*
1026 * Temporarily try to avoid having to use the specified PG
1027 */
1028static void bypass_pg(struct multipath *m, struct priority_group *pg,
1029 int bypassed)
1030{
1031 unsigned long flags;
1032
1033 spin_lock_irqsave(&m->lock, flags);
1034
1035 pg->bypassed = bypassed;
1036 m->current_pgpath = NULL;
1037 m->current_pg = NULL;
1038
1039 spin_unlock_irqrestore(&m->lock, flags);
1040
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001041 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042}
1043
1044/*
1045 * Switch to using the specified PG from the next I/O that gets mapped
1046 */
1047static int switch_pg_num(struct multipath *m, const char *pgstr)
1048{
1049 struct priority_group *pg;
1050 unsigned pgnum;
1051 unsigned long flags;
1052
1053 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1054 (pgnum > m->nr_priority_groups)) {
1055 DMWARN("invalid PG number supplied to switch_pg_num");
1056 return -EINVAL;
1057 }
1058
1059 spin_lock_irqsave(&m->lock, flags);
1060 list_for_each_entry(pg, &m->priority_groups, list) {
1061 pg->bypassed = 0;
1062 if (--pgnum)
1063 continue;
1064
1065 m->current_pgpath = NULL;
1066 m->current_pg = NULL;
1067 m->next_pg = pg;
1068 }
1069 spin_unlock_irqrestore(&m->lock, flags);
1070
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001071 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 return 0;
1073}
1074
1075/*
1076 * Set/clear bypassed status of a PG.
1077 * PGs are numbered upwards from 1 in the order they were declared.
1078 */
1079static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1080{
1081 struct priority_group *pg;
1082 unsigned pgnum;
1083
1084 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1085 (pgnum > m->nr_priority_groups)) {
1086 DMWARN("invalid PG number supplied to bypass_pg");
1087 return -EINVAL;
1088 }
1089
1090 list_for_each_entry(pg, &m->priority_groups, list) {
1091 if (!--pgnum)
1092 break;
1093 }
1094
1095 bypass_pg(m, pg, bypassed);
1096 return 0;
1097}
1098
1099/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001100 * Should we retry pg_init immediately?
1101 */
1102static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1103{
1104 unsigned long flags;
1105 int limit_reached = 0;
1106
1107 spin_lock_irqsave(&m->lock, flags);
1108
1109 if (m->pg_init_count <= m->pg_init_retries)
1110 m->pg_init_required = 1;
1111 else
1112 limit_reached = 1;
1113
1114 spin_unlock_irqrestore(&m->lock, flags);
1115
1116 return limit_reached;
1117}
1118
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001119static void pg_init_done(struct dm_path *path, int errors)
1120{
1121 struct pgpath *pgpath = path_to_pgpath(path);
1122 struct priority_group *pg = pgpath->pg;
1123 struct multipath *m = pg->m;
1124 unsigned long flags;
1125
1126 /* device or driver problems */
1127 switch (errors) {
1128 case SCSI_DH_OK:
1129 break;
1130 case SCSI_DH_NOSYS:
1131 if (!m->hw_handler_name) {
1132 errors = 0;
1133 break;
1134 }
1135 DMERR("Cannot failover device because scsi_dh_%s was not "
1136 "loaded.", m->hw_handler_name);
1137 /*
1138 * Fail path for now, so we do not ping pong
1139 */
1140 fail_path(pgpath);
1141 break;
1142 case SCSI_DH_DEV_TEMP_BUSY:
1143 /*
1144 * Probably doing something like FW upgrade on the
1145 * controller so try the other pg.
1146 */
1147 bypass_pg(m, pg, 1);
1148 break;
1149 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1150 case SCSI_DH_RETRY:
1151 case SCSI_DH_IMM_RETRY:
1152 case SCSI_DH_RES_TEMP_UNAVAIL:
1153 if (pg_init_limit_reached(m, pgpath))
1154 fail_path(pgpath);
1155 errors = 0;
1156 break;
1157 default:
1158 /*
1159 * We probably do not want to fail the path for a device
1160 * error, but this is what the old dm did. In future
1161 * patches we can do more advanced handling.
1162 */
1163 fail_path(pgpath);
1164 }
1165
1166 spin_lock_irqsave(&m->lock, flags);
1167 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001168 if (pgpath == m->current_pgpath) {
1169 DMERR("Could not failover device. Error %d.", errors);
1170 m->current_pgpath = NULL;
1171 m->current_pg = NULL;
1172 }
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001173 } else if (!m->pg_init_required) {
1174 m->queue_io = 0;
1175 pg->bypassed = 0;
1176 }
1177
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001178 m->pg_init_in_progress--;
1179 if (!m->pg_init_in_progress)
1180 queue_work(kmultipathd, &m->process_queued_ios);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001181 spin_unlock_irqrestore(&m->lock, flags);
1182}
1183
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001184static void activate_path(struct work_struct *work)
1185{
1186 int ret;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001187 struct pgpath *pgpath =
1188 container_of(work, struct pgpath, activate_path);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001189
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001190 ret = scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev));
1191 pg_init_done(&pgpath->path, ret);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001192}
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194/*
1195 * end_io handling
1196 */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001197static int do_end_io(struct multipath *m, struct request *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001198 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001200 /*
1201 * We don't queue any clone request inside the multipath target
1202 * during end I/O handling, since those clone requests don't have
1203 * bio clones. If we queue them inside the multipath target,
1204 * we need to make bio clones, that requires memory allocation.
1205 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1206 * don't have bio clones.)
1207 * Instead of queueing the clone request here, we queue the original
1208 * request into dm core, which will remake a clone request and
1209 * clone bios for it and resubmit it later.
1210 */
1211 int r = DM_ENDIO_REQUEUE;
Stefan Bader640eb3b2005-11-21 21:32:35 -08001212 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001214 if (!error && !clone->errors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return 0; /* I/O complete */
1216
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001217 if (error == -EOPNOTSUPP)
1218 return error;
1219
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001220 if (mpio->pgpath)
1221 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Stefan Bader640eb3b2005-11-21 21:32:35 -08001223 spin_lock_irqsave(&m->lock, flags);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001224 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1225 r = -EIO;
Stefan Bader640eb3b2005-11-21 21:32:35 -08001226 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001228 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001231static int multipath_end_io(struct dm_target *ti, struct request *clone,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 int error, union map_info *map_context)
1233{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001234 struct multipath *m = ti->private;
1235 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 struct pgpath *pgpath = mpio->pgpath;
1237 struct path_selector *ps;
1238 int r;
1239
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001240 r = do_end_io(m, clone, error, mpio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (pgpath) {
1242 ps = &pgpath->pg->ps;
1243 if (ps->type->end_io)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001244 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001246 mempool_free(mpio, m->mpio_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 return r;
1249}
1250
1251/*
1252 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001253 * the last path fails we must error any remaining I/O.
1254 * Note that if the freeze_bdev fails while suspending, the
1255 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 */
1257static void multipath_presuspend(struct dm_target *ti)
1258{
1259 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001261 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262}
1263
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001264/*
1265 * Restore the queue_if_no_path setting.
1266 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267static void multipath_resume(struct dm_target *ti)
1268{
1269 struct multipath *m = (struct multipath *) ti->private;
1270 unsigned long flags;
1271
1272 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001273 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 spin_unlock_irqrestore(&m->lock, flags);
1275}
1276
1277/*
1278 * Info output has the following format:
1279 * num_multipath_feature_args [multipath_feature_args]*
1280 * num_handler_status_args [handler_status_args]*
1281 * num_groups init_group_number
1282 * [A|D|E num_ps_status_args [ps_status_args]*
1283 * num_paths num_selector_args
1284 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1285 *
1286 * Table output has the following format (identical to the constructor string):
1287 * num_feature_args [features_args]*
1288 * num_handler_args hw_handler [hw_handler_args]*
1289 * num_groups init_group_number
1290 * [priority selector-name num_ps_args [ps_args]*
1291 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1292 */
1293static int multipath_status(struct dm_target *ti, status_type_t type,
1294 char *result, unsigned int maxlen)
1295{
1296 int sz = 0;
1297 unsigned long flags;
1298 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 struct priority_group *pg;
1300 struct pgpath *p;
1301 unsigned pg_num;
1302 char state;
1303
1304 spin_lock_irqsave(&m->lock, flags);
1305
1306 /* Features */
1307 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001308 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1309 else {
1310 DMEMIT("%u ", m->queue_if_no_path +
1311 (m->pg_init_retries > 0) * 2);
1312 if (m->queue_if_no_path)
1313 DMEMIT("queue_if_no_path ");
1314 if (m->pg_init_retries)
1315 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001318 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 DMEMIT("0 ");
1320 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001321 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 DMEMIT("%u ", m->nr_priority_groups);
1324
1325 if (m->next_pg)
1326 pg_num = m->next_pg->pg_num;
1327 else if (m->current_pg)
1328 pg_num = m->current_pg->pg_num;
1329 else
1330 pg_num = 1;
1331
1332 DMEMIT("%u ", pg_num);
1333
1334 switch (type) {
1335 case STATUSTYPE_INFO:
1336 list_for_each_entry(pg, &m->priority_groups, list) {
1337 if (pg->bypassed)
1338 state = 'D'; /* Disabled */
1339 else if (pg == m->current_pg)
1340 state = 'A'; /* Currently Active */
1341 else
1342 state = 'E'; /* Enabled */
1343
1344 DMEMIT("%c ", state);
1345
1346 if (pg->ps.type->status)
1347 sz += pg->ps.type->status(&pg->ps, NULL, type,
1348 result + sz,
1349 maxlen - sz);
1350 else
1351 DMEMIT("0 ");
1352
1353 DMEMIT("%u %u ", pg->nr_pgpaths,
1354 pg->ps.type->info_args);
1355
1356 list_for_each_entry(p, &pg->pgpaths, list) {
1357 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001358 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 p->fail_count);
1360 if (pg->ps.type->status)
1361 sz += pg->ps.type->status(&pg->ps,
1362 &p->path, type, result + sz,
1363 maxlen - sz);
1364 }
1365 }
1366 break;
1367
1368 case STATUSTYPE_TABLE:
1369 list_for_each_entry(pg, &m->priority_groups, list) {
1370 DMEMIT("%s ", pg->ps.type->name);
1371
1372 if (pg->ps.type->status)
1373 sz += pg->ps.type->status(&pg->ps, NULL, type,
1374 result + sz,
1375 maxlen - sz);
1376 else
1377 DMEMIT("0 ");
1378
1379 DMEMIT("%u %u ", pg->nr_pgpaths,
1380 pg->ps.type->table_args);
1381
1382 list_for_each_entry(p, &pg->pgpaths, list) {
1383 DMEMIT("%s ", p->path.dev->name);
1384 if (pg->ps.type->status)
1385 sz += pg->ps.type->status(&pg->ps,
1386 &p->path, type, result + sz,
1387 maxlen - sz);
1388 }
1389 }
1390 break;
1391 }
1392
1393 spin_unlock_irqrestore(&m->lock, flags);
1394
1395 return 0;
1396}
1397
1398static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1399{
1400 int r;
1401 struct dm_dev *dev;
1402 struct multipath *m = (struct multipath *) ti->private;
1403 action_fn action;
1404
1405 if (argc == 1) {
1406 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001407 return queue_if_no_path(m, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001409 return queue_if_no_path(m, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411
1412 if (argc != 2)
1413 goto error;
1414
1415 if (!strnicmp(argv[0], MESG_STR("disable_group")))
1416 return bypass_pg_num(m, argv[1], 1);
1417 else if (!strnicmp(argv[0], MESG_STR("enable_group")))
1418 return bypass_pg_num(m, argv[1], 0);
1419 else if (!strnicmp(argv[0], MESG_STR("switch_group")))
1420 return switch_pg_num(m, argv[1]);
1421 else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
1422 action = reinstate_path;
1423 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1424 action = fail_path;
1425 else
1426 goto error;
1427
1428 r = dm_get_device(ti, argv[1], ti->begin, ti->len,
1429 dm_table_get_mode(ti->table), &dev);
1430 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001431 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 argv[1]);
1433 return -EINVAL;
1434 }
1435
1436 r = action_dev(m, dev, action);
1437
1438 dm_put_device(ti, dev);
1439
1440 return r;
1441
1442error:
1443 DMWARN("Unrecognised multipath message received.");
1444 return -EINVAL;
1445}
1446
Al Viro647b3d02007-08-28 22:15:59 -04001447static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
Milan Broz9af4aa32006-10-03 01:15:20 -07001448 unsigned long arg)
1449{
1450 struct multipath *m = (struct multipath *) ti->private;
1451 struct block_device *bdev = NULL;
Al Viro633a08b2007-08-29 20:34:12 -04001452 fmode_t mode = 0;
Milan Broz9af4aa32006-10-03 01:15:20 -07001453 unsigned long flags;
1454 int r = 0;
1455
1456 spin_lock_irqsave(&m->lock, flags);
1457
1458 if (!m->current_pgpath)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001459 __choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001460
Milan Broze90dae12006-10-03 01:15:22 -07001461 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001462 bdev = m->current_pgpath->path.dev->bdev;
Al Viro633a08b2007-08-29 20:34:12 -04001463 mode = m->current_pgpath->path.dev->mode;
Milan Broze90dae12006-10-03 01:15:22 -07001464 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001465
1466 if (m->queue_io)
1467 r = -EAGAIN;
1468 else if (!bdev)
1469 r = -EIO;
1470
1471 spin_unlock_irqrestore(&m->lock, flags);
1472
Al Viro633a08b2007-08-29 20:34:12 -04001473 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001474}
1475
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001476static int multipath_iterate_devices(struct dm_target *ti,
1477 iterate_devices_callout_fn fn, void *data)
1478{
1479 struct multipath *m = ti->private;
1480 struct priority_group *pg;
1481 struct pgpath *p;
1482 int ret = 0;
1483
1484 list_for_each_entry(pg, &m->priority_groups, list) {
1485 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01001486 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001487 if (ret)
1488 goto out;
1489 }
1490 }
1491
1492out:
1493 return ret;
1494}
1495
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001496static int __pgpath_busy(struct pgpath *pgpath)
1497{
1498 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1499
1500 return dm_underlying_device_busy(q);
1501}
1502
1503/*
1504 * We return "busy", only when we can map I/Os but underlying devices
1505 * are busy (so even if we map I/Os now, the I/Os will wait on
1506 * the underlying queue).
1507 * In other words, if we want to kill I/Os or queue them inside us
1508 * due to map unavailability, we don't return "busy". Otherwise,
1509 * dm core won't give us the I/Os and we can't do what we want.
1510 */
1511static int multipath_busy(struct dm_target *ti)
1512{
1513 int busy = 0, has_active = 0;
1514 struct multipath *m = ti->private;
1515 struct priority_group *pg;
1516 struct pgpath *pgpath;
1517 unsigned long flags;
1518
1519 spin_lock_irqsave(&m->lock, flags);
1520
1521 /* Guess which priority_group will be used at next mapping time */
1522 if (unlikely(!m->current_pgpath && m->next_pg))
1523 pg = m->next_pg;
1524 else if (likely(m->current_pg))
1525 pg = m->current_pg;
1526 else
1527 /*
1528 * We don't know which pg will be used at next mapping time.
1529 * We don't call __choose_pgpath() here to avoid to trigger
1530 * pg_init just by busy checking.
1531 * So we don't know whether underlying devices we will be using
1532 * at next mapping time are busy or not. Just try mapping.
1533 */
1534 goto out;
1535
1536 /*
1537 * If there is one non-busy active path at least, the path selector
1538 * will be able to select it. So we consider such a pg as not busy.
1539 */
1540 busy = 1;
1541 list_for_each_entry(pgpath, &pg->pgpaths, list)
1542 if (pgpath->is_active) {
1543 has_active = 1;
1544
1545 if (!__pgpath_busy(pgpath)) {
1546 busy = 0;
1547 break;
1548 }
1549 }
1550
1551 if (!has_active)
1552 /*
1553 * No active path in this pg, so this pg won't be used and
1554 * the current_pg will be changed at next mapping time.
1555 * We need to try mapping to determine it.
1556 */
1557 busy = 0;
1558
1559out:
1560 spin_unlock_irqrestore(&m->lock, flags);
1561
1562 return busy;
1563}
1564
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565/*-----------------------------------------------------------------
1566 * Module setup
1567 *---------------------------------------------------------------*/
1568static struct target_type multipath_target = {
1569 .name = "multipath",
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001570 .version = {1, 1, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 .module = THIS_MODULE,
1572 .ctr = multipath_ctr,
1573 .dtr = multipath_dtr,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001574 .map_rq = multipath_map,
1575 .rq_end_io = multipath_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 .presuspend = multipath_presuspend,
1577 .resume = multipath_resume,
1578 .status = multipath_status,
1579 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001580 .ioctl = multipath_ioctl,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001581 .iterate_devices = multipath_iterate_devices,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001582 .busy = multipath_busy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583};
1584
1585static int __init dm_multipath_init(void)
1586{
1587 int r;
1588
1589 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001590 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 if (!_mpio_cache)
1592 return -ENOMEM;
1593
1594 r = dm_register_target(&multipath_target);
1595 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001596 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 kmem_cache_destroy(_mpio_cache);
1598 return -EINVAL;
1599 }
1600
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001601 kmultipathd = create_workqueue("kmpathd");
1602 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001603 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001604 dm_unregister_target(&multipath_target);
1605 kmem_cache_destroy(_mpio_cache);
1606 return -ENOMEM;
1607 }
1608
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001609 /*
1610 * A separate workqueue is used to handle the device handlers
1611 * to avoid overloading existing workqueue. Overloading the
1612 * old workqueue would also create a bottleneck in the
1613 * path of the storage hardware device activation.
1614 */
1615 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1616 if (!kmpath_handlerd) {
1617 DMERR("failed to create workqueue kmpath_handlerd");
1618 destroy_workqueue(kmultipathd);
1619 dm_unregister_target(&multipath_target);
1620 kmem_cache_destroy(_mpio_cache);
1621 return -ENOMEM;
1622 }
1623
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001624 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 multipath_target.version[0], multipath_target.version[1],
1626 multipath_target.version[2]);
1627
1628 return r;
1629}
1630
1631static void __exit dm_multipath_exit(void)
1632{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001633 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001634 destroy_workqueue(kmultipathd);
1635
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001636 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 kmem_cache_destroy(_mpio_cache);
1638}
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640module_init(dm_multipath_init);
1641module_exit(dm_multipath_exit);
1642
1643MODULE_DESCRIPTION(DM_NAME " multipath target");
1644MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1645MODULE_LICENSE("GPL");