11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (C) 2003 Sistina Software Limited. 31da177e4SLinus Torvalds * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This file is released under the GPL. 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 8586e80e6SMikulas Patocka #include <linux/device-mapper.h> 9586e80e6SMikulas Patocka 101da177e4SLinus Torvalds #include "dm-path-selector.h" 11b15546f9SMike Anderson #include "dm-uevent.h" 121da177e4SLinus Torvalds 131da177e4SLinus Torvalds #include <linux/ctype.h> 141da177e4SLinus Torvalds #include <linux/init.h> 151da177e4SLinus Torvalds #include <linux/mempool.h> 161da177e4SLinus Torvalds #include <linux/module.h> 171da177e4SLinus Torvalds #include <linux/pagemap.h> 181da177e4SLinus Torvalds #include <linux/slab.h> 191da177e4SLinus Torvalds #include <linux/time.h> 201da177e4SLinus Torvalds #include <linux/workqueue.h> 21cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h> 2260063497SArun Sharma #include <linux/atomic.h> 231da177e4SLinus Torvalds 2472d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath" 254e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000 264e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1) 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds /* Path properties */ 291da177e4SLinus Torvalds struct pgpath { 301da177e4SLinus Torvalds struct list_head list; 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds struct priority_group *pg; /* Owning PG */ 336680073dSKiyoshi Ueda unsigned is_active; /* Path status */ 341da177e4SLinus Torvalds unsigned fail_count; /* Cumulative failure count */ 351da177e4SLinus Torvalds 36c922d5f7SJosef "Jeff" Sipek struct dm_path path; 374e2d19e4SChandra Seetharaman struct delayed_work activate_path; 381da177e4SLinus Torvalds }; 391da177e4SLinus Torvalds 401da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path) 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds /* 431da177e4SLinus Torvalds * Paths are grouped into Priority Groups and numbered from 1 upwards. 441da177e4SLinus Torvalds * Each has a path selector which controls which path gets used. 451da177e4SLinus Torvalds */ 461da177e4SLinus Torvalds struct priority_group { 471da177e4SLinus Torvalds struct list_head list; 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds struct multipath *m; /* Owning multipath instance */ 501da177e4SLinus Torvalds struct path_selector ps; 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds unsigned pg_num; /* Reference number */ 531da177e4SLinus Torvalds unsigned bypassed; /* Temporarily bypass this PG? */ 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds unsigned nr_pgpaths; /* Number of paths in PG */ 561da177e4SLinus Torvalds struct list_head pgpaths; 571da177e4SLinus Torvalds }; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds /* Multipath context */ 601da177e4SLinus Torvalds struct multipath { 611da177e4SLinus Torvalds struct list_head list; 621da177e4SLinus Torvalds struct dm_target *ti; 631da177e4SLinus Torvalds 64cfae5c9bSChandra Seetharaman const char *hw_handler_name; 652bfd2e13SChandra Seetharaman char *hw_handler_params; 664e2d19e4SChandra Seetharaman 671fbdd2b3SMike Snitzer spinlock_t lock; 681fbdd2b3SMike Snitzer 691da177e4SLinus Torvalds unsigned nr_priority_groups; 701da177e4SLinus Torvalds struct list_head priority_groups; 714e2d19e4SChandra Seetharaman 724e2d19e4SChandra Seetharaman wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */ 734e2d19e4SChandra Seetharaman 741da177e4SLinus Torvalds unsigned pg_init_required; /* pg_init needs calling? */ 75c3cd4f6bSAlasdair G Kergon unsigned pg_init_in_progress; /* Only one pg_init allowed at once */ 764e2d19e4SChandra Seetharaman unsigned pg_init_delay_retry; /* Delay pg_init retry? */ 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds unsigned nr_valid_paths; /* Total number of usable paths */ 791da177e4SLinus Torvalds struct pgpath *current_pgpath; 801da177e4SLinus Torvalds struct priority_group *current_pg; 811da177e4SLinus Torvalds struct priority_group *next_pg; /* Switch to this PG if set */ 821da177e4SLinus Torvalds unsigned repeat_count; /* I/Os left before calling PS again */ 831da177e4SLinus Torvalds 841fbdd2b3SMike Snitzer unsigned queue_io:1; /* Must we queue all I/O? */ 851fbdd2b3SMike Snitzer unsigned queue_if_no_path:1; /* Queue I/O if last path fails? */ 861fbdd2b3SMike Snitzer unsigned saved_queue_if_no_path:1; /* Saved state during suspension */ 871fbdd2b3SMike Snitzer 88c9e45581SDave Wysochanski unsigned pg_init_retries; /* Number of times to retry pg_init */ 89c9e45581SDave Wysochanski unsigned pg_init_count; /* Number of times pg_init called */ 904e2d19e4SChandra Seetharaman unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */ 911da177e4SLinus Torvalds 921fbdd2b3SMike Snitzer unsigned queue_size; 931da177e4SLinus Torvalds struct work_struct process_queued_ios; 94f40c67f0SKiyoshi Ueda struct list_head queued_ios; 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds struct work_struct trigger_event; 971da177e4SLinus Torvalds 981da177e4SLinus Torvalds /* 99028867acSAlasdair G Kergon * We must use a mempool of dm_mpath_io structs so that we 1001da177e4SLinus Torvalds * can resubmit bios on error. 1011da177e4SLinus Torvalds */ 1021da177e4SLinus Torvalds mempool_t *mpio_pool; 1036380f26fSMike Anderson 1046380f26fSMike Anderson struct mutex work_mutex; 1051da177e4SLinus Torvalds }; 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * Context information attached to each bio we process. 1091da177e4SLinus Torvalds */ 110028867acSAlasdair G Kergon struct dm_mpath_io { 1111da177e4SLinus Torvalds struct pgpath *pgpath; 11202ab823fSKiyoshi Ueda size_t nr_bytes; 1131da177e4SLinus Torvalds }; 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath); 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds #define MIN_IOS 256 /* Mempool size */ 1181da177e4SLinus Torvalds 119e18b890bSChristoph Lameter static struct kmem_cache *_mpio_cache; 1201da177e4SLinus Torvalds 121bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd; 122c4028958SDavid Howells static void process_queued_ios(struct work_struct *work); 123c4028958SDavid Howells static void trigger_event(struct work_struct *work); 124bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work); 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /*----------------------------------------------- 1281da177e4SLinus Torvalds * Allocation routines 1291da177e4SLinus Torvalds *-----------------------------------------------*/ 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void) 1321da177e4SLinus Torvalds { 133e69fae56SMicha³ Miros³aw struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL); 1341da177e4SLinus Torvalds 135224cb3e9SMike Anderson if (pgpath) { 1366680073dSKiyoshi Ueda pgpath->is_active = 1; 1374e2d19e4SChandra Seetharaman INIT_DELAYED_WORK(&pgpath->activate_path, activate_path); 138224cb3e9SMike Anderson } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds return pgpath; 1411da177e4SLinus Torvalds } 1421da177e4SLinus Torvalds 143028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath) 1441da177e4SLinus Torvalds { 1451da177e4SLinus Torvalds kfree(pgpath); 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void) 1491da177e4SLinus Torvalds { 1501da177e4SLinus Torvalds struct priority_group *pg; 1511da177e4SLinus Torvalds 152e69fae56SMicha³ Miros³aw pg = kzalloc(sizeof(*pg), GFP_KERNEL); 1531da177e4SLinus Torvalds 154e69fae56SMicha³ Miros³aw if (pg) 1551da177e4SLinus Torvalds INIT_LIST_HEAD(&pg->pgpaths); 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds return pg; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds struct pgpath *pgpath, *tmp; 163ae11b1b3SHannes Reinecke struct multipath *m = ti->private; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds list_for_each_entry_safe(pgpath, tmp, pgpaths, list) { 1661da177e4SLinus Torvalds list_del(&pgpath->list); 167ae11b1b3SHannes Reinecke if (m->hw_handler_name) 168ae11b1b3SHannes Reinecke scsi_dh_detach(bdev_get_queue(pgpath->path.dev->bdev)); 1691da177e4SLinus Torvalds dm_put_device(ti, pgpath->path.dev); 1701da177e4SLinus Torvalds free_pgpath(pgpath); 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg, 1751da177e4SLinus Torvalds struct dm_target *ti) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds struct path_selector *ps = &pg->ps; 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds if (ps->type) { 1801da177e4SLinus Torvalds ps->type->destroy(ps); 1811da177e4SLinus Torvalds dm_put_path_selector(ps->type); 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds free_pgpaths(&pg->pgpaths, ti); 1851da177e4SLinus Torvalds kfree(pg); 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 18828f16c20SMicha³ Miros³aw static struct multipath *alloc_multipath(struct dm_target *ti) 1891da177e4SLinus Torvalds { 1901da177e4SLinus Torvalds struct multipath *m; 1911da177e4SLinus Torvalds 192e69fae56SMicha³ Miros³aw m = kzalloc(sizeof(*m), GFP_KERNEL); 1931da177e4SLinus Torvalds if (m) { 1941da177e4SLinus Torvalds INIT_LIST_HEAD(&m->priority_groups); 195f40c67f0SKiyoshi Ueda INIT_LIST_HEAD(&m->queued_ios); 1961da177e4SLinus Torvalds spin_lock_init(&m->lock); 1971da177e4SLinus Torvalds m->queue_io = 1; 1984e2d19e4SChandra Seetharaman m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT; 199c4028958SDavid Howells INIT_WORK(&m->process_queued_ios, process_queued_ios); 200c4028958SDavid Howells INIT_WORK(&m->trigger_event, trigger_event); 2012bded7bdSKiyoshi Ueda init_waitqueue_head(&m->pg_init_wait); 2026380f26fSMike Anderson mutex_init(&m->work_mutex); 20393d2341cSMatthew Dobson m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache); 2041da177e4SLinus Torvalds if (!m->mpio_pool) { 2051da177e4SLinus Torvalds kfree(m); 2061da177e4SLinus Torvalds return NULL; 2071da177e4SLinus Torvalds } 20828f16c20SMicha³ Miros³aw m->ti = ti; 20928f16c20SMicha³ Miros³aw ti->private = m; 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds return m; 2131da177e4SLinus Torvalds } 2141da177e4SLinus Torvalds 2151da177e4SLinus Torvalds static void free_multipath(struct multipath *m) 2161da177e4SLinus Torvalds { 2171da177e4SLinus Torvalds struct priority_group *pg, *tmp; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { 2201da177e4SLinus Torvalds list_del(&pg->list); 2211da177e4SLinus Torvalds free_priority_group(pg, m->ti); 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 224cfae5c9bSChandra Seetharaman kfree(m->hw_handler_name); 2252bfd2e13SChandra Seetharaman kfree(m->hw_handler_params); 2261da177e4SLinus Torvalds mempool_destroy(m->mpio_pool); 2271da177e4SLinus Torvalds kfree(m); 2281da177e4SLinus Torvalds } 2291da177e4SLinus Torvalds 230466891f9SJun'ichi Nomura static int set_mapinfo(struct multipath *m, union map_info *info) 231466891f9SJun'ichi Nomura { 232466891f9SJun'ichi Nomura struct dm_mpath_io *mpio; 233466891f9SJun'ichi Nomura 234466891f9SJun'ichi Nomura mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC); 235466891f9SJun'ichi Nomura if (!mpio) 236466891f9SJun'ichi Nomura return -ENOMEM; 237466891f9SJun'ichi Nomura 238466891f9SJun'ichi Nomura memset(mpio, 0, sizeof(*mpio)); 239466891f9SJun'ichi Nomura info->ptr = mpio; 240466891f9SJun'ichi Nomura 241466891f9SJun'ichi Nomura return 0; 242466891f9SJun'ichi Nomura } 243466891f9SJun'ichi Nomura 244466891f9SJun'ichi Nomura static void clear_mapinfo(struct multipath *m, union map_info *info) 245466891f9SJun'ichi Nomura { 246466891f9SJun'ichi Nomura struct dm_mpath_io *mpio = info->ptr; 247466891f9SJun'ichi Nomura 248466891f9SJun'ichi Nomura info->ptr = NULL; 249466891f9SJun'ichi Nomura mempool_free(mpio, m->mpio_pool); 250466891f9SJun'ichi Nomura } 2511da177e4SLinus Torvalds 2521da177e4SLinus Torvalds /*----------------------------------------------- 2531da177e4SLinus Torvalds * Path selection 2541da177e4SLinus Torvalds *-----------------------------------------------*/ 2551da177e4SLinus Torvalds 256fb612642SKiyoshi Ueda static void __pg_init_all_paths(struct multipath *m) 257fb612642SKiyoshi Ueda { 258fb612642SKiyoshi Ueda struct pgpath *pgpath; 2594e2d19e4SChandra Seetharaman unsigned long pg_init_delay = 0; 260fb612642SKiyoshi Ueda 261fb612642SKiyoshi Ueda m->pg_init_count++; 262fb612642SKiyoshi Ueda m->pg_init_required = 0; 2634e2d19e4SChandra Seetharaman if (m->pg_init_delay_retry) 2644e2d19e4SChandra Seetharaman pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ? 2654e2d19e4SChandra Seetharaman m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS); 266fb612642SKiyoshi Ueda list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) { 267fb612642SKiyoshi Ueda /* Skip failed paths */ 268fb612642SKiyoshi Ueda if (!pgpath->is_active) 269fb612642SKiyoshi Ueda continue; 2704e2d19e4SChandra Seetharaman if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path, 2714e2d19e4SChandra Seetharaman pg_init_delay)) 272fb612642SKiyoshi Ueda m->pg_init_in_progress++; 273fb612642SKiyoshi Ueda } 274fb612642SKiyoshi Ueda } 275fb612642SKiyoshi Ueda 2761da177e4SLinus Torvalds static void __switch_pg(struct multipath *m, struct pgpath *pgpath) 2771da177e4SLinus Torvalds { 2781da177e4SLinus Torvalds m->current_pg = pgpath->pg; 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds /* Must we initialise the PG first, and queue I/O till it's ready? */ 281cfae5c9bSChandra Seetharaman if (m->hw_handler_name) { 2821da177e4SLinus Torvalds m->pg_init_required = 1; 2831da177e4SLinus Torvalds m->queue_io = 1; 2841da177e4SLinus Torvalds } else { 2851da177e4SLinus Torvalds m->pg_init_required = 0; 2861da177e4SLinus Torvalds m->queue_io = 0; 2871da177e4SLinus Torvalds } 288c9e45581SDave Wysochanski 289c9e45581SDave Wysochanski m->pg_init_count = 0; 2901da177e4SLinus Torvalds } 2911da177e4SLinus Torvalds 29202ab823fSKiyoshi Ueda static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg, 29302ab823fSKiyoshi Ueda size_t nr_bytes) 2941da177e4SLinus Torvalds { 295c922d5f7SJosef "Jeff" Sipek struct dm_path *path; 2961da177e4SLinus Torvalds 29702ab823fSKiyoshi Ueda path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes); 2981da177e4SLinus Torvalds if (!path) 2991da177e4SLinus Torvalds return -ENXIO; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds m->current_pgpath = path_to_pgpath(path); 3021da177e4SLinus Torvalds 3031da177e4SLinus Torvalds if (m->current_pg != pg) 3041da177e4SLinus Torvalds __switch_pg(m, m->current_pgpath); 3051da177e4SLinus Torvalds 3061da177e4SLinus Torvalds return 0; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 30902ab823fSKiyoshi Ueda static void __choose_pgpath(struct multipath *m, size_t nr_bytes) 3101da177e4SLinus Torvalds { 3111da177e4SLinus Torvalds struct priority_group *pg; 3121da177e4SLinus Torvalds unsigned bypassed = 1; 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds if (!m->nr_valid_paths) 3151da177e4SLinus Torvalds goto failed; 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds /* Were we instructed to switch PG? */ 3181da177e4SLinus Torvalds if (m->next_pg) { 3191da177e4SLinus Torvalds pg = m->next_pg; 3201da177e4SLinus Torvalds m->next_pg = NULL; 32102ab823fSKiyoshi Ueda if (!__choose_path_in_pg(m, pg, nr_bytes)) 3221da177e4SLinus Torvalds return; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds /* Don't change PG until it has no remaining paths */ 32602ab823fSKiyoshi Ueda if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes)) 3271da177e4SLinus Torvalds return; 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds /* 3301da177e4SLinus Torvalds * Loop through priority groups until we find a valid path. 3311da177e4SLinus Torvalds * First time we skip PGs marked 'bypassed'. 332*f220fd4eSMike Christie * Second time we only try the ones we skipped, but set 333*f220fd4eSMike Christie * pg_init_delay_retry so we do not hammer controllers. 3341da177e4SLinus Torvalds */ 3351da177e4SLinus Torvalds do { 3361da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 3371da177e4SLinus Torvalds if (pg->bypassed == bypassed) 3381da177e4SLinus Torvalds continue; 339*f220fd4eSMike Christie if (!__choose_path_in_pg(m, pg, nr_bytes)) { 340*f220fd4eSMike Christie if (!bypassed) 341*f220fd4eSMike Christie m->pg_init_delay_retry = 1; 3421da177e4SLinus Torvalds return; 3431da177e4SLinus Torvalds } 344*f220fd4eSMike Christie } 3451da177e4SLinus Torvalds } while (bypassed--); 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds failed: 3481da177e4SLinus Torvalds m->current_pgpath = NULL; 3491da177e4SLinus Torvalds m->current_pg = NULL; 3501da177e4SLinus Torvalds } 3511da177e4SLinus Torvalds 35245e15720SKiyoshi Ueda /* 35345e15720SKiyoshi Ueda * Check whether bios must be queued in the device-mapper core rather 35445e15720SKiyoshi Ueda * than here in the target. 35545e15720SKiyoshi Ueda * 35645e15720SKiyoshi Ueda * m->lock must be held on entry. 35745e15720SKiyoshi Ueda * 35845e15720SKiyoshi Ueda * If m->queue_if_no_path and m->saved_queue_if_no_path hold the 35945e15720SKiyoshi Ueda * same value then we are not between multipath_presuspend() 36045e15720SKiyoshi Ueda * and multipath_resume() calls and we have no need to check 36145e15720SKiyoshi Ueda * for the DMF_NOFLUSH_SUSPENDING flag. 36245e15720SKiyoshi Ueda */ 36345e15720SKiyoshi Ueda static int __must_push_back(struct multipath *m) 36445e15720SKiyoshi Ueda { 36545e15720SKiyoshi Ueda return (m->queue_if_no_path != m->saved_queue_if_no_path && 36645e15720SKiyoshi Ueda dm_noflush_suspending(m->ti)); 36745e15720SKiyoshi Ueda } 36845e15720SKiyoshi Ueda 369f40c67f0SKiyoshi Ueda static int map_io(struct multipath *m, struct request *clone, 370466891f9SJun'ichi Nomura union map_info *map_context, unsigned was_queued) 3711da177e4SLinus Torvalds { 372d2a7ad29SKiyoshi Ueda int r = DM_MAPIO_REMAPPED; 373f40c67f0SKiyoshi Ueda size_t nr_bytes = blk_rq_bytes(clone); 3741da177e4SLinus Torvalds unsigned long flags; 3751da177e4SLinus Torvalds struct pgpath *pgpath; 376f40c67f0SKiyoshi Ueda struct block_device *bdev; 377466891f9SJun'ichi Nomura struct dm_mpath_io *mpio = map_context->ptr; 3781da177e4SLinus Torvalds 3791da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds /* Do we need to select a new pgpath? */ 3821da177e4SLinus Torvalds if (!m->current_pgpath || 3831da177e4SLinus Torvalds (!m->queue_io && (m->repeat_count && --m->repeat_count == 0))) 38402ab823fSKiyoshi Ueda __choose_pgpath(m, nr_bytes); 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds pgpath = m->current_pgpath; 3871da177e4SLinus Torvalds 3881da177e4SLinus Torvalds if (was_queued) 3891da177e4SLinus Torvalds m->queue_size--; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds if ((pgpath && m->queue_io) || 392436d4108SAlasdair G Kergon (!pgpath && m->queue_if_no_path)) { 3931da177e4SLinus Torvalds /* Queue for the daemon to resubmit */ 394f40c67f0SKiyoshi Ueda list_add_tail(&clone->queuelist, &m->queued_ios); 3951da177e4SLinus Torvalds m->queue_size++; 396c3cd4f6bSAlasdair G Kergon if ((m->pg_init_required && !m->pg_init_in_progress) || 397c3cd4f6bSAlasdair G Kergon !m->queue_io) 398c557308eSAlasdair G Kergon queue_work(kmultipathd, &m->process_queued_ios); 3991da177e4SLinus Torvalds pgpath = NULL; 400d2a7ad29SKiyoshi Ueda r = DM_MAPIO_SUBMITTED; 401f40c67f0SKiyoshi Ueda } else if (pgpath) { 402f40c67f0SKiyoshi Ueda bdev = pgpath->path.dev->bdev; 403f40c67f0SKiyoshi Ueda clone->q = bdev_get_queue(bdev); 404f40c67f0SKiyoshi Ueda clone->rq_disk = bdev->bd_disk; 405f40c67f0SKiyoshi Ueda } else if (__must_push_back(m)) 40645e15720SKiyoshi Ueda r = DM_MAPIO_REQUEUE; 40745e15720SKiyoshi Ueda else 40845e15720SKiyoshi Ueda r = -EIO; /* Failed */ 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds mpio->pgpath = pgpath; 41102ab823fSKiyoshi Ueda mpio->nr_bytes = nr_bytes; 41202ab823fSKiyoshi Ueda 41302ab823fSKiyoshi Ueda if (r == DM_MAPIO_REMAPPED && pgpath->pg->ps.type->start_io) 41402ab823fSKiyoshi Ueda pgpath->pg->ps.type->start_io(&pgpath->pg->ps, &pgpath->path, 41502ab823fSKiyoshi Ueda nr_bytes); 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds return r; 4201da177e4SLinus Torvalds } 4211da177e4SLinus Torvalds 4221da177e4SLinus Torvalds /* 4231da177e4SLinus Torvalds * If we run out of usable paths, should we queue I/O or error it? 4241da177e4SLinus Torvalds */ 425485ef69eSAlasdair G Kergon static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path, 426485ef69eSAlasdair G Kergon unsigned save_old_value) 4271da177e4SLinus Torvalds { 4281da177e4SLinus Torvalds unsigned long flags; 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 4311da177e4SLinus Torvalds 432485ef69eSAlasdair G Kergon if (save_old_value) 433436d4108SAlasdair G Kergon m->saved_queue_if_no_path = m->queue_if_no_path; 434485ef69eSAlasdair G Kergon else 435485ef69eSAlasdair G Kergon m->saved_queue_if_no_path = queue_if_no_path; 4361da177e4SLinus Torvalds m->queue_if_no_path = queue_if_no_path; 437c3cd4f6bSAlasdair G Kergon if (!m->queue_if_no_path && m->queue_size) 438c557308eSAlasdair G Kergon queue_work(kmultipathd, &m->process_queued_ios); 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds return 0; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds /*----------------------------------------------------------------- 4461da177e4SLinus Torvalds * The multipath daemon is responsible for resubmitting queued ios. 4471da177e4SLinus Torvalds *---------------------------------------------------------------*/ 4481da177e4SLinus Torvalds 4491da177e4SLinus Torvalds static void dispatch_queued_ios(struct multipath *m) 4501da177e4SLinus Torvalds { 4511da177e4SLinus Torvalds int r; 4521da177e4SLinus Torvalds unsigned long flags; 4531da177e4SLinus Torvalds union map_info *info; 454f40c67f0SKiyoshi Ueda struct request *clone, *n; 455f40c67f0SKiyoshi Ueda LIST_HEAD(cl); 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 458f40c67f0SKiyoshi Ueda list_splice_init(&m->queued_ios, &cl); 4591da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 4601da177e4SLinus Torvalds 461f40c67f0SKiyoshi Ueda list_for_each_entry_safe(clone, n, &cl, queuelist) { 462f40c67f0SKiyoshi Ueda list_del_init(&clone->queuelist); 4631da177e4SLinus Torvalds 464f40c67f0SKiyoshi Ueda info = dm_get_rq_mapinfo(clone); 4651da177e4SLinus Torvalds 466466891f9SJun'ichi Nomura r = map_io(m, clone, info, 1); 467f40c67f0SKiyoshi Ueda if (r < 0) { 468466891f9SJun'ichi Nomura clear_mapinfo(m, info); 469f40c67f0SKiyoshi Ueda dm_kill_unmapped_request(clone, r); 470f40c67f0SKiyoshi Ueda } else if (r == DM_MAPIO_REMAPPED) 471f40c67f0SKiyoshi Ueda dm_dispatch_request(clone); 472f40c67f0SKiyoshi Ueda else if (r == DM_MAPIO_REQUEUE) { 473466891f9SJun'ichi Nomura clear_mapinfo(m, info); 474f40c67f0SKiyoshi Ueda dm_requeue_unmapped_request(clone); 475f40c67f0SKiyoshi Ueda } 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds } 4781da177e4SLinus Torvalds 479c4028958SDavid Howells static void process_queued_ios(struct work_struct *work) 4801da177e4SLinus Torvalds { 481c4028958SDavid Howells struct multipath *m = 482c4028958SDavid Howells container_of(work, struct multipath, process_queued_ios); 483fb612642SKiyoshi Ueda struct pgpath *pgpath = NULL; 484e54f77ddSChandra Seetharaman unsigned must_queue = 1; 4851da177e4SLinus Torvalds unsigned long flags; 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 4881da177e4SLinus Torvalds 489c3cd4f6bSAlasdair G Kergon if (!m->queue_size) 490c3cd4f6bSAlasdair G Kergon goto out; 491c3cd4f6bSAlasdair G Kergon 4921da177e4SLinus Torvalds if (!m->current_pgpath) 49302ab823fSKiyoshi Ueda __choose_pgpath(m, 0); 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds pgpath = m->current_pgpath; 4961da177e4SLinus Torvalds 497c3cd4f6bSAlasdair G Kergon if ((pgpath && !m->queue_io) || 498c3cd4f6bSAlasdair G Kergon (!pgpath && !m->queue_if_no_path)) 499c3cd4f6bSAlasdair G Kergon must_queue = 0; 5001da177e4SLinus Torvalds 501fb612642SKiyoshi Ueda if (m->pg_init_required && !m->pg_init_in_progress && pgpath) 502fb612642SKiyoshi Ueda __pg_init_all_paths(m); 503fb612642SKiyoshi Ueda 504c3cd4f6bSAlasdair G Kergon out: 5051da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 5061da177e4SLinus Torvalds if (!must_queue) 5071da177e4SLinus Torvalds dispatch_queued_ios(m); 5081da177e4SLinus Torvalds } 5091da177e4SLinus Torvalds 5101da177e4SLinus Torvalds /* 5111da177e4SLinus Torvalds * An event is triggered whenever a path is taken out of use. 5121da177e4SLinus Torvalds * Includes path failure and PG bypass. 5131da177e4SLinus Torvalds */ 514c4028958SDavid Howells static void trigger_event(struct work_struct *work) 5151da177e4SLinus Torvalds { 516c4028958SDavid Howells struct multipath *m = 517c4028958SDavid Howells container_of(work, struct multipath, trigger_event); 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds dm_table_event(m->ti->table); 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds /*----------------------------------------------------------------- 5231da177e4SLinus Torvalds * Constructor/argument parsing: 5241da177e4SLinus Torvalds * <#multipath feature args> [<arg>]* 5251da177e4SLinus Torvalds * <#hw_handler args> [hw_handler [<arg>]*] 5261da177e4SLinus Torvalds * <#priority groups> 5271da177e4SLinus Torvalds * <initial priority group> 5281da177e4SLinus Torvalds * [<selector> <#selector args> [<arg>]* 5291da177e4SLinus Torvalds * <#paths> <#per-path selector args> 5301da177e4SLinus Torvalds * [<path> [<arg>]* ]+ ]+ 5311da177e4SLinus Torvalds *---------------------------------------------------------------*/ 532498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg, 5331da177e4SLinus Torvalds struct dm_target *ti) 5341da177e4SLinus Torvalds { 5351da177e4SLinus Torvalds int r; 5361da177e4SLinus Torvalds struct path_selector_type *pst; 5371da177e4SLinus Torvalds unsigned ps_argc; 5381da177e4SLinus Torvalds 539498f0103SMike Snitzer static struct dm_arg _args[] = { 54072d94861SAlasdair G Kergon {0, 1024, "invalid number of path selector args"}, 5411da177e4SLinus Torvalds }; 5421da177e4SLinus Torvalds 543498f0103SMike Snitzer pst = dm_get_path_selector(dm_shift_arg(as)); 5441da177e4SLinus Torvalds if (!pst) { 54572d94861SAlasdair G Kergon ti->error = "unknown path selector type"; 5461da177e4SLinus Torvalds return -EINVAL; 5471da177e4SLinus Torvalds } 5481da177e4SLinus Torvalds 549498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &ps_argc, &ti->error); 550371b2e34SMikulas Patocka if (r) { 551371b2e34SMikulas Patocka dm_put_path_selector(pst); 5521da177e4SLinus Torvalds return -EINVAL; 553371b2e34SMikulas Patocka } 5541da177e4SLinus Torvalds 5551da177e4SLinus Torvalds r = pst->create(&pg->ps, ps_argc, as->argv); 5561da177e4SLinus Torvalds if (r) { 5571da177e4SLinus Torvalds dm_put_path_selector(pst); 55872d94861SAlasdair G Kergon ti->error = "path selector constructor failed"; 5591da177e4SLinus Torvalds return r; 5601da177e4SLinus Torvalds } 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds pg->ps.type = pst; 563498f0103SMike Snitzer dm_consume_args(as, ps_argc); 5641da177e4SLinus Torvalds 5651da177e4SLinus Torvalds return 0; 5661da177e4SLinus Torvalds } 5671da177e4SLinus Torvalds 568498f0103SMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps, 5691da177e4SLinus Torvalds struct dm_target *ti) 5701da177e4SLinus Torvalds { 5711da177e4SLinus Torvalds int r; 5721da177e4SLinus Torvalds struct pgpath *p; 573ae11b1b3SHannes Reinecke struct multipath *m = ti->private; 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds /* we need at least a path arg */ 5761da177e4SLinus Torvalds if (as->argc < 1) { 57772d94861SAlasdair G Kergon ti->error = "no device given"; 57801460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 5791da177e4SLinus Torvalds } 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds p = alloc_pgpath(); 5821da177e4SLinus Torvalds if (!p) 58301460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 5841da177e4SLinus Torvalds 585498f0103SMike Snitzer r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table), 5868215d6ecSNikanth Karthikesan &p->path.dev); 5871da177e4SLinus Torvalds if (r) { 58872d94861SAlasdair G Kergon ti->error = "error getting device"; 5891da177e4SLinus Torvalds goto bad; 5901da177e4SLinus Torvalds } 5911da177e4SLinus Torvalds 592ae11b1b3SHannes Reinecke if (m->hw_handler_name) { 593a0cf7ea9SHannes Reinecke struct request_queue *q = bdev_get_queue(p->path.dev->bdev); 594a0cf7ea9SHannes Reinecke 595a0cf7ea9SHannes Reinecke r = scsi_dh_attach(q, m->hw_handler_name); 596a0cf7ea9SHannes Reinecke if (r == -EBUSY) { 597a0cf7ea9SHannes Reinecke /* 598a0cf7ea9SHannes Reinecke * Already attached to different hw_handler, 599a0cf7ea9SHannes Reinecke * try to reattach with correct one. 600a0cf7ea9SHannes Reinecke */ 601a0cf7ea9SHannes Reinecke scsi_dh_detach(q); 602a0cf7ea9SHannes Reinecke r = scsi_dh_attach(q, m->hw_handler_name); 603a0cf7ea9SHannes Reinecke } 604a0cf7ea9SHannes Reinecke 605ae11b1b3SHannes Reinecke if (r < 0) { 606a0cf7ea9SHannes Reinecke ti->error = "error attaching hardware handler"; 607ae11b1b3SHannes Reinecke dm_put_device(ti, p->path.dev); 608ae11b1b3SHannes Reinecke goto bad; 609ae11b1b3SHannes Reinecke } 6102bfd2e13SChandra Seetharaman 6112bfd2e13SChandra Seetharaman if (m->hw_handler_params) { 6122bfd2e13SChandra Seetharaman r = scsi_dh_set_params(q, m->hw_handler_params); 6132bfd2e13SChandra Seetharaman if (r < 0) { 6142bfd2e13SChandra Seetharaman ti->error = "unable to set hardware " 6152bfd2e13SChandra Seetharaman "handler parameters"; 6162bfd2e13SChandra Seetharaman scsi_dh_detach(q); 6172bfd2e13SChandra Seetharaman dm_put_device(ti, p->path.dev); 6182bfd2e13SChandra Seetharaman goto bad; 6192bfd2e13SChandra Seetharaman } 6202bfd2e13SChandra Seetharaman } 621ae11b1b3SHannes Reinecke } 622ae11b1b3SHannes Reinecke 6231da177e4SLinus Torvalds r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); 6241da177e4SLinus Torvalds if (r) { 6251da177e4SLinus Torvalds dm_put_device(ti, p->path.dev); 6261da177e4SLinus Torvalds goto bad; 6271da177e4SLinus Torvalds } 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds return p; 6301da177e4SLinus Torvalds 6311da177e4SLinus Torvalds bad: 6321da177e4SLinus Torvalds free_pgpath(p); 63301460f35SBenjamin Marzinski return ERR_PTR(r); 6341da177e4SLinus Torvalds } 6351da177e4SLinus Torvalds 636498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as, 63728f16c20SMicha³ Miros³aw struct multipath *m) 6381da177e4SLinus Torvalds { 639498f0103SMike Snitzer static struct dm_arg _args[] = { 64072d94861SAlasdair G Kergon {1, 1024, "invalid number of paths"}, 64172d94861SAlasdair G Kergon {0, 1024, "invalid number of selector args"} 6421da177e4SLinus Torvalds }; 6431da177e4SLinus Torvalds 6441da177e4SLinus Torvalds int r; 645498f0103SMike Snitzer unsigned i, nr_selector_args, nr_args; 6461da177e4SLinus Torvalds struct priority_group *pg; 64728f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 6481da177e4SLinus Torvalds 6491da177e4SLinus Torvalds if (as->argc < 2) { 6501da177e4SLinus Torvalds as->argc = 0; 65101460f35SBenjamin Marzinski ti->error = "not enough priority group arguments"; 65201460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 6531da177e4SLinus Torvalds } 6541da177e4SLinus Torvalds 6551da177e4SLinus Torvalds pg = alloc_priority_group(); 6561da177e4SLinus Torvalds if (!pg) { 65772d94861SAlasdair G Kergon ti->error = "couldn't allocate priority group"; 65801460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds pg->m = m; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds r = parse_path_selector(as, pg, ti); 6631da177e4SLinus Torvalds if (r) 6641da177e4SLinus Torvalds goto bad; 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds /* 6671da177e4SLinus Torvalds * read the paths 6681da177e4SLinus Torvalds */ 669498f0103SMike Snitzer r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error); 6701da177e4SLinus Torvalds if (r) 6711da177e4SLinus Torvalds goto bad; 6721da177e4SLinus Torvalds 673498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error); 6741da177e4SLinus Torvalds if (r) 6751da177e4SLinus Torvalds goto bad; 6761da177e4SLinus Torvalds 677498f0103SMike Snitzer nr_args = 1 + nr_selector_args; 6781da177e4SLinus Torvalds for (i = 0; i < pg->nr_pgpaths; i++) { 6791da177e4SLinus Torvalds struct pgpath *pgpath; 680498f0103SMike Snitzer struct dm_arg_set path_args; 6811da177e4SLinus Torvalds 682498f0103SMike Snitzer if (as->argc < nr_args) { 683148acff6SMikulas Patocka ti->error = "not enough path parameters"; 6846bbf79a1SAlasdair G Kergon r = -EINVAL; 6851da177e4SLinus Torvalds goto bad; 686148acff6SMikulas Patocka } 6871da177e4SLinus Torvalds 688498f0103SMike Snitzer path_args.argc = nr_args; 6891da177e4SLinus Torvalds path_args.argv = as->argv; 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds pgpath = parse_path(&path_args, &pg->ps, ti); 69201460f35SBenjamin Marzinski if (IS_ERR(pgpath)) { 69301460f35SBenjamin Marzinski r = PTR_ERR(pgpath); 6941da177e4SLinus Torvalds goto bad; 69501460f35SBenjamin Marzinski } 6961da177e4SLinus Torvalds 6971da177e4SLinus Torvalds pgpath->pg = pg; 6981da177e4SLinus Torvalds list_add_tail(&pgpath->list, &pg->pgpaths); 699498f0103SMike Snitzer dm_consume_args(as, nr_args); 7001da177e4SLinus Torvalds } 7011da177e4SLinus Torvalds 7021da177e4SLinus Torvalds return pg; 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds bad: 7051da177e4SLinus Torvalds free_priority_group(pg, ti); 70601460f35SBenjamin Marzinski return ERR_PTR(r); 7071da177e4SLinus Torvalds } 7081da177e4SLinus Torvalds 709498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m) 7101da177e4SLinus Torvalds { 7111da177e4SLinus Torvalds unsigned hw_argc; 7122bfd2e13SChandra Seetharaman int ret; 71328f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 7141da177e4SLinus Torvalds 715498f0103SMike Snitzer static struct dm_arg _args[] = { 71672d94861SAlasdair G Kergon {0, 1024, "invalid number of hardware handler args"}, 7171da177e4SLinus Torvalds }; 7181da177e4SLinus Torvalds 719498f0103SMike Snitzer if (dm_read_arg_group(_args, as, &hw_argc, &ti->error)) 7201da177e4SLinus Torvalds return -EINVAL; 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds if (!hw_argc) 7231da177e4SLinus Torvalds return 0; 7241da177e4SLinus Torvalds 725498f0103SMike Snitzer m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL); 726510193a2SMike Snitzer if (!try_then_request_module(scsi_dh_handler_exist(m->hw_handler_name), 727510193a2SMike Snitzer "scsi_dh_%s", m->hw_handler_name)) { 72872d94861SAlasdair G Kergon ti->error = "unknown hardware handler type"; 7292bfd2e13SChandra Seetharaman ret = -EINVAL; 7302bfd2e13SChandra Seetharaman goto fail; 7311da177e4SLinus Torvalds } 73214e98c5cSChandra Seetharaman 7332bfd2e13SChandra Seetharaman if (hw_argc > 1) { 7342bfd2e13SChandra Seetharaman char *p; 7352bfd2e13SChandra Seetharaman int i, j, len = 4; 7362bfd2e13SChandra Seetharaman 7372bfd2e13SChandra Seetharaman for (i = 0; i <= hw_argc - 2; i++) 7382bfd2e13SChandra Seetharaman len += strlen(as->argv[i]) + 1; 7392bfd2e13SChandra Seetharaman p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); 7402bfd2e13SChandra Seetharaman if (!p) { 7412bfd2e13SChandra Seetharaman ti->error = "memory allocation failed"; 7422bfd2e13SChandra Seetharaman ret = -ENOMEM; 7432bfd2e13SChandra Seetharaman goto fail; 7442bfd2e13SChandra Seetharaman } 7452bfd2e13SChandra Seetharaman j = sprintf(p, "%d", hw_argc - 1); 7462bfd2e13SChandra Seetharaman for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1) 7472bfd2e13SChandra Seetharaman j = sprintf(p, "%s", as->argv[i]); 7482bfd2e13SChandra Seetharaman } 749498f0103SMike Snitzer dm_consume_args(as, hw_argc - 1); 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds return 0; 7522bfd2e13SChandra Seetharaman fail: 7532bfd2e13SChandra Seetharaman kfree(m->hw_handler_name); 7542bfd2e13SChandra Seetharaman m->hw_handler_name = NULL; 7552bfd2e13SChandra Seetharaman return ret; 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 758498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m) 7591da177e4SLinus Torvalds { 7601da177e4SLinus Torvalds int r; 7611da177e4SLinus Torvalds unsigned argc; 76228f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 763498f0103SMike Snitzer const char *arg_name; 7641da177e4SLinus Torvalds 765498f0103SMike Snitzer static struct dm_arg _args[] = { 7664e2d19e4SChandra Seetharaman {0, 5, "invalid number of feature args"}, 767c9e45581SDave Wysochanski {1, 50, "pg_init_retries must be between 1 and 50"}, 7684e2d19e4SChandra Seetharaman {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"}, 7691da177e4SLinus Torvalds }; 7701da177e4SLinus Torvalds 771498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &argc, &ti->error); 7721da177e4SLinus Torvalds if (r) 7731da177e4SLinus Torvalds return -EINVAL; 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds if (!argc) 7761da177e4SLinus Torvalds return 0; 7771da177e4SLinus Torvalds 778c9e45581SDave Wysochanski do { 779498f0103SMike Snitzer arg_name = dm_shift_arg(as); 780c9e45581SDave Wysochanski argc--; 781c9e45581SDave Wysochanski 782498f0103SMike Snitzer if (!strcasecmp(arg_name, "queue_if_no_path")) { 783c9e45581SDave Wysochanski r = queue_if_no_path(m, 1, 0); 784c9e45581SDave Wysochanski continue; 7851da177e4SLinus Torvalds } 786c9e45581SDave Wysochanski 787498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_retries") && 788c9e45581SDave Wysochanski (argc >= 1)) { 789498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error); 790c9e45581SDave Wysochanski argc--; 791c9e45581SDave Wysochanski continue; 792c9e45581SDave Wysochanski } 793c9e45581SDave Wysochanski 794498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_delay_msecs") && 7954e2d19e4SChandra Seetharaman (argc >= 1)) { 796498f0103SMike Snitzer r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error); 7974e2d19e4SChandra Seetharaman argc--; 7984e2d19e4SChandra Seetharaman continue; 7994e2d19e4SChandra Seetharaman } 8004e2d19e4SChandra Seetharaman 801c9e45581SDave Wysochanski ti->error = "Unrecognised multipath feature request"; 802c9e45581SDave Wysochanski r = -EINVAL; 803c9e45581SDave Wysochanski } while (argc && !r); 804c9e45581SDave Wysochanski 805c9e45581SDave Wysochanski return r; 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds static int multipath_ctr(struct dm_target *ti, unsigned int argc, 8091da177e4SLinus Torvalds char **argv) 8101da177e4SLinus Torvalds { 811498f0103SMike Snitzer /* target arguments */ 812498f0103SMike Snitzer static struct dm_arg _args[] = { 813a490a07aSMike Snitzer {0, 1024, "invalid number of priority groups"}, 814a490a07aSMike Snitzer {0, 1024, "invalid initial priority group number"}, 8151da177e4SLinus Torvalds }; 8161da177e4SLinus Torvalds 8171da177e4SLinus Torvalds int r; 8181da177e4SLinus Torvalds struct multipath *m; 819498f0103SMike Snitzer struct dm_arg_set as; 8201da177e4SLinus Torvalds unsigned pg_count = 0; 8211da177e4SLinus Torvalds unsigned next_pg_num; 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds as.argc = argc; 8241da177e4SLinus Torvalds as.argv = argv; 8251da177e4SLinus Torvalds 82628f16c20SMicha³ Miros³aw m = alloc_multipath(ti); 8271da177e4SLinus Torvalds if (!m) { 82872d94861SAlasdair G Kergon ti->error = "can't allocate multipath"; 8291da177e4SLinus Torvalds return -EINVAL; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 83228f16c20SMicha³ Miros³aw r = parse_features(&as, m); 8331da177e4SLinus Torvalds if (r) 8341da177e4SLinus Torvalds goto bad; 8351da177e4SLinus Torvalds 83628f16c20SMicha³ Miros³aw r = parse_hw_handler(&as, m); 8371da177e4SLinus Torvalds if (r) 8381da177e4SLinus Torvalds goto bad; 8391da177e4SLinus Torvalds 840498f0103SMike Snitzer r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error); 8411da177e4SLinus Torvalds if (r) 8421da177e4SLinus Torvalds goto bad; 8431da177e4SLinus Torvalds 844498f0103SMike Snitzer r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error); 8451da177e4SLinus Torvalds if (r) 8461da177e4SLinus Torvalds goto bad; 8471da177e4SLinus Torvalds 848a490a07aSMike Snitzer if ((!m->nr_priority_groups && next_pg_num) || 849a490a07aSMike Snitzer (m->nr_priority_groups && !next_pg_num)) { 850a490a07aSMike Snitzer ti->error = "invalid initial priority group"; 851a490a07aSMike Snitzer r = -EINVAL; 852a490a07aSMike Snitzer goto bad; 853a490a07aSMike Snitzer } 854a490a07aSMike Snitzer 8551da177e4SLinus Torvalds /* parse the priority groups */ 8561da177e4SLinus Torvalds while (as.argc) { 8571da177e4SLinus Torvalds struct priority_group *pg; 8581da177e4SLinus Torvalds 85928f16c20SMicha³ Miros³aw pg = parse_priority_group(&as, m); 86001460f35SBenjamin Marzinski if (IS_ERR(pg)) { 86101460f35SBenjamin Marzinski r = PTR_ERR(pg); 8621da177e4SLinus Torvalds goto bad; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds m->nr_valid_paths += pg->nr_pgpaths; 8661da177e4SLinus Torvalds list_add_tail(&pg->list, &m->priority_groups); 8671da177e4SLinus Torvalds pg_count++; 8681da177e4SLinus Torvalds pg->pg_num = pg_count; 8691da177e4SLinus Torvalds if (!--next_pg_num) 8701da177e4SLinus Torvalds m->next_pg = pg; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds if (pg_count != m->nr_priority_groups) { 87472d94861SAlasdair G Kergon ti->error = "priority group count mismatch"; 8751da177e4SLinus Torvalds r = -EINVAL; 8761da177e4SLinus Torvalds goto bad; 8771da177e4SLinus Torvalds } 8781da177e4SLinus Torvalds 8798627921fSMikulas Patocka ti->num_flush_requests = 1; 880959eb4e5SMike Snitzer ti->num_discard_requests = 1; 8818627921fSMikulas Patocka 8821da177e4SLinus Torvalds return 0; 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds bad: 8851da177e4SLinus Torvalds free_multipath(m); 8861da177e4SLinus Torvalds return r; 8871da177e4SLinus Torvalds } 8881da177e4SLinus Torvalds 8892bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m) 8902bded7bdSKiyoshi Ueda { 8912bded7bdSKiyoshi Ueda DECLARE_WAITQUEUE(wait, current); 8922bded7bdSKiyoshi Ueda unsigned long flags; 8932bded7bdSKiyoshi Ueda 8942bded7bdSKiyoshi Ueda add_wait_queue(&m->pg_init_wait, &wait); 8952bded7bdSKiyoshi Ueda 8962bded7bdSKiyoshi Ueda while (1) { 8972bded7bdSKiyoshi Ueda set_current_state(TASK_UNINTERRUPTIBLE); 8982bded7bdSKiyoshi Ueda 8992bded7bdSKiyoshi Ueda spin_lock_irqsave(&m->lock, flags); 9002bded7bdSKiyoshi Ueda if (!m->pg_init_in_progress) { 9012bded7bdSKiyoshi Ueda spin_unlock_irqrestore(&m->lock, flags); 9022bded7bdSKiyoshi Ueda break; 9032bded7bdSKiyoshi Ueda } 9042bded7bdSKiyoshi Ueda spin_unlock_irqrestore(&m->lock, flags); 9052bded7bdSKiyoshi Ueda 9062bded7bdSKiyoshi Ueda io_schedule(); 9072bded7bdSKiyoshi Ueda } 9082bded7bdSKiyoshi Ueda set_current_state(TASK_RUNNING); 9092bded7bdSKiyoshi Ueda 9102bded7bdSKiyoshi Ueda remove_wait_queue(&m->pg_init_wait, &wait); 9112bded7bdSKiyoshi Ueda } 9122bded7bdSKiyoshi Ueda 9132bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m) 9141da177e4SLinus Torvalds { 915bab7cfc7SChandra Seetharaman flush_workqueue(kmpath_handlerd); 9162bded7bdSKiyoshi Ueda multipath_wait_for_pg_init_completion(m); 917a044d016SAlasdair G Kergon flush_workqueue(kmultipathd); 918d5ffa387STejun Heo flush_work_sync(&m->trigger_event); 9196df400abSKiyoshi Ueda } 9206df400abSKiyoshi Ueda 9216df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti) 9226df400abSKiyoshi Ueda { 9236df400abSKiyoshi Ueda struct multipath *m = ti->private; 9246df400abSKiyoshi Ueda 9252bded7bdSKiyoshi Ueda flush_multipath_work(m); 9261da177e4SLinus Torvalds free_multipath(m); 9271da177e4SLinus Torvalds } 9281da177e4SLinus Torvalds 9291da177e4SLinus Torvalds /* 930f40c67f0SKiyoshi Ueda * Map cloned requests 9311da177e4SLinus Torvalds */ 932f40c67f0SKiyoshi Ueda static int multipath_map(struct dm_target *ti, struct request *clone, 9331da177e4SLinus Torvalds union map_info *map_context) 9341da177e4SLinus Torvalds { 9351da177e4SLinus Torvalds int r; 9361da177e4SLinus Torvalds struct multipath *m = (struct multipath *) ti->private; 9371da177e4SLinus Torvalds 938466891f9SJun'ichi Nomura if (set_mapinfo(m, map_context) < 0) 939f40c67f0SKiyoshi Ueda /* ENOMEM, requeue */ 940f40c67f0SKiyoshi Ueda return DM_MAPIO_REQUEUE; 9411da177e4SLinus Torvalds 942f40c67f0SKiyoshi Ueda clone->cmd_flags |= REQ_FAILFAST_TRANSPORT; 943466891f9SJun'ichi Nomura r = map_io(m, clone, map_context, 0); 94445e15720SKiyoshi Ueda if (r < 0 || r == DM_MAPIO_REQUEUE) 945466891f9SJun'ichi Nomura clear_mapinfo(m, map_context); 9461da177e4SLinus Torvalds 9471da177e4SLinus Torvalds return r; 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds /* 9511da177e4SLinus Torvalds * Take a path out of use. 9521da177e4SLinus Torvalds */ 9531da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath) 9541da177e4SLinus Torvalds { 9551da177e4SLinus Torvalds unsigned long flags; 9561da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 9591da177e4SLinus Torvalds 9606680073dSKiyoshi Ueda if (!pgpath->is_active) 9611da177e4SLinus Torvalds goto out; 9621da177e4SLinus Torvalds 96372d94861SAlasdair G Kergon DMWARN("Failing path %s.", pgpath->path.dev->name); 9641da177e4SLinus Torvalds 9651da177e4SLinus Torvalds pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); 9666680073dSKiyoshi Ueda pgpath->is_active = 0; 9671da177e4SLinus Torvalds pgpath->fail_count++; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds m->nr_valid_paths--; 9701da177e4SLinus Torvalds 9711da177e4SLinus Torvalds if (pgpath == m->current_pgpath) 9721da177e4SLinus Torvalds m->current_pgpath = NULL; 9731da177e4SLinus Torvalds 974b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti, 975b15546f9SMike Anderson pgpath->path.dev->name, m->nr_valid_paths); 976b15546f9SMike Anderson 977fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 9781da177e4SLinus Torvalds 9791da177e4SLinus Torvalds out: 9801da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds return 0; 9831da177e4SLinus Torvalds } 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds /* 9861da177e4SLinus Torvalds * Reinstate a previously-failed path 9871da177e4SLinus Torvalds */ 9881da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath) 9891da177e4SLinus Torvalds { 9901da177e4SLinus Torvalds int r = 0; 9911da177e4SLinus Torvalds unsigned long flags; 9921da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 9951da177e4SLinus Torvalds 9966680073dSKiyoshi Ueda if (pgpath->is_active) 9971da177e4SLinus Torvalds goto out; 9981da177e4SLinus Torvalds 999def052d2SAlasdair G Kergon if (!pgpath->pg->ps.type->reinstate_path) { 10001da177e4SLinus Torvalds DMWARN("Reinstate path not supported by path selector %s", 10011da177e4SLinus Torvalds pgpath->pg->ps.type->name); 10021da177e4SLinus Torvalds r = -EINVAL; 10031da177e4SLinus Torvalds goto out; 10041da177e4SLinus Torvalds } 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); 10071da177e4SLinus Torvalds if (r) 10081da177e4SLinus Torvalds goto out; 10091da177e4SLinus Torvalds 10106680073dSKiyoshi Ueda pgpath->is_active = 1; 10111da177e4SLinus Torvalds 1012e54f77ddSChandra Seetharaman if (!m->nr_valid_paths++ && m->queue_size) { 10131da177e4SLinus Torvalds m->current_pgpath = NULL; 1014c557308eSAlasdair G Kergon queue_work(kmultipathd, &m->process_queued_ios); 1015e54f77ddSChandra Seetharaman } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) { 10164e2d19e4SChandra Seetharaman if (queue_work(kmpath_handlerd, &pgpath->activate_path.work)) 1017e54f77ddSChandra Seetharaman m->pg_init_in_progress++; 1018e54f77ddSChandra Seetharaman } 10191da177e4SLinus Torvalds 1020b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti, 1021b15546f9SMike Anderson pgpath->path.dev->name, m->nr_valid_paths); 1022b15546f9SMike Anderson 1023fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds out: 10261da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds return r; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds /* 10321da177e4SLinus Torvalds * Fail or reinstate all paths that match the provided struct dm_dev. 10331da177e4SLinus Torvalds */ 10341da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev, 10351da177e4SLinus Torvalds action_fn action) 10361da177e4SLinus Torvalds { 103719040c0bSMike Snitzer int r = -EINVAL; 10381da177e4SLinus Torvalds struct pgpath *pgpath; 10391da177e4SLinus Torvalds struct priority_group *pg; 10401da177e4SLinus Torvalds 10411da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 10421da177e4SLinus Torvalds list_for_each_entry(pgpath, &pg->pgpaths, list) { 10431da177e4SLinus Torvalds if (pgpath->path.dev == dev) 10441da177e4SLinus Torvalds r = action(pgpath); 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds return r; 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds /* 10521da177e4SLinus Torvalds * Temporarily try to avoid having to use the specified PG 10531da177e4SLinus Torvalds */ 10541da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg, 10551da177e4SLinus Torvalds int bypassed) 10561da177e4SLinus Torvalds { 10571da177e4SLinus Torvalds unsigned long flags; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 10601da177e4SLinus Torvalds 10611da177e4SLinus Torvalds pg->bypassed = bypassed; 10621da177e4SLinus Torvalds m->current_pgpath = NULL; 10631da177e4SLinus Torvalds m->current_pg = NULL; 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 10661da177e4SLinus Torvalds 1067fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds /* 10711da177e4SLinus Torvalds * Switch to using the specified PG from the next I/O that gets mapped 10721da177e4SLinus Torvalds */ 10731da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr) 10741da177e4SLinus Torvalds { 10751da177e4SLinus Torvalds struct priority_group *pg; 10761da177e4SLinus Torvalds unsigned pgnum; 10771da177e4SLinus Torvalds unsigned long flags; 107831998ef1SMikulas Patocka char dummy; 10791da177e4SLinus Torvalds 108031998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 10811da177e4SLinus Torvalds (pgnum > m->nr_priority_groups)) { 10821da177e4SLinus Torvalds DMWARN("invalid PG number supplied to switch_pg_num"); 10831da177e4SLinus Torvalds return -EINVAL; 10841da177e4SLinus Torvalds } 10851da177e4SLinus Torvalds 10861da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 10871da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 10881da177e4SLinus Torvalds pg->bypassed = 0; 10891da177e4SLinus Torvalds if (--pgnum) 10901da177e4SLinus Torvalds continue; 10911da177e4SLinus Torvalds 10921da177e4SLinus Torvalds m->current_pgpath = NULL; 10931da177e4SLinus Torvalds m->current_pg = NULL; 10941da177e4SLinus Torvalds m->next_pg = pg; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 10971da177e4SLinus Torvalds 1098fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 10991da177e4SLinus Torvalds return 0; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 11021da177e4SLinus Torvalds /* 11031da177e4SLinus Torvalds * Set/clear bypassed status of a PG. 11041da177e4SLinus Torvalds * PGs are numbered upwards from 1 in the order they were declared. 11051da177e4SLinus Torvalds */ 11061da177e4SLinus Torvalds static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed) 11071da177e4SLinus Torvalds { 11081da177e4SLinus Torvalds struct priority_group *pg; 11091da177e4SLinus Torvalds unsigned pgnum; 111031998ef1SMikulas Patocka char dummy; 11111da177e4SLinus Torvalds 111231998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 11131da177e4SLinus Torvalds (pgnum > m->nr_priority_groups)) { 11141da177e4SLinus Torvalds DMWARN("invalid PG number supplied to bypass_pg"); 11151da177e4SLinus Torvalds return -EINVAL; 11161da177e4SLinus Torvalds } 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 11191da177e4SLinus Torvalds if (!--pgnum) 11201da177e4SLinus Torvalds break; 11211da177e4SLinus Torvalds } 11221da177e4SLinus Torvalds 11231da177e4SLinus Torvalds bypass_pg(m, pg, bypassed); 11241da177e4SLinus Torvalds return 0; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds 11271da177e4SLinus Torvalds /* 1128c9e45581SDave Wysochanski * Should we retry pg_init immediately? 1129c9e45581SDave Wysochanski */ 1130c9e45581SDave Wysochanski static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath) 1131c9e45581SDave Wysochanski { 1132c9e45581SDave Wysochanski unsigned long flags; 1133c9e45581SDave Wysochanski int limit_reached = 0; 1134c9e45581SDave Wysochanski 1135c9e45581SDave Wysochanski spin_lock_irqsave(&m->lock, flags); 1136c9e45581SDave Wysochanski 1137c9e45581SDave Wysochanski if (m->pg_init_count <= m->pg_init_retries) 1138c9e45581SDave Wysochanski m->pg_init_required = 1; 1139c9e45581SDave Wysochanski else 1140c9e45581SDave Wysochanski limit_reached = 1; 1141c9e45581SDave Wysochanski 1142c9e45581SDave Wysochanski spin_unlock_irqrestore(&m->lock, flags); 1143c9e45581SDave Wysochanski 1144c9e45581SDave Wysochanski return limit_reached; 1145c9e45581SDave Wysochanski } 1146c9e45581SDave Wysochanski 11473ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors) 1148cfae5c9bSChandra Seetharaman { 114983c0d5d5SMoger, Babu struct pgpath *pgpath = data; 1150cfae5c9bSChandra Seetharaman struct priority_group *pg = pgpath->pg; 1151cfae5c9bSChandra Seetharaman struct multipath *m = pg->m; 1152cfae5c9bSChandra Seetharaman unsigned long flags; 11534e2d19e4SChandra Seetharaman unsigned delay_retry = 0; 1154cfae5c9bSChandra Seetharaman 1155cfae5c9bSChandra Seetharaman /* device or driver problems */ 1156cfae5c9bSChandra Seetharaman switch (errors) { 1157cfae5c9bSChandra Seetharaman case SCSI_DH_OK: 1158cfae5c9bSChandra Seetharaman break; 1159cfae5c9bSChandra Seetharaman case SCSI_DH_NOSYS: 1160cfae5c9bSChandra Seetharaman if (!m->hw_handler_name) { 1161cfae5c9bSChandra Seetharaman errors = 0; 1162cfae5c9bSChandra Seetharaman break; 1163cfae5c9bSChandra Seetharaman } 1164f7b934c8SMoger, Babu DMERR("Could not failover the device: Handler scsi_dh_%s " 1165f7b934c8SMoger, Babu "Error %d.", m->hw_handler_name, errors); 1166cfae5c9bSChandra Seetharaman /* 1167cfae5c9bSChandra Seetharaman * Fail path for now, so we do not ping pong 1168cfae5c9bSChandra Seetharaman */ 1169cfae5c9bSChandra Seetharaman fail_path(pgpath); 1170cfae5c9bSChandra Seetharaman break; 1171cfae5c9bSChandra Seetharaman case SCSI_DH_DEV_TEMP_BUSY: 1172cfae5c9bSChandra Seetharaman /* 1173cfae5c9bSChandra Seetharaman * Probably doing something like FW upgrade on the 1174cfae5c9bSChandra Seetharaman * controller so try the other pg. 1175cfae5c9bSChandra Seetharaman */ 1176cfae5c9bSChandra Seetharaman bypass_pg(m, pg, 1); 1177cfae5c9bSChandra Seetharaman break; 1178cfae5c9bSChandra Seetharaman case SCSI_DH_RETRY: 11794e2d19e4SChandra Seetharaman /* Wait before retrying. */ 11804e2d19e4SChandra Seetharaman delay_retry = 1; 1181cfae5c9bSChandra Seetharaman case SCSI_DH_IMM_RETRY: 1182cfae5c9bSChandra Seetharaman case SCSI_DH_RES_TEMP_UNAVAIL: 1183cfae5c9bSChandra Seetharaman if (pg_init_limit_reached(m, pgpath)) 1184cfae5c9bSChandra Seetharaman fail_path(pgpath); 1185cfae5c9bSChandra Seetharaman errors = 0; 1186cfae5c9bSChandra Seetharaman break; 1187cfae5c9bSChandra Seetharaman default: 1188cfae5c9bSChandra Seetharaman /* 1189cfae5c9bSChandra Seetharaman * We probably do not want to fail the path for a device 1190cfae5c9bSChandra Seetharaman * error, but this is what the old dm did. In future 1191cfae5c9bSChandra Seetharaman * patches we can do more advanced handling. 1192cfae5c9bSChandra Seetharaman */ 1193cfae5c9bSChandra Seetharaman fail_path(pgpath); 1194cfae5c9bSChandra Seetharaman } 1195cfae5c9bSChandra Seetharaman 1196cfae5c9bSChandra Seetharaman spin_lock_irqsave(&m->lock, flags); 1197cfae5c9bSChandra Seetharaman if (errors) { 1198e54f77ddSChandra Seetharaman if (pgpath == m->current_pgpath) { 1199cfae5c9bSChandra Seetharaman DMERR("Could not failover device. Error %d.", errors); 1200cfae5c9bSChandra Seetharaman m->current_pgpath = NULL; 1201cfae5c9bSChandra Seetharaman m->current_pg = NULL; 1202e54f77ddSChandra Seetharaman } 1203d0259bf0SKiyoshi Ueda } else if (!m->pg_init_required) 1204cfae5c9bSChandra Seetharaman pg->bypassed = 0; 1205cfae5c9bSChandra Seetharaman 1206d0259bf0SKiyoshi Ueda if (--m->pg_init_in_progress) 1207d0259bf0SKiyoshi Ueda /* Activations of other paths are still on going */ 1208d0259bf0SKiyoshi Ueda goto out; 1209d0259bf0SKiyoshi Ueda 1210d0259bf0SKiyoshi Ueda if (!m->pg_init_required) 1211d0259bf0SKiyoshi Ueda m->queue_io = 0; 1212d0259bf0SKiyoshi Ueda 12134e2d19e4SChandra Seetharaman m->pg_init_delay_retry = delay_retry; 1214cfae5c9bSChandra Seetharaman queue_work(kmultipathd, &m->process_queued_ios); 1215d0259bf0SKiyoshi Ueda 12162bded7bdSKiyoshi Ueda /* 12172bded7bdSKiyoshi Ueda * Wake up any thread waiting to suspend. 12182bded7bdSKiyoshi Ueda */ 12192bded7bdSKiyoshi Ueda wake_up(&m->pg_init_wait); 12202bded7bdSKiyoshi Ueda 1221d0259bf0SKiyoshi Ueda out: 1222cfae5c9bSChandra Seetharaman spin_unlock_irqrestore(&m->lock, flags); 1223cfae5c9bSChandra Seetharaman } 1224cfae5c9bSChandra Seetharaman 1225bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work) 1226bab7cfc7SChandra Seetharaman { 1227e54f77ddSChandra Seetharaman struct pgpath *pgpath = 12284e2d19e4SChandra Seetharaman container_of(work, struct pgpath, activate_path.work); 1229bab7cfc7SChandra Seetharaman 12303ae31f6aSChandra Seetharaman scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev), 123183c0d5d5SMoger, Babu pg_init_done, pgpath); 1232bab7cfc7SChandra Seetharaman } 1233bab7cfc7SChandra Seetharaman 12341da177e4SLinus Torvalds /* 12351da177e4SLinus Torvalds * end_io handling 12361da177e4SLinus Torvalds */ 1237f40c67f0SKiyoshi Ueda static int do_end_io(struct multipath *m, struct request *clone, 1238028867acSAlasdair G Kergon int error, struct dm_mpath_io *mpio) 12391da177e4SLinus Torvalds { 1240f40c67f0SKiyoshi Ueda /* 1241f40c67f0SKiyoshi Ueda * We don't queue any clone request inside the multipath target 1242f40c67f0SKiyoshi Ueda * during end I/O handling, since those clone requests don't have 1243f40c67f0SKiyoshi Ueda * bio clones. If we queue them inside the multipath target, 1244f40c67f0SKiyoshi Ueda * we need to make bio clones, that requires memory allocation. 1245f40c67f0SKiyoshi Ueda * (See drivers/md/dm.c:end_clone_bio() about why the clone requests 1246f40c67f0SKiyoshi Ueda * don't have bio clones.) 1247f40c67f0SKiyoshi Ueda * Instead of queueing the clone request here, we queue the original 1248f40c67f0SKiyoshi Ueda * request into dm core, which will remake a clone request and 1249f40c67f0SKiyoshi Ueda * clone bios for it and resubmit it later. 1250f40c67f0SKiyoshi Ueda */ 1251f40c67f0SKiyoshi Ueda int r = DM_ENDIO_REQUEUE; 1252640eb3b0SStefan Bader unsigned long flags; 12531da177e4SLinus Torvalds 1254f40c67f0SKiyoshi Ueda if (!error && !clone->errors) 12551da177e4SLinus Torvalds return 0; /* I/O complete */ 12561da177e4SLinus Torvalds 12576f13f6fbSMartin K. Petersen if (error == -EOPNOTSUPP || error == -EREMOTEIO || error == -EILSEQ) 1258959eb4e5SMike Snitzer return error; 1259959eb4e5SMike Snitzer 1260cfae5c9bSChandra Seetharaman if (mpio->pgpath) 12611da177e4SLinus Torvalds fail_path(mpio->pgpath); 12621da177e4SLinus Torvalds 1263640eb3b0SStefan Bader spin_lock_irqsave(&m->lock, flags); 1264751b2a7dSHannes Reinecke if (!m->nr_valid_paths) { 1265751b2a7dSHannes Reinecke if (!m->queue_if_no_path) { 1266751b2a7dSHannes Reinecke if (!__must_push_back(m)) 1267f40c67f0SKiyoshi Ueda r = -EIO; 1268751b2a7dSHannes Reinecke } else { 1269751b2a7dSHannes Reinecke if (error == -EBADE) 1270751b2a7dSHannes Reinecke r = error; 1271751b2a7dSHannes Reinecke } 1272751b2a7dSHannes Reinecke } 1273640eb3b0SStefan Bader spin_unlock_irqrestore(&m->lock, flags); 12741da177e4SLinus Torvalds 1275f40c67f0SKiyoshi Ueda return r; 12761da177e4SLinus Torvalds } 12771da177e4SLinus Torvalds 1278f40c67f0SKiyoshi Ueda static int multipath_end_io(struct dm_target *ti, struct request *clone, 12791da177e4SLinus Torvalds int error, union map_info *map_context) 12801da177e4SLinus Torvalds { 1281028867acSAlasdair G Kergon struct multipath *m = ti->private; 1282028867acSAlasdair G Kergon struct dm_mpath_io *mpio = map_context->ptr; 12831da177e4SLinus Torvalds struct pgpath *pgpath = mpio->pgpath; 12841da177e4SLinus Torvalds struct path_selector *ps; 12851da177e4SLinus Torvalds int r; 12861da177e4SLinus Torvalds 1287466891f9SJun'ichi Nomura BUG_ON(!mpio); 1288466891f9SJun'ichi Nomura 1289f40c67f0SKiyoshi Ueda r = do_end_io(m, clone, error, mpio); 12901da177e4SLinus Torvalds if (pgpath) { 12911da177e4SLinus Torvalds ps = &pgpath->pg->ps; 12921da177e4SLinus Torvalds if (ps->type->end_io) 129302ab823fSKiyoshi Ueda ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); 12941da177e4SLinus Torvalds } 1295466891f9SJun'ichi Nomura clear_mapinfo(m, map_context); 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds return r; 12981da177e4SLinus Torvalds } 12991da177e4SLinus Torvalds 13001da177e4SLinus Torvalds /* 13011da177e4SLinus Torvalds * Suspend can't complete until all the I/O is processed so if 1302436d4108SAlasdair G Kergon * the last path fails we must error any remaining I/O. 1303436d4108SAlasdair G Kergon * Note that if the freeze_bdev fails while suspending, the 1304436d4108SAlasdair G Kergon * queue_if_no_path state is lost - userspace should reset it. 13051da177e4SLinus Torvalds */ 13061da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti) 13071da177e4SLinus Torvalds { 13081da177e4SLinus Torvalds struct multipath *m = (struct multipath *) ti->private; 13091da177e4SLinus Torvalds 1310485ef69eSAlasdair G Kergon queue_if_no_path(m, 0, 1); 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 13136df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti) 13146df400abSKiyoshi Ueda { 13156380f26fSMike Anderson struct multipath *m = ti->private; 13166380f26fSMike Anderson 13176380f26fSMike Anderson mutex_lock(&m->work_mutex); 13182bded7bdSKiyoshi Ueda flush_multipath_work(m); 13196380f26fSMike Anderson mutex_unlock(&m->work_mutex); 13206df400abSKiyoshi Ueda } 13216df400abSKiyoshi Ueda 1322436d4108SAlasdair G Kergon /* 1323436d4108SAlasdair G Kergon * Restore the queue_if_no_path setting. 1324436d4108SAlasdair G Kergon */ 13251da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti) 13261da177e4SLinus Torvalds { 13271da177e4SLinus Torvalds struct multipath *m = (struct multipath *) ti->private; 13281da177e4SLinus Torvalds unsigned long flags; 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 1331436d4108SAlasdair G Kergon m->queue_if_no_path = m->saved_queue_if_no_path; 13321da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds /* 13361da177e4SLinus Torvalds * Info output has the following format: 13371da177e4SLinus Torvalds * num_multipath_feature_args [multipath_feature_args]* 13381da177e4SLinus Torvalds * num_handler_status_args [handler_status_args]* 13391da177e4SLinus Torvalds * num_groups init_group_number 13401da177e4SLinus Torvalds * [A|D|E num_ps_status_args [ps_status_args]* 13411da177e4SLinus Torvalds * num_paths num_selector_args 13421da177e4SLinus Torvalds * [path_dev A|F fail_count [selector_args]* ]+ ]+ 13431da177e4SLinus Torvalds * 13441da177e4SLinus Torvalds * Table output has the following format (identical to the constructor string): 13451da177e4SLinus Torvalds * num_feature_args [features_args]* 13461da177e4SLinus Torvalds * num_handler_args hw_handler [hw_handler_args]* 13471da177e4SLinus Torvalds * num_groups init_group_number 13481da177e4SLinus Torvalds * [priority selector-name num_ps_args [ps_args]* 13491da177e4SLinus Torvalds * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ 13501da177e4SLinus Torvalds */ 13511da177e4SLinus Torvalds static int multipath_status(struct dm_target *ti, status_type_t type, 13521da177e4SLinus Torvalds char *result, unsigned int maxlen) 13531da177e4SLinus Torvalds { 13541da177e4SLinus Torvalds int sz = 0; 13551da177e4SLinus Torvalds unsigned long flags; 13561da177e4SLinus Torvalds struct multipath *m = (struct multipath *) ti->private; 13571da177e4SLinus Torvalds struct priority_group *pg; 13581da177e4SLinus Torvalds struct pgpath *p; 13591da177e4SLinus Torvalds unsigned pg_num; 13601da177e4SLinus Torvalds char state; 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13631da177e4SLinus Torvalds 13641da177e4SLinus Torvalds /* Features */ 13651da177e4SLinus Torvalds if (type == STATUSTYPE_INFO) 1366c9e45581SDave Wysochanski DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count); 1367c9e45581SDave Wysochanski else { 1368c9e45581SDave Wysochanski DMEMIT("%u ", m->queue_if_no_path + 13694e2d19e4SChandra Seetharaman (m->pg_init_retries > 0) * 2 + 13704e2d19e4SChandra Seetharaman (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2); 1371c9e45581SDave Wysochanski if (m->queue_if_no_path) 1372c9e45581SDave Wysochanski DMEMIT("queue_if_no_path "); 1373c9e45581SDave Wysochanski if (m->pg_init_retries) 1374c9e45581SDave Wysochanski DMEMIT("pg_init_retries %u ", m->pg_init_retries); 13754e2d19e4SChandra Seetharaman if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) 13764e2d19e4SChandra Seetharaman DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs); 1377c9e45581SDave Wysochanski } 13781da177e4SLinus Torvalds 1379cfae5c9bSChandra Seetharaman if (!m->hw_handler_name || type == STATUSTYPE_INFO) 13801da177e4SLinus Torvalds DMEMIT("0 "); 13811da177e4SLinus Torvalds else 1382cfae5c9bSChandra Seetharaman DMEMIT("1 %s ", m->hw_handler_name); 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds DMEMIT("%u ", m->nr_priority_groups); 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds if (m->next_pg) 13871da177e4SLinus Torvalds pg_num = m->next_pg->pg_num; 13881da177e4SLinus Torvalds else if (m->current_pg) 13891da177e4SLinus Torvalds pg_num = m->current_pg->pg_num; 13901da177e4SLinus Torvalds else 1391a490a07aSMike Snitzer pg_num = (m->nr_priority_groups ? 1 : 0); 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds DMEMIT("%u ", pg_num); 13941da177e4SLinus Torvalds 13951da177e4SLinus Torvalds switch (type) { 13961da177e4SLinus Torvalds case STATUSTYPE_INFO: 13971da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 13981da177e4SLinus Torvalds if (pg->bypassed) 13991da177e4SLinus Torvalds state = 'D'; /* Disabled */ 14001da177e4SLinus Torvalds else if (pg == m->current_pg) 14011da177e4SLinus Torvalds state = 'A'; /* Currently Active */ 14021da177e4SLinus Torvalds else 14031da177e4SLinus Torvalds state = 'E'; /* Enabled */ 14041da177e4SLinus Torvalds 14051da177e4SLinus Torvalds DMEMIT("%c ", state); 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds if (pg->ps.type->status) 14081da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 14091da177e4SLinus Torvalds result + sz, 14101da177e4SLinus Torvalds maxlen - sz); 14111da177e4SLinus Torvalds else 14121da177e4SLinus Torvalds DMEMIT("0 "); 14131da177e4SLinus Torvalds 14141da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 14151da177e4SLinus Torvalds pg->ps.type->info_args); 14161da177e4SLinus Torvalds 14171da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 14181da177e4SLinus Torvalds DMEMIT("%s %s %u ", p->path.dev->name, 14196680073dSKiyoshi Ueda p->is_active ? "A" : "F", 14201da177e4SLinus Torvalds p->fail_count); 14211da177e4SLinus Torvalds if (pg->ps.type->status) 14221da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 14231da177e4SLinus Torvalds &p->path, type, result + sz, 14241da177e4SLinus Torvalds maxlen - sz); 14251da177e4SLinus Torvalds } 14261da177e4SLinus Torvalds } 14271da177e4SLinus Torvalds break; 14281da177e4SLinus Torvalds 14291da177e4SLinus Torvalds case STATUSTYPE_TABLE: 14301da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 14311da177e4SLinus Torvalds DMEMIT("%s ", pg->ps.type->name); 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds if (pg->ps.type->status) 14341da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 14351da177e4SLinus Torvalds result + sz, 14361da177e4SLinus Torvalds maxlen - sz); 14371da177e4SLinus Torvalds else 14381da177e4SLinus Torvalds DMEMIT("0 "); 14391da177e4SLinus Torvalds 14401da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 14411da177e4SLinus Torvalds pg->ps.type->table_args); 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 14441da177e4SLinus Torvalds DMEMIT("%s ", p->path.dev->name); 14451da177e4SLinus Torvalds if (pg->ps.type->status) 14461da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 14471da177e4SLinus Torvalds &p->path, type, result + sz, 14481da177e4SLinus Torvalds maxlen - sz); 14491da177e4SLinus Torvalds } 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds break; 14521da177e4SLinus Torvalds } 14531da177e4SLinus Torvalds 14541da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds return 0; 14571da177e4SLinus Torvalds } 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv) 14601da177e4SLinus Torvalds { 14616380f26fSMike Anderson int r = -EINVAL; 14621da177e4SLinus Torvalds struct dm_dev *dev; 14631da177e4SLinus Torvalds struct multipath *m = (struct multipath *) ti->private; 14641da177e4SLinus Torvalds action_fn action; 14651da177e4SLinus Torvalds 14666380f26fSMike Anderson mutex_lock(&m->work_mutex); 14676380f26fSMike Anderson 1468c2f3d24bSKiyoshi Ueda if (dm_suspended(ti)) { 1469c2f3d24bSKiyoshi Ueda r = -EBUSY; 1470c2f3d24bSKiyoshi Ueda goto out; 1471c2f3d24bSKiyoshi Ueda } 1472c2f3d24bSKiyoshi Ueda 14731da177e4SLinus Torvalds if (argc == 1) { 1474498f0103SMike Snitzer if (!strcasecmp(argv[0], "queue_if_no_path")) { 14756380f26fSMike Anderson r = queue_if_no_path(m, 1, 0); 14766380f26fSMike Anderson goto out; 1477498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "fail_if_no_path")) { 14786380f26fSMike Anderson r = queue_if_no_path(m, 0, 0); 14796380f26fSMike Anderson goto out; 14806380f26fSMike Anderson } 14811da177e4SLinus Torvalds } 14821da177e4SLinus Torvalds 14836380f26fSMike Anderson if (argc != 2) { 14846380f26fSMike Anderson DMWARN("Unrecognised multipath message received."); 14856380f26fSMike Anderson goto out; 14866380f26fSMike Anderson } 14871da177e4SLinus Torvalds 1488498f0103SMike Snitzer if (!strcasecmp(argv[0], "disable_group")) { 14896380f26fSMike Anderson r = bypass_pg_num(m, argv[1], 1); 14906380f26fSMike Anderson goto out; 1491498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "enable_group")) { 14926380f26fSMike Anderson r = bypass_pg_num(m, argv[1], 0); 14936380f26fSMike Anderson goto out; 1494498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "switch_group")) { 14956380f26fSMike Anderson r = switch_pg_num(m, argv[1]); 14966380f26fSMike Anderson goto out; 1497498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "reinstate_path")) 14981da177e4SLinus Torvalds action = reinstate_path; 1499498f0103SMike Snitzer else if (!strcasecmp(argv[0], "fail_path")) 15001da177e4SLinus Torvalds action = fail_path; 15016380f26fSMike Anderson else { 15026380f26fSMike Anderson DMWARN("Unrecognised multipath message received."); 15036380f26fSMike Anderson goto out; 15046380f26fSMike Anderson } 15051da177e4SLinus Torvalds 15068215d6ecSNikanth Karthikesan r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev); 15071da177e4SLinus Torvalds if (r) { 150872d94861SAlasdair G Kergon DMWARN("message: error getting device %s", 15091da177e4SLinus Torvalds argv[1]); 15106380f26fSMike Anderson goto out; 15111da177e4SLinus Torvalds } 15121da177e4SLinus Torvalds 15131da177e4SLinus Torvalds r = action_dev(m, dev, action); 15141da177e4SLinus Torvalds 15151da177e4SLinus Torvalds dm_put_device(ti, dev); 15161da177e4SLinus Torvalds 15176380f26fSMike Anderson out: 15186380f26fSMike Anderson mutex_unlock(&m->work_mutex); 15191da177e4SLinus Torvalds return r; 15201da177e4SLinus Torvalds } 15211da177e4SLinus Torvalds 1522647b3d00SAl Viro static int multipath_ioctl(struct dm_target *ti, unsigned int cmd, 15239af4aa30SMilan Broz unsigned long arg) 15249af4aa30SMilan Broz { 15259af4aa30SMilan Broz struct multipath *m = (struct multipath *) ti->private; 15269af4aa30SMilan Broz struct block_device *bdev = NULL; 1527633a08b8SAl Viro fmode_t mode = 0; 15289af4aa30SMilan Broz unsigned long flags; 15299af4aa30SMilan Broz int r = 0; 15309af4aa30SMilan Broz 15319af4aa30SMilan Broz spin_lock_irqsave(&m->lock, flags); 15329af4aa30SMilan Broz 15339af4aa30SMilan Broz if (!m->current_pgpath) 153402ab823fSKiyoshi Ueda __choose_pgpath(m, 0); 15359af4aa30SMilan Broz 1536e90dae1fSMilan Broz if (m->current_pgpath) { 15379af4aa30SMilan Broz bdev = m->current_pgpath->path.dev->bdev; 1538633a08b8SAl Viro mode = m->current_pgpath->path.dev->mode; 1539e90dae1fSMilan Broz } 15409af4aa30SMilan Broz 15419af4aa30SMilan Broz if (m->queue_io) 15429af4aa30SMilan Broz r = -EAGAIN; 15439af4aa30SMilan Broz else if (!bdev) 15449af4aa30SMilan Broz r = -EIO; 15459af4aa30SMilan Broz 15469af4aa30SMilan Broz spin_unlock_irqrestore(&m->lock, flags); 15479af4aa30SMilan Broz 1548ec8013beSPaolo Bonzini /* 1549ec8013beSPaolo Bonzini * Only pass ioctls through if the device sizes match exactly. 1550ec8013beSPaolo Bonzini */ 1551ec8013beSPaolo Bonzini if (!r && ti->len != i_size_read(bdev->bd_inode) >> SECTOR_SHIFT) 1552ec8013beSPaolo Bonzini r = scsi_verify_blk_ioctl(NULL, cmd); 1553ec8013beSPaolo Bonzini 1554633a08b8SAl Viro return r ? : __blkdev_driver_ioctl(bdev, mode, cmd, arg); 15559af4aa30SMilan Broz } 15569af4aa30SMilan Broz 1557af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti, 1558af4874e0SMike Snitzer iterate_devices_callout_fn fn, void *data) 1559af4874e0SMike Snitzer { 1560af4874e0SMike Snitzer struct multipath *m = ti->private; 1561af4874e0SMike Snitzer struct priority_group *pg; 1562af4874e0SMike Snitzer struct pgpath *p; 1563af4874e0SMike Snitzer int ret = 0; 1564af4874e0SMike Snitzer 1565af4874e0SMike Snitzer list_for_each_entry(pg, &m->priority_groups, list) { 1566af4874e0SMike Snitzer list_for_each_entry(p, &pg->pgpaths, list) { 15675dea271bSMike Snitzer ret = fn(ti, p->path.dev, ti->begin, ti->len, data); 1568af4874e0SMike Snitzer if (ret) 1569af4874e0SMike Snitzer goto out; 1570af4874e0SMike Snitzer } 1571af4874e0SMike Snitzer } 1572af4874e0SMike Snitzer 1573af4874e0SMike Snitzer out: 1574af4874e0SMike Snitzer return ret; 1575af4874e0SMike Snitzer } 1576af4874e0SMike Snitzer 1577f40c67f0SKiyoshi Ueda static int __pgpath_busy(struct pgpath *pgpath) 1578f40c67f0SKiyoshi Ueda { 1579f40c67f0SKiyoshi Ueda struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1580f40c67f0SKiyoshi Ueda 1581f40c67f0SKiyoshi Ueda return dm_underlying_device_busy(q); 1582f40c67f0SKiyoshi Ueda } 1583f40c67f0SKiyoshi Ueda 1584f40c67f0SKiyoshi Ueda /* 1585f40c67f0SKiyoshi Ueda * We return "busy", only when we can map I/Os but underlying devices 1586f40c67f0SKiyoshi Ueda * are busy (so even if we map I/Os now, the I/Os will wait on 1587f40c67f0SKiyoshi Ueda * the underlying queue). 1588f40c67f0SKiyoshi Ueda * In other words, if we want to kill I/Os or queue them inside us 1589f40c67f0SKiyoshi Ueda * due to map unavailability, we don't return "busy". Otherwise, 1590f40c67f0SKiyoshi Ueda * dm core won't give us the I/Os and we can't do what we want. 1591f40c67f0SKiyoshi Ueda */ 1592f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti) 1593f40c67f0SKiyoshi Ueda { 1594f40c67f0SKiyoshi Ueda int busy = 0, has_active = 0; 1595f40c67f0SKiyoshi Ueda struct multipath *m = ti->private; 1596f40c67f0SKiyoshi Ueda struct priority_group *pg; 1597f40c67f0SKiyoshi Ueda struct pgpath *pgpath; 1598f40c67f0SKiyoshi Ueda unsigned long flags; 1599f40c67f0SKiyoshi Ueda 1600f40c67f0SKiyoshi Ueda spin_lock_irqsave(&m->lock, flags); 1601f40c67f0SKiyoshi Ueda 1602f40c67f0SKiyoshi Ueda /* Guess which priority_group will be used at next mapping time */ 1603f40c67f0SKiyoshi Ueda if (unlikely(!m->current_pgpath && m->next_pg)) 1604f40c67f0SKiyoshi Ueda pg = m->next_pg; 1605f40c67f0SKiyoshi Ueda else if (likely(m->current_pg)) 1606f40c67f0SKiyoshi Ueda pg = m->current_pg; 1607f40c67f0SKiyoshi Ueda else 1608f40c67f0SKiyoshi Ueda /* 1609f40c67f0SKiyoshi Ueda * We don't know which pg will be used at next mapping time. 1610f40c67f0SKiyoshi Ueda * We don't call __choose_pgpath() here to avoid to trigger 1611f40c67f0SKiyoshi Ueda * pg_init just by busy checking. 1612f40c67f0SKiyoshi Ueda * So we don't know whether underlying devices we will be using 1613f40c67f0SKiyoshi Ueda * at next mapping time are busy or not. Just try mapping. 1614f40c67f0SKiyoshi Ueda */ 1615f40c67f0SKiyoshi Ueda goto out; 1616f40c67f0SKiyoshi Ueda 1617f40c67f0SKiyoshi Ueda /* 1618f40c67f0SKiyoshi Ueda * If there is one non-busy active path at least, the path selector 1619f40c67f0SKiyoshi Ueda * will be able to select it. So we consider such a pg as not busy. 1620f40c67f0SKiyoshi Ueda */ 1621f40c67f0SKiyoshi Ueda busy = 1; 1622f40c67f0SKiyoshi Ueda list_for_each_entry(pgpath, &pg->pgpaths, list) 1623f40c67f0SKiyoshi Ueda if (pgpath->is_active) { 1624f40c67f0SKiyoshi Ueda has_active = 1; 1625f40c67f0SKiyoshi Ueda 1626f40c67f0SKiyoshi Ueda if (!__pgpath_busy(pgpath)) { 1627f40c67f0SKiyoshi Ueda busy = 0; 1628f40c67f0SKiyoshi Ueda break; 1629f40c67f0SKiyoshi Ueda } 1630f40c67f0SKiyoshi Ueda } 1631f40c67f0SKiyoshi Ueda 1632f40c67f0SKiyoshi Ueda if (!has_active) 1633f40c67f0SKiyoshi Ueda /* 1634f40c67f0SKiyoshi Ueda * No active path in this pg, so this pg won't be used and 1635f40c67f0SKiyoshi Ueda * the current_pg will be changed at next mapping time. 1636f40c67f0SKiyoshi Ueda * We need to try mapping to determine it. 1637f40c67f0SKiyoshi Ueda */ 1638f40c67f0SKiyoshi Ueda busy = 0; 1639f40c67f0SKiyoshi Ueda 1640f40c67f0SKiyoshi Ueda out: 1641f40c67f0SKiyoshi Ueda spin_unlock_irqrestore(&m->lock, flags); 1642f40c67f0SKiyoshi Ueda 1643f40c67f0SKiyoshi Ueda return busy; 1644f40c67f0SKiyoshi Ueda } 1645f40c67f0SKiyoshi Ueda 16461da177e4SLinus Torvalds /*----------------------------------------------------------------- 16471da177e4SLinus Torvalds * Module setup 16481da177e4SLinus Torvalds *---------------------------------------------------------------*/ 16491da177e4SLinus Torvalds static struct target_type multipath_target = { 16501da177e4SLinus Torvalds .name = "multipath", 165119040c0bSMike Snitzer .version = {1, 3, 0}, 16521da177e4SLinus Torvalds .module = THIS_MODULE, 16531da177e4SLinus Torvalds .ctr = multipath_ctr, 16541da177e4SLinus Torvalds .dtr = multipath_dtr, 1655f40c67f0SKiyoshi Ueda .map_rq = multipath_map, 1656f40c67f0SKiyoshi Ueda .rq_end_io = multipath_end_io, 16571da177e4SLinus Torvalds .presuspend = multipath_presuspend, 16586df400abSKiyoshi Ueda .postsuspend = multipath_postsuspend, 16591da177e4SLinus Torvalds .resume = multipath_resume, 16601da177e4SLinus Torvalds .status = multipath_status, 16611da177e4SLinus Torvalds .message = multipath_message, 16629af4aa30SMilan Broz .ioctl = multipath_ioctl, 1663af4874e0SMike Snitzer .iterate_devices = multipath_iterate_devices, 1664f40c67f0SKiyoshi Ueda .busy = multipath_busy, 16651da177e4SLinus Torvalds }; 16661da177e4SLinus Torvalds 16671da177e4SLinus Torvalds static int __init dm_multipath_init(void) 16681da177e4SLinus Torvalds { 16691da177e4SLinus Torvalds int r; 16701da177e4SLinus Torvalds 16711da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 1672028867acSAlasdair G Kergon _mpio_cache = KMEM_CACHE(dm_mpath_io, 0); 16731da177e4SLinus Torvalds if (!_mpio_cache) 16741da177e4SLinus Torvalds return -ENOMEM; 16751da177e4SLinus Torvalds 16761da177e4SLinus Torvalds r = dm_register_target(&multipath_target); 16771da177e4SLinus Torvalds if (r < 0) { 16780cd33124SAlasdair G Kergon DMERR("register failed %d", r); 16791da177e4SLinus Torvalds kmem_cache_destroy(_mpio_cache); 16801da177e4SLinus Torvalds return -EINVAL; 16811da177e4SLinus Torvalds } 16821da177e4SLinus Torvalds 16834d4d66abSTejun Heo kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0); 1684c557308eSAlasdair G Kergon if (!kmultipathd) { 16850cd33124SAlasdair G Kergon DMERR("failed to create workqueue kmpathd"); 1686c557308eSAlasdair G Kergon dm_unregister_target(&multipath_target); 1687c557308eSAlasdair G Kergon kmem_cache_destroy(_mpio_cache); 1688c557308eSAlasdair G Kergon return -ENOMEM; 1689c557308eSAlasdair G Kergon } 1690c557308eSAlasdair G Kergon 1691bab7cfc7SChandra Seetharaman /* 1692bab7cfc7SChandra Seetharaman * A separate workqueue is used to handle the device handlers 1693bab7cfc7SChandra Seetharaman * to avoid overloading existing workqueue. Overloading the 1694bab7cfc7SChandra Seetharaman * old workqueue would also create a bottleneck in the 1695bab7cfc7SChandra Seetharaman * path of the storage hardware device activation. 1696bab7cfc7SChandra Seetharaman */ 16974d4d66abSTejun Heo kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd", 16984d4d66abSTejun Heo WQ_MEM_RECLAIM); 1699bab7cfc7SChandra Seetharaman if (!kmpath_handlerd) { 1700bab7cfc7SChandra Seetharaman DMERR("failed to create workqueue kmpath_handlerd"); 1701bab7cfc7SChandra Seetharaman destroy_workqueue(kmultipathd); 1702bab7cfc7SChandra Seetharaman dm_unregister_target(&multipath_target); 1703bab7cfc7SChandra Seetharaman kmem_cache_destroy(_mpio_cache); 1704bab7cfc7SChandra Seetharaman return -ENOMEM; 1705bab7cfc7SChandra Seetharaman } 1706bab7cfc7SChandra Seetharaman 170772d94861SAlasdair G Kergon DMINFO("version %u.%u.%u loaded", 17081da177e4SLinus Torvalds multipath_target.version[0], multipath_target.version[1], 17091da177e4SLinus Torvalds multipath_target.version[2]); 17101da177e4SLinus Torvalds 17111da177e4SLinus Torvalds return r; 17121da177e4SLinus Torvalds } 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds static void __exit dm_multipath_exit(void) 17151da177e4SLinus Torvalds { 1716bab7cfc7SChandra Seetharaman destroy_workqueue(kmpath_handlerd); 1717c557308eSAlasdair G Kergon destroy_workqueue(kmultipathd); 1718c557308eSAlasdair G Kergon 171910d3bd09SMikulas Patocka dm_unregister_target(&multipath_target); 17201da177e4SLinus Torvalds kmem_cache_destroy(_mpio_cache); 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds 17231da177e4SLinus Torvalds module_init(dm_multipath_init); 17241da177e4SLinus Torvalds module_exit(dm_multipath_exit); 17251da177e4SLinus Torvalds 17261da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target"); 17271da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); 17281da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1729