]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | ||
586e80e6 MP |
8 | #include <linux/device-mapper.h> |
9 | ||
f4790826 | 10 | #include "dm.h" |
1da177e4 | 11 | #include "dm-path-selector.h" |
b15546f9 | 12 | #include "dm-uevent.h" |
1da177e4 | 13 | |
e5863d9a | 14 | #include <linux/blkdev.h> |
1da177e4 LT |
15 | #include <linux/ctype.h> |
16 | #include <linux/init.h> | |
17 | #include <linux/mempool.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/pagemap.h> | |
20 | #include <linux/slab.h> | |
21 | #include <linux/time.h> | |
22 | #include <linux/workqueue.h> | |
35991652 | 23 | #include <linux/delay.h> |
cfae5c9b | 24 | #include <scsi/scsi_dh.h> |
60063497 | 25 | #include <linux/atomic.h> |
78ce23b5 | 26 | #include <linux/blk-mq.h> |
1da177e4 | 27 | |
72d94861 | 28 | #define DM_MSG_PREFIX "multipath" |
4e2d19e4 CS |
29 | #define DM_PG_INIT_DELAY_MSECS 2000 |
30 | #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1) | |
1da177e4 LT |
31 | |
32 | /* Path properties */ | |
33 | struct pgpath { | |
34 | struct list_head list; | |
35 | ||
36 | struct priority_group *pg; /* Owning PG */ | |
37 | unsigned fail_count; /* Cumulative failure count */ | |
38 | ||
c922d5f7 | 39 | struct dm_path path; |
4e2d19e4 | 40 | struct delayed_work activate_path; |
be7d31cc MS |
41 | |
42 | bool is_active:1; /* Path status */ | |
1da177e4 LT |
43 | }; |
44 | ||
45 | #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path) | |
46 | ||
47 | /* | |
48 | * Paths are grouped into Priority Groups and numbered from 1 upwards. | |
49 | * Each has a path selector which controls which path gets used. | |
50 | */ | |
51 | struct priority_group { | |
52 | struct list_head list; | |
53 | ||
54 | struct multipath *m; /* Owning multipath instance */ | |
55 | struct path_selector ps; | |
56 | ||
57 | unsigned pg_num; /* Reference number */ | |
1da177e4 LT |
58 | unsigned nr_pgpaths; /* Number of paths in PG */ |
59 | struct list_head pgpaths; | |
be7d31cc MS |
60 | |
61 | bool bypassed:1; /* Temporarily bypass this PG? */ | |
1da177e4 LT |
62 | }; |
63 | ||
64 | /* Multipath context */ | |
65 | struct multipath { | |
66 | struct list_head list; | |
67 | struct dm_target *ti; | |
68 | ||
cfae5c9b | 69 | const char *hw_handler_name; |
2bfd2e13 | 70 | char *hw_handler_params; |
4e2d19e4 | 71 | |
1fbdd2b3 MS |
72 | spinlock_t lock; |
73 | ||
1da177e4 LT |
74 | unsigned nr_priority_groups; |
75 | struct list_head priority_groups; | |
4e2d19e4 CS |
76 | |
77 | wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */ | |
78 | ||
1da177e4 LT |
79 | struct pgpath *current_pgpath; |
80 | struct priority_group *current_pg; | |
81 | struct priority_group *next_pg; /* Switch to this PG if set */ | |
1da177e4 | 82 | |
518257b1 | 83 | unsigned long flags; /* Multipath state flags */ |
1fbdd2b3 | 84 | |
c9e45581 | 85 | unsigned pg_init_retries; /* Number of times to retry pg_init */ |
4e2d19e4 | 86 | unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */ |
1da177e4 | 87 | |
91e968aa MS |
88 | atomic_t nr_valid_paths; /* Total number of usable paths */ |
89 | atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */ | |
90 | atomic_t pg_init_count; /* Number of times pg_init called */ | |
91 | ||
1da177e4 | 92 | /* |
028867ac | 93 | * We must use a mempool of dm_mpath_io structs so that we |
1da177e4 LT |
94 | * can resubmit bios on error. |
95 | */ | |
96 | mempool_t *mpio_pool; | |
6380f26f MA |
97 | |
98 | struct mutex work_mutex; | |
20800cb3 | 99 | struct work_struct trigger_event; |
1da177e4 LT |
100 | }; |
101 | ||
102 | /* | |
103 | * Context information attached to each bio we process. | |
104 | */ | |
028867ac | 105 | struct dm_mpath_io { |
1da177e4 | 106 | struct pgpath *pgpath; |
02ab823f | 107 | size_t nr_bytes; |
1da177e4 LT |
108 | }; |
109 | ||
110 | typedef int (*action_fn) (struct pgpath *pgpath); | |
111 | ||
e18b890b | 112 | static struct kmem_cache *_mpio_cache; |
1da177e4 | 113 | |
bab7cfc7 | 114 | static struct workqueue_struct *kmultipathd, *kmpath_handlerd; |
c4028958 | 115 | static void trigger_event(struct work_struct *work); |
bab7cfc7 | 116 | static void activate_path(struct work_struct *work); |
1da177e4 | 117 | |
518257b1 MS |
118 | /*----------------------------------------------- |
119 | * Multipath state flags. | |
120 | *-----------------------------------------------*/ | |
121 | ||
122 | #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */ | |
123 | #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */ | |
124 | #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */ | |
125 | #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */ | |
126 | #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */ | |
127 | #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */ | |
128 | #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */ | |
1da177e4 LT |
129 | |
130 | /*----------------------------------------------- | |
131 | * Allocation routines | |
132 | *-----------------------------------------------*/ | |
133 | ||
134 | static struct pgpath *alloc_pgpath(void) | |
135 | { | |
e69fae56 | 136 | struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL); |
1da177e4 | 137 | |
224cb3e9 | 138 | if (pgpath) { |
be7d31cc | 139 | pgpath->is_active = true; |
4e2d19e4 | 140 | INIT_DELAYED_WORK(&pgpath->activate_path, activate_path); |
224cb3e9 | 141 | } |
1da177e4 LT |
142 | |
143 | return pgpath; | |
144 | } | |
145 | ||
028867ac | 146 | static void free_pgpath(struct pgpath *pgpath) |
1da177e4 LT |
147 | { |
148 | kfree(pgpath); | |
149 | } | |
150 | ||
151 | static struct priority_group *alloc_priority_group(void) | |
152 | { | |
153 | struct priority_group *pg; | |
154 | ||
e69fae56 | 155 | pg = kzalloc(sizeof(*pg), GFP_KERNEL); |
1da177e4 | 156 | |
e69fae56 MM |
157 | if (pg) |
158 | INIT_LIST_HEAD(&pg->pgpaths); | |
1da177e4 LT |
159 | |
160 | return pg; | |
161 | } | |
162 | ||
163 | static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti) | |
164 | { | |
165 | struct pgpath *pgpath, *tmp; | |
166 | ||
167 | list_for_each_entry_safe(pgpath, tmp, pgpaths, list) { | |
168 | list_del(&pgpath->list); | |
169 | dm_put_device(ti, pgpath->path.dev); | |
170 | free_pgpath(pgpath); | |
171 | } | |
172 | } | |
173 | ||
174 | static void free_priority_group(struct priority_group *pg, | |
175 | struct dm_target *ti) | |
176 | { | |
177 | struct path_selector *ps = &pg->ps; | |
178 | ||
179 | if (ps->type) { | |
180 | ps->type->destroy(ps); | |
181 | dm_put_path_selector(ps->type); | |
182 | } | |
183 | ||
184 | free_pgpaths(&pg->pgpaths, ti); | |
185 | kfree(pg); | |
186 | } | |
187 | ||
8637a6bf | 188 | static struct multipath *alloc_multipath(struct dm_target *ti, bool use_blk_mq) |
1da177e4 LT |
189 | { |
190 | struct multipath *m; | |
191 | ||
e69fae56 | 192 | m = kzalloc(sizeof(*m), GFP_KERNEL); |
1da177e4 | 193 | if (m) { |
1da177e4 LT |
194 | INIT_LIST_HEAD(&m->priority_groups); |
195 | spin_lock_init(&m->lock); | |
518257b1 | 196 | set_bit(MPATHF_QUEUE_IO, &m->flags); |
91e968aa MS |
197 | atomic_set(&m->nr_valid_paths, 0); |
198 | atomic_set(&m->pg_init_in_progress, 0); | |
199 | atomic_set(&m->pg_init_count, 0); | |
4e2d19e4 | 200 | m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT; |
c4028958 | 201 | INIT_WORK(&m->trigger_event, trigger_event); |
2bded7bd | 202 | init_waitqueue_head(&m->pg_init_wait); |
6380f26f | 203 | mutex_init(&m->work_mutex); |
8637a6bf MS |
204 | |
205 | m->mpio_pool = NULL; | |
206 | if (!use_blk_mq) { | |
207 | unsigned min_ios = dm_get_reserved_rq_based_ios(); | |
208 | ||
209 | m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache); | |
210 | if (!m->mpio_pool) { | |
211 | kfree(m); | |
212 | return NULL; | |
213 | } | |
1da177e4 | 214 | } |
8637a6bf | 215 | |
28f16c20 MM |
216 | m->ti = ti; |
217 | ti->private = m; | |
1da177e4 LT |
218 | } |
219 | ||
220 | return m; | |
221 | } | |
222 | ||
223 | static void free_multipath(struct multipath *m) | |
224 | { | |
225 | struct priority_group *pg, *tmp; | |
1da177e4 LT |
226 | |
227 | list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { | |
228 | list_del(&pg->list); | |
229 | free_priority_group(pg, m->ti); | |
230 | } | |
231 | ||
cfae5c9b | 232 | kfree(m->hw_handler_name); |
2bfd2e13 | 233 | kfree(m->hw_handler_params); |
1da177e4 LT |
234 | mempool_destroy(m->mpio_pool); |
235 | kfree(m); | |
236 | } | |
237 | ||
2eff1924 MS |
238 | static struct dm_mpath_io *get_mpio(union map_info *info) |
239 | { | |
240 | return info->ptr; | |
241 | } | |
242 | ||
243 | static struct dm_mpath_io *set_mpio(struct multipath *m, union map_info *info) | |
466891f9 JN |
244 | { |
245 | struct dm_mpath_io *mpio; | |
246 | ||
8637a6bf MS |
247 | if (!m->mpio_pool) { |
248 | /* Use blk-mq pdu memory requested via per_io_data_size */ | |
2eff1924 | 249 | mpio = get_mpio(info); |
8637a6bf MS |
250 | memset(mpio, 0, sizeof(*mpio)); |
251 | return mpio; | |
252 | } | |
253 | ||
466891f9 JN |
254 | mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC); |
255 | if (!mpio) | |
2eff1924 | 256 | return NULL; |
466891f9 JN |
257 | |
258 | memset(mpio, 0, sizeof(*mpio)); | |
259 | info->ptr = mpio; | |
260 | ||
2eff1924 | 261 | return mpio; |
466891f9 JN |
262 | } |
263 | ||
2eff1924 | 264 | static void clear_request_fn_mpio(struct multipath *m, union map_info *info) |
466891f9 | 265 | { |
2eff1924 | 266 | /* Only needed for non blk-mq (.request_fn) multipath */ |
8637a6bf MS |
267 | if (m->mpio_pool) { |
268 | struct dm_mpath_io *mpio = info->ptr; | |
466891f9 | 269 | |
8637a6bf MS |
270 | info->ptr = NULL; |
271 | mempool_free(mpio, m->mpio_pool); | |
272 | } | |
466891f9 | 273 | } |
1da177e4 LT |
274 | |
275 | /*----------------------------------------------- | |
276 | * Path selection | |
277 | *-----------------------------------------------*/ | |
278 | ||
3e9f1be1 | 279 | static int __pg_init_all_paths(struct multipath *m) |
fb612642 KU |
280 | { |
281 | struct pgpath *pgpath; | |
4e2d19e4 | 282 | unsigned long pg_init_delay = 0; |
fb612642 | 283 | |
91e968aa | 284 | if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) |
3e9f1be1 | 285 | return 0; |
17f4ff45 | 286 | |
91e968aa | 287 | atomic_inc(&m->pg_init_count); |
518257b1 | 288 | clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); |
3e9f1be1 HR |
289 | |
290 | /* Check here to reset pg_init_required */ | |
291 | if (!m->current_pg) | |
292 | return 0; | |
293 | ||
518257b1 | 294 | if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags)) |
4e2d19e4 CS |
295 | pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ? |
296 | m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS); | |
fb612642 KU |
297 | list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) { |
298 | /* Skip failed paths */ | |
299 | if (!pgpath->is_active) | |
300 | continue; | |
4e2d19e4 CS |
301 | if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path, |
302 | pg_init_delay)) | |
91e968aa | 303 | atomic_inc(&m->pg_init_in_progress); |
fb612642 | 304 | } |
91e968aa | 305 | return atomic_read(&m->pg_init_in_progress); |
fb612642 KU |
306 | } |
307 | ||
2da1610a | 308 | static int pg_init_all_paths(struct multipath *m) |
1da177e4 | 309 | { |
2da1610a MS |
310 | int r; |
311 | unsigned long flags; | |
312 | ||
313 | spin_lock_irqsave(&m->lock, flags); | |
314 | r = __pg_init_all_paths(m); | |
315 | spin_unlock_irqrestore(&m->lock, flags); | |
316 | ||
317 | return r; | |
318 | } | |
319 | ||
320 | static void __switch_pg(struct multipath *m, struct priority_group *pg) | |
321 | { | |
322 | m->current_pg = pg; | |
1da177e4 LT |
323 | |
324 | /* Must we initialise the PG first, and queue I/O till it's ready? */ | |
cfae5c9b | 325 | if (m->hw_handler_name) { |
518257b1 MS |
326 | set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); |
327 | set_bit(MPATHF_QUEUE_IO, &m->flags); | |
1da177e4 | 328 | } else { |
518257b1 MS |
329 | clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); |
330 | clear_bit(MPATHF_QUEUE_IO, &m->flags); | |
1da177e4 | 331 | } |
c9e45581 | 332 | |
91e968aa | 333 | atomic_set(&m->pg_init_count, 0); |
1da177e4 LT |
334 | } |
335 | ||
2da1610a MS |
336 | static struct pgpath *choose_path_in_pg(struct multipath *m, |
337 | struct priority_group *pg, | |
338 | size_t nr_bytes) | |
1da177e4 | 339 | { |
2da1610a | 340 | unsigned long flags; |
c922d5f7 | 341 | struct dm_path *path; |
2da1610a | 342 | struct pgpath *pgpath; |
1da177e4 | 343 | |
90a4323c | 344 | path = pg->ps.type->select_path(&pg->ps, nr_bytes); |
1da177e4 | 345 | if (!path) |
2da1610a | 346 | return ERR_PTR(-ENXIO); |
1da177e4 | 347 | |
2da1610a | 348 | pgpath = path_to_pgpath(path); |
1da177e4 | 349 | |
2da1610a MS |
350 | if (unlikely(lockless_dereference(m->current_pg) != pg)) { |
351 | /* Only update current_pgpath if pg changed */ | |
352 | spin_lock_irqsave(&m->lock, flags); | |
353 | m->current_pgpath = pgpath; | |
354 | __switch_pg(m, pg); | |
355 | spin_unlock_irqrestore(&m->lock, flags); | |
356 | } | |
1da177e4 | 357 | |
2da1610a | 358 | return pgpath; |
1da177e4 LT |
359 | } |
360 | ||
2da1610a | 361 | static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes) |
1da177e4 | 362 | { |
2da1610a | 363 | unsigned long flags; |
1da177e4 | 364 | struct priority_group *pg; |
2da1610a | 365 | struct pgpath *pgpath; |
be7d31cc | 366 | bool bypassed = true; |
1da177e4 | 367 | |
91e968aa | 368 | if (!atomic_read(&m->nr_valid_paths)) { |
518257b1 | 369 | clear_bit(MPATHF_QUEUE_IO, &m->flags); |
1da177e4 | 370 | goto failed; |
1f271972 | 371 | } |
1da177e4 LT |
372 | |
373 | /* Were we instructed to switch PG? */ | |
2da1610a MS |
374 | if (lockless_dereference(m->next_pg)) { |
375 | spin_lock_irqsave(&m->lock, flags); | |
1da177e4 | 376 | pg = m->next_pg; |
2da1610a MS |
377 | if (!pg) { |
378 | spin_unlock_irqrestore(&m->lock, flags); | |
379 | goto check_current_pg; | |
380 | } | |
1da177e4 | 381 | m->next_pg = NULL; |
2da1610a MS |
382 | spin_unlock_irqrestore(&m->lock, flags); |
383 | pgpath = choose_path_in_pg(m, pg, nr_bytes); | |
384 | if (!IS_ERR_OR_NULL(pgpath)) | |
385 | return pgpath; | |
1da177e4 LT |
386 | } |
387 | ||
388 | /* Don't change PG until it has no remaining paths */ | |
2da1610a MS |
389 | check_current_pg: |
390 | pg = lockless_dereference(m->current_pg); | |
391 | if (pg) { | |
392 | pgpath = choose_path_in_pg(m, pg, nr_bytes); | |
393 | if (!IS_ERR_OR_NULL(pgpath)) | |
394 | return pgpath; | |
395 | } | |
1da177e4 LT |
396 | |
397 | /* | |
398 | * Loop through priority groups until we find a valid path. | |
399 | * First time we skip PGs marked 'bypassed'. | |
f220fd4e MC |
400 | * Second time we only try the ones we skipped, but set |
401 | * pg_init_delay_retry so we do not hammer controllers. | |
1da177e4 LT |
402 | */ |
403 | do { | |
404 | list_for_each_entry(pg, &m->priority_groups, list) { | |
405 | if (pg->bypassed == bypassed) | |
406 | continue; | |
2da1610a MS |
407 | pgpath = choose_path_in_pg(m, pg, nr_bytes); |
408 | if (!IS_ERR_OR_NULL(pgpath)) { | |
f220fd4e | 409 | if (!bypassed) |
518257b1 | 410 | set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); |
2da1610a | 411 | return pgpath; |
f220fd4e | 412 | } |
1da177e4 LT |
413 | } |
414 | } while (bypassed--); | |
415 | ||
416 | failed: | |
2da1610a | 417 | spin_lock_irqsave(&m->lock, flags); |
1da177e4 LT |
418 | m->current_pgpath = NULL; |
419 | m->current_pg = NULL; | |
2da1610a MS |
420 | spin_unlock_irqrestore(&m->lock, flags); |
421 | ||
422 | return NULL; | |
1da177e4 LT |
423 | } |
424 | ||
45e15720 KU |
425 | /* |
426 | * Check whether bios must be queued in the device-mapper core rather | |
427 | * than here in the target. | |
428 | * | |
45e15720 KU |
429 | * If m->queue_if_no_path and m->saved_queue_if_no_path hold the |
430 | * same value then we are not between multipath_presuspend() | |
431 | * and multipath_resume() calls and we have no need to check | |
432 | * for the DMF_NOFLUSH_SUSPENDING flag. | |
433 | */ | |
2da1610a | 434 | static int must_push_back(struct multipath *m) |
45e15720 | 435 | { |
518257b1 MS |
436 | return (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) || |
437 | ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) != | |
438 | test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) && | |
e8099177 | 439 | dm_noflush_suspending(m->ti))); |
45e15720 KU |
440 | } |
441 | ||
36fcffcc HR |
442 | /* |
443 | * Map cloned requests | |
444 | */ | |
e5863d9a MS |
445 | static int __multipath_map(struct dm_target *ti, struct request *clone, |
446 | union map_info *map_context, | |
447 | struct request *rq, struct request **__clone) | |
1da177e4 | 448 | { |
7943bd6d | 449 | struct multipath *m = ti->private; |
e3bde04f | 450 | int r = DM_MAPIO_REQUEUE; |
e5863d9a | 451 | size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq); |
1da177e4 | 452 | struct pgpath *pgpath; |
f40c67f0 | 453 | struct block_device *bdev; |
e3bde04f | 454 | struct dm_mpath_io *mpio; |
1da177e4 | 455 | |
1da177e4 | 456 | /* Do we need to select a new pgpath? */ |
2da1610a MS |
457 | pgpath = lockless_dereference(m->current_pgpath); |
458 | if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags)) | |
459 | pgpath = choose_pgpath(m, nr_bytes); | |
1da177e4 | 460 | |
9bf59a61 | 461 | if (!pgpath) { |
2da1610a | 462 | if (!must_push_back(m)) |
9bf59a61 | 463 | r = -EIO; /* Failed */ |
2da1610a | 464 | return r; |
518257b1 MS |
465 | } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) || |
466 | test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { | |
2da1610a MS |
467 | pg_init_all_paths(m); |
468 | return r; | |
9bf59a61 | 469 | } |
6afbc01d | 470 | |
2eff1924 MS |
471 | mpio = set_mpio(m, map_context); |
472 | if (!mpio) | |
9bf59a61 | 473 | /* ENOMEM, requeue */ |
2da1610a | 474 | return r; |
9bf59a61 | 475 | |
2eb6e1e3 KB |
476 | mpio->pgpath = pgpath; |
477 | mpio->nr_bytes = nr_bytes; | |
478 | ||
9bf59a61 | 479 | bdev = pgpath->path.dev->bdev; |
2eb6e1e3 | 480 | |
e5863d9a | 481 | if (clone) { |
c5248f79 MS |
482 | /* |
483 | * Old request-based interface: allocated clone is passed in. | |
484 | * Used by: .request_fn stacked on .request_fn path(s). | |
485 | */ | |
e5863d9a MS |
486 | clone->q = bdev_get_queue(bdev); |
487 | clone->rq_disk = bdev->bd_disk; | |
488 | clone->cmd_flags |= REQ_FAILFAST_TRANSPORT; | |
489 | } else { | |
eca7ee6d MS |
490 | /* |
491 | * blk-mq request-based interface; used by both: | |
492 | * .request_fn stacked on blk-mq path(s) and | |
493 | * blk-mq stacked on blk-mq path(s). | |
494 | */ | |
78ce23b5 MS |
495 | *__clone = blk_mq_alloc_request(bdev_get_queue(bdev), |
496 | rq_data_dir(rq), BLK_MQ_REQ_NOWAIT); | |
4c6dd53d | 497 | if (IS_ERR(*__clone)) { |
e5863d9a | 498 | /* ENOMEM, requeue */ |
2eff1924 | 499 | clear_request_fn_mpio(m, map_context); |
e5863d9a | 500 | return r; |
4c6dd53d | 501 | } |
e5863d9a MS |
502 | (*__clone)->bio = (*__clone)->biotail = NULL; |
503 | (*__clone)->rq_disk = bdev->bd_disk; | |
504 | (*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT; | |
505 | } | |
506 | ||
9bf59a61 MS |
507 | if (pgpath->pg->ps.type->start_io) |
508 | pgpath->pg->ps.type->start_io(&pgpath->pg->ps, | |
509 | &pgpath->path, | |
510 | nr_bytes); | |
2eb6e1e3 | 511 | return DM_MAPIO_REMAPPED; |
1da177e4 LT |
512 | } |
513 | ||
e5863d9a MS |
514 | static int multipath_map(struct dm_target *ti, struct request *clone, |
515 | union map_info *map_context) | |
516 | { | |
517 | return __multipath_map(ti, clone, map_context, NULL, NULL); | |
518 | } | |
519 | ||
520 | static int multipath_clone_and_map(struct dm_target *ti, struct request *rq, | |
521 | union map_info *map_context, | |
522 | struct request **clone) | |
523 | { | |
524 | return __multipath_map(ti, NULL, map_context, rq, clone); | |
525 | } | |
526 | ||
527 | static void multipath_release_clone(struct request *clone) | |
528 | { | |
78ce23b5 | 529 | blk_mq_free_request(clone); |
e5863d9a MS |
530 | } |
531 | ||
1da177e4 LT |
532 | /* |
533 | * If we run out of usable paths, should we queue I/O or error it? | |
534 | */ | |
be7d31cc MS |
535 | static int queue_if_no_path(struct multipath *m, bool queue_if_no_path, |
536 | bool save_old_value) | |
1da177e4 LT |
537 | { |
538 | unsigned long flags; | |
539 | ||
540 | spin_lock_irqsave(&m->lock, flags); | |
541 | ||
518257b1 MS |
542 | if (save_old_value) { |
543 | if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) | |
544 | set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); | |
545 | else | |
546 | clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); | |
547 | } else { | |
548 | if (queue_if_no_path) | |
549 | set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); | |
550 | else | |
551 | clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); | |
552 | } | |
553 | if (queue_if_no_path) | |
554 | set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); | |
485ef69e | 555 | else |
518257b1 MS |
556 | clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); |
557 | ||
1da177e4 LT |
558 | spin_unlock_irqrestore(&m->lock, flags); |
559 | ||
63d832c3 HR |
560 | if (!queue_if_no_path) |
561 | dm_table_run_md_queue_async(m->ti->table); | |
562 | ||
1da177e4 LT |
563 | return 0; |
564 | } | |
565 | ||
1da177e4 LT |
566 | /* |
567 | * An event is triggered whenever a path is taken out of use. | |
568 | * Includes path failure and PG bypass. | |
569 | */ | |
c4028958 | 570 | static void trigger_event(struct work_struct *work) |
1da177e4 | 571 | { |
c4028958 DH |
572 | struct multipath *m = |
573 | container_of(work, struct multipath, trigger_event); | |
1da177e4 LT |
574 | |
575 | dm_table_event(m->ti->table); | |
576 | } | |
577 | ||
578 | /*----------------------------------------------------------------- | |
579 | * Constructor/argument parsing: | |
580 | * <#multipath feature args> [<arg>]* | |
581 | * <#hw_handler args> [hw_handler [<arg>]*] | |
582 | * <#priority groups> | |
583 | * <initial priority group> | |
584 | * [<selector> <#selector args> [<arg>]* | |
585 | * <#paths> <#per-path selector args> | |
586 | * [<path> [<arg>]* ]+ ]+ | |
587 | *---------------------------------------------------------------*/ | |
498f0103 | 588 | static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg, |
1da177e4 LT |
589 | struct dm_target *ti) |
590 | { | |
591 | int r; | |
592 | struct path_selector_type *pst; | |
593 | unsigned ps_argc; | |
594 | ||
498f0103 | 595 | static struct dm_arg _args[] = { |
72d94861 | 596 | {0, 1024, "invalid number of path selector args"}, |
1da177e4 LT |
597 | }; |
598 | ||
498f0103 | 599 | pst = dm_get_path_selector(dm_shift_arg(as)); |
1da177e4 | 600 | if (!pst) { |
72d94861 | 601 | ti->error = "unknown path selector type"; |
1da177e4 LT |
602 | return -EINVAL; |
603 | } | |
604 | ||
498f0103 | 605 | r = dm_read_arg_group(_args, as, &ps_argc, &ti->error); |
371b2e34 MP |
606 | if (r) { |
607 | dm_put_path_selector(pst); | |
1da177e4 | 608 | return -EINVAL; |
371b2e34 | 609 | } |
1da177e4 LT |
610 | |
611 | r = pst->create(&pg->ps, ps_argc, as->argv); | |
612 | if (r) { | |
613 | dm_put_path_selector(pst); | |
72d94861 | 614 | ti->error = "path selector constructor failed"; |
1da177e4 LT |
615 | return r; |
616 | } | |
617 | ||
618 | pg->ps.type = pst; | |
498f0103 | 619 | dm_consume_args(as, ps_argc); |
1da177e4 LT |
620 | |
621 | return 0; | |
622 | } | |
623 | ||
498f0103 | 624 | static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps, |
1da177e4 LT |
625 | struct dm_target *ti) |
626 | { | |
627 | int r; | |
628 | struct pgpath *p; | |
ae11b1b3 | 629 | struct multipath *m = ti->private; |
a58a935d MS |
630 | struct request_queue *q = NULL; |
631 | const char *attached_handler_name; | |
1da177e4 LT |
632 | |
633 | /* we need at least a path arg */ | |
634 | if (as->argc < 1) { | |
72d94861 | 635 | ti->error = "no device given"; |
01460f35 | 636 | return ERR_PTR(-EINVAL); |
1da177e4 LT |
637 | } |
638 | ||
639 | p = alloc_pgpath(); | |
640 | if (!p) | |
01460f35 | 641 | return ERR_PTR(-ENOMEM); |
1da177e4 | 642 | |
498f0103 | 643 | r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table), |
8215d6ec | 644 | &p->path.dev); |
1da177e4 | 645 | if (r) { |
72d94861 | 646 | ti->error = "error getting device"; |
1da177e4 LT |
647 | goto bad; |
648 | } | |
649 | ||
518257b1 | 650 | if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name) |
a58a935d MS |
651 | q = bdev_get_queue(p->path.dev->bdev); |
652 | ||
518257b1 | 653 | if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) { |
1bab0de0 | 654 | retain: |
a58a935d MS |
655 | attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL); |
656 | if (attached_handler_name) { | |
657 | /* | |
658 | * Reset hw_handler_name to match the attached handler | |
659 | * and clear any hw_handler_params associated with the | |
660 | * ignored handler. | |
661 | * | |
662 | * NB. This modifies the table line to show the actual | |
663 | * handler instead of the original table passed in. | |
664 | */ | |
665 | kfree(m->hw_handler_name); | |
666 | m->hw_handler_name = attached_handler_name; | |
667 | ||
668 | kfree(m->hw_handler_params); | |
669 | m->hw_handler_params = NULL; | |
670 | } | |
671 | } | |
a0cf7ea9 | 672 | |
a58a935d | 673 | if (m->hw_handler_name) { |
a0cf7ea9 HR |
674 | r = scsi_dh_attach(q, m->hw_handler_name); |
675 | if (r == -EBUSY) { | |
1bab0de0 | 676 | char b[BDEVNAME_SIZE]; |
a0cf7ea9 | 677 | |
1bab0de0 CH |
678 | printk(KERN_INFO "dm-mpath: retaining handler on device %s\n", |
679 | bdevname(p->path.dev->bdev, b)); | |
680 | goto retain; | |
681 | } | |
ae11b1b3 | 682 | if (r < 0) { |
a0cf7ea9 | 683 | ti->error = "error attaching hardware handler"; |
ae11b1b3 HR |
684 | dm_put_device(ti, p->path.dev); |
685 | goto bad; | |
686 | } | |
2bfd2e13 CS |
687 | |
688 | if (m->hw_handler_params) { | |
689 | r = scsi_dh_set_params(q, m->hw_handler_params); | |
690 | if (r < 0) { | |
691 | ti->error = "unable to set hardware " | |
692 | "handler parameters"; | |
2bfd2e13 CS |
693 | dm_put_device(ti, p->path.dev); |
694 | goto bad; | |
695 | } | |
696 | } | |
ae11b1b3 HR |
697 | } |
698 | ||
1da177e4 LT |
699 | r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); |
700 | if (r) { | |
701 | dm_put_device(ti, p->path.dev); | |
702 | goto bad; | |
703 | } | |
704 | ||
705 | return p; | |
706 | ||
707 | bad: | |
708 | free_pgpath(p); | |
01460f35 | 709 | return ERR_PTR(r); |
1da177e4 LT |
710 | } |
711 | ||
498f0103 | 712 | static struct priority_group *parse_priority_group(struct dm_arg_set *as, |
28f16c20 | 713 | struct multipath *m) |
1da177e4 | 714 | { |
498f0103 | 715 | static struct dm_arg _args[] = { |
72d94861 AK |
716 | {1, 1024, "invalid number of paths"}, |
717 | {0, 1024, "invalid number of selector args"} | |
1da177e4 LT |
718 | }; |
719 | ||
720 | int r; | |
498f0103 | 721 | unsigned i, nr_selector_args, nr_args; |
1da177e4 | 722 | struct priority_group *pg; |
28f16c20 | 723 | struct dm_target *ti = m->ti; |
1da177e4 LT |
724 | |
725 | if (as->argc < 2) { | |
726 | as->argc = 0; | |
01460f35 BM |
727 | ti->error = "not enough priority group arguments"; |
728 | return ERR_PTR(-EINVAL); | |
1da177e4 LT |
729 | } |
730 | ||
731 | pg = alloc_priority_group(); | |
732 | if (!pg) { | |
72d94861 | 733 | ti->error = "couldn't allocate priority group"; |
01460f35 | 734 | return ERR_PTR(-ENOMEM); |
1da177e4 LT |
735 | } |
736 | pg->m = m; | |
737 | ||
738 | r = parse_path_selector(as, pg, ti); | |
739 | if (r) | |
740 | goto bad; | |
741 | ||
742 | /* | |
743 | * read the paths | |
744 | */ | |
498f0103 | 745 | r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error); |
1da177e4 LT |
746 | if (r) |
747 | goto bad; | |
748 | ||
498f0103 | 749 | r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error); |
1da177e4 LT |
750 | if (r) |
751 | goto bad; | |
752 | ||
498f0103 | 753 | nr_args = 1 + nr_selector_args; |
1da177e4 LT |
754 | for (i = 0; i < pg->nr_pgpaths; i++) { |
755 | struct pgpath *pgpath; | |
498f0103 | 756 | struct dm_arg_set path_args; |
1da177e4 | 757 | |
498f0103 | 758 | if (as->argc < nr_args) { |
148acff6 | 759 | ti->error = "not enough path parameters"; |
6bbf79a1 | 760 | r = -EINVAL; |
1da177e4 | 761 | goto bad; |
148acff6 | 762 | } |
1da177e4 | 763 | |
498f0103 | 764 | path_args.argc = nr_args; |
1da177e4 LT |
765 | path_args.argv = as->argv; |
766 | ||
767 | pgpath = parse_path(&path_args, &pg->ps, ti); | |
01460f35 BM |
768 | if (IS_ERR(pgpath)) { |
769 | r = PTR_ERR(pgpath); | |
1da177e4 | 770 | goto bad; |
01460f35 | 771 | } |
1da177e4 LT |
772 | |
773 | pgpath->pg = pg; | |
774 | list_add_tail(&pgpath->list, &pg->pgpaths); | |
498f0103 | 775 | dm_consume_args(as, nr_args); |
1da177e4 LT |
776 | } |
777 | ||
778 | return pg; | |
779 | ||
780 | bad: | |
781 | free_priority_group(pg, ti); | |
01460f35 | 782 | return ERR_PTR(r); |
1da177e4 LT |
783 | } |
784 | ||
498f0103 | 785 | static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m) |
1da177e4 | 786 | { |
1da177e4 | 787 | unsigned hw_argc; |
2bfd2e13 | 788 | int ret; |
28f16c20 | 789 | struct dm_target *ti = m->ti; |
1da177e4 | 790 | |
498f0103 | 791 | static struct dm_arg _args[] = { |
72d94861 | 792 | {0, 1024, "invalid number of hardware handler args"}, |
1da177e4 LT |
793 | }; |
794 | ||
498f0103 | 795 | if (dm_read_arg_group(_args, as, &hw_argc, &ti->error)) |
1da177e4 LT |
796 | return -EINVAL; |
797 | ||
798 | if (!hw_argc) | |
799 | return 0; | |
800 | ||
498f0103 | 801 | m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL); |
14e98c5c | 802 | |
2bfd2e13 CS |
803 | if (hw_argc > 1) { |
804 | char *p; | |
805 | int i, j, len = 4; | |
806 | ||
807 | for (i = 0; i <= hw_argc - 2; i++) | |
808 | len += strlen(as->argv[i]) + 1; | |
809 | p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); | |
810 | if (!p) { | |
811 | ti->error = "memory allocation failed"; | |
812 | ret = -ENOMEM; | |
813 | goto fail; | |
814 | } | |
815 | j = sprintf(p, "%d", hw_argc - 1); | |
816 | for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1) | |
817 | j = sprintf(p, "%s", as->argv[i]); | |
818 | } | |
498f0103 | 819 | dm_consume_args(as, hw_argc - 1); |
1da177e4 LT |
820 | |
821 | return 0; | |
2bfd2e13 CS |
822 | fail: |
823 | kfree(m->hw_handler_name); | |
824 | m->hw_handler_name = NULL; | |
825 | return ret; | |
1da177e4 LT |
826 | } |
827 | ||
498f0103 | 828 | static int parse_features(struct dm_arg_set *as, struct multipath *m) |
1da177e4 LT |
829 | { |
830 | int r; | |
831 | unsigned argc; | |
28f16c20 | 832 | struct dm_target *ti = m->ti; |
498f0103 | 833 | const char *arg_name; |
1da177e4 | 834 | |
498f0103 | 835 | static struct dm_arg _args[] = { |
a58a935d | 836 | {0, 6, "invalid number of feature args"}, |
c9e45581 | 837 | {1, 50, "pg_init_retries must be between 1 and 50"}, |
4e2d19e4 | 838 | {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"}, |
1da177e4 LT |
839 | }; |
840 | ||
498f0103 | 841 | r = dm_read_arg_group(_args, as, &argc, &ti->error); |
1da177e4 LT |
842 | if (r) |
843 | return -EINVAL; | |
844 | ||
845 | if (!argc) | |
846 | return 0; | |
847 | ||
c9e45581 | 848 | do { |
498f0103 | 849 | arg_name = dm_shift_arg(as); |
c9e45581 DW |
850 | argc--; |
851 | ||
498f0103 | 852 | if (!strcasecmp(arg_name, "queue_if_no_path")) { |
be7d31cc | 853 | r = queue_if_no_path(m, true, false); |
c9e45581 DW |
854 | continue; |
855 | } | |
856 | ||
a58a935d | 857 | if (!strcasecmp(arg_name, "retain_attached_hw_handler")) { |
518257b1 | 858 | set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); |
a58a935d MS |
859 | continue; |
860 | } | |
861 | ||
498f0103 | 862 | if (!strcasecmp(arg_name, "pg_init_retries") && |
c9e45581 | 863 | (argc >= 1)) { |
498f0103 | 864 | r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error); |
c9e45581 DW |
865 | argc--; |
866 | continue; | |
867 | } | |
868 | ||
498f0103 | 869 | if (!strcasecmp(arg_name, "pg_init_delay_msecs") && |
4e2d19e4 | 870 | (argc >= 1)) { |
498f0103 | 871 | r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error); |
4e2d19e4 CS |
872 | argc--; |
873 | continue; | |
874 | } | |
875 | ||
1da177e4 | 876 | ti->error = "Unrecognised multipath feature request"; |
c9e45581 DW |
877 | r = -EINVAL; |
878 | } while (argc && !r); | |
879 | ||
880 | return r; | |
1da177e4 LT |
881 | } |
882 | ||
883 | static int multipath_ctr(struct dm_target *ti, unsigned int argc, | |
884 | char **argv) | |
885 | { | |
498f0103 MS |
886 | /* target arguments */ |
887 | static struct dm_arg _args[] = { | |
a490a07a MS |
888 | {0, 1024, "invalid number of priority groups"}, |
889 | {0, 1024, "invalid initial priority group number"}, | |
1da177e4 LT |
890 | }; |
891 | ||
892 | int r; | |
893 | struct multipath *m; | |
498f0103 | 894 | struct dm_arg_set as; |
1da177e4 LT |
895 | unsigned pg_count = 0; |
896 | unsigned next_pg_num; | |
8637a6bf | 897 | bool use_blk_mq = dm_use_blk_mq(dm_table_get_md(ti->table)); |
1da177e4 LT |
898 | |
899 | as.argc = argc; | |
900 | as.argv = argv; | |
901 | ||
8637a6bf | 902 | m = alloc_multipath(ti, use_blk_mq); |
1da177e4 | 903 | if (!m) { |
72d94861 | 904 | ti->error = "can't allocate multipath"; |
1da177e4 LT |
905 | return -EINVAL; |
906 | } | |
907 | ||
28f16c20 | 908 | r = parse_features(&as, m); |
1da177e4 LT |
909 | if (r) |
910 | goto bad; | |
911 | ||
28f16c20 | 912 | r = parse_hw_handler(&as, m); |
1da177e4 LT |
913 | if (r) |
914 | goto bad; | |
915 | ||
498f0103 | 916 | r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error); |
1da177e4 LT |
917 | if (r) |
918 | goto bad; | |
919 | ||
498f0103 | 920 | r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error); |
1da177e4 LT |
921 | if (r) |
922 | goto bad; | |
923 | ||
a490a07a MS |
924 | if ((!m->nr_priority_groups && next_pg_num) || |
925 | (m->nr_priority_groups && !next_pg_num)) { | |
926 | ti->error = "invalid initial priority group"; | |
927 | r = -EINVAL; | |
928 | goto bad; | |
929 | } | |
930 | ||
1da177e4 LT |
931 | /* parse the priority groups */ |
932 | while (as.argc) { | |
933 | struct priority_group *pg; | |
91e968aa | 934 | unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths); |
1da177e4 | 935 | |
28f16c20 | 936 | pg = parse_priority_group(&as, m); |
01460f35 BM |
937 | if (IS_ERR(pg)) { |
938 | r = PTR_ERR(pg); | |
1da177e4 LT |
939 | goto bad; |
940 | } | |
941 | ||
91e968aa MS |
942 | nr_valid_paths += pg->nr_pgpaths; |
943 | atomic_set(&m->nr_valid_paths, nr_valid_paths); | |
944 | ||
1da177e4 LT |
945 | list_add_tail(&pg->list, &m->priority_groups); |
946 | pg_count++; | |
947 | pg->pg_num = pg_count; | |
948 | if (!--next_pg_num) | |
949 | m->next_pg = pg; | |
950 | } | |
951 | ||
952 | if (pg_count != m->nr_priority_groups) { | |
72d94861 | 953 | ti->error = "priority group count mismatch"; |
1da177e4 LT |
954 | r = -EINVAL; |
955 | goto bad; | |
956 | } | |
957 | ||
55a62eef AK |
958 | ti->num_flush_bios = 1; |
959 | ti->num_discard_bios = 1; | |
042bcef8 | 960 | ti->num_write_same_bios = 1; |
8637a6bf MS |
961 | if (use_blk_mq) |
962 | ti->per_io_data_size = sizeof(struct dm_mpath_io); | |
8627921f | 963 | |
1da177e4 LT |
964 | return 0; |
965 | ||
966 | bad: | |
967 | free_multipath(m); | |
968 | return r; | |
969 | } | |
970 | ||
2bded7bd KU |
971 | static void multipath_wait_for_pg_init_completion(struct multipath *m) |
972 | { | |
973 | DECLARE_WAITQUEUE(wait, current); | |
2bded7bd KU |
974 | |
975 | add_wait_queue(&m->pg_init_wait, &wait); | |
976 | ||
977 | while (1) { | |
978 | set_current_state(TASK_UNINTERRUPTIBLE); | |
979 | ||
91e968aa | 980 | if (!atomic_read(&m->pg_init_in_progress)) |
2bded7bd | 981 | break; |
2bded7bd KU |
982 | |
983 | io_schedule(); | |
984 | } | |
985 | set_current_state(TASK_RUNNING); | |
986 | ||
987 | remove_wait_queue(&m->pg_init_wait, &wait); | |
988 | } | |
989 | ||
990 | static void flush_multipath_work(struct multipath *m) | |
1da177e4 | 991 | { |
518257b1 MS |
992 | set_bit(MPATHF_PG_INIT_DISABLED, &m->flags); |
993 | smp_mb__after_atomic(); | |
954a73d5 | 994 | |
bab7cfc7 | 995 | flush_workqueue(kmpath_handlerd); |
2bded7bd | 996 | multipath_wait_for_pg_init_completion(m); |
a044d016 | 997 | flush_workqueue(kmultipathd); |
43829731 | 998 | flush_work(&m->trigger_event); |
954a73d5 | 999 | |
518257b1 MS |
1000 | clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags); |
1001 | smp_mb__after_atomic(); | |
6df400ab KU |
1002 | } |
1003 | ||
1004 | static void multipath_dtr(struct dm_target *ti) | |
1005 | { | |
1006 | struct multipath *m = ti->private; | |
1007 | ||
2bded7bd | 1008 | flush_multipath_work(m); |
1da177e4 LT |
1009 | free_multipath(m); |
1010 | } | |
1011 | ||
1da177e4 LT |
1012 | /* |
1013 | * Take a path out of use. | |
1014 | */ | |
1015 | static int fail_path(struct pgpath *pgpath) | |
1016 | { | |
1017 | unsigned long flags; | |
1018 | struct multipath *m = pgpath->pg->m; | |
1019 | ||
1020 | spin_lock_irqsave(&m->lock, flags); | |
1021 | ||
6680073d | 1022 | if (!pgpath->is_active) |
1da177e4 LT |
1023 | goto out; |
1024 | ||
72d94861 | 1025 | DMWARN("Failing path %s.", pgpath->path.dev->name); |
1da177e4 LT |
1026 | |
1027 | pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); | |
be7d31cc | 1028 | pgpath->is_active = false; |
1da177e4 LT |
1029 | pgpath->fail_count++; |
1030 | ||
91e968aa | 1031 | atomic_dec(&m->nr_valid_paths); |
1da177e4 LT |
1032 | |
1033 | if (pgpath == m->current_pgpath) | |
1034 | m->current_pgpath = NULL; | |
1035 | ||
b15546f9 | 1036 | dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti, |
91e968aa | 1037 | pgpath->path.dev->name, atomic_read(&m->nr_valid_paths)); |
b15546f9 | 1038 | |
fe9cf30e | 1039 | schedule_work(&m->trigger_event); |
1da177e4 LT |
1040 | |
1041 | out: | |
1042 | spin_unlock_irqrestore(&m->lock, flags); | |
1043 | ||
1044 | return 0; | |
1045 | } | |
1046 | ||
1047 | /* | |
1048 | * Reinstate a previously-failed path | |
1049 | */ | |
1050 | static int reinstate_path(struct pgpath *pgpath) | |
1051 | { | |
63d832c3 | 1052 | int r = 0, run_queue = 0; |
1da177e4 LT |
1053 | unsigned long flags; |
1054 | struct multipath *m = pgpath->pg->m; | |
91e968aa | 1055 | unsigned nr_valid_paths; |
1da177e4 LT |
1056 | |
1057 | spin_lock_irqsave(&m->lock, flags); | |
1058 | ||
6680073d | 1059 | if (pgpath->is_active) |
1da177e4 LT |
1060 | goto out; |
1061 | ||
ec31f3f7 | 1062 | DMWARN("Reinstating path %s.", pgpath->path.dev->name); |
1da177e4 LT |
1063 | |
1064 | r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); | |
1065 | if (r) | |
1066 | goto out; | |
1067 | ||
be7d31cc | 1068 | pgpath->is_active = true; |
1da177e4 | 1069 | |
91e968aa MS |
1070 | nr_valid_paths = atomic_inc_return(&m->nr_valid_paths); |
1071 | if (nr_valid_paths == 1) { | |
e54f77dd | 1072 | m->current_pgpath = NULL; |
63d832c3 | 1073 | run_queue = 1; |
e54f77dd | 1074 | } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) { |
4e2d19e4 | 1075 | if (queue_work(kmpath_handlerd, &pgpath->activate_path.work)) |
91e968aa | 1076 | atomic_inc(&m->pg_init_in_progress); |
e54f77dd | 1077 | } |
1da177e4 | 1078 | |
b15546f9 | 1079 | dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti, |
91e968aa | 1080 | pgpath->path.dev->name, nr_valid_paths); |
b15546f9 | 1081 | |
fe9cf30e | 1082 | schedule_work(&m->trigger_event); |
1da177e4 LT |
1083 | |
1084 | out: | |
1085 | spin_unlock_irqrestore(&m->lock, flags); | |
63d832c3 HR |
1086 | if (run_queue) |
1087 | dm_table_run_md_queue_async(m->ti->table); | |
1da177e4 LT |
1088 | |
1089 | return r; | |
1090 | } | |
1091 | ||
1092 | /* | |
1093 | * Fail or reinstate all paths that match the provided struct dm_dev. | |
1094 | */ | |
1095 | static int action_dev(struct multipath *m, struct dm_dev *dev, | |
1096 | action_fn action) | |
1097 | { | |
19040c0b | 1098 | int r = -EINVAL; |
1da177e4 LT |
1099 | struct pgpath *pgpath; |
1100 | struct priority_group *pg; | |
1101 | ||
1102 | list_for_each_entry(pg, &m->priority_groups, list) { | |
1103 | list_for_each_entry(pgpath, &pg->pgpaths, list) { | |
1104 | if (pgpath->path.dev == dev) | |
1105 | r = action(pgpath); | |
1106 | } | |
1107 | } | |
1108 | ||
1109 | return r; | |
1110 | } | |
1111 | ||
1112 | /* | |
1113 | * Temporarily try to avoid having to use the specified PG | |
1114 | */ | |
1115 | static void bypass_pg(struct multipath *m, struct priority_group *pg, | |
be7d31cc | 1116 | bool bypassed) |
1da177e4 LT |
1117 | { |
1118 | unsigned long flags; | |
1119 | ||
1120 | spin_lock_irqsave(&m->lock, flags); | |
1121 | ||
1122 | pg->bypassed = bypassed; | |
1123 | m->current_pgpath = NULL; | |
1124 | m->current_pg = NULL; | |
1125 | ||
1126 | spin_unlock_irqrestore(&m->lock, flags); | |
1127 | ||
fe9cf30e | 1128 | schedule_work(&m->trigger_event); |
1da177e4 LT |
1129 | } |
1130 | ||
1131 | /* | |
1132 | * Switch to using the specified PG from the next I/O that gets mapped | |
1133 | */ | |
1134 | static int switch_pg_num(struct multipath *m, const char *pgstr) | |
1135 | { | |
1136 | struct priority_group *pg; | |
1137 | unsigned pgnum; | |
1138 | unsigned long flags; | |
31998ef1 | 1139 | char dummy; |
1da177e4 | 1140 | |
31998ef1 | 1141 | if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || |
1da177e4 LT |
1142 | (pgnum > m->nr_priority_groups)) { |
1143 | DMWARN("invalid PG number supplied to switch_pg_num"); | |
1144 | return -EINVAL; | |
1145 | } | |
1146 | ||
1147 | spin_lock_irqsave(&m->lock, flags); | |
1148 | list_for_each_entry(pg, &m->priority_groups, list) { | |
be7d31cc | 1149 | pg->bypassed = false; |
1da177e4 LT |
1150 | if (--pgnum) |
1151 | continue; | |
1152 | ||
1153 | m->current_pgpath = NULL; | |
1154 | m->current_pg = NULL; | |
1155 | m->next_pg = pg; | |
1156 | } | |
1157 | spin_unlock_irqrestore(&m->lock, flags); | |
1158 | ||
fe9cf30e | 1159 | schedule_work(&m->trigger_event); |
1da177e4 LT |
1160 | return 0; |
1161 | } | |
1162 | ||
1163 | /* | |
1164 | * Set/clear bypassed status of a PG. | |
1165 | * PGs are numbered upwards from 1 in the order they were declared. | |
1166 | */ | |
be7d31cc | 1167 | static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed) |
1da177e4 LT |
1168 | { |
1169 | struct priority_group *pg; | |
1170 | unsigned pgnum; | |
31998ef1 | 1171 | char dummy; |
1da177e4 | 1172 | |
31998ef1 | 1173 | if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || |
1da177e4 LT |
1174 | (pgnum > m->nr_priority_groups)) { |
1175 | DMWARN("invalid PG number supplied to bypass_pg"); | |
1176 | return -EINVAL; | |
1177 | } | |
1178 | ||
1179 | list_for_each_entry(pg, &m->priority_groups, list) { | |
1180 | if (!--pgnum) | |
1181 | break; | |
1182 | } | |
1183 | ||
1184 | bypass_pg(m, pg, bypassed); | |
1185 | return 0; | |
1186 | } | |
1187 | ||
c9e45581 DW |
1188 | /* |
1189 | * Should we retry pg_init immediately? | |
1190 | */ | |
be7d31cc | 1191 | static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath) |
c9e45581 DW |
1192 | { |
1193 | unsigned long flags; | |
be7d31cc | 1194 | bool limit_reached = false; |
c9e45581 DW |
1195 | |
1196 | spin_lock_irqsave(&m->lock, flags); | |
1197 | ||
91e968aa MS |
1198 | if (atomic_read(&m->pg_init_count) <= m->pg_init_retries && |
1199 | !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) | |
518257b1 | 1200 | set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); |
c9e45581 | 1201 | else |
be7d31cc | 1202 | limit_reached = true; |
c9e45581 DW |
1203 | |
1204 | spin_unlock_irqrestore(&m->lock, flags); | |
1205 | ||
1206 | return limit_reached; | |
1207 | } | |
1208 | ||
3ae31f6a | 1209 | static void pg_init_done(void *data, int errors) |
cfae5c9b | 1210 | { |
83c0d5d5 | 1211 | struct pgpath *pgpath = data; |
cfae5c9b CS |
1212 | struct priority_group *pg = pgpath->pg; |
1213 | struct multipath *m = pg->m; | |
1214 | unsigned long flags; | |
be7d31cc | 1215 | bool delay_retry = false; |
cfae5c9b CS |
1216 | |
1217 | /* device or driver problems */ | |
1218 | switch (errors) { | |
1219 | case SCSI_DH_OK: | |
1220 | break; | |
1221 | case SCSI_DH_NOSYS: | |
1222 | if (!m->hw_handler_name) { | |
1223 | errors = 0; | |
1224 | break; | |
1225 | } | |
f7b934c8 MB |
1226 | DMERR("Could not failover the device: Handler scsi_dh_%s " |
1227 | "Error %d.", m->hw_handler_name, errors); | |
cfae5c9b CS |
1228 | /* |
1229 | * Fail path for now, so we do not ping pong | |
1230 | */ | |
1231 | fail_path(pgpath); | |
1232 | break; | |
1233 | case SCSI_DH_DEV_TEMP_BUSY: | |
1234 | /* | |
1235 | * Probably doing something like FW upgrade on the | |
1236 | * controller so try the other pg. | |
1237 | */ | |
be7d31cc | 1238 | bypass_pg(m, pg, true); |
cfae5c9b | 1239 | break; |
cfae5c9b | 1240 | case SCSI_DH_RETRY: |
4e2d19e4 CS |
1241 | /* Wait before retrying. */ |
1242 | delay_retry = 1; | |
cfae5c9b CS |
1243 | case SCSI_DH_IMM_RETRY: |
1244 | case SCSI_DH_RES_TEMP_UNAVAIL: | |
1245 | if (pg_init_limit_reached(m, pgpath)) | |
1246 | fail_path(pgpath); | |
1247 | errors = 0; | |
1248 | break; | |
ec31f3f7 | 1249 | case SCSI_DH_DEV_OFFLINED: |
cfae5c9b CS |
1250 | default: |
1251 | /* | |
1252 | * We probably do not want to fail the path for a device | |
1253 | * error, but this is what the old dm did. In future | |
1254 | * patches we can do more advanced handling. | |
1255 | */ | |
1256 | fail_path(pgpath); | |
1257 | } | |
1258 | ||
1259 | spin_lock_irqsave(&m->lock, flags); | |
1260 | if (errors) { | |
e54f77dd CS |
1261 | if (pgpath == m->current_pgpath) { |
1262 | DMERR("Could not failover device. Error %d.", errors); | |
1263 | m->current_pgpath = NULL; | |
1264 | m->current_pg = NULL; | |
1265 | } | |
518257b1 | 1266 | } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) |
be7d31cc | 1267 | pg->bypassed = false; |
cfae5c9b | 1268 | |
91e968aa | 1269 | if (atomic_dec_return(&m->pg_init_in_progress) > 0) |
d0259bf0 KU |
1270 | /* Activations of other paths are still on going */ |
1271 | goto out; | |
1272 | ||
518257b1 MS |
1273 | if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { |
1274 | if (delay_retry) | |
1275 | set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); | |
1276 | else | |
1277 | clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); | |
1278 | ||
3e9f1be1 HR |
1279 | if (__pg_init_all_paths(m)) |
1280 | goto out; | |
1281 | } | |
518257b1 | 1282 | clear_bit(MPATHF_QUEUE_IO, &m->flags); |
d0259bf0 | 1283 | |
2bded7bd KU |
1284 | /* |
1285 | * Wake up any thread waiting to suspend. | |
1286 | */ | |
1287 | wake_up(&m->pg_init_wait); | |
1288 | ||
d0259bf0 | 1289 | out: |
cfae5c9b CS |
1290 | spin_unlock_irqrestore(&m->lock, flags); |
1291 | } | |
1292 | ||
bab7cfc7 CS |
1293 | static void activate_path(struct work_struct *work) |
1294 | { | |
e54f77dd | 1295 | struct pgpath *pgpath = |
4e2d19e4 | 1296 | container_of(work, struct pgpath, activate_path.work); |
bab7cfc7 | 1297 | |
3a017509 HR |
1298 | if (pgpath->is_active) |
1299 | scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev), | |
1300 | pg_init_done, pgpath); | |
1301 | else | |
1302 | pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED); | |
bab7cfc7 CS |
1303 | } |
1304 | ||
7e782af5 HR |
1305 | static int noretry_error(int error) |
1306 | { | |
1307 | switch (error) { | |
1308 | case -EOPNOTSUPP: | |
1309 | case -EREMOTEIO: | |
1310 | case -EILSEQ: | |
1311 | case -ENODATA: | |
cc9d3c38 | 1312 | case -ENOSPC: |
7e782af5 HR |
1313 | return 1; |
1314 | } | |
1315 | ||
1316 | /* Anything else could be a path failure, so should be retried */ | |
1317 | return 0; | |
1318 | } | |
1319 | ||
1da177e4 LT |
1320 | /* |
1321 | * end_io handling | |
1322 | */ | |
f40c67f0 | 1323 | static int do_end_io(struct multipath *m, struct request *clone, |
028867ac | 1324 | int error, struct dm_mpath_io *mpio) |
1da177e4 | 1325 | { |
f40c67f0 KU |
1326 | /* |
1327 | * We don't queue any clone request inside the multipath target | |
1328 | * during end I/O handling, since those clone requests don't have | |
1329 | * bio clones. If we queue them inside the multipath target, | |
1330 | * we need to make bio clones, that requires memory allocation. | |
1331 | * (See drivers/md/dm.c:end_clone_bio() about why the clone requests | |
1332 | * don't have bio clones.) | |
1333 | * Instead of queueing the clone request here, we queue the original | |
1334 | * request into dm core, which will remake a clone request and | |
1335 | * clone bios for it and resubmit it later. | |
1336 | */ | |
1337 | int r = DM_ENDIO_REQUEUE; | |
1da177e4 | 1338 | |
f40c67f0 | 1339 | if (!error && !clone->errors) |
1da177e4 LT |
1340 | return 0; /* I/O complete */ |
1341 | ||
7eee4ae2 | 1342 | if (noretry_error(error)) |
959eb4e5 MS |
1343 | return error; |
1344 | ||
cfae5c9b CS |
1345 | if (mpio->pgpath) |
1346 | fail_path(mpio->pgpath); | |
1da177e4 | 1347 | |
91e968aa | 1348 | if (!atomic_read(&m->nr_valid_paths)) { |
518257b1 | 1349 | if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { |
2da1610a | 1350 | if (!must_push_back(m)) |
751b2a7d HR |
1351 | r = -EIO; |
1352 | } else { | |
1353 | if (error == -EBADE) | |
1354 | r = error; | |
1355 | } | |
1356 | } | |
1da177e4 | 1357 | |
f40c67f0 | 1358 | return r; |
1da177e4 LT |
1359 | } |
1360 | ||
f40c67f0 | 1361 | static int multipath_end_io(struct dm_target *ti, struct request *clone, |
1da177e4 LT |
1362 | int error, union map_info *map_context) |
1363 | { | |
028867ac | 1364 | struct multipath *m = ti->private; |
2eff1924 | 1365 | struct dm_mpath_io *mpio = get_mpio(map_context); |
a71a261f | 1366 | struct pgpath *pgpath; |
1da177e4 LT |
1367 | struct path_selector *ps; |
1368 | int r; | |
1369 | ||
466891f9 JN |
1370 | BUG_ON(!mpio); |
1371 | ||
2eff1924 | 1372 | r = do_end_io(m, clone, error, mpio); |
a71a261f | 1373 | pgpath = mpio->pgpath; |
1da177e4 LT |
1374 | if (pgpath) { |
1375 | ps = &pgpath->pg->ps; | |
1376 | if (ps->type->end_io) | |
02ab823f | 1377 | ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); |
1da177e4 | 1378 | } |
2eff1924 | 1379 | clear_request_fn_mpio(m, map_context); |
1da177e4 LT |
1380 | |
1381 | return r; | |
1382 | } | |
1383 | ||
1384 | /* | |
1385 | * Suspend can't complete until all the I/O is processed so if | |
436d4108 AK |
1386 | * the last path fails we must error any remaining I/O. |
1387 | * Note that if the freeze_bdev fails while suspending, the | |
1388 | * queue_if_no_path state is lost - userspace should reset it. | |
1da177e4 LT |
1389 | */ |
1390 | static void multipath_presuspend(struct dm_target *ti) | |
1391 | { | |
7943bd6d | 1392 | struct multipath *m = ti->private; |
1da177e4 | 1393 | |
be7d31cc | 1394 | queue_if_no_path(m, false, true); |
1da177e4 LT |
1395 | } |
1396 | ||
6df400ab KU |
1397 | static void multipath_postsuspend(struct dm_target *ti) |
1398 | { | |
6380f26f MA |
1399 | struct multipath *m = ti->private; |
1400 | ||
1401 | mutex_lock(&m->work_mutex); | |
2bded7bd | 1402 | flush_multipath_work(m); |
6380f26f | 1403 | mutex_unlock(&m->work_mutex); |
6df400ab KU |
1404 | } |
1405 | ||
436d4108 AK |
1406 | /* |
1407 | * Restore the queue_if_no_path setting. | |
1408 | */ | |
1da177e4 LT |
1409 | static void multipath_resume(struct dm_target *ti) |
1410 | { | |
7943bd6d | 1411 | struct multipath *m = ti->private; |
1da177e4 | 1412 | |
518257b1 MS |
1413 | if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) |
1414 | set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); | |
1415 | else | |
1416 | clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); | |
1417 | smp_mb__after_atomic(); | |
1da177e4 LT |
1418 | } |
1419 | ||
1420 | /* | |
1421 | * Info output has the following format: | |
1422 | * num_multipath_feature_args [multipath_feature_args]* | |
1423 | * num_handler_status_args [handler_status_args]* | |
1424 | * num_groups init_group_number | |
1425 | * [A|D|E num_ps_status_args [ps_status_args]* | |
1426 | * num_paths num_selector_args | |
1427 | * [path_dev A|F fail_count [selector_args]* ]+ ]+ | |
1428 | * | |
1429 | * Table output has the following format (identical to the constructor string): | |
1430 | * num_feature_args [features_args]* | |
1431 | * num_handler_args hw_handler [hw_handler_args]* | |
1432 | * num_groups init_group_number | |
1433 | * [priority selector-name num_ps_args [ps_args]* | |
1434 | * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ | |
1435 | */ | |
fd7c092e MP |
1436 | static void multipath_status(struct dm_target *ti, status_type_t type, |
1437 | unsigned status_flags, char *result, unsigned maxlen) | |
1da177e4 LT |
1438 | { |
1439 | int sz = 0; | |
1440 | unsigned long flags; | |
7943bd6d | 1441 | struct multipath *m = ti->private; |
1da177e4 LT |
1442 | struct priority_group *pg; |
1443 | struct pgpath *p; | |
1444 | unsigned pg_num; | |
1445 | char state; | |
1446 | ||
1447 | spin_lock_irqsave(&m->lock, flags); | |
1448 | ||
1449 | /* Features */ | |
1450 | if (type == STATUSTYPE_INFO) | |
91e968aa MS |
1451 | DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags), |
1452 | atomic_read(&m->pg_init_count)); | |
c9e45581 | 1453 | else { |
518257b1 | 1454 | DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) + |
4e2d19e4 | 1455 | (m->pg_init_retries > 0) * 2 + |
a58a935d | 1456 | (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 + |
518257b1 MS |
1457 | test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)); |
1458 | if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) | |
c9e45581 DW |
1459 | DMEMIT("queue_if_no_path "); |
1460 | if (m->pg_init_retries) | |
1461 | DMEMIT("pg_init_retries %u ", m->pg_init_retries); | |
4e2d19e4 CS |
1462 | if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) |
1463 | DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs); | |
518257b1 | 1464 | if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) |
a58a935d | 1465 | DMEMIT("retain_attached_hw_handler "); |
c9e45581 | 1466 | } |
1da177e4 | 1467 | |
cfae5c9b | 1468 | if (!m->hw_handler_name || type == STATUSTYPE_INFO) |
1da177e4 LT |
1469 | DMEMIT("0 "); |
1470 | else | |
cfae5c9b | 1471 | DMEMIT("1 %s ", m->hw_handler_name); |
1da177e4 LT |
1472 | |
1473 | DMEMIT("%u ", m->nr_priority_groups); | |
1474 | ||
1475 | if (m->next_pg) | |
1476 | pg_num = m->next_pg->pg_num; | |
1477 | else if (m->current_pg) | |
1478 | pg_num = m->current_pg->pg_num; | |
1479 | else | |
a490a07a | 1480 | pg_num = (m->nr_priority_groups ? 1 : 0); |
1da177e4 LT |
1481 | |
1482 | DMEMIT("%u ", pg_num); | |
1483 | ||
1484 | switch (type) { | |
1485 | case STATUSTYPE_INFO: | |
1486 | list_for_each_entry(pg, &m->priority_groups, list) { | |
1487 | if (pg->bypassed) | |
1488 | state = 'D'; /* Disabled */ | |
1489 | else if (pg == m->current_pg) | |
1490 | state = 'A'; /* Currently Active */ | |
1491 | else | |
1492 | state = 'E'; /* Enabled */ | |
1493 | ||
1494 | DMEMIT("%c ", state); | |
1495 | ||
1496 | if (pg->ps.type->status) | |
1497 | sz += pg->ps.type->status(&pg->ps, NULL, type, | |
1498 | result + sz, | |
1499 | maxlen - sz); | |
1500 | else | |
1501 | DMEMIT("0 "); | |
1502 | ||
1503 | DMEMIT("%u %u ", pg->nr_pgpaths, | |
1504 | pg->ps.type->info_args); | |
1505 | ||
1506 | list_for_each_entry(p, &pg->pgpaths, list) { | |
1507 | DMEMIT("%s %s %u ", p->path.dev->name, | |
6680073d | 1508 | p->is_active ? "A" : "F", |
1da177e4 LT |
1509 | p->fail_count); |
1510 | if (pg->ps.type->status) | |
1511 | sz += pg->ps.type->status(&pg->ps, | |
1512 | &p->path, type, result + sz, | |
1513 | maxlen - sz); | |
1514 | } | |
1515 | } | |
1516 | break; | |
1517 | ||
1518 | case STATUSTYPE_TABLE: | |
1519 | list_for_each_entry(pg, &m->priority_groups, list) { | |
1520 | DMEMIT("%s ", pg->ps.type->name); | |
1521 | ||
1522 | if (pg->ps.type->status) | |
1523 | sz += pg->ps.type->status(&pg->ps, NULL, type, | |
1524 | result + sz, | |
1525 | maxlen - sz); | |
1526 | else | |
1527 | DMEMIT("0 "); | |
1528 | ||
1529 | DMEMIT("%u %u ", pg->nr_pgpaths, | |
1530 | pg->ps.type->table_args); | |
1531 | ||
1532 | list_for_each_entry(p, &pg->pgpaths, list) { | |
1533 | DMEMIT("%s ", p->path.dev->name); | |
1534 | if (pg->ps.type->status) | |
1535 | sz += pg->ps.type->status(&pg->ps, | |
1536 | &p->path, type, result + sz, | |
1537 | maxlen - sz); | |
1538 | } | |
1539 | } | |
1540 | break; | |
1541 | } | |
1542 | ||
1543 | spin_unlock_irqrestore(&m->lock, flags); | |
1da177e4 LT |
1544 | } |
1545 | ||
1546 | static int multipath_message(struct dm_target *ti, unsigned argc, char **argv) | |
1547 | { | |
6380f26f | 1548 | int r = -EINVAL; |
1da177e4 | 1549 | struct dm_dev *dev; |
7943bd6d | 1550 | struct multipath *m = ti->private; |
1da177e4 LT |
1551 | action_fn action; |
1552 | ||
6380f26f MA |
1553 | mutex_lock(&m->work_mutex); |
1554 | ||
c2f3d24b KU |
1555 | if (dm_suspended(ti)) { |
1556 | r = -EBUSY; | |
1557 | goto out; | |
1558 | } | |
1559 | ||
1da177e4 | 1560 | if (argc == 1) { |
498f0103 | 1561 | if (!strcasecmp(argv[0], "queue_if_no_path")) { |
be7d31cc | 1562 | r = queue_if_no_path(m, true, false); |
6380f26f | 1563 | goto out; |
498f0103 | 1564 | } else if (!strcasecmp(argv[0], "fail_if_no_path")) { |
be7d31cc | 1565 | r = queue_if_no_path(m, false, false); |
6380f26f MA |
1566 | goto out; |
1567 | } | |
1da177e4 LT |
1568 | } |
1569 | ||
6380f26f | 1570 | if (argc != 2) { |
a356e426 | 1571 | DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc); |
6380f26f MA |
1572 | goto out; |
1573 | } | |
1da177e4 | 1574 | |
498f0103 | 1575 | if (!strcasecmp(argv[0], "disable_group")) { |
be7d31cc | 1576 | r = bypass_pg_num(m, argv[1], true); |
6380f26f | 1577 | goto out; |
498f0103 | 1578 | } else if (!strcasecmp(argv[0], "enable_group")) { |
be7d31cc | 1579 | r = bypass_pg_num(m, argv[1], false); |
6380f26f | 1580 | goto out; |
498f0103 | 1581 | } else if (!strcasecmp(argv[0], "switch_group")) { |
6380f26f MA |
1582 | r = switch_pg_num(m, argv[1]); |
1583 | goto out; | |
498f0103 | 1584 | } else if (!strcasecmp(argv[0], "reinstate_path")) |
1da177e4 | 1585 | action = reinstate_path; |
498f0103 | 1586 | else if (!strcasecmp(argv[0], "fail_path")) |
1da177e4 | 1587 | action = fail_path; |
6380f26f | 1588 | else { |
a356e426 | 1589 | DMWARN("Unrecognised multipath message received: %s", argv[0]); |
6380f26f MA |
1590 | goto out; |
1591 | } | |
1da177e4 | 1592 | |
8215d6ec | 1593 | r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev); |
1da177e4 | 1594 | if (r) { |
72d94861 | 1595 | DMWARN("message: error getting device %s", |
1da177e4 | 1596 | argv[1]); |
6380f26f | 1597 | goto out; |
1da177e4 LT |
1598 | } |
1599 | ||
1600 | r = action_dev(m, dev, action); | |
1601 | ||
1602 | dm_put_device(ti, dev); | |
1603 | ||
6380f26f MA |
1604 | out: |
1605 | mutex_unlock(&m->work_mutex); | |
1da177e4 | 1606 | return r; |
1da177e4 LT |
1607 | } |
1608 | ||
e56f81e0 CH |
1609 | static int multipath_prepare_ioctl(struct dm_target *ti, |
1610 | struct block_device **bdev, fmode_t *mode) | |
9af4aa30 | 1611 | { |
35991652 | 1612 | struct multipath *m = ti->private; |
2da1610a | 1613 | struct pgpath *current_pgpath; |
35991652 MP |
1614 | int r; |
1615 | ||
2da1610a MS |
1616 | current_pgpath = lockless_dereference(m->current_pgpath); |
1617 | if (!current_pgpath) | |
1618 | current_pgpath = choose_pgpath(m, 0); | |
9af4aa30 | 1619 | |
2da1610a | 1620 | if (current_pgpath) { |
518257b1 | 1621 | if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) { |
2da1610a MS |
1622 | *bdev = current_pgpath->path.dev->bdev; |
1623 | *mode = current_pgpath->path.dev->mode; | |
43e43c9e JN |
1624 | r = 0; |
1625 | } else { | |
1626 | /* pg_init has not started or completed */ | |
1627 | r = -ENOTCONN; | |
1628 | } | |
1629 | } else { | |
1630 | /* No path is available */ | |
518257b1 | 1631 | if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) |
43e43c9e JN |
1632 | r = -ENOTCONN; |
1633 | else | |
1634 | r = -EIO; | |
e90dae1f | 1635 | } |
9af4aa30 | 1636 | |
5bbbfdf6 | 1637 | if (r == -ENOTCONN) { |
2da1610a | 1638 | if (!lockless_dereference(m->current_pg)) { |
3e9f1be1 | 1639 | /* Path status changed, redo selection */ |
2da1610a | 1640 | (void) choose_pgpath(m, 0); |
3e9f1be1 | 1641 | } |
518257b1 | 1642 | if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) |
2da1610a | 1643 | pg_init_all_paths(m); |
63d832c3 | 1644 | dm_table_run_md_queue_async(m->ti->table); |
3e9f1be1 | 1645 | } |
35991652 | 1646 | |
e56f81e0 CH |
1647 | /* |
1648 | * Only pass ioctls through if the device sizes match exactly. | |
1649 | */ | |
1650 | if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT) | |
1651 | return 1; | |
1652 | return r; | |
9af4aa30 MB |
1653 | } |
1654 | ||
af4874e0 MS |
1655 | static int multipath_iterate_devices(struct dm_target *ti, |
1656 | iterate_devices_callout_fn fn, void *data) | |
1657 | { | |
1658 | struct multipath *m = ti->private; | |
1659 | struct priority_group *pg; | |
1660 | struct pgpath *p; | |
1661 | int ret = 0; | |
1662 | ||
1663 | list_for_each_entry(pg, &m->priority_groups, list) { | |
1664 | list_for_each_entry(p, &pg->pgpaths, list) { | |
5dea271b | 1665 | ret = fn(ti, p->path.dev, ti->begin, ti->len, data); |
af4874e0 MS |
1666 | if (ret) |
1667 | goto out; | |
1668 | } | |
1669 | } | |
1670 | ||
1671 | out: | |
1672 | return ret; | |
1673 | } | |
1674 | ||
9f54cec5 | 1675 | static int pgpath_busy(struct pgpath *pgpath) |
f40c67f0 KU |
1676 | { |
1677 | struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); | |
1678 | ||
52b09914 | 1679 | return blk_lld_busy(q); |
f40c67f0 KU |
1680 | } |
1681 | ||
1682 | /* | |
1683 | * We return "busy", only when we can map I/Os but underlying devices | |
1684 | * are busy (so even if we map I/Os now, the I/Os will wait on | |
1685 | * the underlying queue). | |
1686 | * In other words, if we want to kill I/Os or queue them inside us | |
1687 | * due to map unavailability, we don't return "busy". Otherwise, | |
1688 | * dm core won't give us the I/Os and we can't do what we want. | |
1689 | */ | |
1690 | static int multipath_busy(struct dm_target *ti) | |
1691 | { | |
be7d31cc | 1692 | bool busy = false, has_active = false; |
f40c67f0 | 1693 | struct multipath *m = ti->private; |
2da1610a | 1694 | struct priority_group *pg, *next_pg; |
f40c67f0 | 1695 | struct pgpath *pgpath; |
f40c67f0 | 1696 | |
7a7a3b45 | 1697 | /* pg_init in progress or no paths available */ |
91e968aa | 1698 | if (atomic_read(&m->pg_init_in_progress) || |
2da1610a MS |
1699 | (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) |
1700 | return true; | |
1701 | ||
f40c67f0 | 1702 | /* Guess which priority_group will be used at next mapping time */ |
2da1610a MS |
1703 | pg = lockless_dereference(m->current_pg); |
1704 | next_pg = lockless_dereference(m->next_pg); | |
1705 | if (unlikely(!lockless_dereference(m->current_pgpath) && next_pg)) | |
1706 | pg = next_pg; | |
1707 | ||
1708 | if (!pg) { | |
f40c67f0 KU |
1709 | /* |
1710 | * We don't know which pg will be used at next mapping time. | |
2da1610a | 1711 | * We don't call choose_pgpath() here to avoid to trigger |
f40c67f0 KU |
1712 | * pg_init just by busy checking. |
1713 | * So we don't know whether underlying devices we will be using | |
1714 | * at next mapping time are busy or not. Just try mapping. | |
1715 | */ | |
2da1610a MS |
1716 | return busy; |
1717 | } | |
f40c67f0 KU |
1718 | |
1719 | /* | |
1720 | * If there is one non-busy active path at least, the path selector | |
1721 | * will be able to select it. So we consider such a pg as not busy. | |
1722 | */ | |
be7d31cc | 1723 | busy = true; |
2da1610a | 1724 | list_for_each_entry(pgpath, &pg->pgpaths, list) { |
f40c67f0 | 1725 | if (pgpath->is_active) { |
be7d31cc | 1726 | has_active = true; |
9f54cec5 | 1727 | if (!pgpath_busy(pgpath)) { |
be7d31cc | 1728 | busy = false; |
f40c67f0 KU |
1729 | break; |
1730 | } | |
1731 | } | |
2da1610a | 1732 | } |
f40c67f0 | 1733 | |
2da1610a | 1734 | if (!has_active) { |
f40c67f0 KU |
1735 | /* |
1736 | * No active path in this pg, so this pg won't be used and | |
1737 | * the current_pg will be changed at next mapping time. | |
1738 | * We need to try mapping to determine it. | |
1739 | */ | |
be7d31cc | 1740 | busy = false; |
2da1610a | 1741 | } |
f40c67f0 KU |
1742 | |
1743 | return busy; | |
1744 | } | |
1745 | ||
1da177e4 LT |
1746 | /*----------------------------------------------------------------- |
1747 | * Module setup | |
1748 | *---------------------------------------------------------------*/ | |
1749 | static struct target_type multipath_target = { | |
1750 | .name = "multipath", | |
16f12266 MS |
1751 | .version = {1, 11, 0}, |
1752 | .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE, | |
1da177e4 LT |
1753 | .module = THIS_MODULE, |
1754 | .ctr = multipath_ctr, | |
1755 | .dtr = multipath_dtr, | |
f40c67f0 | 1756 | .map_rq = multipath_map, |
e5863d9a MS |
1757 | .clone_and_map_rq = multipath_clone_and_map, |
1758 | .release_clone_rq = multipath_release_clone, | |
f40c67f0 | 1759 | .rq_end_io = multipath_end_io, |
1da177e4 | 1760 | .presuspend = multipath_presuspend, |
6df400ab | 1761 | .postsuspend = multipath_postsuspend, |
1da177e4 LT |
1762 | .resume = multipath_resume, |
1763 | .status = multipath_status, | |
1764 | .message = multipath_message, | |
e56f81e0 | 1765 | .prepare_ioctl = multipath_prepare_ioctl, |
af4874e0 | 1766 | .iterate_devices = multipath_iterate_devices, |
f40c67f0 | 1767 | .busy = multipath_busy, |
1da177e4 LT |
1768 | }; |
1769 | ||
1770 | static int __init dm_multipath_init(void) | |
1771 | { | |
1772 | int r; | |
1773 | ||
1774 | /* allocate a slab for the dm_ios */ | |
028867ac | 1775 | _mpio_cache = KMEM_CACHE(dm_mpath_io, 0); |
1da177e4 LT |
1776 | if (!_mpio_cache) |
1777 | return -ENOMEM; | |
1778 | ||
1779 | r = dm_register_target(&multipath_target); | |
1780 | if (r < 0) { | |
0cd33124 | 1781 | DMERR("register failed %d", r); |
ff658e9c JT |
1782 | r = -EINVAL; |
1783 | goto bad_register_target; | |
1da177e4 LT |
1784 | } |
1785 | ||
4d4d66ab | 1786 | kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0); |
c557308e | 1787 | if (!kmultipathd) { |
0cd33124 | 1788 | DMERR("failed to create workqueue kmpathd"); |
ff658e9c JT |
1789 | r = -ENOMEM; |
1790 | goto bad_alloc_kmultipathd; | |
c557308e AK |
1791 | } |
1792 | ||
bab7cfc7 CS |
1793 | /* |
1794 | * A separate workqueue is used to handle the device handlers | |
1795 | * to avoid overloading existing workqueue. Overloading the | |
1796 | * old workqueue would also create a bottleneck in the | |
1797 | * path of the storage hardware device activation. | |
1798 | */ | |
4d4d66ab TH |
1799 | kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd", |
1800 | WQ_MEM_RECLAIM); | |
bab7cfc7 CS |
1801 | if (!kmpath_handlerd) { |
1802 | DMERR("failed to create workqueue kmpath_handlerd"); | |
ff658e9c JT |
1803 | r = -ENOMEM; |
1804 | goto bad_alloc_kmpath_handlerd; | |
bab7cfc7 CS |
1805 | } |
1806 | ||
72d94861 | 1807 | DMINFO("version %u.%u.%u loaded", |
1da177e4 LT |
1808 | multipath_target.version[0], multipath_target.version[1], |
1809 | multipath_target.version[2]); | |
1810 | ||
ff658e9c JT |
1811 | return 0; |
1812 | ||
1813 | bad_alloc_kmpath_handlerd: | |
1814 | destroy_workqueue(kmultipathd); | |
1815 | bad_alloc_kmultipathd: | |
1816 | dm_unregister_target(&multipath_target); | |
1817 | bad_register_target: | |
1818 | kmem_cache_destroy(_mpio_cache); | |
1819 | ||
1da177e4 LT |
1820 | return r; |
1821 | } | |
1822 | ||
1823 | static void __exit dm_multipath_exit(void) | |
1824 | { | |
bab7cfc7 | 1825 | destroy_workqueue(kmpath_handlerd); |
c557308e AK |
1826 | destroy_workqueue(kmultipathd); |
1827 | ||
10d3bd09 | 1828 | dm_unregister_target(&multipath_target); |
1da177e4 LT |
1829 | kmem_cache_destroy(_mpio_cache); |
1830 | } | |
1831 | ||
1da177e4 LT |
1832 | module_init(dm_multipath_init); |
1833 | module_exit(dm_multipath_exit); | |
1834 | ||
1835 | MODULE_DESCRIPTION(DM_NAME " multipath target"); | |
1836 | MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); | |
1837 | MODULE_LICENSE("GPL"); |