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 10f4790826SMike Snitzer #include "dm.h" 111da177e4SLinus Torvalds #include "dm-path-selector.h" 12b15546f9SMike Anderson #include "dm-uevent.h" 131da177e4SLinus Torvalds 14e5863d9aSMike Snitzer #include <linux/blkdev.h> 151da177e4SLinus Torvalds #include <linux/ctype.h> 161da177e4SLinus Torvalds #include <linux/init.h> 171da177e4SLinus Torvalds #include <linux/mempool.h> 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/pagemap.h> 201da177e4SLinus Torvalds #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/time.h> 221da177e4SLinus Torvalds #include <linux/workqueue.h> 2335991652SMikulas Patocka #include <linux/delay.h> 24cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h> 2560063497SArun Sharma #include <linux/atomic.h> 261da177e4SLinus Torvalds 2772d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath" 284e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000 294e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1) 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds /* Path properties */ 321da177e4SLinus Torvalds struct pgpath { 331da177e4SLinus Torvalds struct list_head list; 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds struct priority_group *pg; /* Owning PG */ 366680073dSKiyoshi Ueda unsigned is_active; /* Path status */ 371da177e4SLinus Torvalds unsigned fail_count; /* Cumulative failure count */ 381da177e4SLinus Torvalds 39c922d5f7SJosef "Jeff" Sipek struct dm_path path; 404e2d19e4SChandra Seetharaman struct delayed_work activate_path; 411da177e4SLinus Torvalds }; 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path) 441da177e4SLinus Torvalds 451da177e4SLinus Torvalds /* 461da177e4SLinus Torvalds * Paths are grouped into Priority Groups and numbered from 1 upwards. 471da177e4SLinus Torvalds * Each has a path selector which controls which path gets used. 481da177e4SLinus Torvalds */ 491da177e4SLinus Torvalds struct priority_group { 501da177e4SLinus Torvalds struct list_head list; 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds struct multipath *m; /* Owning multipath instance */ 531da177e4SLinus Torvalds struct path_selector ps; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds unsigned pg_num; /* Reference number */ 561da177e4SLinus Torvalds unsigned bypassed; /* Temporarily bypass this PG? */ 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds unsigned nr_pgpaths; /* Number of paths in PG */ 591da177e4SLinus Torvalds struct list_head pgpaths; 601da177e4SLinus Torvalds }; 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds /* Multipath context */ 631da177e4SLinus Torvalds struct multipath { 641da177e4SLinus Torvalds struct list_head list; 651da177e4SLinus Torvalds struct dm_target *ti; 661da177e4SLinus Torvalds 67cfae5c9bSChandra Seetharaman const char *hw_handler_name; 682bfd2e13SChandra Seetharaman char *hw_handler_params; 694e2d19e4SChandra Seetharaman 701fbdd2b3SMike Snitzer spinlock_t lock; 711fbdd2b3SMike Snitzer 721da177e4SLinus Torvalds unsigned nr_priority_groups; 731da177e4SLinus Torvalds struct list_head priority_groups; 744e2d19e4SChandra Seetharaman 754e2d19e4SChandra Seetharaman wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */ 764e2d19e4SChandra Seetharaman 771da177e4SLinus Torvalds unsigned pg_init_required; /* pg_init needs calling? */ 78c3cd4f6bSAlasdair G Kergon unsigned pg_init_in_progress; /* Only one pg_init allowed at once */ 794e2d19e4SChandra Seetharaman unsigned pg_init_delay_retry; /* Delay pg_init retry? */ 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds unsigned nr_valid_paths; /* Total number of usable paths */ 821da177e4SLinus Torvalds struct pgpath *current_pgpath; 831da177e4SLinus Torvalds struct priority_group *current_pg; 841da177e4SLinus Torvalds struct priority_group *next_pg; /* Switch to this PG if set */ 851da177e4SLinus Torvalds unsigned repeat_count; /* I/Os left before calling PS again */ 861da177e4SLinus Torvalds 871fbdd2b3SMike Snitzer unsigned queue_io:1; /* Must we queue all I/O? */ 881fbdd2b3SMike Snitzer unsigned queue_if_no_path:1; /* Queue I/O if last path fails? */ 891fbdd2b3SMike Snitzer unsigned saved_queue_if_no_path:1; /* Saved state during suspension */ 90a58a935dSMike Snitzer unsigned retain_attached_hw_handler:1; /* If there's already a hw_handler present, don't change it. */ 91954a73d5SShiva Krishna Merla unsigned pg_init_disabled:1; /* pg_init is not currently allowed */ 921fbdd2b3SMike Snitzer 93c9e45581SDave Wysochanski unsigned pg_init_retries; /* Number of times to retry pg_init */ 94c9e45581SDave Wysochanski unsigned pg_init_count; /* Number of times pg_init called */ 954e2d19e4SChandra Seetharaman unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */ 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds struct work_struct trigger_event; 981da177e4SLinus Torvalds 991da177e4SLinus Torvalds /* 100028867acSAlasdair G Kergon * We must use a mempool of dm_mpath_io structs so that we 1011da177e4SLinus Torvalds * can resubmit bios on error. 1021da177e4SLinus Torvalds */ 1031da177e4SLinus Torvalds mempool_t *mpio_pool; 1046380f26fSMike Anderson 1056380f26fSMike Anderson struct mutex work_mutex; 1061da177e4SLinus Torvalds }; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* 1091da177e4SLinus Torvalds * Context information attached to each bio we process. 1101da177e4SLinus Torvalds */ 111028867acSAlasdair G Kergon struct dm_mpath_io { 1121da177e4SLinus Torvalds struct pgpath *pgpath; 11302ab823fSKiyoshi Ueda size_t nr_bytes; 1141da177e4SLinus Torvalds }; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath); 1171da177e4SLinus Torvalds 118e18b890bSChristoph Lameter static struct kmem_cache *_mpio_cache; 1191da177e4SLinus Torvalds 120bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd; 121c4028958SDavid Howells static void trigger_event(struct work_struct *work); 122bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work); 123e8099177SHannes Reinecke static int __pgpath_busy(struct pgpath *pgpath); 1241da177e4SLinus Torvalds 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /*----------------------------------------------- 1271da177e4SLinus Torvalds * Allocation routines 1281da177e4SLinus Torvalds *-----------------------------------------------*/ 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void) 1311da177e4SLinus Torvalds { 132e69fae56SMicha³ Miros³aw struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL); 1331da177e4SLinus Torvalds 134224cb3e9SMike Anderson if (pgpath) { 1356680073dSKiyoshi Ueda pgpath->is_active = 1; 1364e2d19e4SChandra Seetharaman INIT_DELAYED_WORK(&pgpath->activate_path, activate_path); 137224cb3e9SMike Anderson } 1381da177e4SLinus Torvalds 1391da177e4SLinus Torvalds return pgpath; 1401da177e4SLinus Torvalds } 1411da177e4SLinus Torvalds 142028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath) 1431da177e4SLinus Torvalds { 1441da177e4SLinus Torvalds kfree(pgpath); 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 1471da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void) 1481da177e4SLinus Torvalds { 1491da177e4SLinus Torvalds struct priority_group *pg; 1501da177e4SLinus Torvalds 151e69fae56SMicha³ Miros³aw pg = kzalloc(sizeof(*pg), GFP_KERNEL); 1521da177e4SLinus Torvalds 153e69fae56SMicha³ Miros³aw if (pg) 1541da177e4SLinus Torvalds INIT_LIST_HEAD(&pg->pgpaths); 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds return pg; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti) 1601da177e4SLinus Torvalds { 1611da177e4SLinus Torvalds struct pgpath *pgpath, *tmp; 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds list_for_each_entry_safe(pgpath, tmp, pgpaths, list) { 1641da177e4SLinus Torvalds list_del(&pgpath->list); 1651da177e4SLinus Torvalds dm_put_device(ti, pgpath->path.dev); 1661da177e4SLinus Torvalds free_pgpath(pgpath); 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg, 1711da177e4SLinus Torvalds struct dm_target *ti) 1721da177e4SLinus Torvalds { 1731da177e4SLinus Torvalds struct path_selector *ps = &pg->ps; 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds if (ps->type) { 1761da177e4SLinus Torvalds ps->type->destroy(ps); 1771da177e4SLinus Torvalds dm_put_path_selector(ps->type); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds free_pgpaths(&pg->pgpaths, ti); 1811da177e4SLinus Torvalds kfree(pg); 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 184*8637a6bfSMike Snitzer static struct multipath *alloc_multipath(struct dm_target *ti, bool use_blk_mq) 1851da177e4SLinus Torvalds { 1861da177e4SLinus Torvalds struct multipath *m; 1871da177e4SLinus Torvalds 188e69fae56SMicha³ Miros³aw m = kzalloc(sizeof(*m), GFP_KERNEL); 1891da177e4SLinus Torvalds if (m) { 1901da177e4SLinus Torvalds INIT_LIST_HEAD(&m->priority_groups); 1911da177e4SLinus Torvalds spin_lock_init(&m->lock); 1921da177e4SLinus Torvalds m->queue_io = 1; 1934e2d19e4SChandra Seetharaman m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT; 194c4028958SDavid Howells INIT_WORK(&m->trigger_event, trigger_event); 1952bded7bdSKiyoshi Ueda init_waitqueue_head(&m->pg_init_wait); 1966380f26fSMike Anderson mutex_init(&m->work_mutex); 197*8637a6bfSMike Snitzer 198*8637a6bfSMike Snitzer m->mpio_pool = NULL; 199*8637a6bfSMike Snitzer if (!use_blk_mq) { 200*8637a6bfSMike Snitzer unsigned min_ios = dm_get_reserved_rq_based_ios(); 201*8637a6bfSMike Snitzer 202f4790826SMike Snitzer m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache); 2031da177e4SLinus Torvalds if (!m->mpio_pool) { 2041da177e4SLinus Torvalds kfree(m); 2051da177e4SLinus Torvalds return NULL; 2061da177e4SLinus Torvalds } 207*8637a6bfSMike Snitzer } 208*8637a6bfSMike Snitzer 20928f16c20SMicha³ Miros³aw m->ti = ti; 21028f16c20SMicha³ Miros³aw ti->private = m; 2111da177e4SLinus Torvalds } 2121da177e4SLinus Torvalds 2131da177e4SLinus Torvalds return m; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds static void free_multipath(struct multipath *m) 2171da177e4SLinus Torvalds { 2181da177e4SLinus Torvalds struct priority_group *pg, *tmp; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { 2211da177e4SLinus Torvalds list_del(&pg->list); 2221da177e4SLinus Torvalds free_priority_group(pg, m->ti); 2231da177e4SLinus Torvalds } 2241da177e4SLinus Torvalds 225cfae5c9bSChandra Seetharaman kfree(m->hw_handler_name); 2262bfd2e13SChandra Seetharaman kfree(m->hw_handler_params); 2271da177e4SLinus Torvalds mempool_destroy(m->mpio_pool); 2281da177e4SLinus Torvalds kfree(m); 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 231466891f9SJun'ichi Nomura static int set_mapinfo(struct multipath *m, union map_info *info) 232466891f9SJun'ichi Nomura { 233466891f9SJun'ichi Nomura struct dm_mpath_io *mpio; 234466891f9SJun'ichi Nomura 235*8637a6bfSMike Snitzer if (!m->mpio_pool) { 236*8637a6bfSMike Snitzer /* Use blk-mq pdu memory requested via per_io_data_size */ 237*8637a6bfSMike Snitzer mpio = info->ptr; 238*8637a6bfSMike Snitzer memset(mpio, 0, sizeof(*mpio)); 239*8637a6bfSMike Snitzer return mpio; 240*8637a6bfSMike Snitzer } 241*8637a6bfSMike Snitzer 242466891f9SJun'ichi Nomura mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC); 243466891f9SJun'ichi Nomura if (!mpio) 244466891f9SJun'ichi Nomura return -ENOMEM; 245466891f9SJun'ichi Nomura 246466891f9SJun'ichi Nomura memset(mpio, 0, sizeof(*mpio)); 247466891f9SJun'ichi Nomura info->ptr = mpio; 248466891f9SJun'ichi Nomura 249466891f9SJun'ichi Nomura return 0; 250466891f9SJun'ichi Nomura } 251466891f9SJun'ichi Nomura 252466891f9SJun'ichi Nomura static void clear_mapinfo(struct multipath *m, union map_info *info) 253466891f9SJun'ichi Nomura { 254*8637a6bfSMike Snitzer /* Only needed for non blk-mq */ 255*8637a6bfSMike Snitzer if (m->mpio_pool) { 256466891f9SJun'ichi Nomura struct dm_mpath_io *mpio = info->ptr; 257466891f9SJun'ichi Nomura 258466891f9SJun'ichi Nomura info->ptr = NULL; 259466891f9SJun'ichi Nomura mempool_free(mpio, m->mpio_pool); 260466891f9SJun'ichi Nomura } 261*8637a6bfSMike Snitzer } 2621da177e4SLinus Torvalds 2631da177e4SLinus Torvalds /*----------------------------------------------- 2641da177e4SLinus Torvalds * Path selection 2651da177e4SLinus Torvalds *-----------------------------------------------*/ 2661da177e4SLinus Torvalds 2673e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m) 268fb612642SKiyoshi Ueda { 269fb612642SKiyoshi Ueda struct pgpath *pgpath; 2704e2d19e4SChandra Seetharaman unsigned long pg_init_delay = 0; 271fb612642SKiyoshi Ueda 27217f4ff45SHannes Reinecke if (m->pg_init_in_progress || m->pg_init_disabled) 2733e9f1be1SHannes Reinecke return 0; 27417f4ff45SHannes Reinecke 275fb612642SKiyoshi Ueda m->pg_init_count++; 276fb612642SKiyoshi Ueda m->pg_init_required = 0; 2773e9f1be1SHannes Reinecke 2783e9f1be1SHannes Reinecke /* Check here to reset pg_init_required */ 2793e9f1be1SHannes Reinecke if (!m->current_pg) 2803e9f1be1SHannes Reinecke return 0; 2813e9f1be1SHannes Reinecke 2824e2d19e4SChandra Seetharaman if (m->pg_init_delay_retry) 2834e2d19e4SChandra Seetharaman pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ? 2844e2d19e4SChandra Seetharaman m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS); 285fb612642SKiyoshi Ueda list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) { 286fb612642SKiyoshi Ueda /* Skip failed paths */ 287fb612642SKiyoshi Ueda if (!pgpath->is_active) 288fb612642SKiyoshi Ueda continue; 2894e2d19e4SChandra Seetharaman if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path, 2904e2d19e4SChandra Seetharaman pg_init_delay)) 291fb612642SKiyoshi Ueda m->pg_init_in_progress++; 292fb612642SKiyoshi Ueda } 2933e9f1be1SHannes Reinecke return m->pg_init_in_progress; 294fb612642SKiyoshi Ueda } 295fb612642SKiyoshi Ueda 2961da177e4SLinus Torvalds static void __switch_pg(struct multipath *m, struct pgpath *pgpath) 2971da177e4SLinus Torvalds { 2981da177e4SLinus Torvalds m->current_pg = pgpath->pg; 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds /* Must we initialise the PG first, and queue I/O till it's ready? */ 301cfae5c9bSChandra Seetharaman if (m->hw_handler_name) { 3021da177e4SLinus Torvalds m->pg_init_required = 1; 3031da177e4SLinus Torvalds m->queue_io = 1; 3041da177e4SLinus Torvalds } else { 3051da177e4SLinus Torvalds m->pg_init_required = 0; 3061da177e4SLinus Torvalds m->queue_io = 0; 3071da177e4SLinus Torvalds } 308c9e45581SDave Wysochanski 309c9e45581SDave Wysochanski m->pg_init_count = 0; 3101da177e4SLinus Torvalds } 3111da177e4SLinus Torvalds 31202ab823fSKiyoshi Ueda static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg, 31302ab823fSKiyoshi Ueda size_t nr_bytes) 3141da177e4SLinus Torvalds { 315c922d5f7SJosef "Jeff" Sipek struct dm_path *path; 3161da177e4SLinus Torvalds 31702ab823fSKiyoshi Ueda path = pg->ps.type->select_path(&pg->ps, &m->repeat_count, nr_bytes); 3181da177e4SLinus Torvalds if (!path) 3191da177e4SLinus Torvalds return -ENXIO; 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds m->current_pgpath = path_to_pgpath(path); 3221da177e4SLinus Torvalds 3231da177e4SLinus Torvalds if (m->current_pg != pg) 3241da177e4SLinus Torvalds __switch_pg(m, m->current_pgpath); 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds return 0; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 32902ab823fSKiyoshi Ueda static void __choose_pgpath(struct multipath *m, size_t nr_bytes) 3301da177e4SLinus Torvalds { 3311da177e4SLinus Torvalds struct priority_group *pg; 3321da177e4SLinus Torvalds unsigned bypassed = 1; 3331da177e4SLinus Torvalds 3341f271972SBenjamin Marzinski if (!m->nr_valid_paths) { 3351f271972SBenjamin Marzinski m->queue_io = 0; 3361da177e4SLinus Torvalds goto failed; 3371f271972SBenjamin Marzinski } 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds /* Were we instructed to switch PG? */ 3401da177e4SLinus Torvalds if (m->next_pg) { 3411da177e4SLinus Torvalds pg = m->next_pg; 3421da177e4SLinus Torvalds m->next_pg = NULL; 34302ab823fSKiyoshi Ueda if (!__choose_path_in_pg(m, pg, nr_bytes)) 3441da177e4SLinus Torvalds return; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds /* Don't change PG until it has no remaining paths */ 34802ab823fSKiyoshi Ueda if (m->current_pg && !__choose_path_in_pg(m, m->current_pg, nr_bytes)) 3491da177e4SLinus Torvalds return; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds /* 3521da177e4SLinus Torvalds * Loop through priority groups until we find a valid path. 3531da177e4SLinus Torvalds * First time we skip PGs marked 'bypassed'. 354f220fd4eSMike Christie * Second time we only try the ones we skipped, but set 355f220fd4eSMike Christie * pg_init_delay_retry so we do not hammer controllers. 3561da177e4SLinus Torvalds */ 3571da177e4SLinus Torvalds do { 3581da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 3591da177e4SLinus Torvalds if (pg->bypassed == bypassed) 3601da177e4SLinus Torvalds continue; 361f220fd4eSMike Christie if (!__choose_path_in_pg(m, pg, nr_bytes)) { 362f220fd4eSMike Christie if (!bypassed) 363f220fd4eSMike Christie m->pg_init_delay_retry = 1; 3641da177e4SLinus Torvalds return; 3651da177e4SLinus Torvalds } 366f220fd4eSMike Christie } 3671da177e4SLinus Torvalds } while (bypassed--); 3681da177e4SLinus Torvalds 3691da177e4SLinus Torvalds failed: 3701da177e4SLinus Torvalds m->current_pgpath = NULL; 3711da177e4SLinus Torvalds m->current_pg = NULL; 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 37445e15720SKiyoshi Ueda /* 37545e15720SKiyoshi Ueda * Check whether bios must be queued in the device-mapper core rather 37645e15720SKiyoshi Ueda * than here in the target. 37745e15720SKiyoshi Ueda * 37845e15720SKiyoshi Ueda * m->lock must be held on entry. 37945e15720SKiyoshi Ueda * 38045e15720SKiyoshi Ueda * If m->queue_if_no_path and m->saved_queue_if_no_path hold the 38145e15720SKiyoshi Ueda * same value then we are not between multipath_presuspend() 38245e15720SKiyoshi Ueda * and multipath_resume() calls and we have no need to check 38345e15720SKiyoshi Ueda * for the DMF_NOFLUSH_SUSPENDING flag. 38445e15720SKiyoshi Ueda */ 38545e15720SKiyoshi Ueda static int __must_push_back(struct multipath *m) 38645e15720SKiyoshi Ueda { 387e8099177SHannes Reinecke return (m->queue_if_no_path || 388e8099177SHannes Reinecke (m->queue_if_no_path != m->saved_queue_if_no_path && 389e8099177SHannes Reinecke dm_noflush_suspending(m->ti))); 39045e15720SKiyoshi Ueda } 39145e15720SKiyoshi Ueda 39236fcffccSHannes Reinecke /* 39336fcffccSHannes Reinecke * Map cloned requests 39436fcffccSHannes Reinecke */ 395e5863d9aSMike Snitzer static int __multipath_map(struct dm_target *ti, struct request *clone, 396e5863d9aSMike Snitzer union map_info *map_context, 397e5863d9aSMike Snitzer struct request *rq, struct request **__clone) 3981da177e4SLinus Torvalds { 39936fcffccSHannes Reinecke struct multipath *m = (struct multipath *) ti->private; 400e3bde04fSHannes Reinecke int r = DM_MAPIO_REQUEUE; 401e5863d9aSMike Snitzer size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq); 4021da177e4SLinus Torvalds struct pgpath *pgpath; 403f40c67f0SKiyoshi Ueda struct block_device *bdev; 404e3bde04fSHannes Reinecke struct dm_mpath_io *mpio; 4051da177e4SLinus Torvalds 4062eb6e1e3SKeith Busch spin_lock_irq(&m->lock); 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /* Do we need to select a new pgpath? */ 4091da177e4SLinus Torvalds if (!m->current_pgpath || 4101da177e4SLinus Torvalds (!m->queue_io && (m->repeat_count && --m->repeat_count == 0))) 41102ab823fSKiyoshi Ueda __choose_pgpath(m, nr_bytes); 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds pgpath = m->current_pgpath; 4141da177e4SLinus Torvalds 4159bf59a61SMike Snitzer if (!pgpath) { 4169bf59a61SMike Snitzer if (!__must_push_back(m)) 4179bf59a61SMike Snitzer r = -EIO; /* Failed */ 4189bf59a61SMike Snitzer goto out_unlock; 4196afbc01dSMike Snitzer } else if (m->queue_io || m->pg_init_required) { 4209bf59a61SMike Snitzer __pg_init_all_paths(m); 4219bf59a61SMike Snitzer goto out_unlock; 4229bf59a61SMike Snitzer } 4236afbc01dSMike Snitzer 424e3bde04fSHannes Reinecke if (set_mapinfo(m, map_context) < 0) 425e3bde04fSHannes Reinecke /* ENOMEM, requeue */ 426e3bde04fSHannes Reinecke goto out_unlock; 427e3bde04fSHannes Reinecke 428e3bde04fSHannes Reinecke mpio = map_context->ptr; 429e8099177SHannes Reinecke mpio->pgpath = pgpath; 430e8099177SHannes Reinecke mpio->nr_bytes = nr_bytes; 4312eb6e1e3SKeith Busch 4322eb6e1e3SKeith Busch bdev = pgpath->path.dev->bdev; 4332eb6e1e3SKeith Busch 434e5863d9aSMike Snitzer spin_unlock_irq(&m->lock); 435e5863d9aSMike Snitzer 436e5863d9aSMike Snitzer if (clone) { 437c5248f79SMike Snitzer /* 438c5248f79SMike Snitzer * Old request-based interface: allocated clone is passed in. 439c5248f79SMike Snitzer * Used by: .request_fn stacked on .request_fn path(s). 440c5248f79SMike Snitzer */ 4412eb6e1e3SKeith Busch clone->q = bdev_get_queue(bdev); 4422eb6e1e3SKeith Busch clone->rq_disk = bdev->bd_disk; 4432eb6e1e3SKeith Busch clone->cmd_flags |= REQ_FAILFAST_TRANSPORT; 444e5863d9aSMike Snitzer } else { 445eca7ee6dSMike Snitzer /* 446eca7ee6dSMike Snitzer * blk-mq request-based interface; used by both: 447eca7ee6dSMike Snitzer * .request_fn stacked on blk-mq path(s) and 448eca7ee6dSMike Snitzer * blk-mq stacked on blk-mq path(s). 449eca7ee6dSMike Snitzer */ 450e5863d9aSMike Snitzer *__clone = blk_get_request(bdev_get_queue(bdev), 45102233342SMike Snitzer rq_data_dir(rq), GFP_ATOMIC); 4524c6dd53dSMike Snitzer if (IS_ERR(*__clone)) { 453e5863d9aSMike Snitzer /* ENOMEM, requeue */ 4544c6dd53dSMike Snitzer clear_mapinfo(m, map_context); 455e5863d9aSMike Snitzer return r; 4564c6dd53dSMike Snitzer } 457e5863d9aSMike Snitzer (*__clone)->bio = (*__clone)->biotail = NULL; 458e5863d9aSMike Snitzer (*__clone)->rq_disk = bdev->bd_disk; 459e5863d9aSMike Snitzer (*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT; 460e5863d9aSMike Snitzer } 4612eb6e1e3SKeith Busch 462e8099177SHannes Reinecke if (pgpath->pg->ps.type->start_io) 463e8099177SHannes Reinecke pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 464e8099177SHannes Reinecke &pgpath->path, 465e8099177SHannes Reinecke nr_bytes); 4662eb6e1e3SKeith Busch return DM_MAPIO_REMAPPED; 4671da177e4SLinus Torvalds 468e3bde04fSHannes Reinecke out_unlock: 4692eb6e1e3SKeith Busch spin_unlock_irq(&m->lock); 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds return r; 4721da177e4SLinus Torvalds } 4731da177e4SLinus Torvalds 474e5863d9aSMike Snitzer static int multipath_map(struct dm_target *ti, struct request *clone, 475e5863d9aSMike Snitzer union map_info *map_context) 476e5863d9aSMike Snitzer { 477e5863d9aSMike Snitzer return __multipath_map(ti, clone, map_context, NULL, NULL); 478e5863d9aSMike Snitzer } 479e5863d9aSMike Snitzer 480e5863d9aSMike Snitzer static int multipath_clone_and_map(struct dm_target *ti, struct request *rq, 481e5863d9aSMike Snitzer union map_info *map_context, 482e5863d9aSMike Snitzer struct request **clone) 483e5863d9aSMike Snitzer { 484e5863d9aSMike Snitzer return __multipath_map(ti, NULL, map_context, rq, clone); 485e5863d9aSMike Snitzer } 486e5863d9aSMike Snitzer 487e5863d9aSMike Snitzer static void multipath_release_clone(struct request *clone) 488e5863d9aSMike Snitzer { 489e5863d9aSMike Snitzer blk_put_request(clone); 490e5863d9aSMike Snitzer } 491e5863d9aSMike Snitzer 4921da177e4SLinus Torvalds /* 4931da177e4SLinus Torvalds * If we run out of usable paths, should we queue I/O or error it? 4941da177e4SLinus Torvalds */ 495485ef69eSAlasdair G Kergon static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path, 496485ef69eSAlasdair G Kergon unsigned save_old_value) 4971da177e4SLinus Torvalds { 4981da177e4SLinus Torvalds unsigned long flags; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 5011da177e4SLinus Torvalds 502485ef69eSAlasdair G Kergon if (save_old_value) 503436d4108SAlasdair G Kergon m->saved_queue_if_no_path = m->queue_if_no_path; 504485ef69eSAlasdair G Kergon else 505485ef69eSAlasdair G Kergon m->saved_queue_if_no_path = queue_if_no_path; 5061da177e4SLinus Torvalds m->queue_if_no_path = queue_if_no_path; 5071da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 5081da177e4SLinus Torvalds 50963d832c3SHannes Reinecke if (!queue_if_no_path) 51063d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 51163d832c3SHannes Reinecke 5121da177e4SLinus Torvalds return 0; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds /* 5161da177e4SLinus Torvalds * An event is triggered whenever a path is taken out of use. 5171da177e4SLinus Torvalds * Includes path failure and PG bypass. 5181da177e4SLinus Torvalds */ 519c4028958SDavid Howells static void trigger_event(struct work_struct *work) 5201da177e4SLinus Torvalds { 521c4028958SDavid Howells struct multipath *m = 522c4028958SDavid Howells container_of(work, struct multipath, trigger_event); 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds dm_table_event(m->ti->table); 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds /*----------------------------------------------------------------- 5281da177e4SLinus Torvalds * Constructor/argument parsing: 5291da177e4SLinus Torvalds * <#multipath feature args> [<arg>]* 5301da177e4SLinus Torvalds * <#hw_handler args> [hw_handler [<arg>]*] 5311da177e4SLinus Torvalds * <#priority groups> 5321da177e4SLinus Torvalds * <initial priority group> 5331da177e4SLinus Torvalds * [<selector> <#selector args> [<arg>]* 5341da177e4SLinus Torvalds * <#paths> <#per-path selector args> 5351da177e4SLinus Torvalds * [<path> [<arg>]* ]+ ]+ 5361da177e4SLinus Torvalds *---------------------------------------------------------------*/ 537498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg, 5381da177e4SLinus Torvalds struct dm_target *ti) 5391da177e4SLinus Torvalds { 5401da177e4SLinus Torvalds int r; 5411da177e4SLinus Torvalds struct path_selector_type *pst; 5421da177e4SLinus Torvalds unsigned ps_argc; 5431da177e4SLinus Torvalds 544498f0103SMike Snitzer static struct dm_arg _args[] = { 54572d94861SAlasdair G Kergon {0, 1024, "invalid number of path selector args"}, 5461da177e4SLinus Torvalds }; 5471da177e4SLinus Torvalds 548498f0103SMike Snitzer pst = dm_get_path_selector(dm_shift_arg(as)); 5491da177e4SLinus Torvalds if (!pst) { 55072d94861SAlasdair G Kergon ti->error = "unknown path selector type"; 5511da177e4SLinus Torvalds return -EINVAL; 5521da177e4SLinus Torvalds } 5531da177e4SLinus Torvalds 554498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &ps_argc, &ti->error); 555371b2e34SMikulas Patocka if (r) { 556371b2e34SMikulas Patocka dm_put_path_selector(pst); 5571da177e4SLinus Torvalds return -EINVAL; 558371b2e34SMikulas Patocka } 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds r = pst->create(&pg->ps, ps_argc, as->argv); 5611da177e4SLinus Torvalds if (r) { 5621da177e4SLinus Torvalds dm_put_path_selector(pst); 56372d94861SAlasdair G Kergon ti->error = "path selector constructor failed"; 5641da177e4SLinus Torvalds return r; 5651da177e4SLinus Torvalds } 5661da177e4SLinus Torvalds 5671da177e4SLinus Torvalds pg->ps.type = pst; 568498f0103SMike Snitzer dm_consume_args(as, ps_argc); 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds return 0; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 573498f0103SMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps, 5741da177e4SLinus Torvalds struct dm_target *ti) 5751da177e4SLinus Torvalds { 5761da177e4SLinus Torvalds int r; 5771da177e4SLinus Torvalds struct pgpath *p; 578ae11b1b3SHannes Reinecke struct multipath *m = ti->private; 579a58a935dSMike Snitzer struct request_queue *q = NULL; 580a58a935dSMike Snitzer const char *attached_handler_name; 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds /* we need at least a path arg */ 5831da177e4SLinus Torvalds if (as->argc < 1) { 58472d94861SAlasdair G Kergon ti->error = "no device given"; 58501460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 5861da177e4SLinus Torvalds } 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds p = alloc_pgpath(); 5891da177e4SLinus Torvalds if (!p) 59001460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 5911da177e4SLinus Torvalds 592498f0103SMike Snitzer r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table), 5938215d6ecSNikanth Karthikesan &p->path.dev); 5941da177e4SLinus Torvalds if (r) { 59572d94861SAlasdair G Kergon ti->error = "error getting device"; 5961da177e4SLinus Torvalds goto bad; 5971da177e4SLinus Torvalds } 5981da177e4SLinus Torvalds 599a58a935dSMike Snitzer if (m->retain_attached_hw_handler || m->hw_handler_name) 600a58a935dSMike Snitzer q = bdev_get_queue(p->path.dev->bdev); 601a0cf7ea9SHannes Reinecke 602a58a935dSMike Snitzer if (m->retain_attached_hw_handler) { 6031bab0de0SChristoph Hellwig retain: 604a58a935dSMike Snitzer attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL); 605a58a935dSMike Snitzer if (attached_handler_name) { 606a58a935dSMike Snitzer /* 607a58a935dSMike Snitzer * Reset hw_handler_name to match the attached handler 608a58a935dSMike Snitzer * and clear any hw_handler_params associated with the 609a58a935dSMike Snitzer * ignored handler. 610a58a935dSMike Snitzer * 611a58a935dSMike Snitzer * NB. This modifies the table line to show the actual 612a58a935dSMike Snitzer * handler instead of the original table passed in. 613a58a935dSMike Snitzer */ 614a58a935dSMike Snitzer kfree(m->hw_handler_name); 615a58a935dSMike Snitzer m->hw_handler_name = attached_handler_name; 616a58a935dSMike Snitzer 617a58a935dSMike Snitzer kfree(m->hw_handler_params); 618a58a935dSMike Snitzer m->hw_handler_params = NULL; 619a58a935dSMike Snitzer } 620a58a935dSMike Snitzer } 621a58a935dSMike Snitzer 622a58a935dSMike Snitzer if (m->hw_handler_name) { 623a0cf7ea9SHannes Reinecke r = scsi_dh_attach(q, m->hw_handler_name); 624a0cf7ea9SHannes Reinecke if (r == -EBUSY) { 6251bab0de0SChristoph Hellwig char b[BDEVNAME_SIZE]; 626a0cf7ea9SHannes Reinecke 6271bab0de0SChristoph Hellwig printk(KERN_INFO "dm-mpath: retaining handler on device %s\n", 6281bab0de0SChristoph Hellwig bdevname(p->path.dev->bdev, b)); 6291bab0de0SChristoph Hellwig goto retain; 6301bab0de0SChristoph Hellwig } 631ae11b1b3SHannes Reinecke if (r < 0) { 632a0cf7ea9SHannes Reinecke ti->error = "error attaching hardware handler"; 633ae11b1b3SHannes Reinecke dm_put_device(ti, p->path.dev); 634ae11b1b3SHannes Reinecke goto bad; 635ae11b1b3SHannes Reinecke } 6362bfd2e13SChandra Seetharaman 6372bfd2e13SChandra Seetharaman if (m->hw_handler_params) { 6382bfd2e13SChandra Seetharaman r = scsi_dh_set_params(q, m->hw_handler_params); 6392bfd2e13SChandra Seetharaman if (r < 0) { 6402bfd2e13SChandra Seetharaman ti->error = "unable to set hardware " 6412bfd2e13SChandra Seetharaman "handler parameters"; 6422bfd2e13SChandra Seetharaman dm_put_device(ti, p->path.dev); 6432bfd2e13SChandra Seetharaman goto bad; 6442bfd2e13SChandra Seetharaman } 6452bfd2e13SChandra Seetharaman } 646ae11b1b3SHannes Reinecke } 647ae11b1b3SHannes Reinecke 6481da177e4SLinus Torvalds r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); 6491da177e4SLinus Torvalds if (r) { 6501da177e4SLinus Torvalds dm_put_device(ti, p->path.dev); 6511da177e4SLinus Torvalds goto bad; 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds return p; 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds bad: 6571da177e4SLinus Torvalds free_pgpath(p); 65801460f35SBenjamin Marzinski return ERR_PTR(r); 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds 661498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as, 66228f16c20SMicha³ Miros³aw struct multipath *m) 6631da177e4SLinus Torvalds { 664498f0103SMike Snitzer static struct dm_arg _args[] = { 66572d94861SAlasdair G Kergon {1, 1024, "invalid number of paths"}, 66672d94861SAlasdair G Kergon {0, 1024, "invalid number of selector args"} 6671da177e4SLinus Torvalds }; 6681da177e4SLinus Torvalds 6691da177e4SLinus Torvalds int r; 670498f0103SMike Snitzer unsigned i, nr_selector_args, nr_args; 6711da177e4SLinus Torvalds struct priority_group *pg; 67228f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 6731da177e4SLinus Torvalds 6741da177e4SLinus Torvalds if (as->argc < 2) { 6751da177e4SLinus Torvalds as->argc = 0; 67601460f35SBenjamin Marzinski ti->error = "not enough priority group arguments"; 67701460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds pg = alloc_priority_group(); 6811da177e4SLinus Torvalds if (!pg) { 68272d94861SAlasdair G Kergon ti->error = "couldn't allocate priority group"; 68301460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 6841da177e4SLinus Torvalds } 6851da177e4SLinus Torvalds pg->m = m; 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds r = parse_path_selector(as, pg, ti); 6881da177e4SLinus Torvalds if (r) 6891da177e4SLinus Torvalds goto bad; 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds /* 6921da177e4SLinus Torvalds * read the paths 6931da177e4SLinus Torvalds */ 694498f0103SMike Snitzer r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error); 6951da177e4SLinus Torvalds if (r) 6961da177e4SLinus Torvalds goto bad; 6971da177e4SLinus Torvalds 698498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error); 6991da177e4SLinus Torvalds if (r) 7001da177e4SLinus Torvalds goto bad; 7011da177e4SLinus Torvalds 702498f0103SMike Snitzer nr_args = 1 + nr_selector_args; 7031da177e4SLinus Torvalds for (i = 0; i < pg->nr_pgpaths; i++) { 7041da177e4SLinus Torvalds struct pgpath *pgpath; 705498f0103SMike Snitzer struct dm_arg_set path_args; 7061da177e4SLinus Torvalds 707498f0103SMike Snitzer if (as->argc < nr_args) { 708148acff6SMikulas Patocka ti->error = "not enough path parameters"; 7096bbf79a1SAlasdair G Kergon r = -EINVAL; 7101da177e4SLinus Torvalds goto bad; 711148acff6SMikulas Patocka } 7121da177e4SLinus Torvalds 713498f0103SMike Snitzer path_args.argc = nr_args; 7141da177e4SLinus Torvalds path_args.argv = as->argv; 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds pgpath = parse_path(&path_args, &pg->ps, ti); 71701460f35SBenjamin Marzinski if (IS_ERR(pgpath)) { 71801460f35SBenjamin Marzinski r = PTR_ERR(pgpath); 7191da177e4SLinus Torvalds goto bad; 72001460f35SBenjamin Marzinski } 7211da177e4SLinus Torvalds 7221da177e4SLinus Torvalds pgpath->pg = pg; 7231da177e4SLinus Torvalds list_add_tail(&pgpath->list, &pg->pgpaths); 724498f0103SMike Snitzer dm_consume_args(as, nr_args); 7251da177e4SLinus Torvalds } 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds return pg; 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds bad: 7301da177e4SLinus Torvalds free_priority_group(pg, ti); 73101460f35SBenjamin Marzinski return ERR_PTR(r); 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds 734498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m) 7351da177e4SLinus Torvalds { 7361da177e4SLinus Torvalds unsigned hw_argc; 7372bfd2e13SChandra Seetharaman int ret; 73828f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 7391da177e4SLinus Torvalds 740498f0103SMike Snitzer static struct dm_arg _args[] = { 74172d94861SAlasdair G Kergon {0, 1024, "invalid number of hardware handler args"}, 7421da177e4SLinus Torvalds }; 7431da177e4SLinus Torvalds 744498f0103SMike Snitzer if (dm_read_arg_group(_args, as, &hw_argc, &ti->error)) 7451da177e4SLinus Torvalds return -EINVAL; 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds if (!hw_argc) 7481da177e4SLinus Torvalds return 0; 7491da177e4SLinus Torvalds 750498f0103SMike Snitzer m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL); 75114e98c5cSChandra Seetharaman 7522bfd2e13SChandra Seetharaman if (hw_argc > 1) { 7532bfd2e13SChandra Seetharaman char *p; 7542bfd2e13SChandra Seetharaman int i, j, len = 4; 7552bfd2e13SChandra Seetharaman 7562bfd2e13SChandra Seetharaman for (i = 0; i <= hw_argc - 2; i++) 7572bfd2e13SChandra Seetharaman len += strlen(as->argv[i]) + 1; 7582bfd2e13SChandra Seetharaman p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); 7592bfd2e13SChandra Seetharaman if (!p) { 7602bfd2e13SChandra Seetharaman ti->error = "memory allocation failed"; 7612bfd2e13SChandra Seetharaman ret = -ENOMEM; 7622bfd2e13SChandra Seetharaman goto fail; 7632bfd2e13SChandra Seetharaman } 7642bfd2e13SChandra Seetharaman j = sprintf(p, "%d", hw_argc - 1); 7652bfd2e13SChandra Seetharaman for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1) 7662bfd2e13SChandra Seetharaman j = sprintf(p, "%s", as->argv[i]); 7672bfd2e13SChandra Seetharaman } 768498f0103SMike Snitzer dm_consume_args(as, hw_argc - 1); 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds return 0; 7712bfd2e13SChandra Seetharaman fail: 7722bfd2e13SChandra Seetharaman kfree(m->hw_handler_name); 7732bfd2e13SChandra Seetharaman m->hw_handler_name = NULL; 7742bfd2e13SChandra Seetharaman return ret; 7751da177e4SLinus Torvalds } 7761da177e4SLinus Torvalds 777498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m) 7781da177e4SLinus Torvalds { 7791da177e4SLinus Torvalds int r; 7801da177e4SLinus Torvalds unsigned argc; 78128f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 782498f0103SMike Snitzer const char *arg_name; 7831da177e4SLinus Torvalds 784498f0103SMike Snitzer static struct dm_arg _args[] = { 785a58a935dSMike Snitzer {0, 6, "invalid number of feature args"}, 786c9e45581SDave Wysochanski {1, 50, "pg_init_retries must be between 1 and 50"}, 7874e2d19e4SChandra Seetharaman {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"}, 7881da177e4SLinus Torvalds }; 7891da177e4SLinus Torvalds 790498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &argc, &ti->error); 7911da177e4SLinus Torvalds if (r) 7921da177e4SLinus Torvalds return -EINVAL; 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds if (!argc) 7951da177e4SLinus Torvalds return 0; 7961da177e4SLinus Torvalds 797c9e45581SDave Wysochanski do { 798498f0103SMike Snitzer arg_name = dm_shift_arg(as); 799c9e45581SDave Wysochanski argc--; 800c9e45581SDave Wysochanski 801498f0103SMike Snitzer if (!strcasecmp(arg_name, "queue_if_no_path")) { 802c9e45581SDave Wysochanski r = queue_if_no_path(m, 1, 0); 803c9e45581SDave Wysochanski continue; 8041da177e4SLinus Torvalds } 805c9e45581SDave Wysochanski 806a58a935dSMike Snitzer if (!strcasecmp(arg_name, "retain_attached_hw_handler")) { 807a58a935dSMike Snitzer m->retain_attached_hw_handler = 1; 808a58a935dSMike Snitzer continue; 809a58a935dSMike Snitzer } 810a58a935dSMike Snitzer 811498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_retries") && 812c9e45581SDave Wysochanski (argc >= 1)) { 813498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error); 814c9e45581SDave Wysochanski argc--; 815c9e45581SDave Wysochanski continue; 816c9e45581SDave Wysochanski } 817c9e45581SDave Wysochanski 818498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_delay_msecs") && 8194e2d19e4SChandra Seetharaman (argc >= 1)) { 820498f0103SMike Snitzer r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error); 8214e2d19e4SChandra Seetharaman argc--; 8224e2d19e4SChandra Seetharaman continue; 8234e2d19e4SChandra Seetharaman } 8244e2d19e4SChandra Seetharaman 825c9e45581SDave Wysochanski ti->error = "Unrecognised multipath feature request"; 826c9e45581SDave Wysochanski r = -EINVAL; 827c9e45581SDave Wysochanski } while (argc && !r); 828c9e45581SDave Wysochanski 829c9e45581SDave Wysochanski return r; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8321da177e4SLinus Torvalds static int multipath_ctr(struct dm_target *ti, unsigned int argc, 8331da177e4SLinus Torvalds char **argv) 8341da177e4SLinus Torvalds { 835498f0103SMike Snitzer /* target arguments */ 836498f0103SMike Snitzer static struct dm_arg _args[] = { 837a490a07aSMike Snitzer {0, 1024, "invalid number of priority groups"}, 838a490a07aSMike Snitzer {0, 1024, "invalid initial priority group number"}, 8391da177e4SLinus Torvalds }; 8401da177e4SLinus Torvalds 8411da177e4SLinus Torvalds int r; 8421da177e4SLinus Torvalds struct multipath *m; 843498f0103SMike Snitzer struct dm_arg_set as; 8441da177e4SLinus Torvalds unsigned pg_count = 0; 8451da177e4SLinus Torvalds unsigned next_pg_num; 846*8637a6bfSMike Snitzer bool use_blk_mq = dm_use_blk_mq(dm_table_get_md(ti->table)); 8471da177e4SLinus Torvalds 8481da177e4SLinus Torvalds as.argc = argc; 8491da177e4SLinus Torvalds as.argv = argv; 8501da177e4SLinus Torvalds 851*8637a6bfSMike Snitzer m = alloc_multipath(ti, use_blk_mq); 8521da177e4SLinus Torvalds if (!m) { 85372d94861SAlasdair G Kergon ti->error = "can't allocate multipath"; 8541da177e4SLinus Torvalds return -EINVAL; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds 85728f16c20SMicha³ Miros³aw r = parse_features(&as, m); 8581da177e4SLinus Torvalds if (r) 8591da177e4SLinus Torvalds goto bad; 8601da177e4SLinus Torvalds 86128f16c20SMicha³ Miros³aw r = parse_hw_handler(&as, m); 8621da177e4SLinus Torvalds if (r) 8631da177e4SLinus Torvalds goto bad; 8641da177e4SLinus Torvalds 865498f0103SMike Snitzer r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error); 8661da177e4SLinus Torvalds if (r) 8671da177e4SLinus Torvalds goto bad; 8681da177e4SLinus Torvalds 869498f0103SMike Snitzer r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error); 8701da177e4SLinus Torvalds if (r) 8711da177e4SLinus Torvalds goto bad; 8721da177e4SLinus Torvalds 873a490a07aSMike Snitzer if ((!m->nr_priority_groups && next_pg_num) || 874a490a07aSMike Snitzer (m->nr_priority_groups && !next_pg_num)) { 875a490a07aSMike Snitzer ti->error = "invalid initial priority group"; 876a490a07aSMike Snitzer r = -EINVAL; 877a490a07aSMike Snitzer goto bad; 878a490a07aSMike Snitzer } 879a490a07aSMike Snitzer 8801da177e4SLinus Torvalds /* parse the priority groups */ 8811da177e4SLinus Torvalds while (as.argc) { 8821da177e4SLinus Torvalds struct priority_group *pg; 8831da177e4SLinus Torvalds 88428f16c20SMicha³ Miros³aw pg = parse_priority_group(&as, m); 88501460f35SBenjamin Marzinski if (IS_ERR(pg)) { 88601460f35SBenjamin Marzinski r = PTR_ERR(pg); 8871da177e4SLinus Torvalds goto bad; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds 8901da177e4SLinus Torvalds m->nr_valid_paths += pg->nr_pgpaths; 8911da177e4SLinus Torvalds list_add_tail(&pg->list, &m->priority_groups); 8921da177e4SLinus Torvalds pg_count++; 8931da177e4SLinus Torvalds pg->pg_num = pg_count; 8941da177e4SLinus Torvalds if (!--next_pg_num) 8951da177e4SLinus Torvalds m->next_pg = pg; 8961da177e4SLinus Torvalds } 8971da177e4SLinus Torvalds 8981da177e4SLinus Torvalds if (pg_count != m->nr_priority_groups) { 89972d94861SAlasdair G Kergon ti->error = "priority group count mismatch"; 9001da177e4SLinus Torvalds r = -EINVAL; 9011da177e4SLinus Torvalds goto bad; 9021da177e4SLinus Torvalds } 9031da177e4SLinus Torvalds 90455a62eefSAlasdair G Kergon ti->num_flush_bios = 1; 90555a62eefSAlasdair G Kergon ti->num_discard_bios = 1; 906042bcef8SMike Snitzer ti->num_write_same_bios = 1; 907*8637a6bfSMike Snitzer if (use_blk_mq) 908*8637a6bfSMike Snitzer ti->per_io_data_size = sizeof(struct dm_mpath_io); 9098627921fSMikulas Patocka 9101da177e4SLinus Torvalds return 0; 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds bad: 9131da177e4SLinus Torvalds free_multipath(m); 9141da177e4SLinus Torvalds return r; 9151da177e4SLinus Torvalds } 9161da177e4SLinus Torvalds 9172bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m) 9182bded7bdSKiyoshi Ueda { 9192bded7bdSKiyoshi Ueda DECLARE_WAITQUEUE(wait, current); 9202bded7bdSKiyoshi Ueda unsigned long flags; 9212bded7bdSKiyoshi Ueda 9222bded7bdSKiyoshi Ueda add_wait_queue(&m->pg_init_wait, &wait); 9232bded7bdSKiyoshi Ueda 9242bded7bdSKiyoshi Ueda while (1) { 9252bded7bdSKiyoshi Ueda set_current_state(TASK_UNINTERRUPTIBLE); 9262bded7bdSKiyoshi Ueda 9272bded7bdSKiyoshi Ueda spin_lock_irqsave(&m->lock, flags); 9282bded7bdSKiyoshi Ueda if (!m->pg_init_in_progress) { 9292bded7bdSKiyoshi Ueda spin_unlock_irqrestore(&m->lock, flags); 9302bded7bdSKiyoshi Ueda break; 9312bded7bdSKiyoshi Ueda } 9322bded7bdSKiyoshi Ueda spin_unlock_irqrestore(&m->lock, flags); 9332bded7bdSKiyoshi Ueda 9342bded7bdSKiyoshi Ueda io_schedule(); 9352bded7bdSKiyoshi Ueda } 9362bded7bdSKiyoshi Ueda set_current_state(TASK_RUNNING); 9372bded7bdSKiyoshi Ueda 9382bded7bdSKiyoshi Ueda remove_wait_queue(&m->pg_init_wait, &wait); 9392bded7bdSKiyoshi Ueda } 9402bded7bdSKiyoshi Ueda 9412bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m) 9421da177e4SLinus Torvalds { 943954a73d5SShiva Krishna Merla unsigned long flags; 944954a73d5SShiva Krishna Merla 945954a73d5SShiva Krishna Merla spin_lock_irqsave(&m->lock, flags); 946954a73d5SShiva Krishna Merla m->pg_init_disabled = 1; 947954a73d5SShiva Krishna Merla spin_unlock_irqrestore(&m->lock, flags); 948954a73d5SShiva Krishna Merla 949bab7cfc7SChandra Seetharaman flush_workqueue(kmpath_handlerd); 9502bded7bdSKiyoshi Ueda multipath_wait_for_pg_init_completion(m); 951a044d016SAlasdair G Kergon flush_workqueue(kmultipathd); 95243829731STejun Heo flush_work(&m->trigger_event); 953954a73d5SShiva Krishna Merla 954954a73d5SShiva Krishna Merla spin_lock_irqsave(&m->lock, flags); 955954a73d5SShiva Krishna Merla m->pg_init_disabled = 0; 956954a73d5SShiva Krishna Merla spin_unlock_irqrestore(&m->lock, flags); 9576df400abSKiyoshi Ueda } 9586df400abSKiyoshi Ueda 9596df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti) 9606df400abSKiyoshi Ueda { 9616df400abSKiyoshi Ueda struct multipath *m = ti->private; 9626df400abSKiyoshi Ueda 9632bded7bdSKiyoshi Ueda flush_multipath_work(m); 9641da177e4SLinus Torvalds free_multipath(m); 9651da177e4SLinus Torvalds } 9661da177e4SLinus Torvalds 9671da177e4SLinus Torvalds /* 9681da177e4SLinus Torvalds * Take a path out of use. 9691da177e4SLinus Torvalds */ 9701da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath) 9711da177e4SLinus Torvalds { 9721da177e4SLinus Torvalds unsigned long flags; 9731da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 9761da177e4SLinus Torvalds 9776680073dSKiyoshi Ueda if (!pgpath->is_active) 9781da177e4SLinus Torvalds goto out; 9791da177e4SLinus Torvalds 98072d94861SAlasdair G Kergon DMWARN("Failing path %s.", pgpath->path.dev->name); 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); 9836680073dSKiyoshi Ueda pgpath->is_active = 0; 9841da177e4SLinus Torvalds pgpath->fail_count++; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds m->nr_valid_paths--; 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds if (pgpath == m->current_pgpath) 9891da177e4SLinus Torvalds m->current_pgpath = NULL; 9901da177e4SLinus Torvalds 991b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti, 992b15546f9SMike Anderson pgpath->path.dev->name, m->nr_valid_paths); 993b15546f9SMike Anderson 994fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 9951da177e4SLinus Torvalds 9961da177e4SLinus Torvalds out: 9971da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 9981da177e4SLinus Torvalds 9991da177e4SLinus Torvalds return 0; 10001da177e4SLinus Torvalds } 10011da177e4SLinus Torvalds 10021da177e4SLinus Torvalds /* 10031da177e4SLinus Torvalds * Reinstate a previously-failed path 10041da177e4SLinus Torvalds */ 10051da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath) 10061da177e4SLinus Torvalds { 100763d832c3SHannes Reinecke int r = 0, run_queue = 0; 10081da177e4SLinus Torvalds unsigned long flags; 10091da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 10101da177e4SLinus Torvalds 10111da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 10121da177e4SLinus Torvalds 10136680073dSKiyoshi Ueda if (pgpath->is_active) 10141da177e4SLinus Torvalds goto out; 10151da177e4SLinus Torvalds 1016def052d2SAlasdair G Kergon if (!pgpath->pg->ps.type->reinstate_path) { 10171da177e4SLinus Torvalds DMWARN("Reinstate path not supported by path selector %s", 10181da177e4SLinus Torvalds pgpath->pg->ps.type->name); 10191da177e4SLinus Torvalds r = -EINVAL; 10201da177e4SLinus Torvalds goto out; 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); 10241da177e4SLinus Torvalds if (r) 10251da177e4SLinus Torvalds goto out; 10261da177e4SLinus Torvalds 10276680073dSKiyoshi Ueda pgpath->is_active = 1; 10281da177e4SLinus Torvalds 1029e8099177SHannes Reinecke if (!m->nr_valid_paths++) { 10301da177e4SLinus Torvalds m->current_pgpath = NULL; 103163d832c3SHannes Reinecke run_queue = 1; 1032e54f77ddSChandra Seetharaman } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) { 10334e2d19e4SChandra Seetharaman if (queue_work(kmpath_handlerd, &pgpath->activate_path.work)) 1034e54f77ddSChandra Seetharaman m->pg_init_in_progress++; 1035e54f77ddSChandra Seetharaman } 10361da177e4SLinus Torvalds 1037b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti, 1038b15546f9SMike Anderson pgpath->path.dev->name, m->nr_valid_paths); 1039b15546f9SMike Anderson 1040fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 10411da177e4SLinus Torvalds 10421da177e4SLinus Torvalds out: 10431da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 104463d832c3SHannes Reinecke if (run_queue) 104563d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds return r; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds /* 10511da177e4SLinus Torvalds * Fail or reinstate all paths that match the provided struct dm_dev. 10521da177e4SLinus Torvalds */ 10531da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev, 10541da177e4SLinus Torvalds action_fn action) 10551da177e4SLinus Torvalds { 105619040c0bSMike Snitzer int r = -EINVAL; 10571da177e4SLinus Torvalds struct pgpath *pgpath; 10581da177e4SLinus Torvalds struct priority_group *pg; 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 10611da177e4SLinus Torvalds list_for_each_entry(pgpath, &pg->pgpaths, list) { 10621da177e4SLinus Torvalds if (pgpath->path.dev == dev) 10631da177e4SLinus Torvalds r = action(pgpath); 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds } 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds return r; 10681da177e4SLinus Torvalds } 10691da177e4SLinus Torvalds 10701da177e4SLinus Torvalds /* 10711da177e4SLinus Torvalds * Temporarily try to avoid having to use the specified PG 10721da177e4SLinus Torvalds */ 10731da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg, 10741da177e4SLinus Torvalds int bypassed) 10751da177e4SLinus Torvalds { 10761da177e4SLinus Torvalds unsigned long flags; 10771da177e4SLinus Torvalds 10781da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 10791da177e4SLinus Torvalds 10801da177e4SLinus Torvalds pg->bypassed = bypassed; 10811da177e4SLinus Torvalds m->current_pgpath = NULL; 10821da177e4SLinus Torvalds m->current_pg = NULL; 10831da177e4SLinus Torvalds 10841da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 10851da177e4SLinus Torvalds 1086fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 10871da177e4SLinus Torvalds } 10881da177e4SLinus Torvalds 10891da177e4SLinus Torvalds /* 10901da177e4SLinus Torvalds * Switch to using the specified PG from the next I/O that gets mapped 10911da177e4SLinus Torvalds */ 10921da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr) 10931da177e4SLinus Torvalds { 10941da177e4SLinus Torvalds struct priority_group *pg; 10951da177e4SLinus Torvalds unsigned pgnum; 10961da177e4SLinus Torvalds unsigned long flags; 109731998ef1SMikulas Patocka char dummy; 10981da177e4SLinus Torvalds 109931998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 11001da177e4SLinus Torvalds (pgnum > m->nr_priority_groups)) { 11011da177e4SLinus Torvalds DMWARN("invalid PG number supplied to switch_pg_num"); 11021da177e4SLinus Torvalds return -EINVAL; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 11061da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 11071da177e4SLinus Torvalds pg->bypassed = 0; 11081da177e4SLinus Torvalds if (--pgnum) 11091da177e4SLinus Torvalds continue; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds m->current_pgpath = NULL; 11121da177e4SLinus Torvalds m->current_pg = NULL; 11131da177e4SLinus Torvalds m->next_pg = pg; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 11161da177e4SLinus Torvalds 1117fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 11181da177e4SLinus Torvalds return 0; 11191da177e4SLinus Torvalds } 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds /* 11221da177e4SLinus Torvalds * Set/clear bypassed status of a PG. 11231da177e4SLinus Torvalds * PGs are numbered upwards from 1 in the order they were declared. 11241da177e4SLinus Torvalds */ 11251da177e4SLinus Torvalds static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed) 11261da177e4SLinus Torvalds { 11271da177e4SLinus Torvalds struct priority_group *pg; 11281da177e4SLinus Torvalds unsigned pgnum; 112931998ef1SMikulas Patocka char dummy; 11301da177e4SLinus Torvalds 113131998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 11321da177e4SLinus Torvalds (pgnum > m->nr_priority_groups)) { 11331da177e4SLinus Torvalds DMWARN("invalid PG number supplied to bypass_pg"); 11341da177e4SLinus Torvalds return -EINVAL; 11351da177e4SLinus Torvalds } 11361da177e4SLinus Torvalds 11371da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 11381da177e4SLinus Torvalds if (!--pgnum) 11391da177e4SLinus Torvalds break; 11401da177e4SLinus Torvalds } 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds bypass_pg(m, pg, bypassed); 11431da177e4SLinus Torvalds return 0; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 11461da177e4SLinus Torvalds /* 1147c9e45581SDave Wysochanski * Should we retry pg_init immediately? 1148c9e45581SDave Wysochanski */ 1149c9e45581SDave Wysochanski static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath) 1150c9e45581SDave Wysochanski { 1151c9e45581SDave Wysochanski unsigned long flags; 1152c9e45581SDave Wysochanski int limit_reached = 0; 1153c9e45581SDave Wysochanski 1154c9e45581SDave Wysochanski spin_lock_irqsave(&m->lock, flags); 1155c9e45581SDave Wysochanski 1156954a73d5SShiva Krishna Merla if (m->pg_init_count <= m->pg_init_retries && !m->pg_init_disabled) 1157c9e45581SDave Wysochanski m->pg_init_required = 1; 1158c9e45581SDave Wysochanski else 1159c9e45581SDave Wysochanski limit_reached = 1; 1160c9e45581SDave Wysochanski 1161c9e45581SDave Wysochanski spin_unlock_irqrestore(&m->lock, flags); 1162c9e45581SDave Wysochanski 1163c9e45581SDave Wysochanski return limit_reached; 1164c9e45581SDave Wysochanski } 1165c9e45581SDave Wysochanski 11663ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors) 1167cfae5c9bSChandra Seetharaman { 116883c0d5d5SMoger, Babu struct pgpath *pgpath = data; 1169cfae5c9bSChandra Seetharaman struct priority_group *pg = pgpath->pg; 1170cfae5c9bSChandra Seetharaman struct multipath *m = pg->m; 1171cfae5c9bSChandra Seetharaman unsigned long flags; 11724e2d19e4SChandra Seetharaman unsigned delay_retry = 0; 1173cfae5c9bSChandra Seetharaman 1174cfae5c9bSChandra Seetharaman /* device or driver problems */ 1175cfae5c9bSChandra Seetharaman switch (errors) { 1176cfae5c9bSChandra Seetharaman case SCSI_DH_OK: 1177cfae5c9bSChandra Seetharaman break; 1178cfae5c9bSChandra Seetharaman case SCSI_DH_NOSYS: 1179cfae5c9bSChandra Seetharaman if (!m->hw_handler_name) { 1180cfae5c9bSChandra Seetharaman errors = 0; 1181cfae5c9bSChandra Seetharaman break; 1182cfae5c9bSChandra Seetharaman } 1183f7b934c8SMoger, Babu DMERR("Could not failover the device: Handler scsi_dh_%s " 1184f7b934c8SMoger, Babu "Error %d.", m->hw_handler_name, errors); 1185cfae5c9bSChandra Seetharaman /* 1186cfae5c9bSChandra Seetharaman * Fail path for now, so we do not ping pong 1187cfae5c9bSChandra Seetharaman */ 1188cfae5c9bSChandra Seetharaman fail_path(pgpath); 1189cfae5c9bSChandra Seetharaman break; 1190cfae5c9bSChandra Seetharaman case SCSI_DH_DEV_TEMP_BUSY: 1191cfae5c9bSChandra Seetharaman /* 1192cfae5c9bSChandra Seetharaman * Probably doing something like FW upgrade on the 1193cfae5c9bSChandra Seetharaman * controller so try the other pg. 1194cfae5c9bSChandra Seetharaman */ 1195cfae5c9bSChandra Seetharaman bypass_pg(m, pg, 1); 1196cfae5c9bSChandra Seetharaman break; 1197cfae5c9bSChandra Seetharaman case SCSI_DH_RETRY: 11984e2d19e4SChandra Seetharaman /* Wait before retrying. */ 11994e2d19e4SChandra Seetharaman delay_retry = 1; 1200cfae5c9bSChandra Seetharaman case SCSI_DH_IMM_RETRY: 1201cfae5c9bSChandra Seetharaman case SCSI_DH_RES_TEMP_UNAVAIL: 1202cfae5c9bSChandra Seetharaman if (pg_init_limit_reached(m, pgpath)) 1203cfae5c9bSChandra Seetharaman fail_path(pgpath); 1204cfae5c9bSChandra Seetharaman errors = 0; 1205cfae5c9bSChandra Seetharaman break; 1206cfae5c9bSChandra Seetharaman default: 1207cfae5c9bSChandra Seetharaman /* 1208cfae5c9bSChandra Seetharaman * We probably do not want to fail the path for a device 1209cfae5c9bSChandra Seetharaman * error, but this is what the old dm did. In future 1210cfae5c9bSChandra Seetharaman * patches we can do more advanced handling. 1211cfae5c9bSChandra Seetharaman */ 1212cfae5c9bSChandra Seetharaman fail_path(pgpath); 1213cfae5c9bSChandra Seetharaman } 1214cfae5c9bSChandra Seetharaman 1215cfae5c9bSChandra Seetharaman spin_lock_irqsave(&m->lock, flags); 1216cfae5c9bSChandra Seetharaman if (errors) { 1217e54f77ddSChandra Seetharaman if (pgpath == m->current_pgpath) { 1218cfae5c9bSChandra Seetharaman DMERR("Could not failover device. Error %d.", errors); 1219cfae5c9bSChandra Seetharaman m->current_pgpath = NULL; 1220cfae5c9bSChandra Seetharaman m->current_pg = NULL; 1221e54f77ddSChandra Seetharaman } 1222d0259bf0SKiyoshi Ueda } else if (!m->pg_init_required) 1223cfae5c9bSChandra Seetharaman pg->bypassed = 0; 1224cfae5c9bSChandra Seetharaman 1225d0259bf0SKiyoshi Ueda if (--m->pg_init_in_progress) 1226d0259bf0SKiyoshi Ueda /* Activations of other paths are still on going */ 1227d0259bf0SKiyoshi Ueda goto out; 1228d0259bf0SKiyoshi Ueda 12293e9f1be1SHannes Reinecke if (m->pg_init_required) { 12304e2d19e4SChandra Seetharaman m->pg_init_delay_retry = delay_retry; 12313e9f1be1SHannes Reinecke if (__pg_init_all_paths(m)) 12323e9f1be1SHannes Reinecke goto out; 12333e9f1be1SHannes Reinecke } 12343e9f1be1SHannes Reinecke m->queue_io = 0; 1235d0259bf0SKiyoshi Ueda 12362bded7bdSKiyoshi Ueda /* 12372bded7bdSKiyoshi Ueda * Wake up any thread waiting to suspend. 12382bded7bdSKiyoshi Ueda */ 12392bded7bdSKiyoshi Ueda wake_up(&m->pg_init_wait); 12402bded7bdSKiyoshi Ueda 1241d0259bf0SKiyoshi Ueda out: 1242cfae5c9bSChandra Seetharaman spin_unlock_irqrestore(&m->lock, flags); 1243cfae5c9bSChandra Seetharaman } 1244cfae5c9bSChandra Seetharaman 1245bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work) 1246bab7cfc7SChandra Seetharaman { 1247e54f77ddSChandra Seetharaman struct pgpath *pgpath = 12484e2d19e4SChandra Seetharaman container_of(work, struct pgpath, activate_path.work); 1249bab7cfc7SChandra Seetharaman 12503a017509SHannes Reinecke if (pgpath->is_active) 12513ae31f6aSChandra Seetharaman scsi_dh_activate(bdev_get_queue(pgpath->path.dev->bdev), 125283c0d5d5SMoger, Babu pg_init_done, pgpath); 12533a017509SHannes Reinecke else 12543a017509SHannes Reinecke pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED); 1255bab7cfc7SChandra Seetharaman } 1256bab7cfc7SChandra Seetharaman 12577e782af5SHannes Reinecke static int noretry_error(int error) 12587e782af5SHannes Reinecke { 12597e782af5SHannes Reinecke switch (error) { 12607e782af5SHannes Reinecke case -EOPNOTSUPP: 12617e782af5SHannes Reinecke case -EREMOTEIO: 12627e782af5SHannes Reinecke case -EILSEQ: 12637e782af5SHannes Reinecke case -ENODATA: 1264cc9d3c38SJun'ichi Nomura case -ENOSPC: 12657e782af5SHannes Reinecke return 1; 12667e782af5SHannes Reinecke } 12677e782af5SHannes Reinecke 12687e782af5SHannes Reinecke /* Anything else could be a path failure, so should be retried */ 12697e782af5SHannes Reinecke return 0; 12707e782af5SHannes Reinecke } 12717e782af5SHannes Reinecke 12721da177e4SLinus Torvalds /* 12731da177e4SLinus Torvalds * end_io handling 12741da177e4SLinus Torvalds */ 1275f40c67f0SKiyoshi Ueda static int do_end_io(struct multipath *m, struct request *clone, 1276028867acSAlasdair G Kergon int error, struct dm_mpath_io *mpio) 12771da177e4SLinus Torvalds { 1278f40c67f0SKiyoshi Ueda /* 1279f40c67f0SKiyoshi Ueda * We don't queue any clone request inside the multipath target 1280f40c67f0SKiyoshi Ueda * during end I/O handling, since those clone requests don't have 1281f40c67f0SKiyoshi Ueda * bio clones. If we queue them inside the multipath target, 1282f40c67f0SKiyoshi Ueda * we need to make bio clones, that requires memory allocation. 1283f40c67f0SKiyoshi Ueda * (See drivers/md/dm.c:end_clone_bio() about why the clone requests 1284f40c67f0SKiyoshi Ueda * don't have bio clones.) 1285f40c67f0SKiyoshi Ueda * Instead of queueing the clone request here, we queue the original 1286f40c67f0SKiyoshi Ueda * request into dm core, which will remake a clone request and 1287f40c67f0SKiyoshi Ueda * clone bios for it and resubmit it later. 1288f40c67f0SKiyoshi Ueda */ 1289f40c67f0SKiyoshi Ueda int r = DM_ENDIO_REQUEUE; 1290640eb3b0SStefan Bader unsigned long flags; 12911da177e4SLinus Torvalds 1292f40c67f0SKiyoshi Ueda if (!error && !clone->errors) 12931da177e4SLinus Torvalds return 0; /* I/O complete */ 12941da177e4SLinus Torvalds 12957eee4ae2SMike Snitzer if (noretry_error(error)) 1296959eb4e5SMike Snitzer return error; 1297959eb4e5SMike Snitzer 1298cfae5c9bSChandra Seetharaman if (mpio->pgpath) 12991da177e4SLinus Torvalds fail_path(mpio->pgpath); 13001da177e4SLinus Torvalds 1301640eb3b0SStefan Bader spin_lock_irqsave(&m->lock, flags); 1302751b2a7dSHannes Reinecke if (!m->nr_valid_paths) { 1303751b2a7dSHannes Reinecke if (!m->queue_if_no_path) { 1304751b2a7dSHannes Reinecke if (!__must_push_back(m)) 1305f40c67f0SKiyoshi Ueda r = -EIO; 1306751b2a7dSHannes Reinecke } else { 1307751b2a7dSHannes Reinecke if (error == -EBADE) 1308751b2a7dSHannes Reinecke r = error; 1309751b2a7dSHannes Reinecke } 1310751b2a7dSHannes Reinecke } 1311640eb3b0SStefan Bader spin_unlock_irqrestore(&m->lock, flags); 13121da177e4SLinus Torvalds 1313f40c67f0SKiyoshi Ueda return r; 13141da177e4SLinus Torvalds } 13151da177e4SLinus Torvalds 1316f40c67f0SKiyoshi Ueda static int multipath_end_io(struct dm_target *ti, struct request *clone, 13171da177e4SLinus Torvalds int error, union map_info *map_context) 13181da177e4SLinus Torvalds { 1319028867acSAlasdair G Kergon struct multipath *m = ti->private; 1320028867acSAlasdair G Kergon struct dm_mpath_io *mpio = map_context->ptr; 1321a71a261fSWei Yongjun struct pgpath *pgpath; 13221da177e4SLinus Torvalds struct path_selector *ps; 13231da177e4SLinus Torvalds int r; 13241da177e4SLinus Torvalds 1325466891f9SJun'ichi Nomura BUG_ON(!mpio); 1326466891f9SJun'ichi Nomura 1327f40c67f0SKiyoshi Ueda r = do_end_io(m, clone, error, mpio); 1328a71a261fSWei Yongjun pgpath = mpio->pgpath; 13291da177e4SLinus Torvalds if (pgpath) { 13301da177e4SLinus Torvalds ps = &pgpath->pg->ps; 13311da177e4SLinus Torvalds if (ps->type->end_io) 133202ab823fSKiyoshi Ueda ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); 13331da177e4SLinus Torvalds } 1334466891f9SJun'ichi Nomura clear_mapinfo(m, map_context); 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds return r; 13371da177e4SLinus Torvalds } 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds /* 13401da177e4SLinus Torvalds * Suspend can't complete until all the I/O is processed so if 1341436d4108SAlasdair G Kergon * the last path fails we must error any remaining I/O. 1342436d4108SAlasdair G Kergon * Note that if the freeze_bdev fails while suspending, the 1343436d4108SAlasdair G Kergon * queue_if_no_path state is lost - userspace should reset it. 13441da177e4SLinus Torvalds */ 13451da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti) 13461da177e4SLinus Torvalds { 13471da177e4SLinus Torvalds struct multipath *m = (struct multipath *) ti->private; 13481da177e4SLinus Torvalds 1349485ef69eSAlasdair G Kergon queue_if_no_path(m, 0, 1); 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds 13526df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti) 13536df400abSKiyoshi Ueda { 13546380f26fSMike Anderson struct multipath *m = ti->private; 13556380f26fSMike Anderson 13566380f26fSMike Anderson mutex_lock(&m->work_mutex); 13572bded7bdSKiyoshi Ueda flush_multipath_work(m); 13586380f26fSMike Anderson mutex_unlock(&m->work_mutex); 13596df400abSKiyoshi Ueda } 13606df400abSKiyoshi Ueda 1361436d4108SAlasdair G Kergon /* 1362436d4108SAlasdair G Kergon * Restore the queue_if_no_path setting. 1363436d4108SAlasdair G Kergon */ 13641da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti) 13651da177e4SLinus Torvalds { 13661da177e4SLinus Torvalds struct multipath *m = (struct multipath *) ti->private; 13671da177e4SLinus Torvalds unsigned long flags; 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 1370436d4108SAlasdair G Kergon m->queue_if_no_path = m->saved_queue_if_no_path; 13711da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13721da177e4SLinus Torvalds } 13731da177e4SLinus Torvalds 13741da177e4SLinus Torvalds /* 13751da177e4SLinus Torvalds * Info output has the following format: 13761da177e4SLinus Torvalds * num_multipath_feature_args [multipath_feature_args]* 13771da177e4SLinus Torvalds * num_handler_status_args [handler_status_args]* 13781da177e4SLinus Torvalds * num_groups init_group_number 13791da177e4SLinus Torvalds * [A|D|E num_ps_status_args [ps_status_args]* 13801da177e4SLinus Torvalds * num_paths num_selector_args 13811da177e4SLinus Torvalds * [path_dev A|F fail_count [selector_args]* ]+ ]+ 13821da177e4SLinus Torvalds * 13831da177e4SLinus Torvalds * Table output has the following format (identical to the constructor string): 13841da177e4SLinus Torvalds * num_feature_args [features_args]* 13851da177e4SLinus Torvalds * num_handler_args hw_handler [hw_handler_args]* 13861da177e4SLinus Torvalds * num_groups init_group_number 13871da177e4SLinus Torvalds * [priority selector-name num_ps_args [ps_args]* 13881da177e4SLinus Torvalds * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ 13891da177e4SLinus Torvalds */ 1390fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type, 13911f4e0ff0SAlasdair G Kergon unsigned status_flags, char *result, unsigned maxlen) 13921da177e4SLinus Torvalds { 13931da177e4SLinus Torvalds int sz = 0; 13941da177e4SLinus Torvalds unsigned long flags; 13951da177e4SLinus Torvalds struct multipath *m = (struct multipath *) ti->private; 13961da177e4SLinus Torvalds struct priority_group *pg; 13971da177e4SLinus Torvalds struct pgpath *p; 13981da177e4SLinus Torvalds unsigned pg_num; 13991da177e4SLinus Torvalds char state; 14001da177e4SLinus Torvalds 14011da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds /* Features */ 14041da177e4SLinus Torvalds if (type == STATUSTYPE_INFO) 1405e8099177SHannes Reinecke DMEMIT("2 %u %u ", m->queue_io, m->pg_init_count); 1406c9e45581SDave Wysochanski else { 1407c9e45581SDave Wysochanski DMEMIT("%u ", m->queue_if_no_path + 14084e2d19e4SChandra Seetharaman (m->pg_init_retries > 0) * 2 + 1409a58a935dSMike Snitzer (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 + 1410a58a935dSMike Snitzer m->retain_attached_hw_handler); 1411c9e45581SDave Wysochanski if (m->queue_if_no_path) 1412c9e45581SDave Wysochanski DMEMIT("queue_if_no_path "); 1413c9e45581SDave Wysochanski if (m->pg_init_retries) 1414c9e45581SDave Wysochanski DMEMIT("pg_init_retries %u ", m->pg_init_retries); 14154e2d19e4SChandra Seetharaman if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) 14164e2d19e4SChandra Seetharaman DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs); 1417a58a935dSMike Snitzer if (m->retain_attached_hw_handler) 1418a58a935dSMike Snitzer DMEMIT("retain_attached_hw_handler "); 1419c9e45581SDave Wysochanski } 14201da177e4SLinus Torvalds 1421cfae5c9bSChandra Seetharaman if (!m->hw_handler_name || type == STATUSTYPE_INFO) 14221da177e4SLinus Torvalds DMEMIT("0 "); 14231da177e4SLinus Torvalds else 1424cfae5c9bSChandra Seetharaman DMEMIT("1 %s ", m->hw_handler_name); 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds DMEMIT("%u ", m->nr_priority_groups); 14271da177e4SLinus Torvalds 14281da177e4SLinus Torvalds if (m->next_pg) 14291da177e4SLinus Torvalds pg_num = m->next_pg->pg_num; 14301da177e4SLinus Torvalds else if (m->current_pg) 14311da177e4SLinus Torvalds pg_num = m->current_pg->pg_num; 14321da177e4SLinus Torvalds else 1433a490a07aSMike Snitzer pg_num = (m->nr_priority_groups ? 1 : 0); 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds DMEMIT("%u ", pg_num); 14361da177e4SLinus Torvalds 14371da177e4SLinus Torvalds switch (type) { 14381da177e4SLinus Torvalds case STATUSTYPE_INFO: 14391da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 14401da177e4SLinus Torvalds if (pg->bypassed) 14411da177e4SLinus Torvalds state = 'D'; /* Disabled */ 14421da177e4SLinus Torvalds else if (pg == m->current_pg) 14431da177e4SLinus Torvalds state = 'A'; /* Currently Active */ 14441da177e4SLinus Torvalds else 14451da177e4SLinus Torvalds state = 'E'; /* Enabled */ 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds DMEMIT("%c ", state); 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds if (pg->ps.type->status) 14501da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 14511da177e4SLinus Torvalds result + sz, 14521da177e4SLinus Torvalds maxlen - sz); 14531da177e4SLinus Torvalds else 14541da177e4SLinus Torvalds DMEMIT("0 "); 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 14571da177e4SLinus Torvalds pg->ps.type->info_args); 14581da177e4SLinus Torvalds 14591da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 14601da177e4SLinus Torvalds DMEMIT("%s %s %u ", p->path.dev->name, 14616680073dSKiyoshi Ueda p->is_active ? "A" : "F", 14621da177e4SLinus Torvalds p->fail_count); 14631da177e4SLinus Torvalds if (pg->ps.type->status) 14641da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 14651da177e4SLinus Torvalds &p->path, type, result + sz, 14661da177e4SLinus Torvalds maxlen - sz); 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds } 14691da177e4SLinus Torvalds break; 14701da177e4SLinus Torvalds 14711da177e4SLinus Torvalds case STATUSTYPE_TABLE: 14721da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 14731da177e4SLinus Torvalds DMEMIT("%s ", pg->ps.type->name); 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds if (pg->ps.type->status) 14761da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 14771da177e4SLinus Torvalds result + sz, 14781da177e4SLinus Torvalds maxlen - sz); 14791da177e4SLinus Torvalds else 14801da177e4SLinus Torvalds DMEMIT("0 "); 14811da177e4SLinus Torvalds 14821da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 14831da177e4SLinus Torvalds pg->ps.type->table_args); 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 14861da177e4SLinus Torvalds DMEMIT("%s ", p->path.dev->name); 14871da177e4SLinus Torvalds if (pg->ps.type->status) 14881da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 14891da177e4SLinus Torvalds &p->path, type, result + sz, 14901da177e4SLinus Torvalds maxlen - sz); 14911da177e4SLinus Torvalds } 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds break; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds 14961da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 14971da177e4SLinus Torvalds } 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv) 15001da177e4SLinus Torvalds { 15016380f26fSMike Anderson int r = -EINVAL; 15021da177e4SLinus Torvalds struct dm_dev *dev; 15031da177e4SLinus Torvalds struct multipath *m = (struct multipath *) ti->private; 15041da177e4SLinus Torvalds action_fn action; 15051da177e4SLinus Torvalds 15066380f26fSMike Anderson mutex_lock(&m->work_mutex); 15076380f26fSMike Anderson 1508c2f3d24bSKiyoshi Ueda if (dm_suspended(ti)) { 1509c2f3d24bSKiyoshi Ueda r = -EBUSY; 1510c2f3d24bSKiyoshi Ueda goto out; 1511c2f3d24bSKiyoshi Ueda } 1512c2f3d24bSKiyoshi Ueda 15131da177e4SLinus Torvalds if (argc == 1) { 1514498f0103SMike Snitzer if (!strcasecmp(argv[0], "queue_if_no_path")) { 15156380f26fSMike Anderson r = queue_if_no_path(m, 1, 0); 15166380f26fSMike Anderson goto out; 1517498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "fail_if_no_path")) { 15186380f26fSMike Anderson r = queue_if_no_path(m, 0, 0); 15196380f26fSMike Anderson goto out; 15206380f26fSMike Anderson } 15211da177e4SLinus Torvalds } 15221da177e4SLinus Torvalds 15236380f26fSMike Anderson if (argc != 2) { 1524a356e426SJose Castillo DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc); 15256380f26fSMike Anderson goto out; 15266380f26fSMike Anderson } 15271da177e4SLinus Torvalds 1528498f0103SMike Snitzer if (!strcasecmp(argv[0], "disable_group")) { 15296380f26fSMike Anderson r = bypass_pg_num(m, argv[1], 1); 15306380f26fSMike Anderson goto out; 1531498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "enable_group")) { 15326380f26fSMike Anderson r = bypass_pg_num(m, argv[1], 0); 15336380f26fSMike Anderson goto out; 1534498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "switch_group")) { 15356380f26fSMike Anderson r = switch_pg_num(m, argv[1]); 15366380f26fSMike Anderson goto out; 1537498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "reinstate_path")) 15381da177e4SLinus Torvalds action = reinstate_path; 1539498f0103SMike Snitzer else if (!strcasecmp(argv[0], "fail_path")) 15401da177e4SLinus Torvalds action = fail_path; 15416380f26fSMike Anderson else { 1542a356e426SJose Castillo DMWARN("Unrecognised multipath message received: %s", argv[0]); 15436380f26fSMike Anderson goto out; 15446380f26fSMike Anderson } 15451da177e4SLinus Torvalds 15468215d6ecSNikanth Karthikesan r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev); 15471da177e4SLinus Torvalds if (r) { 154872d94861SAlasdair G Kergon DMWARN("message: error getting device %s", 15491da177e4SLinus Torvalds argv[1]); 15506380f26fSMike Anderson goto out; 15511da177e4SLinus Torvalds } 15521da177e4SLinus Torvalds 15531da177e4SLinus Torvalds r = action_dev(m, dev, action); 15541da177e4SLinus Torvalds 15551da177e4SLinus Torvalds dm_put_device(ti, dev); 15561da177e4SLinus Torvalds 15576380f26fSMike Anderson out: 15586380f26fSMike Anderson mutex_unlock(&m->work_mutex); 15591da177e4SLinus Torvalds return r; 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds 1562e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti, 1563e56f81e0SChristoph Hellwig struct block_device **bdev, fmode_t *mode) 15649af4aa30SMilan Broz { 156535991652SMikulas Patocka struct multipath *m = ti->private; 15669af4aa30SMilan Broz unsigned long flags; 156735991652SMikulas Patocka int r; 156835991652SMikulas Patocka 15699af4aa30SMilan Broz spin_lock_irqsave(&m->lock, flags); 15709af4aa30SMilan Broz 15719af4aa30SMilan Broz if (!m->current_pgpath) 157202ab823fSKiyoshi Ueda __choose_pgpath(m, 0); 15739af4aa30SMilan Broz 157443e43c9eSJunichi Nomura if (m->current_pgpath) { 157543e43c9eSJunichi Nomura if (!m->queue_io) { 157643e43c9eSJunichi Nomura *bdev = m->current_pgpath->path.dev->bdev; 157743e43c9eSJunichi Nomura *mode = m->current_pgpath->path.dev->mode; 157843e43c9eSJunichi Nomura r = 0; 157943e43c9eSJunichi Nomura } else { 158043e43c9eSJunichi Nomura /* pg_init has not started or completed */ 15816c182cd8SHannes Reinecke r = -ENOTCONN; 158243e43c9eSJunichi Nomura } 158343e43c9eSJunichi Nomura } else { 158443e43c9eSJunichi Nomura /* No path is available */ 158543e43c9eSJunichi Nomura if (m->queue_if_no_path) 158643e43c9eSJunichi Nomura r = -ENOTCONN; 158743e43c9eSJunichi Nomura else 15889af4aa30SMilan Broz r = -EIO; 158943e43c9eSJunichi Nomura } 15909af4aa30SMilan Broz 15919af4aa30SMilan Broz spin_unlock_irqrestore(&m->lock, flags); 15929af4aa30SMilan Broz 15935bbbfdf6SJunichi Nomura if (r == -ENOTCONN) { 15943e9f1be1SHannes Reinecke spin_lock_irqsave(&m->lock, flags); 15953e9f1be1SHannes Reinecke if (!m->current_pg) { 15963e9f1be1SHannes Reinecke /* Path status changed, redo selection */ 15973e9f1be1SHannes Reinecke __choose_pgpath(m, 0); 15983e9f1be1SHannes Reinecke } 15993e9f1be1SHannes Reinecke if (m->pg_init_required) 16003e9f1be1SHannes Reinecke __pg_init_all_paths(m); 16014cdd2ad7SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 160263d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 16033e9f1be1SHannes Reinecke } 160435991652SMikulas Patocka 1605e56f81e0SChristoph Hellwig /* 1606e56f81e0SChristoph Hellwig * Only pass ioctls through if the device sizes match exactly. 1607e56f81e0SChristoph Hellwig */ 1608e56f81e0SChristoph Hellwig if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT) 1609e56f81e0SChristoph Hellwig return 1; 1610e56f81e0SChristoph Hellwig return r; 16119af4aa30SMilan Broz } 16129af4aa30SMilan Broz 1613af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti, 1614af4874e0SMike Snitzer iterate_devices_callout_fn fn, void *data) 1615af4874e0SMike Snitzer { 1616af4874e0SMike Snitzer struct multipath *m = ti->private; 1617af4874e0SMike Snitzer struct priority_group *pg; 1618af4874e0SMike Snitzer struct pgpath *p; 1619af4874e0SMike Snitzer int ret = 0; 1620af4874e0SMike Snitzer 1621af4874e0SMike Snitzer list_for_each_entry(pg, &m->priority_groups, list) { 1622af4874e0SMike Snitzer list_for_each_entry(p, &pg->pgpaths, list) { 16235dea271bSMike Snitzer ret = fn(ti, p->path.dev, ti->begin, ti->len, data); 1624af4874e0SMike Snitzer if (ret) 1625af4874e0SMike Snitzer goto out; 1626af4874e0SMike Snitzer } 1627af4874e0SMike Snitzer } 1628af4874e0SMike Snitzer 1629af4874e0SMike Snitzer out: 1630af4874e0SMike Snitzer return ret; 1631af4874e0SMike Snitzer } 1632af4874e0SMike Snitzer 1633f40c67f0SKiyoshi Ueda static int __pgpath_busy(struct pgpath *pgpath) 1634f40c67f0SKiyoshi Ueda { 1635f40c67f0SKiyoshi Ueda struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1636f40c67f0SKiyoshi Ueda 163752b09914SMike Snitzer return blk_lld_busy(q); 1638f40c67f0SKiyoshi Ueda } 1639f40c67f0SKiyoshi Ueda 1640f40c67f0SKiyoshi Ueda /* 1641f40c67f0SKiyoshi Ueda * We return "busy", only when we can map I/Os but underlying devices 1642f40c67f0SKiyoshi Ueda * are busy (so even if we map I/Os now, the I/Os will wait on 1643f40c67f0SKiyoshi Ueda * the underlying queue). 1644f40c67f0SKiyoshi Ueda * In other words, if we want to kill I/Os or queue them inside us 1645f40c67f0SKiyoshi Ueda * due to map unavailability, we don't return "busy". Otherwise, 1646f40c67f0SKiyoshi Ueda * dm core won't give us the I/Os and we can't do what we want. 1647f40c67f0SKiyoshi Ueda */ 1648f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti) 1649f40c67f0SKiyoshi Ueda { 1650f40c67f0SKiyoshi Ueda int busy = 0, has_active = 0; 1651f40c67f0SKiyoshi Ueda struct multipath *m = ti->private; 1652f40c67f0SKiyoshi Ueda struct priority_group *pg; 1653f40c67f0SKiyoshi Ueda struct pgpath *pgpath; 1654f40c67f0SKiyoshi Ueda unsigned long flags; 1655f40c67f0SKiyoshi Ueda 1656f40c67f0SKiyoshi Ueda spin_lock_irqsave(&m->lock, flags); 1657f40c67f0SKiyoshi Ueda 16587a7a3b45SJun'ichi Nomura /* pg_init in progress or no paths available */ 16597a7a3b45SJun'ichi Nomura if (m->pg_init_in_progress || 16607a7a3b45SJun'ichi Nomura (!m->nr_valid_paths && m->queue_if_no_path)) { 1661b63349a7SHannes Reinecke busy = 1; 1662b63349a7SHannes Reinecke goto out; 1663b63349a7SHannes Reinecke } 1664f40c67f0SKiyoshi Ueda /* Guess which priority_group will be used at next mapping time */ 1665f40c67f0SKiyoshi Ueda if (unlikely(!m->current_pgpath && m->next_pg)) 1666f40c67f0SKiyoshi Ueda pg = m->next_pg; 1667f40c67f0SKiyoshi Ueda else if (likely(m->current_pg)) 1668f40c67f0SKiyoshi Ueda pg = m->current_pg; 1669f40c67f0SKiyoshi Ueda else 1670f40c67f0SKiyoshi Ueda /* 1671f40c67f0SKiyoshi Ueda * We don't know which pg will be used at next mapping time. 1672f40c67f0SKiyoshi Ueda * We don't call __choose_pgpath() here to avoid to trigger 1673f40c67f0SKiyoshi Ueda * pg_init just by busy checking. 1674f40c67f0SKiyoshi Ueda * So we don't know whether underlying devices we will be using 1675f40c67f0SKiyoshi Ueda * at next mapping time are busy or not. Just try mapping. 1676f40c67f0SKiyoshi Ueda */ 1677f40c67f0SKiyoshi Ueda goto out; 1678f40c67f0SKiyoshi Ueda 1679f40c67f0SKiyoshi Ueda /* 1680f40c67f0SKiyoshi Ueda * If there is one non-busy active path at least, the path selector 1681f40c67f0SKiyoshi Ueda * will be able to select it. So we consider such a pg as not busy. 1682f40c67f0SKiyoshi Ueda */ 1683f40c67f0SKiyoshi Ueda busy = 1; 1684f40c67f0SKiyoshi Ueda list_for_each_entry(pgpath, &pg->pgpaths, list) 1685f40c67f0SKiyoshi Ueda if (pgpath->is_active) { 1686f40c67f0SKiyoshi Ueda has_active = 1; 1687f40c67f0SKiyoshi Ueda 1688f40c67f0SKiyoshi Ueda if (!__pgpath_busy(pgpath)) { 1689f40c67f0SKiyoshi Ueda busy = 0; 1690f40c67f0SKiyoshi Ueda break; 1691f40c67f0SKiyoshi Ueda } 1692f40c67f0SKiyoshi Ueda } 1693f40c67f0SKiyoshi Ueda 1694f40c67f0SKiyoshi Ueda if (!has_active) 1695f40c67f0SKiyoshi Ueda /* 1696f40c67f0SKiyoshi Ueda * No active path in this pg, so this pg won't be used and 1697f40c67f0SKiyoshi Ueda * the current_pg will be changed at next mapping time. 1698f40c67f0SKiyoshi Ueda * We need to try mapping to determine it. 1699f40c67f0SKiyoshi Ueda */ 1700f40c67f0SKiyoshi Ueda busy = 0; 1701f40c67f0SKiyoshi Ueda 1702f40c67f0SKiyoshi Ueda out: 1703f40c67f0SKiyoshi Ueda spin_unlock_irqrestore(&m->lock, flags); 1704f40c67f0SKiyoshi Ueda 1705f40c67f0SKiyoshi Ueda return busy; 1706f40c67f0SKiyoshi Ueda } 1707f40c67f0SKiyoshi Ueda 17081da177e4SLinus Torvalds /*----------------------------------------------------------------- 17091da177e4SLinus Torvalds * Module setup 17101da177e4SLinus Torvalds *---------------------------------------------------------------*/ 17111da177e4SLinus Torvalds static struct target_type multipath_target = { 17121da177e4SLinus Torvalds .name = "multipath", 171316f12266SMike Snitzer .version = {1, 11, 0}, 171416f12266SMike Snitzer .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE, 17151da177e4SLinus Torvalds .module = THIS_MODULE, 17161da177e4SLinus Torvalds .ctr = multipath_ctr, 17171da177e4SLinus Torvalds .dtr = multipath_dtr, 1718f40c67f0SKiyoshi Ueda .map_rq = multipath_map, 1719e5863d9aSMike Snitzer .clone_and_map_rq = multipath_clone_and_map, 1720e5863d9aSMike Snitzer .release_clone_rq = multipath_release_clone, 1721f40c67f0SKiyoshi Ueda .rq_end_io = multipath_end_io, 17221da177e4SLinus Torvalds .presuspend = multipath_presuspend, 17236df400abSKiyoshi Ueda .postsuspend = multipath_postsuspend, 17241da177e4SLinus Torvalds .resume = multipath_resume, 17251da177e4SLinus Torvalds .status = multipath_status, 17261da177e4SLinus Torvalds .message = multipath_message, 1727e56f81e0SChristoph Hellwig .prepare_ioctl = multipath_prepare_ioctl, 1728af4874e0SMike Snitzer .iterate_devices = multipath_iterate_devices, 1729f40c67f0SKiyoshi Ueda .busy = multipath_busy, 17301da177e4SLinus Torvalds }; 17311da177e4SLinus Torvalds 17321da177e4SLinus Torvalds static int __init dm_multipath_init(void) 17331da177e4SLinus Torvalds { 17341da177e4SLinus Torvalds int r; 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds /* allocate a slab for the dm_ios */ 1737028867acSAlasdair G Kergon _mpio_cache = KMEM_CACHE(dm_mpath_io, 0); 17381da177e4SLinus Torvalds if (!_mpio_cache) 17391da177e4SLinus Torvalds return -ENOMEM; 17401da177e4SLinus Torvalds 17411da177e4SLinus Torvalds r = dm_register_target(&multipath_target); 17421da177e4SLinus Torvalds if (r < 0) { 17430cd33124SAlasdair G Kergon DMERR("register failed %d", r); 1744ff658e9cSJohannes Thumshirn r = -EINVAL; 1745ff658e9cSJohannes Thumshirn goto bad_register_target; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 17484d4d66abSTejun Heo kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0); 1749c557308eSAlasdair G Kergon if (!kmultipathd) { 17500cd33124SAlasdair G Kergon DMERR("failed to create workqueue kmpathd"); 1751ff658e9cSJohannes Thumshirn r = -ENOMEM; 1752ff658e9cSJohannes Thumshirn goto bad_alloc_kmultipathd; 1753c557308eSAlasdair G Kergon } 1754c557308eSAlasdair G Kergon 1755bab7cfc7SChandra Seetharaman /* 1756bab7cfc7SChandra Seetharaman * A separate workqueue is used to handle the device handlers 1757bab7cfc7SChandra Seetharaman * to avoid overloading existing workqueue. Overloading the 1758bab7cfc7SChandra Seetharaman * old workqueue would also create a bottleneck in the 1759bab7cfc7SChandra Seetharaman * path of the storage hardware device activation. 1760bab7cfc7SChandra Seetharaman */ 17614d4d66abSTejun Heo kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd", 17624d4d66abSTejun Heo WQ_MEM_RECLAIM); 1763bab7cfc7SChandra Seetharaman if (!kmpath_handlerd) { 1764bab7cfc7SChandra Seetharaman DMERR("failed to create workqueue kmpath_handlerd"); 1765ff658e9cSJohannes Thumshirn r = -ENOMEM; 1766ff658e9cSJohannes Thumshirn goto bad_alloc_kmpath_handlerd; 1767bab7cfc7SChandra Seetharaman } 1768bab7cfc7SChandra Seetharaman 176972d94861SAlasdair G Kergon DMINFO("version %u.%u.%u loaded", 17701da177e4SLinus Torvalds multipath_target.version[0], multipath_target.version[1], 17711da177e4SLinus Torvalds multipath_target.version[2]); 17721da177e4SLinus Torvalds 1773ff658e9cSJohannes Thumshirn return 0; 1774ff658e9cSJohannes Thumshirn 1775ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd: 1776ff658e9cSJohannes Thumshirn destroy_workqueue(kmultipathd); 1777ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd: 1778ff658e9cSJohannes Thumshirn dm_unregister_target(&multipath_target); 1779ff658e9cSJohannes Thumshirn bad_register_target: 1780ff658e9cSJohannes Thumshirn kmem_cache_destroy(_mpio_cache); 1781ff658e9cSJohannes Thumshirn 17821da177e4SLinus Torvalds return r; 17831da177e4SLinus Torvalds } 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds static void __exit dm_multipath_exit(void) 17861da177e4SLinus Torvalds { 1787bab7cfc7SChandra Seetharaman destroy_workqueue(kmpath_handlerd); 1788c557308eSAlasdair G Kergon destroy_workqueue(kmultipathd); 1789c557308eSAlasdair G Kergon 179010d3bd09SMikulas Patocka dm_unregister_target(&multipath_target); 17911da177e4SLinus Torvalds kmem_cache_destroy(_mpio_cache); 17921da177e4SLinus Torvalds } 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds module_init(dm_multipath_init); 17951da177e4SLinus Torvalds module_exit(dm_multipath_exit); 17961da177e4SLinus Torvalds 17971da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target"); 17981da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); 17991da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 1800