blob: 406091f9692b9d7094f493ec2d312aa48a6336f4 [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;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +010036 struct work_struct activate_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037};
38
39#define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
40
41/*
42 * Paths are grouped into Priority Groups and numbered from 1 upwards.
43 * Each has a path selector which controls which path gets used.
44 */
45struct priority_group {
46 struct list_head list;
47
48 struct multipath *m; /* Owning multipath instance */
49 struct path_selector ps;
50
51 unsigned pg_num; /* Reference number */
52 unsigned bypassed; /* Temporarily bypass this PG? */
53
54 unsigned nr_pgpaths; /* Number of paths in PG */
55 struct list_head pgpaths;
56};
57
58/* Multipath context */
59struct multipath {
60 struct list_head list;
61 struct dm_target *ti;
62
63 spinlock_t lock;
64
Chandra Seetharamancfae5c92008-05-01 14:50:11 -070065 const char *hw_handler_name;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -070066 char *hw_handler_params;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 unsigned nr_priority_groups;
68 struct list_head priority_groups;
69 unsigned pg_init_required; /* pg_init needs calling? */
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -070070 unsigned pg_init_in_progress; /* Only one pg_init allowed at once */
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +000071 wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119
120/*-----------------------------------------------
121 * Allocation routines
122 *-----------------------------------------------*/
123
124static struct pgpath *alloc_pgpath(void)
125{
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700126 struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Mike Anderson224cb3e2008-08-29 09:36:09 +0200128 if (pgpath) {
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100129 pgpath->is_active = 1;
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
141static struct priority_group *alloc_priority_group(void)
142{
143 struct priority_group *pg;
144
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700145 pg = kzalloc(sizeof(*pg), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700147 if (pg)
148 INIT_LIST_HEAD(&pg->pgpaths);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 return pg;
151}
152
153static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
154{
155 struct pgpath *pgpath, *tmp;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700156 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
159 list_del(&pgpath->list);
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700160 if (m->hw_handler_name)
161 scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 dm_put_device(ti, pgpath->path.dev);
163 free_pgpath(pgpath);
164 }
165}
166
167static void free_priority_group(struct priority_group *pg,
168 struct dm_target *ti)
169{
170 struct path_selector *ps = &pg->ps;
171
172 if (ps->type) {
173 ps->type->destroy(ps);
174 dm_put_path_selector(ps->type);
175 }
176
177 free_pgpaths(&pg->pgpaths, ti);
178 kfree(pg);
179}
180
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700181static struct multipath *alloc_multipath(struct dm_target *ti)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 struct multipath *m;
184
Micha³ Miros³awe69fae52006-10-03 01:15:34 -0700185 m = kzalloc(sizeof(*m), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (m) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 INIT_LIST_HEAD(&m->priority_groups);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100188 INIT_LIST_HEAD(&m->queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 spin_lock_init(&m->lock);
190 m->queue_io = 1;
David Howellsc4028952006-11-22 14:57:56 +0000191 INIT_WORK(&m->process_queued_ios, process_queued_ios);
192 INIT_WORK(&m->trigger_event, trigger_event);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000193 init_waitqueue_head(&m->pg_init_wait);
Mike Anderson6380f262009-12-10 23:52:21 +0000194 mutex_init(&m->work_mutex);
Matthew Dobson93d23412006-03-26 01:37:50 -0800195 m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (!m->mpio_pool) {
197 kfree(m);
198 return NULL;
199 }
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700200 m->ti = ti;
201 ti->private = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
204 return m;
205}
206
207static void free_multipath(struct multipath *m)
208{
209 struct priority_group *pg, *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
212 list_del(&pg->list);
213 free_priority_group(pg, m->ti);
214 }
215
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700216 kfree(m->hw_handler_name);
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700217 kfree(m->hw_handler_params);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 mempool_destroy(m->mpio_pool);
219 kfree(m);
220}
221
222
223/*-----------------------------------------------
224 * Path selection
225 *-----------------------------------------------*/
226
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000227static void __pg_init_all_paths(struct multipath *m)
228{
229 struct pgpath *pgpath;
230
231 m->pg_init_count++;
232 m->pg_init_required = 0;
233 list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
234 /* Skip failed paths */
235 if (!pgpath->is_active)
236 continue;
237 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
238 m->pg_init_in_progress++;
239 }
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
243{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 m->current_pg = pgpath->pg;
245
246 /* Must we initialise the PG first, and queue I/O till it's ready? */
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700247 if (m->hw_handler_name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 m->pg_init_required = 1;
249 m->queue_io = 1;
250 } else {
251 m->pg_init_required = 0;
252 m->queue_io = 0;
253 }
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100254
255 m->pg_init_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100258static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg,
259 size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Josef "Jeff" Sipekc922d5f2006-12-08 02:36:33 -0800261 struct dm_path *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100263 path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (!path)
265 return -ENXIO;
266
267 m->current_pgpath = path_to_pgpath(path);
268
269 if (m->current_pg != pg)
270 __switch_pg(m, m->current_pgpath);
271
272 return 0;
273}
274
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100275static void __choose_pgpath(struct multipath *m, size_t nr_bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct priority_group *pg;
278 unsigned bypassed = 1;
279
280 if (!m->nr_valid_paths)
281 goto failed;
282
283 /* Were we instructed to switch PG? */
284 if (m->next_pg) {
285 pg = m->next_pg;
286 m->next_pg = NULL;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100287 if (!__choose_path_in_pg(m, pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return;
289 }
290
291 /* Don't change PG until it has no remaining paths */
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100292 if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return;
294
295 /*
296 * Loop through priority groups until we find a valid path.
297 * First time we skip PGs marked 'bypassed'.
298 * Second time we only try the ones we skipped.
299 */
300 do {
301 list_for_each_entry(pg, &m->priority_groups, list) {
302 if (pg->bypassed == bypassed)
303 continue;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100304 if (!__choose_path_in_pg(m, pg, nr_bytes))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 return;
306 }
307 } while (bypassed--);
308
309failed:
310 m->current_pgpath = NULL;
311 m->current_pg = NULL;
312}
313
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800314/*
315 * Check whether bios must be queued in the device-mapper core rather
316 * than here in the target.
317 *
318 * m->lock must be held on entry.
319 *
320 * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
321 * same value then we are not between multipath_presuspend()
322 * and multipath_resume() calls and we have no need to check
323 * for the DMF_NOFLUSH_SUSPENDING flag.
324 */
325static int __must_push_back(struct multipath *m)
326{
327 return (m->queue_if_no_path != m->saved_queue_if_no_path &&
328 dm_noflush_suspending(m->ti));
329}
330
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100331static int map_io(struct multipath *m, struct request *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100332 struct dm_mpath_io *mpio, unsigned was_queued)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800334 int r = DM_MAPIO_REMAPPED;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100335 size_t nr_bytes = blk_rq_bytes(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 unsigned long flags;
337 struct pgpath *pgpath;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100338 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 spin_lock_irqsave(&m->lock, flags);
341
342 /* Do we need to select a new pgpath? */
343 if (!m->current_pgpath ||
344 (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100345 __choose_pgpath(m, nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 pgpath = m->current_pgpath;
348
349 if (was_queued)
350 m->queue_size--;
351
352 if ((pgpath && m->queue_io) ||
Alasdair G Kergon436d4102005-07-12 15:53:03 -0700353 (!pgpath && m->queue_if_no_path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /* Queue for the daemon to resubmit */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100355 list_add_tail(&clone->queuelist, &m->queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 m->queue_size++;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700357 if ((m->pg_init_required && !m->pg_init_in_progress) ||
358 !m->queue_io)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700359 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 pgpath = NULL;
Kiyoshi Uedad2a7ad22006-12-08 02:41:06 -0800361 r = DM_MAPIO_SUBMITTED;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100362 } else if (pgpath) {
363 bdev = pgpath->path.dev->bdev;
364 clone->q = bdev_get_queue(bdev);
365 clone->rq_disk = bdev->bd_disk;
366 } else if (__must_push_back(m))
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800367 r = DM_MAPIO_REQUEUE;
368 else
369 r = -EIO; /* Failed */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 mpio->pgpath = pgpath;
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100372 mpio->nr_bytes = nr_bytes;
373
374 if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io)
375 pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path,
376 nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 spin_unlock_irqrestore(&m->lock, flags);
379
380 return r;
381}
382
383/*
384 * If we run out of usable paths, should we queue I/O or error it?
385 */
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700386static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
387 unsigned save_old_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
389 unsigned long flags;
390
391 spin_lock_irqsave(&m->lock, flags);
392
Alasdair G Kergon485ef692005-09-27 21:45:45 -0700393 if (save_old_value)
394 m->saved_queue_if_no_path = m->queue_if_no_path;
395 else
396 m->saved_queue_if_no_path = queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 m->queue_if_no_path = queue_if_no_path;
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700398 if (!m->queue_if_no_path && m->queue_size)
Alasdair G Kergonc5573082005-05-05 16:16:07 -0700399 queue_work(kmultipathd, &m->process_queued_ios);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 spin_unlock_irqrestore(&m->lock, flags);
402
403 return 0;
404}
405
406/*-----------------------------------------------------------------
407 * The multipath daemon is responsible for resubmitting queued ios.
408 *---------------------------------------------------------------*/
409
410static void dispatch_queued_ios(struct multipath *m)
411{
412 int r;
413 unsigned long flags;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100414 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 union map_info *info;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100416 struct request *clone, *n;
417 LIST_HEAD(cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419 spin_lock_irqsave(&m->lock, flags);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100420 list_splice_init(&m->queued_ios, &cl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 spin_unlock_irqrestore(&m->lock, flags);
422
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100423 list_for_each_entry_safe(clone, n, &cl, queuelist) {
424 list_del_init(&clone->queuelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100426 info = dm_get_rq_mapinfo(clone);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 mpio = info->ptr;
428
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100429 r = map_io(m, clone, mpio, 1);
430 if (r < 0) {
431 mempool_free(mpio, m->mpio_pool);
432 dm_kill_unmapped_request(clone, r);
433 } else if (r == DM_MAPIO_REMAPPED)
434 dm_dispatch_request(clone);
435 else if (r == DM_MAPIO_REQUEUE) {
436 mempool_free(mpio, m->mpio_pool);
437 dm_requeue_unmapped_request(clone);
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440}
441
David Howellsc4028952006-11-22 14:57:56 +0000442static void process_queued_ios(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
David Howellsc4028952006-11-22 14:57:56 +0000444 struct multipath *m =
445 container_of(work, struct multipath, process_queued_ios);
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000446 struct pgpath *pgpath = NULL;
Chandra Seetharamane54f77d2009-06-22 10:12:12 +0100447 unsigned must_queue = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 unsigned long flags;
449
450 spin_lock_irqsave(&m->lock, flags);
451
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700452 if (!m->queue_size)
453 goto out;
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (!m->current_pgpath)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +0100456 __choose_pgpath(m, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 pgpath = m->current_pgpath;
459
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700460 if ((pgpath && !m->queue_io) ||
461 (!pgpath && !m->queue_if_no_path))
462 must_queue = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Kiyoshi Uedafb612642010-03-06 02:32:18 +0000464 if (m->pg_init_required && !m->pg_init_in_progress && pgpath)
465 __pg_init_all_paths(m);
466
Alasdair G Kergonc3cd4f62005-07-12 15:53:04 -0700467out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (!must_queue)
470 dispatch_queued_ios(m);
471}
472
473/*
474 * An event is triggered whenever a path is taken out of use.
475 * Includes path failure and PG bypass.
476 */
David Howellsc4028952006-11-22 14:57:56 +0000477static void trigger_event(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
David Howellsc4028952006-11-22 14:57:56 +0000479 struct multipath *m =
480 container_of(work, struct multipath, trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 dm_table_event(m->ti->table);
483}
484
485/*-----------------------------------------------------------------
486 * Constructor/argument parsing:
487 * <#multipath feature args> [<arg>]*
488 * <#hw_handler args> [hw_handler [<arg>]*]
489 * <#priority groups>
490 * <initial priority group>
491 * [<selector> <#selector args> [<arg>]*
492 * <#paths> <#per-path selector args>
493 * [<path> [<arg>]* ]+ ]+
494 *---------------------------------------------------------------*/
495struct param {
496 unsigned min;
497 unsigned max;
498 char *error;
499};
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501static int read_param(struct param *param, char *str, unsigned *v, char **error)
502{
503 if (!str ||
504 (sscanf(str, "%u", v) != 1) ||
505 (*v < param->min) ||
506 (*v > param->max)) {
507 *error = param->error;
508 return -EINVAL;
509 }
510
511 return 0;
512}
513
514struct arg_set {
515 unsigned argc;
516 char **argv;
517};
518
519static char *shift(struct arg_set *as)
520{
521 char *r;
522
523 if (as->argc) {
524 as->argc--;
525 r = *as->argv;
526 as->argv++;
527 return r;
528 }
529
530 return NULL;
531}
532
533static void consume(struct arg_set *as, unsigned n)
534{
535 BUG_ON (as->argc < n);
536 as->argc -= n;
537 as->argv += n;
538}
539
540static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
541 struct dm_target *ti)
542{
543 int r;
544 struct path_selector_type *pst;
545 unsigned ps_argc;
546
547 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700548 {0, 1024, "invalid number of path selector args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 };
550
551 pst = dm_get_path_selector(shift(as));
552 if (!pst) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700553 ti->error = "unknown path selector type";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return -EINVAL;
555 }
556
557 r = read_param(_params, shift(as), &ps_argc, &ti->error);
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100558 if (r) {
559 dm_put_path_selector(pst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 return -EINVAL;
Mikulas Patocka371b2e32008-07-21 12:00:24 +0100561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Mikulas Patocka0e0497c2009-06-22 10:08:02 +0100563 if (ps_argc > as->argc) {
564 dm_put_path_selector(pst);
565 ti->error = "not enough arguments for path selector";
566 return -EINVAL;
567 }
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 r = pst->create(&pg->ps, ps_argc, as->argv);
570 if (r) {
571 dm_put_path_selector(pst);
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700572 ti->error = "path selector constructor failed";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return r;
574 }
575
576 pg->ps.type = pst;
577 consume(as, ps_argc);
578
579 return 0;
580}
581
582static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
583 struct dm_target *ti)
584{
585 int r;
586 struct pgpath *p;
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700587 struct multipath *m = ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 /* we need at least a path arg */
590 if (as->argc < 1) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700591 ti->error = "no device given";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100592 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
595 p = alloc_pgpath();
596 if (!p)
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100597 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +0000599 r = dm_get_device(ti, shift(as), dm_table_get_mode(ti->table),
600 &p->path.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700602 ti->error = "error getting device";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 goto bad;
604 }
605
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700606 if (m->hw_handler_name) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100607 struct request_queue *q = bdev_get_queue(p->path.dev->bdev);
608
609 r = scsi_dh_attach(q, m->hw_handler_name);
610 if (r == -EBUSY) {
611 /*
612 * Already attached to different hw_handler,
613 * try to reattach with correct one.
614 */
615 scsi_dh_detach(q);
616 r = scsi_dh_attach(q, m->hw_handler_name);
617 }
618
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700619 if (r < 0) {
Hannes Reineckea0cf7ea2009-06-22 10:12:11 +0100620 ti->error = "error attaching hardware handler";
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700621 dm_put_device(ti, p->path.dev);
622 goto bad;
623 }
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700624
625 if (m->hw_handler_params) {
626 r = scsi_dh_set_params(q, m->hw_handler_params);
627 if (r < 0) {
628 ti->error = "unable to set hardware "
629 "handler parameters";
630 scsi_dh_detach(q);
631 dm_put_device(ti, p->path.dev);
632 goto bad;
633 }
634 }
Hannes Reineckeae11b1b2008-07-17 17:49:02 -0700635 }
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
638 if (r) {
639 dm_put_device(ti, p->path.dev);
640 goto bad;
641 }
642
643 return p;
644
645 bad:
646 free_pgpath(p);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100647 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648}
649
650static struct priority_group *parse_priority_group(struct arg_set *as,
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700651 struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700654 {1, 1024, "invalid number of paths"},
655 {0, 1024, "invalid number of selector args"}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 };
657
658 int r;
659 unsigned i, nr_selector_args, nr_params;
660 struct priority_group *pg;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700661 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 if (as->argc < 2) {
664 as->argc = 0;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100665 ti->error = "not enough priority group arguments";
666 return ERR_PTR(-EINVAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
669 pg = alloc_priority_group();
670 if (!pg) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700671 ti->error = "couldn't allocate priority group";
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100672 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 }
674 pg->m = m;
675
676 r = parse_path_selector(as, pg, ti);
677 if (r)
678 goto bad;
679
680 /*
681 * read the paths
682 */
683 r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
684 if (r)
685 goto bad;
686
687 r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
688 if (r)
689 goto bad;
690
691 nr_params = 1 + nr_selector_args;
692 for (i = 0; i < pg->nr_pgpaths; i++) {
693 struct pgpath *pgpath;
694 struct arg_set path_args;
695
Mikulas Patocka148acff2008-07-21 12:00:30 +0100696 if (as->argc < nr_params) {
697 ti->error = "not enough path parameters";
Alasdair G Kergon6bbf79a2010-08-12 04:13:49 +0100698 r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 goto bad;
Mikulas Patocka148acff2008-07-21 12:00:30 +0100700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 path_args.argc = nr_params;
703 path_args.argv = as->argv;
704
705 pgpath = parse_path(&path_args, &pg->ps, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100706 if (IS_ERR(pgpath)) {
707 r = PTR_ERR(pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 goto bad;
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
711 pgpath->pg = pg;
712 list_add_tail(&pgpath->list, &pg->pgpaths);
713 consume(as, nr_params);
714 }
715
716 return pg;
717
718 bad:
719 free_priority_group(pg, ti);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100720 return ERR_PTR(r);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700723static int parse_hw_handler(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 unsigned hw_argc;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700726 int ret;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700727 struct dm_target *ti = m->ti;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700730 {0, 1024, "invalid number of hardware handler args"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 };
732
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700733 if (read_param(_params, shift(as), &hw_argc, &ti->error))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 return -EINVAL;
735
736 if (!hw_argc)
737 return 0;
738
Mikulas Patockae094f4f2009-06-22 10:12:10 +0100739 if (hw_argc > as->argc) {
740 ti->error = "not enough arguments for hardware handler";
741 return -EINVAL;
742 }
743
Chandra Seetharamancfae5c92008-05-01 14:50:11 -0700744 m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
745 request_module("scsi_dh_%s", m->hw_handler_name);
746 if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700747 ti->error = "unknown hardware handler type";
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700748 ret = -EINVAL;
749 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 }
Chandra Seetharaman14e98c52008-11-13 23:39:06 +0000751
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700752 if (hw_argc > 1) {
753 char *p;
754 int i, j, len = 4;
755
756 for (i = 0; i <= hw_argc - 2; i++)
757 len += strlen(as->argv[i]) + 1;
758 p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
759 if (!p) {
760 ti->error = "memory allocation failed";
761 ret = -ENOMEM;
762 goto fail;
763 }
764 j = sprintf(p, "%d", hw_argc - 1);
765 for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
766 j = sprintf(p, "%s", as->argv[i]);
767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 consume(as, hw_argc - 1);
769
770 return 0;
Chandra Seetharaman2bfd2e12009-08-03 12:42:45 -0700771fail:
772 kfree(m->hw_handler_name);
773 m->hw_handler_name = NULL;
774 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700777static int parse_features(struct arg_set *as, struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 int r;
780 unsigned argc;
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700781 struct dm_target *ti = m->ti;
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100782 const char *param_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 static struct param _params[] = {
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100785 {0, 3, "invalid number of feature args"},
786 {1, 50, "pg_init_retries must be between 1 and 50"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 };
788
789 r = read_param(_params, shift(as), &argc, &ti->error);
790 if (r)
791 return -EINVAL;
792
793 if (!argc)
794 return 0;
795
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100796 do {
797 param_name = shift(as);
798 argc--;
799
800 if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
801 r = queue_if_no_path(m, 1, 0);
802 continue;
803 }
804
805 if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
806 (argc >= 1)) {
807 r = read_param(_params + 1, shift(as),
808 &m->pg_init_retries, &ti->error);
809 argc--;
810 continue;
811 }
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 ti->error = "Unrecognised multipath feature request";
Dave Wysochanskic9e45582007-10-19 22:47:53 +0100814 r = -EINVAL;
815 } while (argc && !r);
816
817 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820static int multipath_ctr(struct dm_target *ti, unsigned int argc,
821 char **argv)
822{
823 /* target parameters */
824 static struct param _params[] = {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700825 {1, 1024, "invalid number of priority groups"},
826 {1, 1024, "invalid initial priority group number"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 };
828
829 int r;
830 struct multipath *m;
831 struct arg_set as;
832 unsigned pg_count = 0;
833 unsigned next_pg_num;
834
835 as.argc = argc;
836 as.argv = argv;
837
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700838 m = alloc_multipath(ti);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (!m) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700840 ti->error = "can't allocate multipath";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 return -EINVAL;
842 }
843
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700844 r = parse_features(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (r)
846 goto bad;
847
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700848 r = parse_hw_handler(&as, m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (r)
850 goto bad;
851
852 r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
853 if (r)
854 goto bad;
855
856 r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
857 if (r)
858 goto bad;
859
860 /* parse the priority groups */
861 while (as.argc) {
862 struct priority_group *pg;
863
Micha³ Miros³aw28f16c22006-10-03 01:15:33 -0700864 pg = parse_priority_group(&as, m);
Benjamin Marzinski01460f32008-10-10 13:36:57 +0100865 if (IS_ERR(pg)) {
866 r = PTR_ERR(pg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 goto bad;
868 }
869
870 m->nr_valid_paths += pg->nr_pgpaths;
871 list_add_tail(&pg->list, &m->priority_groups);
872 pg_count++;
873 pg->pg_num = pg_count;
874 if (!--next_pg_num)
875 m->next_pg = pg;
876 }
877
878 if (pg_count != m->nr_priority_groups) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700879 ti->error = "priority group count mismatch";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 r = -EINVAL;
881 goto bad;
882 }
883
Mikulas Patocka86279212009-06-22 10:12:24 +0100884 ti->num_flush_requests = 1;
Mike Snitzer959eb4e2010-08-12 04:14:32 +0100885 ti->num_discard_requests = 1;
Mikulas Patocka86279212009-06-22 10:12:24 +0100886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return 0;
888
889 bad:
890 free_multipath(m);
891 return r;
892}
893
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000894static void multipath_wait_for_pg_init_completion(struct multipath *m)
895{
896 DECLARE_WAITQUEUE(wait, current);
897 unsigned long flags;
898
899 add_wait_queue(&m->pg_init_wait, &wait);
900
901 while (1) {
902 set_current_state(TASK_UNINTERRUPTIBLE);
903
904 spin_lock_irqsave(&m->lock, flags);
905 if (!m->pg_init_in_progress) {
906 spin_unlock_irqrestore(&m->lock, flags);
907 break;
908 }
909 spin_unlock_irqrestore(&m->lock, flags);
910
911 io_schedule();
912 }
913 set_current_state(TASK_RUNNING);
914
915 remove_wait_queue(&m->pg_init_wait, &wait);
916}
917
918static void flush_multipath_work(struct multipath *m)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -0700920 flush_workqueue(kmpath_handlerd);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000921 multipath_wait_for_pg_init_completion(m);
Alasdair G Kergona044d012005-07-12 15:53:02 -0700922 flush_workqueue(kmultipathd);
Mikulas Patocka53b351f2009-06-22 10:12:13 +0100923 flush_scheduled_work();
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +0000924}
925
926static void multipath_dtr(struct dm_target *ti)
927{
928 struct multipath *m = ti->private;
929
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +0000930 flush_multipath_work(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 free_multipath(m);
932}
933
934/*
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100935 * Map cloned requests
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100937static int multipath_map(struct dm_target *ti, struct request *clone,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 union map_info *map_context)
939{
940 int r;
Alasdair G Kergon028867a2007-07-12 17:26:32 +0100941 struct dm_mpath_io *mpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 struct multipath *m = (struct multipath *) ti->private;
943
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100944 mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
945 if (!mpio)
946 /* ENOMEM, requeue */
947 return DM_MAPIO_REQUEUE;
948 memset(mpio, 0, sizeof(*mpio));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 map_context->ptr = mpio;
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +0100951 clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
952 r = map_io(m, clone, mpio, 0);
Kiyoshi Ueda45e15722006-12-08 02:41:10 -0800953 if (r < 0 || r == DM_MAPIO_REQUEUE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 mempool_free(mpio, m->mpio_pool);
955
956 return r;
957}
958
959/*
960 * Take a path out of use.
961 */
962static int fail_path(struct pgpath *pgpath)
963{
964 unsigned long flags;
965 struct multipath *m = pgpath->pg->m;
966
967 spin_lock_irqsave(&m->lock, flags);
968
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100969 if (!pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 goto out;
971
Alasdair G Kergon72d94862006-06-26 00:27:35 -0700972 DMWARN("Failing path %s.", pgpath->path.dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
Kiyoshi Ueda66800732008-10-10 13:36:58 +0100975 pgpath->is_active = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 pgpath->fail_count++;
977
978 m->nr_valid_paths--;
979
980 if (pgpath == m->current_pgpath)
981 m->current_pgpath = NULL;
982
Mike Andersonb15546f2007-10-19 22:48:02 +0100983 dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
984 pgpath->path.dev->name, m->nr_valid_paths);
985
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +0000986 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988out:
989 spin_unlock_irqrestore(&m->lock, flags);
990
991 return 0;
992}
993
994/*
995 * Reinstate a previously-failed path
996 */
997static int reinstate_path(struct pgpath *pgpath)
998{
999 int r = 0;
1000 unsigned long flags;
1001 struct multipath *m = pgpath->pg->m;
1002
1003 spin_lock_irqsave(&m->lock, flags);
1004
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001005 if (pgpath->is_active)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 goto out;
1007
Alasdair G Kergondef052d2008-07-21 12:00:31 +01001008 if (!pgpath->pg->ps.type->reinstate_path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 DMWARN("Reinstate path not supported by path selector %s",
1010 pgpath->pg->ps.type->name);
1011 r = -EINVAL;
1012 goto out;
1013 }
1014
1015 r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
1016 if (r)
1017 goto out;
1018
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001019 pgpath->is_active = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001021 if (!m->nr_valid_paths++ && m->queue_size) {
1022 m->current_pgpath = NULL;
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001023 queue_work(kmultipathd, &m->process_queued_ios);
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001024 } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
1025 if (queue_work(kmpath_handlerd, &pgpath->activate_path))
1026 m->pg_init_in_progress++;
1027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Mike Andersonb15546f2007-10-19 22:48:02 +01001029 dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
1030 pgpath->path.dev->name, m->nr_valid_paths);
1031
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001032 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034out:
1035 spin_unlock_irqrestore(&m->lock, flags);
1036
1037 return r;
1038}
1039
1040/*
1041 * Fail or reinstate all paths that match the provided struct dm_dev.
1042 */
1043static int action_dev(struct multipath *m, struct dm_dev *dev,
1044 action_fn action)
1045{
1046 int r = 0;
1047 struct pgpath *pgpath;
1048 struct priority_group *pg;
1049
1050 list_for_each_entry(pg, &m->priority_groups, list) {
1051 list_for_each_entry(pgpath, &pg->pgpaths, list) {
1052 if (pgpath->path.dev == dev)
1053 r = action(pgpath);
1054 }
1055 }
1056
1057 return r;
1058}
1059
1060/*
1061 * Temporarily try to avoid having to use the specified PG
1062 */
1063static void bypass_pg(struct multipath *m, struct priority_group *pg,
1064 int bypassed)
1065{
1066 unsigned long flags;
1067
1068 spin_lock_irqsave(&m->lock, flags);
1069
1070 pg->bypassed = bypassed;
1071 m->current_pgpath = NULL;
1072 m->current_pg = NULL;
1073
1074 spin_unlock_irqrestore(&m->lock, flags);
1075
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001076 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
1079/*
1080 * Switch to using the specified PG from the next I/O that gets mapped
1081 */
1082static int switch_pg_num(struct multipath *m, const char *pgstr)
1083{
1084 struct priority_group *pg;
1085 unsigned pgnum;
1086 unsigned long flags;
1087
1088 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1089 (pgnum > m->nr_priority_groups)) {
1090 DMWARN("invalid PG number supplied to switch_pg_num");
1091 return -EINVAL;
1092 }
1093
1094 spin_lock_irqsave(&m->lock, flags);
1095 list_for_each_entry(pg, &m->priority_groups, list) {
1096 pg->bypassed = 0;
1097 if (--pgnum)
1098 continue;
1099
1100 m->current_pgpath = NULL;
1101 m->current_pg = NULL;
1102 m->next_pg = pg;
1103 }
1104 spin_unlock_irqrestore(&m->lock, flags);
1105
Alasdair G Kergonfe9cf302009-01-06 03:05:13 +00001106 schedule_work(&m->trigger_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return 0;
1108}
1109
1110/*
1111 * Set/clear bypassed status of a PG.
1112 * PGs are numbered upwards from 1 in the order they were declared.
1113 */
1114static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
1115{
1116 struct priority_group *pg;
1117 unsigned pgnum;
1118
1119 if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
1120 (pgnum > m->nr_priority_groups)) {
1121 DMWARN("invalid PG number supplied to bypass_pg");
1122 return -EINVAL;
1123 }
1124
1125 list_for_each_entry(pg, &m->priority_groups, list) {
1126 if (!--pgnum)
1127 break;
1128 }
1129
1130 bypass_pg(m, pg, bypassed);
1131 return 0;
1132}
1133
1134/*
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001135 * Should we retry pg_init immediately?
1136 */
1137static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
1138{
1139 unsigned long flags;
1140 int limit_reached = 0;
1141
1142 spin_lock_irqsave(&m->lock, flags);
1143
1144 if (m->pg_init_count <= m->pg_init_retries)
1145 m->pg_init_required = 1;
1146 else
1147 limit_reached = 1;
1148
1149 spin_unlock_irqrestore(&m->lock, flags);
1150
1151 return limit_reached;
1152}
1153
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001154static void pg_init_done(void *data, int errors)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001155{
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001156 struct pgpath *pgpath = data;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001157 struct priority_group *pg = pgpath->pg;
1158 struct multipath *m = pg->m;
1159 unsigned long flags;
1160
1161 /* device or driver problems */
1162 switch (errors) {
1163 case SCSI_DH_OK:
1164 break;
1165 case SCSI_DH_NOSYS:
1166 if (!m->hw_handler_name) {
1167 errors = 0;
1168 break;
1169 }
Moger, Babuf7b934c2010-03-06 02:29:49 +00001170 DMERR("Could not failover the device: Handler scsi_dh_%s "
1171 "Error %d.", m->hw_handler_name, errors);
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001172 /*
1173 * Fail path for now, so we do not ping pong
1174 */
1175 fail_path(pgpath);
1176 break;
1177 case SCSI_DH_DEV_TEMP_BUSY:
1178 /*
1179 * Probably doing something like FW upgrade on the
1180 * controller so try the other pg.
1181 */
1182 bypass_pg(m, pg, 1);
1183 break;
1184 /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
1185 case SCSI_DH_RETRY:
1186 case SCSI_DH_IMM_RETRY:
1187 case SCSI_DH_RES_TEMP_UNAVAIL:
1188 if (pg_init_limit_reached(m, pgpath))
1189 fail_path(pgpath);
1190 errors = 0;
1191 break;
1192 default:
1193 /*
1194 * We probably do not want to fail the path for a device
1195 * error, but this is what the old dm did. In future
1196 * patches we can do more advanced handling.
1197 */
1198 fail_path(pgpath);
1199 }
1200
1201 spin_lock_irqsave(&m->lock, flags);
1202 if (errors) {
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001203 if (pgpath == m->current_pgpath) {
1204 DMERR("Could not failover device. Error %d.", errors);
1205 m->current_pgpath = NULL;
1206 m->current_pg = NULL;
1207 }
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001208 } else if (!m->pg_init_required)
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001209 pg->bypassed = 0;
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001210
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001211 if (--m->pg_init_in_progress)
1212 /* Activations of other paths are still on going */
1213 goto out;
1214
1215 if (!m->pg_init_required)
1216 m->queue_io = 0;
1217
1218 queue_work(kmultipathd, &m->process_queued_ios);
1219
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001220 /*
1221 * Wake up any thread waiting to suspend.
1222 */
1223 wake_up(&m->pg_init_wait);
1224
Kiyoshi Uedad0259bf2010-03-06 02:30:02 +00001225out:
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001226 spin_unlock_irqrestore(&m->lock, flags);
1227}
1228
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001229static void activate_path(struct work_struct *work)
1230{
Chandra Seetharamane54f77d2009-06-22 10:12:12 +01001231 struct pgpath *pgpath =
1232 container_of(work, struct pgpath, activate_path);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001233
Chandra Seetharaman3ae31f62009-10-21 09:22:46 -07001234 scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev),
Moger, Babu83c0d5d2010-03-06 02:29:45 +00001235 pg_init_done, pgpath);
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238/*
1239 * end_io handling
1240 */
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001241static int do_end_io(struct multipath *m, struct request *clone,
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001242 int error, struct dm_mpath_io *mpio)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243{
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001244 /*
1245 * We don't queue any clone request inside the multipath target
1246 * during end I/O handling, since those clone requests don't have
1247 * bio clones. If we queue them inside the multipath target,
1248 * we need to make bio clones, that requires memory allocation.
1249 * (See drivers/md/dm.c:end_clone_bio() about why the clone requests
1250 * don't have bio clones.)
1251 * Instead of queueing the clone request here, we queue the original
1252 * request into dm core, which will remake a clone request and
1253 * clone bios for it and resubmit it later.
1254 */
1255 int r = DM_ENDIO_REQUEUE;
Stefan Bader640eb3b2005-11-21 21:32:35 -08001256 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001258 if (!error && !clone->errors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0; /* I/O complete */
1260
Alasdair G Kergonf6a80ea2005-07-12 15:53:01 -07001261 if (error == -EOPNOTSUPP)
1262 return error;
1263
Mike Snitzer959eb4e2010-08-12 04:14:32 +01001264 if (clone->cmd_flags & REQ_DISCARD)
1265 /*
1266 * Pass all discard request failures up.
1267 * FIXME: only fail_path if the discard failed due to a
1268 * transport problem. This requires precise understanding
1269 * of the underlying failure (e.g. the SCSI sense).
1270 */
1271 return error;
1272
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001273 if (mpio->pgpath)
1274 fail_path(mpio->pgpath);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Stefan Bader640eb3b2005-11-21 21:32:35 -08001276 spin_lock_irqsave(&m->lock, flags);
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001277 if (!m->nr_valid_paths && !m->queue_if_no_path && !__must_push_back(m))
1278 r = -EIO;
Stefan Bader640eb3b2005-11-21 21:32:35 -08001279 spin_unlock_irqrestore(&m->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001281 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001284static int multipath_end_io(struct dm_target *ti, struct request *clone,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 int error, union map_info *map_context)
1286{
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001287 struct multipath *m = ti->private;
1288 struct dm_mpath_io *mpio = map_context->ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 struct pgpath *pgpath = mpio->pgpath;
1290 struct path_selector *ps;
1291 int r;
1292
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001293 r = do_end_io(m, clone, error, mpio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 if (pgpath) {
1295 ps = &pgpath->pg->ps;
1296 if (ps->type->end_io)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001297 ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001299 mempool_free(mpio, m->mpio_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301 return r;
1302}
1303
1304/*
1305 * Suspend can't complete until all the I/O is processed so if
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001306 * the last path fails we must error any remaining I/O.
1307 * Note that if the freeze_bdev fails while suspending, the
1308 * queue_if_no_path state is lost - userspace should reset it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 */
1310static void multipath_presuspend(struct dm_target *ti)
1311{
1312 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Alasdair G Kergon485ef692005-09-27 21:45:45 -07001314 queue_if_no_path(m, 0, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001317static void multipath_postsuspend(struct dm_target *ti)
1318{
Mike Anderson6380f262009-12-10 23:52:21 +00001319 struct multipath *m = ti->private;
1320
1321 mutex_lock(&m->work_mutex);
Kiyoshi Ueda2bded7b2010-03-06 02:32:13 +00001322 flush_multipath_work(m);
Mike Anderson6380f262009-12-10 23:52:21 +00001323 mutex_unlock(&m->work_mutex);
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001324}
1325
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001326/*
1327 * Restore the queue_if_no_path setting.
1328 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329static void multipath_resume(struct dm_target *ti)
1330{
1331 struct multipath *m = (struct multipath *) ti->private;
1332 unsigned long flags;
1333
1334 spin_lock_irqsave(&m->lock, flags);
Alasdair G Kergon436d4102005-07-12 15:53:03 -07001335 m->queue_if_no_path = m->saved_queue_if_no_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 spin_unlock_irqrestore(&m->lock, flags);
1337}
1338
1339/*
1340 * Info output has the following format:
1341 * num_multipath_feature_args [multipath_feature_args]*
1342 * num_handler_status_args [handler_status_args]*
1343 * num_groups init_group_number
1344 * [A|D|E num_ps_status_args [ps_status_args]*
1345 * num_paths num_selector_args
1346 * [path_dev A|F fail_count [selector_args]* ]+ ]+
1347 *
1348 * Table output has the following format (identical to the constructor string):
1349 * num_feature_args [features_args]*
1350 * num_handler_args hw_handler [hw_handler_args]*
1351 * num_groups init_group_number
1352 * [priority selector-name num_ps_args [ps_args]*
1353 * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
1354 */
1355static int multipath_status(struct dm_target *ti, status_type_t type,
1356 char *result, unsigned int maxlen)
1357{
1358 int sz = 0;
1359 unsigned long flags;
1360 struct multipath *m = (struct multipath *) ti->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 struct priority_group *pg;
1362 struct pgpath *p;
1363 unsigned pg_num;
1364 char state;
1365
1366 spin_lock_irqsave(&m->lock, flags);
1367
1368 /* Features */
1369 if (type == STATUSTYPE_INFO)
Dave Wysochanskic9e45582007-10-19 22:47:53 +01001370 DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
1371 else {
1372 DMEMIT("%u ", m->queue_if_no_path +
1373 (m->pg_init_retries > 0) * 2);
1374 if (m->queue_if_no_path)
1375 DMEMIT("queue_if_no_path ");
1376 if (m->pg_init_retries)
1377 DMEMIT("pg_init_retries %u ", m->pg_init_retries);
1378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001380 if (!m->hw_handler_name || type == STATUSTYPE_INFO)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 DMEMIT("0 ");
1382 else
Chandra Seetharamancfae5c92008-05-01 14:50:11 -07001383 DMEMIT("1 %s ", m->hw_handler_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 DMEMIT("%u ", m->nr_priority_groups);
1386
1387 if (m->next_pg)
1388 pg_num = m->next_pg->pg_num;
1389 else if (m->current_pg)
1390 pg_num = m->current_pg->pg_num;
1391 else
1392 pg_num = 1;
1393
1394 DMEMIT("%u ", pg_num);
1395
1396 switch (type) {
1397 case STATUSTYPE_INFO:
1398 list_for_each_entry(pg, &m->priority_groups, list) {
1399 if (pg->bypassed)
1400 state = 'D'; /* Disabled */
1401 else if (pg == m->current_pg)
1402 state = 'A'; /* Currently Active */
1403 else
1404 state = 'E'; /* Enabled */
1405
1406 DMEMIT("%c ", state);
1407
1408 if (pg->ps.type->status)
1409 sz += pg->ps.type->status(&pg->ps, NULL, type,
1410 result + sz,
1411 maxlen - sz);
1412 else
1413 DMEMIT("0 ");
1414
1415 DMEMIT("%u %u ", pg->nr_pgpaths,
1416 pg->ps.type->info_args);
1417
1418 list_for_each_entry(p, &pg->pgpaths, list) {
1419 DMEMIT("%s %s %u ", p->path.dev->name,
Kiyoshi Ueda66800732008-10-10 13:36:58 +01001420 p->is_active ? "A" : "F",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 p->fail_count);
1422 if (pg->ps.type->status)
1423 sz += pg->ps.type->status(&pg->ps,
1424 &p->path, type, result + sz,
1425 maxlen - sz);
1426 }
1427 }
1428 break;
1429
1430 case STATUSTYPE_TABLE:
1431 list_for_each_entry(pg, &m->priority_groups, list) {
1432 DMEMIT("%s ", pg->ps.type->name);
1433
1434 if (pg->ps.type->status)
1435 sz += pg->ps.type->status(&pg->ps, NULL, type,
1436 result + sz,
1437 maxlen - sz);
1438 else
1439 DMEMIT("0 ");
1440
1441 DMEMIT("%u %u ", pg->nr_pgpaths,
1442 pg->ps.type->table_args);
1443
1444 list_for_each_entry(p, &pg->pgpaths, list) {
1445 DMEMIT("%s ", p->path.dev->name);
1446 if (pg->ps.type->status)
1447 sz += pg->ps.type->status(&pg->ps,
1448 &p->path, type, result + sz,
1449 maxlen - sz);
1450 }
1451 }
1452 break;
1453 }
1454
1455 spin_unlock_irqrestore(&m->lock, flags);
1456
1457 return 0;
1458}
1459
1460static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
1461{
Mike Anderson6380f262009-12-10 23:52:21 +00001462 int r = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 struct dm_dev *dev;
1464 struct multipath *m = (struct multipath *) ti->private;
1465 action_fn action;
1466
Mike Anderson6380f262009-12-10 23:52:21 +00001467 mutex_lock(&m->work_mutex);
1468
Kiyoshi Uedac2f3d242009-12-10 23:52:27 +00001469 if (dm_suspended(ti)) {
1470 r = -EBUSY;
1471 goto out;
1472 }
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 if (argc == 1) {
Mike Anderson6380f262009-12-10 23:52:21 +00001475 if (!strnicmp(argv[0], MESG_STR("queue_if_no_path"))) {
1476 r = queue_if_no_path(m, 1, 0);
1477 goto out;
1478 } else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path"))) {
1479 r = queue_if_no_path(m, 0, 0);
1480 goto out;
1481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
Mike Anderson6380f262009-12-10 23:52:21 +00001484 if (argc != 2) {
1485 DMWARN("Unrecognised multipath message received.");
1486 goto out;
1487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Mike Anderson6380f262009-12-10 23:52:21 +00001489 if (!strnicmp(argv[0], MESG_STR("disable_group"))) {
1490 r = bypass_pg_num(m, argv[1], 1);
1491 goto out;
1492 } else if (!strnicmp(argv[0], MESG_STR("enable_group"))) {
1493 r = bypass_pg_num(m, argv[1], 0);
1494 goto out;
1495 } else if (!strnicmp(argv[0], MESG_STR("switch_group"))) {
1496 r = switch_pg_num(m, argv[1]);
1497 goto out;
1498 } else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 action = reinstate_path;
1500 else if (!strnicmp(argv[0], MESG_STR("fail_path")))
1501 action = fail_path;
Mike Anderson6380f262009-12-10 23:52:21 +00001502 else {
1503 DMWARN("Unrecognised multipath message received.");
1504 goto out;
1505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Nikanth Karthikesan8215d6e2010-03-06 02:32:27 +00001507 r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 if (r) {
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001509 DMWARN("message: error getting device %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 argv[1]);
Mike Anderson6380f262009-12-10 23:52:21 +00001511 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
1513
1514 r = action_dev(m, dev, action);
1515
1516 dm_put_device(ti, dev);
1517
Mike Anderson6380f262009-12-10 23:52:21 +00001518out:
1519 mutex_unlock(&m->work_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521}
1522
Al Viro647b3d02007-08-28 22:15:59 -04001523static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
Milan Broz9af4aa32006-10-03 01:15:20 -07001524 unsigned long arg)
1525{
1526 struct multipath *m = (struct multipath *) ti->private;
1527 struct block_device *bdev = NULL;
Al Viro633a08b2007-08-29 20:34:12 -04001528 fmode_t mode = 0;
Milan Broz9af4aa32006-10-03 01:15:20 -07001529 unsigned long flags;
1530 int r = 0;
1531
1532 spin_lock_irqsave(&m->lock, flags);
1533
1534 if (!m->current_pgpath)
Kiyoshi Ueda02ab8232009-06-22 10:12:27 +01001535 __choose_pgpath(m, 0);
Milan Broz9af4aa32006-10-03 01:15:20 -07001536
Milan Broze90dae12006-10-03 01:15:22 -07001537 if (m->current_pgpath) {
Milan Broz9af4aa32006-10-03 01:15:20 -07001538 bdev = m->current_pgpath->path.dev->bdev;
Al Viro633a08b2007-08-29 20:34:12 -04001539 mode = m->current_pgpath->path.dev->mode;
Milan Broze90dae12006-10-03 01:15:22 -07001540 }
Milan Broz9af4aa32006-10-03 01:15:20 -07001541
1542 if (m->queue_io)
1543 r = -EAGAIN;
1544 else if (!bdev)
1545 r = -EIO;
1546
1547 spin_unlock_irqrestore(&m->lock, flags);
1548
Al Viro633a08b2007-08-29 20:34:12 -04001549 return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg);
Milan Broz9af4aa32006-10-03 01:15:20 -07001550}
1551
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001552static int multipath_iterate_devices(struct dm_target *ti,
1553 iterate_devices_callout_fn fn, void *data)
1554{
1555 struct multipath *m = ti->private;
1556 struct priority_group *pg;
1557 struct pgpath *p;
1558 int ret = 0;
1559
1560 list_for_each_entry(pg, &m->priority_groups, list) {
1561 list_for_each_entry(p, &pg->pgpaths, list) {
Mike Snitzer5dea2712009-07-23 20:30:42 +01001562 ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001563 if (ret)
1564 goto out;
1565 }
1566 }
1567
1568out:
1569 return ret;
1570}
1571
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001572static int __pgpath_busy(struct pgpath *pgpath)
1573{
1574 struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
1575
1576 return dm_underlying_device_busy(q);
1577}
1578
1579/*
1580 * We return "busy", only when we can map I/Os but underlying devices
1581 * are busy (so even if we map I/Os now, the I/Os will wait on
1582 * the underlying queue).
1583 * In other words, if we want to kill I/Os or queue them inside us
1584 * due to map unavailability, we don't return "busy". Otherwise,
1585 * dm core won't give us the I/Os and we can't do what we want.
1586 */
1587static int multipath_busy(struct dm_target *ti)
1588{
1589 int busy = 0, has_active = 0;
1590 struct multipath *m = ti->private;
1591 struct priority_group *pg;
1592 struct pgpath *pgpath;
1593 unsigned long flags;
1594
1595 spin_lock_irqsave(&m->lock, flags);
1596
1597 /* Guess which priority_group will be used at next mapping time */
1598 if (unlikely(!m->current_pgpath && m->next_pg))
1599 pg = m->next_pg;
1600 else if (likely(m->current_pg))
1601 pg = m->current_pg;
1602 else
1603 /*
1604 * We don't know which pg will be used at next mapping time.
1605 * We don't call __choose_pgpath() here to avoid to trigger
1606 * pg_init just by busy checking.
1607 * So we don't know whether underlying devices we will be using
1608 * at next mapping time are busy or not. Just try mapping.
1609 */
1610 goto out;
1611
1612 /*
1613 * If there is one non-busy active path at least, the path selector
1614 * will be able to select it. So we consider such a pg as not busy.
1615 */
1616 busy = 1;
1617 list_for_each_entry(pgpath, &pg->pgpaths, list)
1618 if (pgpath->is_active) {
1619 has_active = 1;
1620
1621 if (!__pgpath_busy(pgpath)) {
1622 busy = 0;
1623 break;
1624 }
1625 }
1626
1627 if (!has_active)
1628 /*
1629 * No active path in this pg, so this pg won't be used and
1630 * the current_pg will be changed at next mapping time.
1631 * We need to try mapping to determine it.
1632 */
1633 busy = 0;
1634
1635out:
1636 spin_unlock_irqrestore(&m->lock, flags);
1637
1638 return busy;
1639}
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641/*-----------------------------------------------------------------
1642 * Module setup
1643 *---------------------------------------------------------------*/
1644static struct target_type multipath_target = {
1645 .name = "multipath",
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001646 .version = {1, 1, 1},
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 .module = THIS_MODULE,
1648 .ctr = multipath_ctr,
1649 .dtr = multipath_dtr,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001650 .map_rq = multipath_map,
1651 .rq_end_io = multipath_end_io,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 .presuspend = multipath_presuspend,
Kiyoshi Ueda6df400a2009-12-10 23:52:19 +00001653 .postsuspend = multipath_postsuspend,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 .resume = multipath_resume,
1655 .status = multipath_status,
1656 .message = multipath_message,
Milan Broz9af4aa32006-10-03 01:15:20 -07001657 .ioctl = multipath_ioctl,
Mike Snitzeraf4874e2009-06-22 10:12:33 +01001658 .iterate_devices = multipath_iterate_devices,
Kiyoshi Uedaf40c67f2009-06-22 10:12:37 +01001659 .busy = multipath_busy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660};
1661
1662static int __init dm_multipath_init(void)
1663{
1664 int r;
1665
1666 /* allocate a slab for the dm_ios */
Alasdair G Kergon028867a2007-07-12 17:26:32 +01001667 _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 if (!_mpio_cache)
1669 return -ENOMEM;
1670
1671 r = dm_register_target(&multipath_target);
1672 if (r < 0) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001673 DMERR("register failed %d", r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 kmem_cache_destroy(_mpio_cache);
1675 return -EINVAL;
1676 }
1677
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001678 kmultipathd = create_workqueue("kmpathd");
1679 if (!kmultipathd) {
Alasdair G Kergon0cd33122007-07-12 17:27:01 +01001680 DMERR("failed to create workqueue kmpathd");
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001681 dm_unregister_target(&multipath_target);
1682 kmem_cache_destroy(_mpio_cache);
1683 return -ENOMEM;
1684 }
1685
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001686 /*
1687 * A separate workqueue is used to handle the device handlers
1688 * to avoid overloading existing workqueue. Overloading the
1689 * old workqueue would also create a bottleneck in the
1690 * path of the storage hardware device activation.
1691 */
1692 kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
1693 if (!kmpath_handlerd) {
1694 DMERR("failed to create workqueue kmpath_handlerd");
1695 destroy_workqueue(kmultipathd);
1696 dm_unregister_target(&multipath_target);
1697 kmem_cache_destroy(_mpio_cache);
1698 return -ENOMEM;
1699 }
1700
Alasdair G Kergon72d94862006-06-26 00:27:35 -07001701 DMINFO("version %u.%u.%u loaded",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 multipath_target.version[0], multipath_target.version[1],
1703 multipath_target.version[2]);
1704
1705 return r;
1706}
1707
1708static void __exit dm_multipath_exit(void)
1709{
Chandra Seetharamanbab7cfc2008-05-01 14:50:22 -07001710 destroy_workqueue(kmpath_handlerd);
Alasdair G Kergonc5573082005-05-05 16:16:07 -07001711 destroy_workqueue(kmultipathd);
1712
Mikulas Patocka10d3bd02009-01-06 03:04:58 +00001713 dm_unregister_target(&multipath_target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 kmem_cache_destroy(_mpio_cache);
1715}
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717module_init(dm_multipath_init);
1718module_exit(dm_multipath_exit);
1719
1720MODULE_DESCRIPTION(DM_NAME " multipath target");
1721MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
1722MODULE_LICENSE("GPL");