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 104cc96131SMike Snitzer #include "dm-rq.h" 1176e33fe4SMike Snitzer #include "dm-bio-record.h" 121da177e4SLinus Torvalds #include "dm-path-selector.h" 13b15546f9SMike Anderson #include "dm-uevent.h" 141da177e4SLinus Torvalds 15e5863d9aSMike Snitzer #include <linux/blkdev.h> 161da177e4SLinus Torvalds #include <linux/ctype.h> 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/mempool.h> 191da177e4SLinus Torvalds #include <linux/module.h> 201da177e4SLinus Torvalds #include <linux/pagemap.h> 211da177e4SLinus Torvalds #include <linux/slab.h> 221da177e4SLinus Torvalds #include <linux/time.h> 231da177e4SLinus Torvalds #include <linux/workqueue.h> 2435991652SMikulas Patocka #include <linux/delay.h> 25cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h> 2660063497SArun Sharma #include <linux/atomic.h> 2778ce23b5SMike Snitzer #include <linux/blk-mq.h> 281da177e4SLinus Torvalds 2972d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath" 304e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000 314e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1) 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds /* Path properties */ 341da177e4SLinus Torvalds struct pgpath { 351da177e4SLinus Torvalds struct list_head list; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds struct priority_group *pg; /* Owning PG */ 381da177e4SLinus Torvalds unsigned fail_count; /* Cumulative failure count */ 391da177e4SLinus Torvalds 40c922d5f7SJosef "Jeff" Sipek struct dm_path path; 414e2d19e4SChandra Seetharaman struct delayed_work activate_path; 42be7d31ccSMike Snitzer 43be7d31ccSMike Snitzer bool is_active:1; /* Path status */ 441da177e4SLinus Torvalds }; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path) 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds /* 491da177e4SLinus Torvalds * Paths are grouped into Priority Groups and numbered from 1 upwards. 501da177e4SLinus Torvalds * Each has a path selector which controls which path gets used. 511da177e4SLinus Torvalds */ 521da177e4SLinus Torvalds struct priority_group { 531da177e4SLinus Torvalds struct list_head list; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds struct multipath *m; /* Owning multipath instance */ 561da177e4SLinus Torvalds struct path_selector ps; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds unsigned pg_num; /* Reference number */ 591da177e4SLinus Torvalds unsigned nr_pgpaths; /* Number of paths in PG */ 601da177e4SLinus Torvalds struct list_head pgpaths; 61be7d31ccSMike Snitzer 62be7d31ccSMike Snitzer bool bypassed:1; /* Temporarily bypass this PG? */ 631da177e4SLinus Torvalds }; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* Multipath context */ 661da177e4SLinus Torvalds struct multipath { 671da177e4SLinus Torvalds struct list_head list; 681da177e4SLinus Torvalds struct dm_target *ti; 691da177e4SLinus Torvalds 70cfae5c9bSChandra Seetharaman const char *hw_handler_name; 712bfd2e13SChandra Seetharaman char *hw_handler_params; 724e2d19e4SChandra Seetharaman 731fbdd2b3SMike Snitzer spinlock_t lock; 741fbdd2b3SMike Snitzer 751da177e4SLinus Torvalds unsigned nr_priority_groups; 761da177e4SLinus Torvalds struct list_head priority_groups; 774e2d19e4SChandra Seetharaman 784e2d19e4SChandra Seetharaman wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */ 794e2d19e4SChandra Seetharaman 801da177e4SLinus Torvalds struct pgpath *current_pgpath; 811da177e4SLinus Torvalds struct priority_group *current_pg; 821da177e4SLinus Torvalds struct priority_group *next_pg; /* Switch to this PG if set */ 831da177e4SLinus Torvalds 84518257b1SMike Snitzer unsigned long flags; /* Multipath state flags */ 851fbdd2b3SMike Snitzer 86c9e45581SDave Wysochanski unsigned pg_init_retries; /* Number of times to retry pg_init */ 874e2d19e4SChandra Seetharaman unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */ 881da177e4SLinus Torvalds 8991e968aaSMike Snitzer atomic_t nr_valid_paths; /* Total number of usable paths */ 9091e968aaSMike Snitzer atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */ 9191e968aaSMike Snitzer atomic_t pg_init_count; /* Number of times pg_init called */ 9291e968aaSMike Snitzer 93e83068a5SMike Snitzer unsigned queue_mode; 94e83068a5SMike Snitzer 951da177e4SLinus Torvalds /* 96028867acSAlasdair G Kergon * We must use a mempool of dm_mpath_io structs so that we 971da177e4SLinus Torvalds * can resubmit bios on error. 981da177e4SLinus Torvalds */ 991da177e4SLinus Torvalds mempool_t *mpio_pool; 1006380f26fSMike Anderson 1016380f26fSMike Anderson struct mutex work_mutex; 10220800cb3SMike Snitzer struct work_struct trigger_event; 10376e33fe4SMike Snitzer 10476e33fe4SMike Snitzer struct work_struct process_queued_bios; 10576e33fe4SMike Snitzer struct bio_list queued_bios; 1061da177e4SLinus Torvalds }; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* 10976e33fe4SMike Snitzer * Context information attached to each io 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); 12376e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work); 1241da177e4SLinus Torvalds 125518257b1SMike Snitzer /*----------------------------------------------- 126518257b1SMike Snitzer * Multipath state flags. 127518257b1SMike Snitzer *-----------------------------------------------*/ 128518257b1SMike Snitzer 129518257b1SMike Snitzer #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */ 130518257b1SMike Snitzer #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */ 131518257b1SMike Snitzer #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */ 132518257b1SMike Snitzer #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */ 133518257b1SMike Snitzer #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */ 134518257b1SMike Snitzer #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */ 135518257b1SMike Snitzer #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */ 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds /*----------------------------------------------- 1381da177e4SLinus Torvalds * Allocation routines 1391da177e4SLinus Torvalds *-----------------------------------------------*/ 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void) 1421da177e4SLinus Torvalds { 143e69fae56SMicha³ Miros³aw struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL); 1441da177e4SLinus Torvalds 145224cb3e9SMike Anderson if (pgpath) { 146be7d31ccSMike Snitzer pgpath->is_active = true; 1474e2d19e4SChandra Seetharaman INIT_DELAYED_WORK(&pgpath->activate_path, activate_path); 148224cb3e9SMike Anderson } 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds return pgpath; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 153028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath) 1541da177e4SLinus Torvalds { 1551da177e4SLinus Torvalds kfree(pgpath); 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds struct priority_group *pg; 1611da177e4SLinus Torvalds 162e69fae56SMicha³ Miros³aw pg = kzalloc(sizeof(*pg), GFP_KERNEL); 1631da177e4SLinus Torvalds 164e69fae56SMicha³ Miros³aw if (pg) 1651da177e4SLinus Torvalds INIT_LIST_HEAD(&pg->pgpaths); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds return pg; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti) 1711da177e4SLinus Torvalds { 1721da177e4SLinus Torvalds struct pgpath *pgpath, *tmp; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds list_for_each_entry_safe(pgpath, tmp, pgpaths, list) { 1751da177e4SLinus Torvalds list_del(&pgpath->list); 1761da177e4SLinus Torvalds dm_put_device(ti, pgpath->path.dev); 1771da177e4SLinus Torvalds free_pgpath(pgpath); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg, 1821da177e4SLinus Torvalds struct dm_target *ti) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds struct path_selector *ps = &pg->ps; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds if (ps->type) { 1871da177e4SLinus Torvalds ps->type->destroy(ps); 1881da177e4SLinus Torvalds dm_put_path_selector(ps->type); 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds free_pgpaths(&pg->pgpaths, ti); 1921da177e4SLinus Torvalds kfree(pg); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 195e83068a5SMike Snitzer static struct multipath *alloc_multipath(struct dm_target *ti) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds struct multipath *m; 1981da177e4SLinus Torvalds 199e69fae56SMicha³ Miros³aw m = kzalloc(sizeof(*m), GFP_KERNEL); 2001da177e4SLinus Torvalds if (m) { 2011da177e4SLinus Torvalds INIT_LIST_HEAD(&m->priority_groups); 2021da177e4SLinus Torvalds spin_lock_init(&m->lock); 203518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 20491e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, 0); 20591e968aaSMike Snitzer atomic_set(&m->pg_init_in_progress, 0); 20691e968aaSMike Snitzer atomic_set(&m->pg_init_count, 0); 2074e2d19e4SChandra Seetharaman m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT; 208c4028958SDavid Howells INIT_WORK(&m->trigger_event, trigger_event); 2092bded7bdSKiyoshi Ueda init_waitqueue_head(&m->pg_init_wait); 2106380f26fSMike Anderson mutex_init(&m->work_mutex); 2118637a6bfSMike Snitzer 2128637a6bfSMike Snitzer m->mpio_pool = NULL; 213e83068a5SMike Snitzer m->queue_mode = DM_TYPE_NONE; 214e83068a5SMike Snitzer 215e83068a5SMike Snitzer m->ti = ti; 216e83068a5SMike Snitzer ti->private = m; 217e83068a5SMike Snitzer } 218e83068a5SMike Snitzer 219e83068a5SMike Snitzer return m; 220e83068a5SMike Snitzer } 221e83068a5SMike Snitzer 222e83068a5SMike Snitzer static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m) 223e83068a5SMike Snitzer { 224e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_NONE) { 225e83068a5SMike Snitzer /* 226e83068a5SMike Snitzer * Default to request-based. 227e83068a5SMike Snitzer */ 228e83068a5SMike Snitzer if (dm_use_blk_mq(dm_table_get_md(ti->table))) 229e83068a5SMike Snitzer m->queue_mode = DM_TYPE_MQ_REQUEST_BASED; 230e83068a5SMike Snitzer else 231e83068a5SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 232e83068a5SMike Snitzer } 233e83068a5SMike Snitzer 234e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_REQUEST_BASED) { 2358637a6bfSMike Snitzer unsigned min_ios = dm_get_reserved_rq_based_ios(); 2368637a6bfSMike Snitzer 237f4790826SMike Snitzer m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache); 238e83068a5SMike Snitzer if (!m->mpio_pool) 239e83068a5SMike Snitzer return -ENOMEM; 2401da177e4SLinus Torvalds } 241e83068a5SMike Snitzer else if (m->queue_mode == DM_TYPE_BIO_BASED) { 24276e33fe4SMike Snitzer INIT_WORK(&m->process_queued_bios, process_queued_bios); 24376e33fe4SMike Snitzer /* 24476e33fe4SMike Snitzer * bio-based doesn't support any direct scsi_dh management; 24576e33fe4SMike Snitzer * it just discovers if a scsi_dh is attached. 24676e33fe4SMike Snitzer */ 24776e33fe4SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 24876e33fe4SMike Snitzer } 24976e33fe4SMike Snitzer 250e83068a5SMike Snitzer dm_table_set_type(ti->table, m->queue_mode); 2511da177e4SLinus Torvalds 252e83068a5SMike Snitzer return 0; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds static void free_multipath(struct multipath *m) 2561da177e4SLinus Torvalds { 2571da177e4SLinus Torvalds struct priority_group *pg, *tmp; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { 2601da177e4SLinus Torvalds list_del(&pg->list); 2611da177e4SLinus Torvalds free_priority_group(pg, m->ti); 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds 264cfae5c9bSChandra Seetharaman kfree(m->hw_handler_name); 2652bfd2e13SChandra Seetharaman kfree(m->hw_handler_params); 2661da177e4SLinus Torvalds mempool_destroy(m->mpio_pool); 2671da177e4SLinus Torvalds kfree(m); 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2702eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info) 2712eff1924SMike Snitzer { 2722eff1924SMike Snitzer return info->ptr; 2732eff1924SMike Snitzer } 2742eff1924SMike Snitzer 2752eff1924SMike Snitzer static struct dm_mpath_io *set_mpio(struct multipath *m, union map_info *info) 276466891f9SJun'ichi Nomura { 277466891f9SJun'ichi Nomura struct dm_mpath_io *mpio; 278466891f9SJun'ichi Nomura 2798637a6bfSMike Snitzer if (!m->mpio_pool) { 2808637a6bfSMike Snitzer /* Use blk-mq pdu memory requested via per_io_data_size */ 2812eff1924SMike Snitzer mpio = get_mpio(info); 2828637a6bfSMike Snitzer memset(mpio, 0, sizeof(*mpio)); 2838637a6bfSMike Snitzer return mpio; 2848637a6bfSMike Snitzer } 2858637a6bfSMike Snitzer 286466891f9SJun'ichi Nomura mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC); 287466891f9SJun'ichi Nomura if (!mpio) 2882eff1924SMike Snitzer return NULL; 289466891f9SJun'ichi Nomura 290466891f9SJun'ichi Nomura memset(mpio, 0, sizeof(*mpio)); 291466891f9SJun'ichi Nomura info->ptr = mpio; 292466891f9SJun'ichi Nomura 2932eff1924SMike Snitzer return mpio; 294466891f9SJun'ichi Nomura } 295466891f9SJun'ichi Nomura 2962eff1924SMike Snitzer static void clear_request_fn_mpio(struct multipath *m, union map_info *info) 297466891f9SJun'ichi Nomura { 2982eff1924SMike Snitzer /* Only needed for non blk-mq (.request_fn) multipath */ 2998637a6bfSMike Snitzer if (m->mpio_pool) { 300466891f9SJun'ichi Nomura struct dm_mpath_io *mpio = info->ptr; 301466891f9SJun'ichi Nomura 302466891f9SJun'ichi Nomura info->ptr = NULL; 303466891f9SJun'ichi Nomura mempool_free(mpio, m->mpio_pool); 304466891f9SJun'ichi Nomura } 3058637a6bfSMike Snitzer } 3061da177e4SLinus Torvalds 307bf661be1SMike Snitzer static size_t multipath_per_bio_data_size(void) 30876e33fe4SMike Snitzer { 309bf661be1SMike Snitzer return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details); 31076e33fe4SMike Snitzer } 31176e33fe4SMike Snitzer 312bf661be1SMike Snitzer static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio) 313bf661be1SMike Snitzer { 314bf661be1SMike Snitzer return dm_per_bio_data(bio, multipath_per_bio_data_size()); 315bf661be1SMike Snitzer } 316bf661be1SMike Snitzer 317bf661be1SMike Snitzer static struct dm_bio_details *get_bio_details_from_bio(struct bio *bio) 318bf661be1SMike Snitzer { 319bf661be1SMike Snitzer /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */ 320bf661be1SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 321bf661be1SMike Snitzer void *bio_details = mpio + 1; 322bf661be1SMike Snitzer 323bf661be1SMike Snitzer return bio_details; 324bf661be1SMike Snitzer } 325bf661be1SMike Snitzer 326bf661be1SMike Snitzer static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p, 327bf661be1SMike Snitzer struct dm_bio_details **bio_details_p) 32876e33fe4SMike Snitzer { 32976e33fe4SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 330bf661be1SMike Snitzer struct dm_bio_details *bio_details = get_bio_details_from_bio(bio); 33176e33fe4SMike Snitzer 33276e33fe4SMike Snitzer memset(mpio, 0, sizeof(*mpio)); 333bf661be1SMike Snitzer memset(bio_details, 0, sizeof(*bio_details)); 334bf661be1SMike Snitzer dm_bio_record(bio_details, bio); 33576e33fe4SMike Snitzer 336bf661be1SMike Snitzer if (mpio_p) 337bf661be1SMike Snitzer *mpio_p = mpio; 338bf661be1SMike Snitzer if (bio_details_p) 339bf661be1SMike Snitzer *bio_details_p = bio_details; 34076e33fe4SMike Snitzer } 34176e33fe4SMike Snitzer 3421da177e4SLinus Torvalds /*----------------------------------------------- 3431da177e4SLinus Torvalds * Path selection 3441da177e4SLinus Torvalds *-----------------------------------------------*/ 3451da177e4SLinus Torvalds 3463e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m) 347fb612642SKiyoshi Ueda { 348fb612642SKiyoshi Ueda struct pgpath *pgpath; 3494e2d19e4SChandra Seetharaman unsigned long pg_init_delay = 0; 350fb612642SKiyoshi Ueda 35191e968aaSMike Snitzer if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 3523e9f1be1SHannes Reinecke return 0; 35317f4ff45SHannes Reinecke 35491e968aaSMike Snitzer atomic_inc(&m->pg_init_count); 355518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 3563e9f1be1SHannes Reinecke 3573e9f1be1SHannes Reinecke /* Check here to reset pg_init_required */ 3583e9f1be1SHannes Reinecke if (!m->current_pg) 3593e9f1be1SHannes Reinecke return 0; 3603e9f1be1SHannes Reinecke 361518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags)) 3624e2d19e4SChandra Seetharaman pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ? 3634e2d19e4SChandra Seetharaman m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS); 364fb612642SKiyoshi Ueda list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) { 365fb612642SKiyoshi Ueda /* Skip failed paths */ 366fb612642SKiyoshi Ueda if (!pgpath->is_active) 367fb612642SKiyoshi Ueda continue; 3684e2d19e4SChandra Seetharaman if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path, 3694e2d19e4SChandra Seetharaman pg_init_delay)) 37091e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 371fb612642SKiyoshi Ueda } 37291e968aaSMike Snitzer return atomic_read(&m->pg_init_in_progress); 373fb612642SKiyoshi Ueda } 374fb612642SKiyoshi Ueda 3752da1610aSMike Snitzer static int pg_init_all_paths(struct multipath *m) 3761da177e4SLinus Torvalds { 3772da1610aSMike Snitzer int r; 3782da1610aSMike Snitzer unsigned long flags; 3792da1610aSMike Snitzer 3802da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 3812da1610aSMike Snitzer r = __pg_init_all_paths(m); 3822da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3832da1610aSMike Snitzer 3842da1610aSMike Snitzer return r; 3852da1610aSMike Snitzer } 3862da1610aSMike Snitzer 3872da1610aSMike Snitzer static void __switch_pg(struct multipath *m, struct priority_group *pg) 3882da1610aSMike Snitzer { 3892da1610aSMike Snitzer m->current_pg = pg; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds /* Must we initialise the PG first, and queue I/O till it's ready? */ 392cfae5c9bSChandra Seetharaman if (m->hw_handler_name) { 393518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 394518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 3951da177e4SLinus Torvalds } else { 396518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 397518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 3981da177e4SLinus Torvalds } 399c9e45581SDave Wysochanski 40091e968aaSMike Snitzer atomic_set(&m->pg_init_count, 0); 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4032da1610aSMike Snitzer static struct pgpath *choose_path_in_pg(struct multipath *m, 4042da1610aSMike Snitzer struct priority_group *pg, 40502ab823fSKiyoshi Ueda size_t nr_bytes) 4061da177e4SLinus Torvalds { 4072da1610aSMike Snitzer unsigned long flags; 408c922d5f7SJosef "Jeff" Sipek struct dm_path *path; 4092da1610aSMike Snitzer struct pgpath *pgpath; 4101da177e4SLinus Torvalds 41190a4323cSMike Snitzer path = pg->ps.type->select_path(&pg->ps, nr_bytes); 4121da177e4SLinus Torvalds if (!path) 4132da1610aSMike Snitzer return ERR_PTR(-ENXIO); 4141da177e4SLinus Torvalds 4152da1610aSMike Snitzer pgpath = path_to_pgpath(path); 4161da177e4SLinus Torvalds 4172da1610aSMike Snitzer if (unlikely(lockless_dereference(m->current_pg) != pg)) { 4182da1610aSMike Snitzer /* Only update current_pgpath if pg changed */ 4192da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 4202da1610aSMike Snitzer m->current_pgpath = pgpath; 4212da1610aSMike Snitzer __switch_pg(m, pg); 4222da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4252da1610aSMike Snitzer return pgpath; 4262da1610aSMike Snitzer } 4272da1610aSMike Snitzer 4282da1610aSMike Snitzer static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes) 4291da177e4SLinus Torvalds { 4302da1610aSMike Snitzer unsigned long flags; 4311da177e4SLinus Torvalds struct priority_group *pg; 4322da1610aSMike Snitzer struct pgpath *pgpath; 433be7d31ccSMike Snitzer bool bypassed = true; 4341da177e4SLinus Torvalds 43591e968aaSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 436518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 4371da177e4SLinus Torvalds goto failed; 4381f271972SBenjamin Marzinski } 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds /* Were we instructed to switch PG? */ 4412da1610aSMike Snitzer if (lockless_dereference(m->next_pg)) { 4422da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 4431da177e4SLinus Torvalds pg = m->next_pg; 4442da1610aSMike Snitzer if (!pg) { 4452da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4462da1610aSMike Snitzer goto check_current_pg; 4472da1610aSMike Snitzer } 4481da177e4SLinus Torvalds m->next_pg = NULL; 4492da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4502da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4512da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 4522da1610aSMike Snitzer return pgpath; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds /* Don't change PG until it has no remaining paths */ 4562da1610aSMike Snitzer check_current_pg: 4572da1610aSMike Snitzer pg = lockless_dereference(m->current_pg); 4582da1610aSMike Snitzer if (pg) { 4592da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4602da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 4612da1610aSMike Snitzer return pgpath; 4622da1610aSMike Snitzer } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds /* 4651da177e4SLinus Torvalds * Loop through priority groups until we find a valid path. 4661da177e4SLinus Torvalds * First time we skip PGs marked 'bypassed'. 467f220fd4eSMike Christie * Second time we only try the ones we skipped, but set 468f220fd4eSMike Christie * pg_init_delay_retry so we do not hammer controllers. 4691da177e4SLinus Torvalds */ 4701da177e4SLinus Torvalds do { 4711da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 4721da177e4SLinus Torvalds if (pg->bypassed == bypassed) 4731da177e4SLinus Torvalds continue; 4742da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4752da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) { 476f220fd4eSMike Christie if (!bypassed) 477518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 4782da1610aSMike Snitzer return pgpath; 4791da177e4SLinus Torvalds } 480f220fd4eSMike Christie } 4811da177e4SLinus Torvalds } while (bypassed--); 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds failed: 4842da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 4851da177e4SLinus Torvalds m->current_pgpath = NULL; 4861da177e4SLinus Torvalds m->current_pg = NULL; 4872da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4882da1610aSMike Snitzer 4892da1610aSMike Snitzer return NULL; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 49245e15720SKiyoshi Ueda /* 49345e15720SKiyoshi Ueda * Check whether bios must be queued in the device-mapper core rather 49445e15720SKiyoshi Ueda * than here in the target. 49545e15720SKiyoshi Ueda * 49645e15720SKiyoshi Ueda * If m->queue_if_no_path and m->saved_queue_if_no_path hold the 49745e15720SKiyoshi Ueda * same value then we are not between multipath_presuspend() 49845e15720SKiyoshi Ueda * and multipath_resume() calls and we have no need to check 49945e15720SKiyoshi Ueda * for the DMF_NOFLUSH_SUSPENDING flag. 50045e15720SKiyoshi Ueda */ 50176e33fe4SMike Snitzer static bool __must_push_back(struct multipath *m) 50276e33fe4SMike Snitzer { 50376e33fe4SMike Snitzer return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) != 50476e33fe4SMike Snitzer test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) && 50576e33fe4SMike Snitzer dm_noflush_suspending(m->ti)); 50676e33fe4SMike Snitzer } 50776e33fe4SMike Snitzer 50876e33fe4SMike Snitzer static bool must_push_back_rq(struct multipath *m) 50945e15720SKiyoshi Ueda { 5101814f2e3SMike Snitzer bool r; 5111814f2e3SMike Snitzer unsigned long flags; 5121814f2e3SMike Snitzer 5131814f2e3SMike Snitzer spin_lock_irqsave(&m->lock, flags); 5141814f2e3SMike Snitzer r = (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) || 51576e33fe4SMike Snitzer __must_push_back(m)); 5161814f2e3SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 5171814f2e3SMike Snitzer 5181814f2e3SMike Snitzer return r; 51976e33fe4SMike Snitzer } 52076e33fe4SMike Snitzer 52176e33fe4SMike Snitzer static bool must_push_back_bio(struct multipath *m) 52276e33fe4SMike Snitzer { 5231814f2e3SMike Snitzer bool r; 5241814f2e3SMike Snitzer unsigned long flags; 5251814f2e3SMike Snitzer 5261814f2e3SMike Snitzer spin_lock_irqsave(&m->lock, flags); 5271814f2e3SMike Snitzer r = __must_push_back(m); 5281814f2e3SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 5291814f2e3SMike Snitzer 5301814f2e3SMike Snitzer return r; 53145e15720SKiyoshi Ueda } 53245e15720SKiyoshi Ueda 53336fcffccSHannes Reinecke /* 53476e33fe4SMike Snitzer * Map cloned requests (request-based multipath) 53536fcffccSHannes Reinecke */ 536e5863d9aSMike Snitzer static int __multipath_map(struct dm_target *ti, struct request *clone, 537e5863d9aSMike Snitzer union map_info *map_context, 538e5863d9aSMike Snitzer struct request *rq, struct request **__clone) 5391da177e4SLinus Torvalds { 5407943bd6dSMike Snitzer struct multipath *m = ti->private; 541e3bde04fSHannes Reinecke int r = DM_MAPIO_REQUEUE; 542e5863d9aSMike Snitzer size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq); 5431da177e4SLinus Torvalds struct pgpath *pgpath; 544f40c67f0SKiyoshi Ueda struct block_device *bdev; 545e3bde04fSHannes Reinecke struct dm_mpath_io *mpio; 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds /* Do we need to select a new pgpath? */ 5482da1610aSMike Snitzer pgpath = lockless_dereference(m->current_pgpath); 5492da1610aSMike Snitzer if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags)) 5502da1610aSMike Snitzer pgpath = choose_pgpath(m, nr_bytes); 5511da177e4SLinus Torvalds 5529bf59a61SMike Snitzer if (!pgpath) { 55376e33fe4SMike Snitzer if (!must_push_back_rq(m)) 5549bf59a61SMike Snitzer r = -EIO; /* Failed */ 5552da1610aSMike Snitzer return r; 556518257b1SMike Snitzer } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) || 557518257b1SMike Snitzer test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 5582da1610aSMike Snitzer pg_init_all_paths(m); 5592da1610aSMike Snitzer return r; 5609bf59a61SMike Snitzer } 5616afbc01dSMike Snitzer 5622eff1924SMike Snitzer mpio = set_mpio(m, map_context); 5632eff1924SMike Snitzer if (!mpio) 564e3bde04fSHannes Reinecke /* ENOMEM, requeue */ 5652da1610aSMike Snitzer return r; 566e3bde04fSHannes Reinecke 567e8099177SHannes Reinecke mpio->pgpath = pgpath; 568e8099177SHannes Reinecke mpio->nr_bytes = nr_bytes; 5692eb6e1e3SKeith Busch 5702eb6e1e3SKeith Busch bdev = pgpath->path.dev->bdev; 5712eb6e1e3SKeith Busch 572e5863d9aSMike Snitzer if (clone) { 573c5248f79SMike Snitzer /* 574c5248f79SMike Snitzer * Old request-based interface: allocated clone is passed in. 575c5248f79SMike Snitzer * Used by: .request_fn stacked on .request_fn path(s). 576c5248f79SMike Snitzer */ 5772eb6e1e3SKeith Busch clone->q = bdev_get_queue(bdev); 5782eb6e1e3SKeith Busch clone->rq_disk = bdev->bd_disk; 5792eb6e1e3SKeith Busch clone->cmd_flags |= REQ_FAILFAST_TRANSPORT; 580e5863d9aSMike Snitzer } else { 581eca7ee6dSMike Snitzer /* 582eca7ee6dSMike Snitzer * blk-mq request-based interface; used by both: 583eca7ee6dSMike Snitzer * .request_fn stacked on blk-mq path(s) and 584eca7ee6dSMike Snitzer * blk-mq stacked on blk-mq path(s). 585eca7ee6dSMike Snitzer */ 58678ce23b5SMike Snitzer *__clone = blk_mq_alloc_request(bdev_get_queue(bdev), 58778ce23b5SMike Snitzer rq_data_dir(rq), BLK_MQ_REQ_NOWAIT); 5884c6dd53dSMike Snitzer if (IS_ERR(*__clone)) { 589e5863d9aSMike Snitzer /* ENOMEM, requeue */ 5902eff1924SMike Snitzer clear_request_fn_mpio(m, map_context); 591e5863d9aSMike Snitzer return r; 5924c6dd53dSMike Snitzer } 593e5863d9aSMike Snitzer (*__clone)->bio = (*__clone)->biotail = NULL; 594e5863d9aSMike Snitzer (*__clone)->rq_disk = bdev->bd_disk; 595e5863d9aSMike Snitzer (*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT; 596e5863d9aSMike Snitzer } 5972eb6e1e3SKeith Busch 598e8099177SHannes Reinecke if (pgpath->pg->ps.type->start_io) 599e8099177SHannes Reinecke pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 600e8099177SHannes Reinecke &pgpath->path, 601e8099177SHannes Reinecke nr_bytes); 6022eb6e1e3SKeith Busch return DM_MAPIO_REMAPPED; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 605e5863d9aSMike Snitzer static int multipath_map(struct dm_target *ti, struct request *clone, 606e5863d9aSMike Snitzer union map_info *map_context) 607e5863d9aSMike Snitzer { 608e5863d9aSMike Snitzer return __multipath_map(ti, clone, map_context, NULL, NULL); 609e5863d9aSMike Snitzer } 610e5863d9aSMike Snitzer 611e5863d9aSMike Snitzer static int multipath_clone_and_map(struct dm_target *ti, struct request *rq, 612e5863d9aSMike Snitzer union map_info *map_context, 613e5863d9aSMike Snitzer struct request **clone) 614e5863d9aSMike Snitzer { 615e5863d9aSMike Snitzer return __multipath_map(ti, NULL, map_context, rq, clone); 616e5863d9aSMike Snitzer } 617e5863d9aSMike Snitzer 618e5863d9aSMike Snitzer static void multipath_release_clone(struct request *clone) 619e5863d9aSMike Snitzer { 62078ce23b5SMike Snitzer blk_mq_free_request(clone); 621e5863d9aSMike Snitzer } 622e5863d9aSMike Snitzer 6231da177e4SLinus Torvalds /* 62476e33fe4SMike Snitzer * Map cloned bios (bio-based multipath) 62576e33fe4SMike Snitzer */ 62676e33fe4SMike Snitzer static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio) 62776e33fe4SMike Snitzer { 62876e33fe4SMike Snitzer size_t nr_bytes = bio->bi_iter.bi_size; 62976e33fe4SMike Snitzer struct pgpath *pgpath; 63076e33fe4SMike Snitzer unsigned long flags; 63176e33fe4SMike Snitzer bool queue_io; 63276e33fe4SMike Snitzer 63376e33fe4SMike Snitzer /* Do we need to select a new pgpath? */ 63476e33fe4SMike Snitzer pgpath = lockless_dereference(m->current_pgpath); 63576e33fe4SMike Snitzer queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags); 63676e33fe4SMike Snitzer if (!pgpath || !queue_io) 63776e33fe4SMike Snitzer pgpath = choose_pgpath(m, nr_bytes); 63876e33fe4SMike Snitzer 63976e33fe4SMike Snitzer if ((pgpath && queue_io) || 64076e33fe4SMike Snitzer (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) { 64176e33fe4SMike Snitzer /* Queue for the daemon to resubmit */ 64276e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 64376e33fe4SMike Snitzer bio_list_add(&m->queued_bios, bio); 64476e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 64576e33fe4SMike Snitzer /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */ 64676e33fe4SMike Snitzer if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 64776e33fe4SMike Snitzer pg_init_all_paths(m); 64876e33fe4SMike Snitzer else if (!queue_io) 64976e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 65076e33fe4SMike Snitzer return DM_MAPIO_SUBMITTED; 65176e33fe4SMike Snitzer } 65276e33fe4SMike Snitzer 65376e33fe4SMike Snitzer if (!pgpath) { 65476e33fe4SMike Snitzer if (!must_push_back_bio(m)) 65576e33fe4SMike Snitzer return -EIO; 65676e33fe4SMike Snitzer return DM_MAPIO_REQUEUE; 65776e33fe4SMike Snitzer } 65876e33fe4SMike Snitzer 65976e33fe4SMike Snitzer mpio->pgpath = pgpath; 66076e33fe4SMike Snitzer mpio->nr_bytes = nr_bytes; 66176e33fe4SMike Snitzer 66276e33fe4SMike Snitzer bio->bi_error = 0; 66376e33fe4SMike Snitzer bio->bi_bdev = pgpath->path.dev->bdev; 6641eff9d32SJens Axboe bio->bi_opf |= REQ_FAILFAST_TRANSPORT; 66576e33fe4SMike Snitzer 66676e33fe4SMike Snitzer if (pgpath->pg->ps.type->start_io) 66776e33fe4SMike Snitzer pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 66876e33fe4SMike Snitzer &pgpath->path, 66976e33fe4SMike Snitzer nr_bytes); 67076e33fe4SMike Snitzer return DM_MAPIO_REMAPPED; 67176e33fe4SMike Snitzer } 67276e33fe4SMike Snitzer 67376e33fe4SMike Snitzer static int multipath_map_bio(struct dm_target *ti, struct bio *bio) 67476e33fe4SMike Snitzer { 67576e33fe4SMike Snitzer struct multipath *m = ti->private; 676bf661be1SMike Snitzer struct dm_mpath_io *mpio = NULL; 677bf661be1SMike Snitzer 678bf661be1SMike Snitzer multipath_init_per_bio_data(bio, &mpio, NULL); 67976e33fe4SMike Snitzer 68076e33fe4SMike Snitzer return __multipath_map_bio(m, bio, mpio); 68176e33fe4SMike Snitzer } 68276e33fe4SMike Snitzer 68376e33fe4SMike Snitzer static void process_queued_bios_list(struct multipath *m) 68476e33fe4SMike Snitzer { 685e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) 68676e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 68776e33fe4SMike Snitzer } 68876e33fe4SMike Snitzer 68976e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work) 69076e33fe4SMike Snitzer { 69176e33fe4SMike Snitzer int r; 69276e33fe4SMike Snitzer unsigned long flags; 69376e33fe4SMike Snitzer struct bio *bio; 69476e33fe4SMike Snitzer struct bio_list bios; 69576e33fe4SMike Snitzer struct blk_plug plug; 69676e33fe4SMike Snitzer struct multipath *m = 69776e33fe4SMike Snitzer container_of(work, struct multipath, process_queued_bios); 69876e33fe4SMike Snitzer 69976e33fe4SMike Snitzer bio_list_init(&bios); 70076e33fe4SMike Snitzer 70176e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 70276e33fe4SMike Snitzer 70376e33fe4SMike Snitzer if (bio_list_empty(&m->queued_bios)) { 70476e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 70576e33fe4SMike Snitzer return; 70676e33fe4SMike Snitzer } 70776e33fe4SMike Snitzer 70876e33fe4SMike Snitzer bio_list_merge(&bios, &m->queued_bios); 70976e33fe4SMike Snitzer bio_list_init(&m->queued_bios); 71076e33fe4SMike Snitzer 71176e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 71276e33fe4SMike Snitzer 71376e33fe4SMike Snitzer blk_start_plug(&plug); 71476e33fe4SMike Snitzer while ((bio = bio_list_pop(&bios))) { 71576e33fe4SMike Snitzer r = __multipath_map_bio(m, bio, get_mpio_from_bio(bio)); 71676e33fe4SMike Snitzer if (r < 0 || r == DM_MAPIO_REQUEUE) { 71776e33fe4SMike Snitzer bio->bi_error = r; 71876e33fe4SMike Snitzer bio_endio(bio); 71976e33fe4SMike Snitzer } else if (r == DM_MAPIO_REMAPPED) 72076e33fe4SMike Snitzer generic_make_request(bio); 72176e33fe4SMike Snitzer } 72276e33fe4SMike Snitzer blk_finish_plug(&plug); 72376e33fe4SMike Snitzer } 72476e33fe4SMike Snitzer 72576e33fe4SMike Snitzer /* 7261da177e4SLinus Torvalds * If we run out of usable paths, should we queue I/O or error it? 7271da177e4SLinus Torvalds */ 728be7d31ccSMike Snitzer static int queue_if_no_path(struct multipath *m, bool queue_if_no_path, 729be7d31ccSMike Snitzer bool save_old_value) 7301da177e4SLinus Torvalds { 7311da177e4SLinus Torvalds unsigned long flags; 7321da177e4SLinus Torvalds 7331da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 7341da177e4SLinus Torvalds 735518257b1SMike Snitzer if (save_old_value) { 736518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 737518257b1SMike Snitzer set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 738485ef69eSAlasdair G Kergon else 739518257b1SMike Snitzer clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 740518257b1SMike Snitzer } else { 741518257b1SMike Snitzer if (queue_if_no_path) 742518257b1SMike Snitzer set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 743518257b1SMike Snitzer else 744518257b1SMike Snitzer clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 745518257b1SMike Snitzer } 746518257b1SMike Snitzer if (queue_if_no_path) 747518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 748518257b1SMike Snitzer else 749518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 750518257b1SMike Snitzer 7511da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 7521da177e4SLinus Torvalds 75376e33fe4SMike Snitzer if (!queue_if_no_path) { 75463d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 75576e33fe4SMike Snitzer process_queued_bios_list(m); 75676e33fe4SMike Snitzer } 75763d832c3SHannes Reinecke 7581da177e4SLinus Torvalds return 0; 7591da177e4SLinus Torvalds } 7601da177e4SLinus Torvalds 7611da177e4SLinus Torvalds /* 7621da177e4SLinus Torvalds * An event is triggered whenever a path is taken out of use. 7631da177e4SLinus Torvalds * Includes path failure and PG bypass. 7641da177e4SLinus Torvalds */ 765c4028958SDavid Howells static void trigger_event(struct work_struct *work) 7661da177e4SLinus Torvalds { 767c4028958SDavid Howells struct multipath *m = 768c4028958SDavid Howells container_of(work, struct multipath, trigger_event); 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds dm_table_event(m->ti->table); 7711da177e4SLinus Torvalds } 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds /*----------------------------------------------------------------- 7741da177e4SLinus Torvalds * Constructor/argument parsing: 7751da177e4SLinus Torvalds * <#multipath feature args> [<arg>]* 7761da177e4SLinus Torvalds * <#hw_handler args> [hw_handler [<arg>]*] 7771da177e4SLinus Torvalds * <#priority groups> 7781da177e4SLinus Torvalds * <initial priority group> 7791da177e4SLinus Torvalds * [<selector> <#selector args> [<arg>]* 7801da177e4SLinus Torvalds * <#paths> <#per-path selector args> 7811da177e4SLinus Torvalds * [<path> [<arg>]* ]+ ]+ 7821da177e4SLinus Torvalds *---------------------------------------------------------------*/ 783498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg, 7841da177e4SLinus Torvalds struct dm_target *ti) 7851da177e4SLinus Torvalds { 7861da177e4SLinus Torvalds int r; 7871da177e4SLinus Torvalds struct path_selector_type *pst; 7881da177e4SLinus Torvalds unsigned ps_argc; 7891da177e4SLinus Torvalds 790498f0103SMike Snitzer static struct dm_arg _args[] = { 79172d94861SAlasdair G Kergon {0, 1024, "invalid number of path selector args"}, 7921da177e4SLinus Torvalds }; 7931da177e4SLinus Torvalds 794498f0103SMike Snitzer pst = dm_get_path_selector(dm_shift_arg(as)); 7951da177e4SLinus Torvalds if (!pst) { 79672d94861SAlasdair G Kergon ti->error = "unknown path selector type"; 7971da177e4SLinus Torvalds return -EINVAL; 7981da177e4SLinus Torvalds } 7991da177e4SLinus Torvalds 800498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &ps_argc, &ti->error); 801371b2e34SMikulas Patocka if (r) { 802371b2e34SMikulas Patocka dm_put_path_selector(pst); 8031da177e4SLinus Torvalds return -EINVAL; 804371b2e34SMikulas Patocka } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds r = pst->create(&pg->ps, ps_argc, as->argv); 8071da177e4SLinus Torvalds if (r) { 8081da177e4SLinus Torvalds dm_put_path_selector(pst); 80972d94861SAlasdair G Kergon ti->error = "path selector constructor failed"; 8101da177e4SLinus Torvalds return r; 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds pg->ps.type = pst; 814498f0103SMike Snitzer dm_consume_args(as, ps_argc); 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds return 0; 8171da177e4SLinus Torvalds } 8181da177e4SLinus Torvalds 819498f0103SMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps, 8201da177e4SLinus Torvalds struct dm_target *ti) 8211da177e4SLinus Torvalds { 8221da177e4SLinus Torvalds int r; 8231da177e4SLinus Torvalds struct pgpath *p; 824ae11b1b3SHannes Reinecke struct multipath *m = ti->private; 825a58a935dSMike Snitzer struct request_queue *q = NULL; 826a58a935dSMike Snitzer const char *attached_handler_name; 8271da177e4SLinus Torvalds 8281da177e4SLinus Torvalds /* we need at least a path arg */ 8291da177e4SLinus Torvalds if (as->argc < 1) { 83072d94861SAlasdair G Kergon ti->error = "no device given"; 83101460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 8321da177e4SLinus Torvalds } 8331da177e4SLinus Torvalds 8341da177e4SLinus Torvalds p = alloc_pgpath(); 8351da177e4SLinus Torvalds if (!p) 83601460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 8371da177e4SLinus Torvalds 838498f0103SMike Snitzer r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table), 8398215d6ecSNikanth Karthikesan &p->path.dev); 8401da177e4SLinus Torvalds if (r) { 84172d94861SAlasdair G Kergon ti->error = "error getting device"; 8421da177e4SLinus Torvalds goto bad; 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds 845518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name) 846a58a935dSMike Snitzer q = bdev_get_queue(p->path.dev->bdev); 847a0cf7ea9SHannes Reinecke 848518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) { 8491bab0de0SChristoph Hellwig retain: 850a58a935dSMike Snitzer attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL); 851a58a935dSMike Snitzer if (attached_handler_name) { 852a58a935dSMike Snitzer /* 853a58a935dSMike Snitzer * Reset hw_handler_name to match the attached handler 854a58a935dSMike Snitzer * and clear any hw_handler_params associated with the 855a58a935dSMike Snitzer * ignored handler. 856a58a935dSMike Snitzer * 857a58a935dSMike Snitzer * NB. This modifies the table line to show the actual 858a58a935dSMike Snitzer * handler instead of the original table passed in. 859a58a935dSMike Snitzer */ 860a58a935dSMike Snitzer kfree(m->hw_handler_name); 861a58a935dSMike Snitzer m->hw_handler_name = attached_handler_name; 862a58a935dSMike Snitzer 863a58a935dSMike Snitzer kfree(m->hw_handler_params); 864a58a935dSMike Snitzer m->hw_handler_params = NULL; 865a58a935dSMike Snitzer } 866a58a935dSMike Snitzer } 867a58a935dSMike Snitzer 868a58a935dSMike Snitzer if (m->hw_handler_name) { 869a0cf7ea9SHannes Reinecke r = scsi_dh_attach(q, m->hw_handler_name); 870a0cf7ea9SHannes Reinecke if (r == -EBUSY) { 8711bab0de0SChristoph Hellwig char b[BDEVNAME_SIZE]; 872a0cf7ea9SHannes Reinecke 8731bab0de0SChristoph Hellwig printk(KERN_INFO "dm-mpath: retaining handler on device %s\n", 8741bab0de0SChristoph Hellwig bdevname(p->path.dev->bdev, b)); 8751bab0de0SChristoph Hellwig goto retain; 8761bab0de0SChristoph Hellwig } 877ae11b1b3SHannes Reinecke if (r < 0) { 878a0cf7ea9SHannes Reinecke ti->error = "error attaching hardware handler"; 879ae11b1b3SHannes Reinecke dm_put_device(ti, p->path.dev); 880ae11b1b3SHannes Reinecke goto bad; 881ae11b1b3SHannes Reinecke } 8822bfd2e13SChandra Seetharaman 8832bfd2e13SChandra Seetharaman if (m->hw_handler_params) { 8842bfd2e13SChandra Seetharaman r = scsi_dh_set_params(q, m->hw_handler_params); 8852bfd2e13SChandra Seetharaman if (r < 0) { 8862bfd2e13SChandra Seetharaman ti->error = "unable to set hardware " 8872bfd2e13SChandra Seetharaman "handler parameters"; 8882bfd2e13SChandra Seetharaman dm_put_device(ti, p->path.dev); 8892bfd2e13SChandra Seetharaman goto bad; 8902bfd2e13SChandra Seetharaman } 8912bfd2e13SChandra Seetharaman } 892ae11b1b3SHannes Reinecke } 893ae11b1b3SHannes Reinecke 8941da177e4SLinus Torvalds r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); 8951da177e4SLinus Torvalds if (r) { 8961da177e4SLinus Torvalds dm_put_device(ti, p->path.dev); 8971da177e4SLinus Torvalds goto bad; 8981da177e4SLinus Torvalds } 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds return p; 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds bad: 9031da177e4SLinus Torvalds free_pgpath(p); 90401460f35SBenjamin Marzinski return ERR_PTR(r); 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 907498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as, 90828f16c20SMicha³ Miros³aw struct multipath *m) 9091da177e4SLinus Torvalds { 910498f0103SMike Snitzer static struct dm_arg _args[] = { 91172d94861SAlasdair G Kergon {1, 1024, "invalid number of paths"}, 91272d94861SAlasdair G Kergon {0, 1024, "invalid number of selector args"} 9131da177e4SLinus Torvalds }; 9141da177e4SLinus Torvalds 9151da177e4SLinus Torvalds int r; 916498f0103SMike Snitzer unsigned i, nr_selector_args, nr_args; 9171da177e4SLinus Torvalds struct priority_group *pg; 91828f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds if (as->argc < 2) { 9211da177e4SLinus Torvalds as->argc = 0; 92201460f35SBenjamin Marzinski ti->error = "not enough priority group arguments"; 92301460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 9241da177e4SLinus Torvalds } 9251da177e4SLinus Torvalds 9261da177e4SLinus Torvalds pg = alloc_priority_group(); 9271da177e4SLinus Torvalds if (!pg) { 92872d94861SAlasdair G Kergon ti->error = "couldn't allocate priority group"; 92901460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 9301da177e4SLinus Torvalds } 9311da177e4SLinus Torvalds pg->m = m; 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds r = parse_path_selector(as, pg, ti); 9341da177e4SLinus Torvalds if (r) 9351da177e4SLinus Torvalds goto bad; 9361da177e4SLinus Torvalds 9371da177e4SLinus Torvalds /* 9381da177e4SLinus Torvalds * read the paths 9391da177e4SLinus Torvalds */ 940498f0103SMike Snitzer r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error); 9411da177e4SLinus Torvalds if (r) 9421da177e4SLinus Torvalds goto bad; 9431da177e4SLinus Torvalds 944498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error); 9451da177e4SLinus Torvalds if (r) 9461da177e4SLinus Torvalds goto bad; 9471da177e4SLinus Torvalds 948498f0103SMike Snitzer nr_args = 1 + nr_selector_args; 9491da177e4SLinus Torvalds for (i = 0; i < pg->nr_pgpaths; i++) { 9501da177e4SLinus Torvalds struct pgpath *pgpath; 951498f0103SMike Snitzer struct dm_arg_set path_args; 9521da177e4SLinus Torvalds 953498f0103SMike Snitzer if (as->argc < nr_args) { 954148acff6SMikulas Patocka ti->error = "not enough path parameters"; 9556bbf79a1SAlasdair G Kergon r = -EINVAL; 9561da177e4SLinus Torvalds goto bad; 957148acff6SMikulas Patocka } 9581da177e4SLinus Torvalds 959498f0103SMike Snitzer path_args.argc = nr_args; 9601da177e4SLinus Torvalds path_args.argv = as->argv; 9611da177e4SLinus Torvalds 9621da177e4SLinus Torvalds pgpath = parse_path(&path_args, &pg->ps, ti); 96301460f35SBenjamin Marzinski if (IS_ERR(pgpath)) { 96401460f35SBenjamin Marzinski r = PTR_ERR(pgpath); 9651da177e4SLinus Torvalds goto bad; 96601460f35SBenjamin Marzinski } 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds pgpath->pg = pg; 9691da177e4SLinus Torvalds list_add_tail(&pgpath->list, &pg->pgpaths); 970498f0103SMike Snitzer dm_consume_args(as, nr_args); 9711da177e4SLinus Torvalds } 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds return pg; 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds bad: 9761da177e4SLinus Torvalds free_priority_group(pg, ti); 97701460f35SBenjamin Marzinski return ERR_PTR(r); 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 980498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m) 9811da177e4SLinus Torvalds { 9821da177e4SLinus Torvalds unsigned hw_argc; 9832bfd2e13SChandra Seetharaman int ret; 98428f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 9851da177e4SLinus Torvalds 986498f0103SMike Snitzer static struct dm_arg _args[] = { 98772d94861SAlasdair G Kergon {0, 1024, "invalid number of hardware handler args"}, 9881da177e4SLinus Torvalds }; 9891da177e4SLinus Torvalds 990498f0103SMike Snitzer if (dm_read_arg_group(_args, as, &hw_argc, &ti->error)) 9911da177e4SLinus Torvalds return -EINVAL; 9921da177e4SLinus Torvalds 9931da177e4SLinus Torvalds if (!hw_argc) 9941da177e4SLinus Torvalds return 0; 9951da177e4SLinus Torvalds 996e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) { 99776e33fe4SMike Snitzer dm_consume_args(as, hw_argc); 99876e33fe4SMike Snitzer DMERR("bio-based multipath doesn't allow hardware handler args"); 99976e33fe4SMike Snitzer return 0; 100076e33fe4SMike Snitzer } 100176e33fe4SMike Snitzer 1002498f0103SMike Snitzer m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL); 100314e98c5cSChandra Seetharaman 10042bfd2e13SChandra Seetharaman if (hw_argc > 1) { 10052bfd2e13SChandra Seetharaman char *p; 10062bfd2e13SChandra Seetharaman int i, j, len = 4; 10072bfd2e13SChandra Seetharaman 10082bfd2e13SChandra Seetharaman for (i = 0; i <= hw_argc - 2; i++) 10092bfd2e13SChandra Seetharaman len += strlen(as->argv[i]) + 1; 10102bfd2e13SChandra Seetharaman p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); 10112bfd2e13SChandra Seetharaman if (!p) { 10122bfd2e13SChandra Seetharaman ti->error = "memory allocation failed"; 10132bfd2e13SChandra Seetharaman ret = -ENOMEM; 10142bfd2e13SChandra Seetharaman goto fail; 10152bfd2e13SChandra Seetharaman } 10162bfd2e13SChandra Seetharaman j = sprintf(p, "%d", hw_argc - 1); 10172bfd2e13SChandra Seetharaman for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1) 10182bfd2e13SChandra Seetharaman j = sprintf(p, "%s", as->argv[i]); 10192bfd2e13SChandra Seetharaman } 1020498f0103SMike Snitzer dm_consume_args(as, hw_argc - 1); 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds return 0; 10232bfd2e13SChandra Seetharaman fail: 10242bfd2e13SChandra Seetharaman kfree(m->hw_handler_name); 10252bfd2e13SChandra Seetharaman m->hw_handler_name = NULL; 10262bfd2e13SChandra Seetharaman return ret; 10271da177e4SLinus Torvalds } 10281da177e4SLinus Torvalds 1029498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m) 10301da177e4SLinus Torvalds { 10311da177e4SLinus Torvalds int r; 10321da177e4SLinus Torvalds unsigned argc; 103328f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 1034498f0103SMike Snitzer const char *arg_name; 10351da177e4SLinus Torvalds 1036498f0103SMike Snitzer static struct dm_arg _args[] = { 1037e83068a5SMike Snitzer {0, 8, "invalid number of feature args"}, 1038c9e45581SDave Wysochanski {1, 50, "pg_init_retries must be between 1 and 50"}, 10394e2d19e4SChandra Seetharaman {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"}, 10401da177e4SLinus Torvalds }; 10411da177e4SLinus Torvalds 1042498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &argc, &ti->error); 10431da177e4SLinus Torvalds if (r) 10441da177e4SLinus Torvalds return -EINVAL; 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds if (!argc) 10471da177e4SLinus Torvalds return 0; 10481da177e4SLinus Torvalds 1049c9e45581SDave Wysochanski do { 1050498f0103SMike Snitzer arg_name = dm_shift_arg(as); 1051c9e45581SDave Wysochanski argc--; 1052c9e45581SDave Wysochanski 1053498f0103SMike Snitzer if (!strcasecmp(arg_name, "queue_if_no_path")) { 1054be7d31ccSMike Snitzer r = queue_if_no_path(m, true, false); 1055c9e45581SDave Wysochanski continue; 10561da177e4SLinus Torvalds } 1057c9e45581SDave Wysochanski 1058a58a935dSMike Snitzer if (!strcasecmp(arg_name, "retain_attached_hw_handler")) { 1059518257b1SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 1060a58a935dSMike Snitzer continue; 1061a58a935dSMike Snitzer } 1062a58a935dSMike Snitzer 1063498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_retries") && 1064c9e45581SDave Wysochanski (argc >= 1)) { 1065498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error); 1066c9e45581SDave Wysochanski argc--; 1067c9e45581SDave Wysochanski continue; 1068c9e45581SDave Wysochanski } 1069c9e45581SDave Wysochanski 1070498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_delay_msecs") && 10714e2d19e4SChandra Seetharaman (argc >= 1)) { 1072498f0103SMike Snitzer r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error); 10734e2d19e4SChandra Seetharaman argc--; 10744e2d19e4SChandra Seetharaman continue; 10754e2d19e4SChandra Seetharaman } 10764e2d19e4SChandra Seetharaman 1077e83068a5SMike Snitzer if (!strcasecmp(arg_name, "queue_mode") && 1078e83068a5SMike Snitzer (argc >= 1)) { 1079e83068a5SMike Snitzer const char *queue_mode_name = dm_shift_arg(as); 1080e83068a5SMike Snitzer 1081e83068a5SMike Snitzer if (!strcasecmp(queue_mode_name, "bio")) 1082e83068a5SMike Snitzer m->queue_mode = DM_TYPE_BIO_BASED; 1083e83068a5SMike Snitzer else if (!strcasecmp(queue_mode_name, "rq")) 1084e83068a5SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 1085e83068a5SMike Snitzer else if (!strcasecmp(queue_mode_name, "mq")) 1086e83068a5SMike Snitzer m->queue_mode = DM_TYPE_MQ_REQUEST_BASED; 1087e83068a5SMike Snitzer else { 1088e83068a5SMike Snitzer ti->error = "Unknown 'queue_mode' requested"; 1089e83068a5SMike Snitzer r = -EINVAL; 1090e83068a5SMike Snitzer } 1091e83068a5SMike Snitzer argc--; 1092e83068a5SMike Snitzer continue; 1093e83068a5SMike Snitzer } 1094e83068a5SMike Snitzer 1095c9e45581SDave Wysochanski ti->error = "Unrecognised multipath feature request"; 1096c9e45581SDave Wysochanski r = -EINVAL; 1097c9e45581SDave Wysochanski } while (argc && !r); 1098c9e45581SDave Wysochanski 1099c9e45581SDave Wysochanski return r; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 1102e83068a5SMike Snitzer static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv) 11031da177e4SLinus Torvalds { 1104498f0103SMike Snitzer /* target arguments */ 1105498f0103SMike Snitzer static struct dm_arg _args[] = { 1106a490a07aSMike Snitzer {0, 1024, "invalid number of priority groups"}, 1107a490a07aSMike Snitzer {0, 1024, "invalid initial priority group number"}, 11081da177e4SLinus Torvalds }; 11091da177e4SLinus Torvalds 11101da177e4SLinus Torvalds int r; 11111da177e4SLinus Torvalds struct multipath *m; 1112498f0103SMike Snitzer struct dm_arg_set as; 11131da177e4SLinus Torvalds unsigned pg_count = 0; 11141da177e4SLinus Torvalds unsigned next_pg_num; 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds as.argc = argc; 11171da177e4SLinus Torvalds as.argv = argv; 11181da177e4SLinus Torvalds 1119e83068a5SMike Snitzer m = alloc_multipath(ti); 11201da177e4SLinus Torvalds if (!m) { 112172d94861SAlasdair G Kergon ti->error = "can't allocate multipath"; 11221da177e4SLinus Torvalds return -EINVAL; 11231da177e4SLinus Torvalds } 11241da177e4SLinus Torvalds 112528f16c20SMicha³ Miros³aw r = parse_features(&as, m); 11261da177e4SLinus Torvalds if (r) 11271da177e4SLinus Torvalds goto bad; 11281da177e4SLinus Torvalds 1129e83068a5SMike Snitzer r = alloc_multipath_stage2(ti, m); 1130e83068a5SMike Snitzer if (r) 1131e83068a5SMike Snitzer goto bad; 1132e83068a5SMike Snitzer 113328f16c20SMicha³ Miros³aw r = parse_hw_handler(&as, m); 11341da177e4SLinus Torvalds if (r) 11351da177e4SLinus Torvalds goto bad; 11361da177e4SLinus Torvalds 1137498f0103SMike Snitzer r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error); 11381da177e4SLinus Torvalds if (r) 11391da177e4SLinus Torvalds goto bad; 11401da177e4SLinus Torvalds 1141498f0103SMike Snitzer r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error); 11421da177e4SLinus Torvalds if (r) 11431da177e4SLinus Torvalds goto bad; 11441da177e4SLinus Torvalds 1145a490a07aSMike Snitzer if ((!m->nr_priority_groups && next_pg_num) || 1146a490a07aSMike Snitzer (m->nr_priority_groups && !next_pg_num)) { 1147a490a07aSMike Snitzer ti->error = "invalid initial priority group"; 1148a490a07aSMike Snitzer r = -EINVAL; 1149a490a07aSMike Snitzer goto bad; 1150a490a07aSMike Snitzer } 1151a490a07aSMike Snitzer 11521da177e4SLinus Torvalds /* parse the priority groups */ 11531da177e4SLinus Torvalds while (as.argc) { 11541da177e4SLinus Torvalds struct priority_group *pg; 115591e968aaSMike Snitzer unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths); 11561da177e4SLinus Torvalds 115728f16c20SMicha³ Miros³aw pg = parse_priority_group(&as, m); 115801460f35SBenjamin Marzinski if (IS_ERR(pg)) { 115901460f35SBenjamin Marzinski r = PTR_ERR(pg); 11601da177e4SLinus Torvalds goto bad; 11611da177e4SLinus Torvalds } 11621da177e4SLinus Torvalds 116391e968aaSMike Snitzer nr_valid_paths += pg->nr_pgpaths; 116491e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, nr_valid_paths); 116591e968aaSMike Snitzer 11661da177e4SLinus Torvalds list_add_tail(&pg->list, &m->priority_groups); 11671da177e4SLinus Torvalds pg_count++; 11681da177e4SLinus Torvalds pg->pg_num = pg_count; 11691da177e4SLinus Torvalds if (!--next_pg_num) 11701da177e4SLinus Torvalds m->next_pg = pg; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds 11731da177e4SLinus Torvalds if (pg_count != m->nr_priority_groups) { 117472d94861SAlasdair G Kergon ti->error = "priority group count mismatch"; 11751da177e4SLinus Torvalds r = -EINVAL; 11761da177e4SLinus Torvalds goto bad; 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds 117955a62eefSAlasdair G Kergon ti->num_flush_bios = 1; 118055a62eefSAlasdair G Kergon ti->num_discard_bios = 1; 1181042bcef8SMike Snitzer ti->num_write_same_bios = 1; 1182e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) 1183bf661be1SMike Snitzer ti->per_io_data_size = multipath_per_bio_data_size(); 1184e83068a5SMike Snitzer else if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED) 11858637a6bfSMike Snitzer ti->per_io_data_size = sizeof(struct dm_mpath_io); 11868627921fSMikulas Patocka 11871da177e4SLinus Torvalds return 0; 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds bad: 11901da177e4SLinus Torvalds free_multipath(m); 11911da177e4SLinus Torvalds return r; 11921da177e4SLinus Torvalds } 11931da177e4SLinus Torvalds 11942bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m) 11952bded7bdSKiyoshi Ueda { 11962bded7bdSKiyoshi Ueda DECLARE_WAITQUEUE(wait, current); 11972bded7bdSKiyoshi Ueda 11982bded7bdSKiyoshi Ueda add_wait_queue(&m->pg_init_wait, &wait); 11992bded7bdSKiyoshi Ueda 12002bded7bdSKiyoshi Ueda while (1) { 12012bded7bdSKiyoshi Ueda set_current_state(TASK_UNINTERRUPTIBLE); 12022bded7bdSKiyoshi Ueda 120391e968aaSMike Snitzer if (!atomic_read(&m->pg_init_in_progress)) 12042bded7bdSKiyoshi Ueda break; 12052bded7bdSKiyoshi Ueda 12062bded7bdSKiyoshi Ueda io_schedule(); 12072bded7bdSKiyoshi Ueda } 12082bded7bdSKiyoshi Ueda set_current_state(TASK_RUNNING); 12092bded7bdSKiyoshi Ueda 12102bded7bdSKiyoshi Ueda remove_wait_queue(&m->pg_init_wait, &wait); 12112bded7bdSKiyoshi Ueda } 12122bded7bdSKiyoshi Ueda 12132bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m) 12141da177e4SLinus Torvalds { 1215518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1216518257b1SMike Snitzer smp_mb__after_atomic(); 1217954a73d5SShiva Krishna Merla 1218bab7cfc7SChandra Seetharaman flush_workqueue(kmpath_handlerd); 12192bded7bdSKiyoshi Ueda multipath_wait_for_pg_init_completion(m); 1220a044d016SAlasdair G Kergon flush_workqueue(kmultipathd); 122143829731STejun Heo flush_work(&m->trigger_event); 1222954a73d5SShiva Krishna Merla 1223518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1224518257b1SMike Snitzer smp_mb__after_atomic(); 12256df400abSKiyoshi Ueda } 12266df400abSKiyoshi Ueda 12276df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti) 12286df400abSKiyoshi Ueda { 12296df400abSKiyoshi Ueda struct multipath *m = ti->private; 12306df400abSKiyoshi Ueda 12312bded7bdSKiyoshi Ueda flush_multipath_work(m); 12321da177e4SLinus Torvalds free_multipath(m); 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds /* 12361da177e4SLinus Torvalds * Take a path out of use. 12371da177e4SLinus Torvalds */ 12381da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath) 12391da177e4SLinus Torvalds { 12401da177e4SLinus Torvalds unsigned long flags; 12411da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 12421da177e4SLinus Torvalds 12431da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 12441da177e4SLinus Torvalds 12456680073dSKiyoshi Ueda if (!pgpath->is_active) 12461da177e4SLinus Torvalds goto out; 12471da177e4SLinus Torvalds 124872d94861SAlasdair G Kergon DMWARN("Failing path %s.", pgpath->path.dev->name); 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); 1251be7d31ccSMike Snitzer pgpath->is_active = false; 12521da177e4SLinus Torvalds pgpath->fail_count++; 12531da177e4SLinus Torvalds 125491e968aaSMike Snitzer atomic_dec(&m->nr_valid_paths); 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds if (pgpath == m->current_pgpath) 12571da177e4SLinus Torvalds m->current_pgpath = NULL; 12581da177e4SLinus Torvalds 1259b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti, 126091e968aaSMike Snitzer pgpath->path.dev->name, atomic_read(&m->nr_valid_paths)); 1261b15546f9SMike Anderson 1262fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 12631da177e4SLinus Torvalds 12641da177e4SLinus Torvalds out: 12651da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds return 0; 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds 12701da177e4SLinus Torvalds /* 12711da177e4SLinus Torvalds * Reinstate a previously-failed path 12721da177e4SLinus Torvalds */ 12731da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath) 12741da177e4SLinus Torvalds { 127563d832c3SHannes Reinecke int r = 0, run_queue = 0; 12761da177e4SLinus Torvalds unsigned long flags; 12771da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 127891e968aaSMike Snitzer unsigned nr_valid_paths; 12791da177e4SLinus Torvalds 12801da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 12811da177e4SLinus Torvalds 12826680073dSKiyoshi Ueda if (pgpath->is_active) 12831da177e4SLinus Torvalds goto out; 12841da177e4SLinus Torvalds 1285ec31f3f7SMike Snitzer DMWARN("Reinstating path %s.", pgpath->path.dev->name); 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); 12881da177e4SLinus Torvalds if (r) 12891da177e4SLinus Torvalds goto out; 12901da177e4SLinus Torvalds 1291be7d31ccSMike Snitzer pgpath->is_active = true; 12921da177e4SLinus Torvalds 129391e968aaSMike Snitzer nr_valid_paths = atomic_inc_return(&m->nr_valid_paths); 129491e968aaSMike Snitzer if (nr_valid_paths == 1) { 12951da177e4SLinus Torvalds m->current_pgpath = NULL; 129663d832c3SHannes Reinecke run_queue = 1; 1297e54f77ddSChandra Seetharaman } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) { 12984e2d19e4SChandra Seetharaman if (queue_work(kmpath_handlerd, &pgpath->activate_path.work)) 129991e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 1300e54f77ddSChandra Seetharaman } 13011da177e4SLinus Torvalds 1302b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti, 130391e968aaSMike Snitzer pgpath->path.dev->name, nr_valid_paths); 1304b15546f9SMike Anderson 1305fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds out: 13081da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 130976e33fe4SMike Snitzer if (run_queue) { 131063d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 131176e33fe4SMike Snitzer process_queued_bios_list(m); 131276e33fe4SMike Snitzer } 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds return r; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds /* 13181da177e4SLinus Torvalds * Fail or reinstate all paths that match the provided struct dm_dev. 13191da177e4SLinus Torvalds */ 13201da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev, 13211da177e4SLinus Torvalds action_fn action) 13221da177e4SLinus Torvalds { 132319040c0bSMike Snitzer int r = -EINVAL; 13241da177e4SLinus Torvalds struct pgpath *pgpath; 13251da177e4SLinus Torvalds struct priority_group *pg; 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 13281da177e4SLinus Torvalds list_for_each_entry(pgpath, &pg->pgpaths, list) { 13291da177e4SLinus Torvalds if (pgpath->path.dev == dev) 13301da177e4SLinus Torvalds r = action(pgpath); 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds } 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds return r; 13351da177e4SLinus Torvalds } 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds /* 13381da177e4SLinus Torvalds * Temporarily try to avoid having to use the specified PG 13391da177e4SLinus Torvalds */ 13401da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg, 1341be7d31ccSMike Snitzer bool bypassed) 13421da177e4SLinus Torvalds { 13431da177e4SLinus Torvalds unsigned long flags; 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds pg->bypassed = bypassed; 13481da177e4SLinus Torvalds m->current_pgpath = NULL; 13491da177e4SLinus Torvalds m->current_pg = NULL; 13501da177e4SLinus Torvalds 13511da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13521da177e4SLinus Torvalds 1353fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13541da177e4SLinus Torvalds } 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds /* 13571da177e4SLinus Torvalds * Switch to using the specified PG from the next I/O that gets mapped 13581da177e4SLinus Torvalds */ 13591da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr) 13601da177e4SLinus Torvalds { 13611da177e4SLinus Torvalds struct priority_group *pg; 13621da177e4SLinus Torvalds unsigned pgnum; 13631da177e4SLinus Torvalds unsigned long flags; 136431998ef1SMikulas Patocka char dummy; 13651da177e4SLinus Torvalds 136631998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 13671da177e4SLinus Torvalds (pgnum > m->nr_priority_groups)) { 13681da177e4SLinus Torvalds DMWARN("invalid PG number supplied to switch_pg_num"); 13691da177e4SLinus Torvalds return -EINVAL; 13701da177e4SLinus Torvalds } 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13731da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 1374be7d31ccSMike Snitzer pg->bypassed = false; 13751da177e4SLinus Torvalds if (--pgnum) 13761da177e4SLinus Torvalds continue; 13771da177e4SLinus Torvalds 13781da177e4SLinus Torvalds m->current_pgpath = NULL; 13791da177e4SLinus Torvalds m->current_pg = NULL; 13801da177e4SLinus Torvalds m->next_pg = pg; 13811da177e4SLinus Torvalds } 13821da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13831da177e4SLinus Torvalds 1384fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13851da177e4SLinus Torvalds return 0; 13861da177e4SLinus Torvalds } 13871da177e4SLinus Torvalds 13881da177e4SLinus Torvalds /* 13891da177e4SLinus Torvalds * Set/clear bypassed status of a PG. 13901da177e4SLinus Torvalds * PGs are numbered upwards from 1 in the order they were declared. 13911da177e4SLinus Torvalds */ 1392be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed) 13931da177e4SLinus Torvalds { 13941da177e4SLinus Torvalds struct priority_group *pg; 13951da177e4SLinus Torvalds unsigned pgnum; 139631998ef1SMikulas Patocka char dummy; 13971da177e4SLinus Torvalds 139831998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 13991da177e4SLinus Torvalds (pgnum > m->nr_priority_groups)) { 14001da177e4SLinus Torvalds DMWARN("invalid PG number supplied to bypass_pg"); 14011da177e4SLinus Torvalds return -EINVAL; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 14051da177e4SLinus Torvalds if (!--pgnum) 14061da177e4SLinus Torvalds break; 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds bypass_pg(m, pg, bypassed); 14101da177e4SLinus Torvalds return 0; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds /* 1414c9e45581SDave Wysochanski * Should we retry pg_init immediately? 1415c9e45581SDave Wysochanski */ 1416be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath) 1417c9e45581SDave Wysochanski { 1418c9e45581SDave Wysochanski unsigned long flags; 1419be7d31ccSMike Snitzer bool limit_reached = false; 1420c9e45581SDave Wysochanski 1421c9e45581SDave Wysochanski spin_lock_irqsave(&m->lock, flags); 1422c9e45581SDave Wysochanski 142391e968aaSMike Snitzer if (atomic_read(&m->pg_init_count) <= m->pg_init_retries && 142491e968aaSMike Snitzer !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 1425518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 1426c9e45581SDave Wysochanski else 1427be7d31ccSMike Snitzer limit_reached = true; 1428c9e45581SDave Wysochanski 1429c9e45581SDave Wysochanski spin_unlock_irqrestore(&m->lock, flags); 1430c9e45581SDave Wysochanski 1431c9e45581SDave Wysochanski return limit_reached; 1432c9e45581SDave Wysochanski } 1433c9e45581SDave Wysochanski 14343ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors) 1435cfae5c9bSChandra Seetharaman { 143683c0d5d5SMoger, Babu struct pgpath *pgpath = data; 1437cfae5c9bSChandra Seetharaman struct priority_group *pg = pgpath->pg; 1438cfae5c9bSChandra Seetharaman struct multipath *m = pg->m; 1439cfae5c9bSChandra Seetharaman unsigned long flags; 1440be7d31ccSMike Snitzer bool delay_retry = false; 1441cfae5c9bSChandra Seetharaman 1442cfae5c9bSChandra Seetharaman /* device or driver problems */ 1443cfae5c9bSChandra Seetharaman switch (errors) { 1444cfae5c9bSChandra Seetharaman case SCSI_DH_OK: 1445cfae5c9bSChandra Seetharaman break; 1446cfae5c9bSChandra Seetharaman case SCSI_DH_NOSYS: 1447cfae5c9bSChandra Seetharaman if (!m->hw_handler_name) { 1448cfae5c9bSChandra Seetharaman errors = 0; 1449cfae5c9bSChandra Seetharaman break; 1450cfae5c9bSChandra Seetharaman } 1451f7b934c8SMoger, Babu DMERR("Could not failover the device: Handler scsi_dh_%s " 1452f7b934c8SMoger, Babu "Error %d.", m->hw_handler_name, errors); 1453cfae5c9bSChandra Seetharaman /* 1454cfae5c9bSChandra Seetharaman * Fail path for now, so we do not ping pong 1455cfae5c9bSChandra Seetharaman */ 1456cfae5c9bSChandra Seetharaman fail_path(pgpath); 1457cfae5c9bSChandra Seetharaman break; 1458cfae5c9bSChandra Seetharaman case SCSI_DH_DEV_TEMP_BUSY: 1459cfae5c9bSChandra Seetharaman /* 1460cfae5c9bSChandra Seetharaman * Probably doing something like FW upgrade on the 1461cfae5c9bSChandra Seetharaman * controller so try the other pg. 1462cfae5c9bSChandra Seetharaman */ 1463be7d31ccSMike Snitzer bypass_pg(m, pg, true); 1464cfae5c9bSChandra Seetharaman break; 1465cfae5c9bSChandra Seetharaman case SCSI_DH_RETRY: 14664e2d19e4SChandra Seetharaman /* Wait before retrying. */ 14674e2d19e4SChandra Seetharaman delay_retry = 1; 1468cfae5c9bSChandra Seetharaman case SCSI_DH_IMM_RETRY: 1469cfae5c9bSChandra Seetharaman case SCSI_DH_RES_TEMP_UNAVAIL: 1470cfae5c9bSChandra Seetharaman if (pg_init_limit_reached(m, pgpath)) 1471cfae5c9bSChandra Seetharaman fail_path(pgpath); 1472cfae5c9bSChandra Seetharaman errors = 0; 1473cfae5c9bSChandra Seetharaman break; 1474ec31f3f7SMike Snitzer case SCSI_DH_DEV_OFFLINED: 1475cfae5c9bSChandra Seetharaman default: 1476cfae5c9bSChandra Seetharaman /* 1477cfae5c9bSChandra Seetharaman * We probably do not want to fail the path for a device 1478cfae5c9bSChandra Seetharaman * error, but this is what the old dm did. In future 1479cfae5c9bSChandra Seetharaman * patches we can do more advanced handling. 1480cfae5c9bSChandra Seetharaman */ 1481cfae5c9bSChandra Seetharaman fail_path(pgpath); 1482cfae5c9bSChandra Seetharaman } 1483cfae5c9bSChandra Seetharaman 1484cfae5c9bSChandra Seetharaman spin_lock_irqsave(&m->lock, flags); 1485cfae5c9bSChandra Seetharaman if (errors) { 1486e54f77ddSChandra Seetharaman if (pgpath == m->current_pgpath) { 1487cfae5c9bSChandra Seetharaman DMERR("Could not failover device. Error %d.", errors); 1488cfae5c9bSChandra Seetharaman m->current_pgpath = NULL; 1489cfae5c9bSChandra Seetharaman m->current_pg = NULL; 1490e54f77ddSChandra Seetharaman } 1491518257b1SMike Snitzer } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 1492be7d31ccSMike Snitzer pg->bypassed = false; 1493cfae5c9bSChandra Seetharaman 149491e968aaSMike Snitzer if (atomic_dec_return(&m->pg_init_in_progress) > 0) 1495d0259bf0SKiyoshi Ueda /* Activations of other paths are still on going */ 1496d0259bf0SKiyoshi Ueda goto out; 1497d0259bf0SKiyoshi Ueda 1498518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 1499518257b1SMike Snitzer if (delay_retry) 1500518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1501518257b1SMike Snitzer else 1502518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1503518257b1SMike Snitzer 15043e9f1be1SHannes Reinecke if (__pg_init_all_paths(m)) 15053e9f1be1SHannes Reinecke goto out; 15063e9f1be1SHannes Reinecke } 1507518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 1508d0259bf0SKiyoshi Ueda 150976e33fe4SMike Snitzer process_queued_bios_list(m); 151076e33fe4SMike Snitzer 15112bded7bdSKiyoshi Ueda /* 15122bded7bdSKiyoshi Ueda * Wake up any thread waiting to suspend. 15132bded7bdSKiyoshi Ueda */ 15142bded7bdSKiyoshi Ueda wake_up(&m->pg_init_wait); 15152bded7bdSKiyoshi Ueda 1516d0259bf0SKiyoshi Ueda out: 1517cfae5c9bSChandra Seetharaman spin_unlock_irqrestore(&m->lock, flags); 1518cfae5c9bSChandra Seetharaman } 1519cfae5c9bSChandra Seetharaman 1520bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work) 1521bab7cfc7SChandra Seetharaman { 1522e54f77ddSChandra Seetharaman struct pgpath *pgpath = 15234e2d19e4SChandra Seetharaman container_of(work, struct pgpath, activate_path.work); 1524*f10e06b7SMike Snitzer struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1525bab7cfc7SChandra Seetharaman 1526*f10e06b7SMike Snitzer if (pgpath->is_active && !blk_queue_dying(q)) 1527*f10e06b7SMike Snitzer scsi_dh_activate(q, pg_init_done, pgpath); 15283a017509SHannes Reinecke else 15293a017509SHannes Reinecke pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED); 1530bab7cfc7SChandra Seetharaman } 1531bab7cfc7SChandra Seetharaman 15327e782af5SHannes Reinecke static int noretry_error(int error) 15337e782af5SHannes Reinecke { 15347e782af5SHannes Reinecke switch (error) { 15357e782af5SHannes Reinecke case -EOPNOTSUPP: 15367e782af5SHannes Reinecke case -EREMOTEIO: 15377e782af5SHannes Reinecke case -EILSEQ: 15387e782af5SHannes Reinecke case -ENODATA: 1539cc9d3c38SJun'ichi Nomura case -ENOSPC: 15407e782af5SHannes Reinecke return 1; 15417e782af5SHannes Reinecke } 15427e782af5SHannes Reinecke 15437e782af5SHannes Reinecke /* Anything else could be a path failure, so should be retried */ 15447e782af5SHannes Reinecke return 0; 15457e782af5SHannes Reinecke } 15467e782af5SHannes Reinecke 15471da177e4SLinus Torvalds /* 15481da177e4SLinus Torvalds * end_io handling 15491da177e4SLinus Torvalds */ 1550f40c67f0SKiyoshi Ueda static int do_end_io(struct multipath *m, struct request *clone, 1551028867acSAlasdair G Kergon int error, struct dm_mpath_io *mpio) 15521da177e4SLinus Torvalds { 1553f40c67f0SKiyoshi Ueda /* 1554f40c67f0SKiyoshi Ueda * We don't queue any clone request inside the multipath target 1555f40c67f0SKiyoshi Ueda * during end I/O handling, since those clone requests don't have 1556f40c67f0SKiyoshi Ueda * bio clones. If we queue them inside the multipath target, 1557f40c67f0SKiyoshi Ueda * we need to make bio clones, that requires memory allocation. 15584cc96131SMike Snitzer * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests 1559f40c67f0SKiyoshi Ueda * don't have bio clones.) 1560f40c67f0SKiyoshi Ueda * Instead of queueing the clone request here, we queue the original 1561f40c67f0SKiyoshi Ueda * request into dm core, which will remake a clone request and 1562f40c67f0SKiyoshi Ueda * clone bios for it and resubmit it later. 1563f40c67f0SKiyoshi Ueda */ 1564f40c67f0SKiyoshi Ueda int r = DM_ENDIO_REQUEUE; 15651da177e4SLinus Torvalds 1566f40c67f0SKiyoshi Ueda if (!error && !clone->errors) 15671da177e4SLinus Torvalds return 0; /* I/O complete */ 15681da177e4SLinus Torvalds 15697eee4ae2SMike Snitzer if (noretry_error(error)) 1570959eb4e5SMike Snitzer return error; 1571959eb4e5SMike Snitzer 1572cfae5c9bSChandra Seetharaman if (mpio->pgpath) 15731da177e4SLinus Torvalds fail_path(mpio->pgpath); 15741da177e4SLinus Torvalds 157591e968aaSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 1576518257b1SMike Snitzer if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 157776e33fe4SMike Snitzer if (!must_push_back_rq(m)) 1578f40c67f0SKiyoshi Ueda r = -EIO; 1579751b2a7dSHannes Reinecke } else { 1580751b2a7dSHannes Reinecke if (error == -EBADE) 1581751b2a7dSHannes Reinecke r = error; 1582751b2a7dSHannes Reinecke } 1583751b2a7dSHannes Reinecke } 15841da177e4SLinus Torvalds 1585f40c67f0SKiyoshi Ueda return r; 15861da177e4SLinus Torvalds } 15871da177e4SLinus Torvalds 1588f40c67f0SKiyoshi Ueda static int multipath_end_io(struct dm_target *ti, struct request *clone, 15891da177e4SLinus Torvalds int error, union map_info *map_context) 15901da177e4SLinus Torvalds { 1591028867acSAlasdair G Kergon struct multipath *m = ti->private; 15922eff1924SMike Snitzer struct dm_mpath_io *mpio = get_mpio(map_context); 1593a71a261fSWei Yongjun struct pgpath *pgpath; 15941da177e4SLinus Torvalds struct path_selector *ps; 15951da177e4SLinus Torvalds int r; 15961da177e4SLinus Torvalds 1597466891f9SJun'ichi Nomura BUG_ON(!mpio); 1598466891f9SJun'ichi Nomura 1599f40c67f0SKiyoshi Ueda r = do_end_io(m, clone, error, mpio); 1600a71a261fSWei Yongjun pgpath = mpio->pgpath; 16011da177e4SLinus Torvalds if (pgpath) { 16021da177e4SLinus Torvalds ps = &pgpath->pg->ps; 16031da177e4SLinus Torvalds if (ps->type->end_io) 160402ab823fSKiyoshi Ueda ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); 16051da177e4SLinus Torvalds } 16062eff1924SMike Snitzer clear_request_fn_mpio(m, map_context); 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds return r; 16091da177e4SLinus Torvalds } 16101da177e4SLinus Torvalds 161176e33fe4SMike Snitzer static int do_end_io_bio(struct multipath *m, struct bio *clone, 161276e33fe4SMike Snitzer int error, struct dm_mpath_io *mpio) 161376e33fe4SMike Snitzer { 161476e33fe4SMike Snitzer unsigned long flags; 161576e33fe4SMike Snitzer 161676e33fe4SMike Snitzer if (!error) 161776e33fe4SMike Snitzer return 0; /* I/O complete */ 161876e33fe4SMike Snitzer 161976e33fe4SMike Snitzer if (noretry_error(error)) 162076e33fe4SMike Snitzer return error; 162176e33fe4SMike Snitzer 162276e33fe4SMike Snitzer if (mpio->pgpath) 162376e33fe4SMike Snitzer fail_path(mpio->pgpath); 162476e33fe4SMike Snitzer 162576e33fe4SMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 162676e33fe4SMike Snitzer if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 162776e33fe4SMike Snitzer if (!must_push_back_bio(m)) 162876e33fe4SMike Snitzer return -EIO; 162976e33fe4SMike Snitzer return DM_ENDIO_REQUEUE; 163076e33fe4SMike Snitzer } else { 163176e33fe4SMike Snitzer if (error == -EBADE) 163276e33fe4SMike Snitzer return error; 163376e33fe4SMike Snitzer } 163476e33fe4SMike Snitzer } 163576e33fe4SMike Snitzer 163676e33fe4SMike Snitzer /* Queue for the daemon to resubmit */ 1637bf661be1SMike Snitzer dm_bio_restore(get_bio_details_from_bio(clone), clone); 163876e33fe4SMike Snitzer 163976e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 164076e33fe4SMike Snitzer bio_list_add(&m->queued_bios, clone); 164176e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 164276e33fe4SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) 164376e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 164476e33fe4SMike Snitzer 164576e33fe4SMike Snitzer return DM_ENDIO_INCOMPLETE; 164676e33fe4SMike Snitzer } 164776e33fe4SMike Snitzer 164876e33fe4SMike Snitzer static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, int error) 164976e33fe4SMike Snitzer { 165076e33fe4SMike Snitzer struct multipath *m = ti->private; 165176e33fe4SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(clone); 165276e33fe4SMike Snitzer struct pgpath *pgpath; 165376e33fe4SMike Snitzer struct path_selector *ps; 165476e33fe4SMike Snitzer int r; 165576e33fe4SMike Snitzer 165676e33fe4SMike Snitzer BUG_ON(!mpio); 165776e33fe4SMike Snitzer 165876e33fe4SMike Snitzer r = do_end_io_bio(m, clone, error, mpio); 165976e33fe4SMike Snitzer pgpath = mpio->pgpath; 166076e33fe4SMike Snitzer if (pgpath) { 166176e33fe4SMike Snitzer ps = &pgpath->pg->ps; 166276e33fe4SMike Snitzer if (ps->type->end_io) 166376e33fe4SMike Snitzer ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); 166476e33fe4SMike Snitzer } 166576e33fe4SMike Snitzer 166676e33fe4SMike Snitzer return r; 166776e33fe4SMike Snitzer } 166876e33fe4SMike Snitzer 16691da177e4SLinus Torvalds /* 16701da177e4SLinus Torvalds * Suspend can't complete until all the I/O is processed so if 1671436d4108SAlasdair G Kergon * the last path fails we must error any remaining I/O. 1672436d4108SAlasdair G Kergon * Note that if the freeze_bdev fails while suspending, the 1673436d4108SAlasdair G Kergon * queue_if_no_path state is lost - userspace should reset it. 16741da177e4SLinus Torvalds */ 16751da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti) 16761da177e4SLinus Torvalds { 16777943bd6dSMike Snitzer struct multipath *m = ti->private; 16781da177e4SLinus Torvalds 1679be7d31ccSMike Snitzer queue_if_no_path(m, false, true); 16801da177e4SLinus Torvalds } 16811da177e4SLinus Torvalds 16826df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti) 16836df400abSKiyoshi Ueda { 16846380f26fSMike Anderson struct multipath *m = ti->private; 16856380f26fSMike Anderson 16866380f26fSMike Anderson mutex_lock(&m->work_mutex); 16872bded7bdSKiyoshi Ueda flush_multipath_work(m); 16886380f26fSMike Anderson mutex_unlock(&m->work_mutex); 16896df400abSKiyoshi Ueda } 16906df400abSKiyoshi Ueda 1691436d4108SAlasdair G Kergon /* 1692436d4108SAlasdair G Kergon * Restore the queue_if_no_path setting. 1693436d4108SAlasdair G Kergon */ 16941da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti) 16951da177e4SLinus Torvalds { 16967943bd6dSMike Snitzer struct multipath *m = ti->private; 16971814f2e3SMike Snitzer unsigned long flags; 16981da177e4SLinus Torvalds 16991814f2e3SMike Snitzer spin_lock_irqsave(&m->lock, flags); 1700518257b1SMike Snitzer if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) 1701518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 1702518257b1SMike Snitzer else 1703518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 17041814f2e3SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 17051da177e4SLinus Torvalds } 17061da177e4SLinus Torvalds 17071da177e4SLinus Torvalds /* 17081da177e4SLinus Torvalds * Info output has the following format: 17091da177e4SLinus Torvalds * num_multipath_feature_args [multipath_feature_args]* 17101da177e4SLinus Torvalds * num_handler_status_args [handler_status_args]* 17111da177e4SLinus Torvalds * num_groups init_group_number 17121da177e4SLinus Torvalds * [A|D|E num_ps_status_args [ps_status_args]* 17131da177e4SLinus Torvalds * num_paths num_selector_args 17141da177e4SLinus Torvalds * [path_dev A|F fail_count [selector_args]* ]+ ]+ 17151da177e4SLinus Torvalds * 17161da177e4SLinus Torvalds * Table output has the following format (identical to the constructor string): 17171da177e4SLinus Torvalds * num_feature_args [features_args]* 17181da177e4SLinus Torvalds * num_handler_args hw_handler [hw_handler_args]* 17191da177e4SLinus Torvalds * num_groups init_group_number 17201da177e4SLinus Torvalds * [priority selector-name num_ps_args [ps_args]* 17211da177e4SLinus Torvalds * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ 17221da177e4SLinus Torvalds */ 1723fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type, 17241f4e0ff0SAlasdair G Kergon unsigned status_flags, char *result, unsigned maxlen) 17251da177e4SLinus Torvalds { 17261da177e4SLinus Torvalds int sz = 0; 17271da177e4SLinus Torvalds unsigned long flags; 17287943bd6dSMike Snitzer struct multipath *m = ti->private; 17291da177e4SLinus Torvalds struct priority_group *pg; 17301da177e4SLinus Torvalds struct pgpath *p; 17311da177e4SLinus Torvalds unsigned pg_num; 17321da177e4SLinus Torvalds char state; 17331da177e4SLinus Torvalds 17341da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 17351da177e4SLinus Torvalds 17361da177e4SLinus Torvalds /* Features */ 17371da177e4SLinus Torvalds if (type == STATUSTYPE_INFO) 173891e968aaSMike Snitzer DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags), 173991e968aaSMike Snitzer atomic_read(&m->pg_init_count)); 1740c9e45581SDave Wysochanski else { 1741518257b1SMike Snitzer DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) + 17424e2d19e4SChandra Seetharaman (m->pg_init_retries > 0) * 2 + 1743a58a935dSMike Snitzer (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 + 1744e83068a5SMike Snitzer test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) + 1745e83068a5SMike Snitzer (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2); 1746e83068a5SMike Snitzer 1747518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 1748c9e45581SDave Wysochanski DMEMIT("queue_if_no_path "); 1749c9e45581SDave Wysochanski if (m->pg_init_retries) 1750c9e45581SDave Wysochanski DMEMIT("pg_init_retries %u ", m->pg_init_retries); 17514e2d19e4SChandra Seetharaman if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) 17524e2d19e4SChandra Seetharaman DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs); 1753518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) 1754a58a935dSMike Snitzer DMEMIT("retain_attached_hw_handler "); 1755e83068a5SMike Snitzer if (m->queue_mode != DM_TYPE_REQUEST_BASED) { 1756e83068a5SMike Snitzer switch(m->queue_mode) { 1757e83068a5SMike Snitzer case DM_TYPE_BIO_BASED: 1758e83068a5SMike Snitzer DMEMIT("queue_mode bio "); 1759e83068a5SMike Snitzer break; 1760e83068a5SMike Snitzer case DM_TYPE_MQ_REQUEST_BASED: 1761e83068a5SMike Snitzer DMEMIT("queue_mode mq "); 1762e83068a5SMike Snitzer break; 1763e83068a5SMike Snitzer } 1764e83068a5SMike Snitzer } 1765c9e45581SDave Wysochanski } 17661da177e4SLinus Torvalds 1767cfae5c9bSChandra Seetharaman if (!m->hw_handler_name || type == STATUSTYPE_INFO) 17681da177e4SLinus Torvalds DMEMIT("0 "); 17691da177e4SLinus Torvalds else 1770cfae5c9bSChandra Seetharaman DMEMIT("1 %s ", m->hw_handler_name); 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds DMEMIT("%u ", m->nr_priority_groups); 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds if (m->next_pg) 17751da177e4SLinus Torvalds pg_num = m->next_pg->pg_num; 17761da177e4SLinus Torvalds else if (m->current_pg) 17771da177e4SLinus Torvalds pg_num = m->current_pg->pg_num; 17781da177e4SLinus Torvalds else 1779a490a07aSMike Snitzer pg_num = (m->nr_priority_groups ? 1 : 0); 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds DMEMIT("%u ", pg_num); 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds switch (type) { 17841da177e4SLinus Torvalds case STATUSTYPE_INFO: 17851da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 17861da177e4SLinus Torvalds if (pg->bypassed) 17871da177e4SLinus Torvalds state = 'D'; /* Disabled */ 17881da177e4SLinus Torvalds else if (pg == m->current_pg) 17891da177e4SLinus Torvalds state = 'A'; /* Currently Active */ 17901da177e4SLinus Torvalds else 17911da177e4SLinus Torvalds state = 'E'; /* Enabled */ 17921da177e4SLinus Torvalds 17931da177e4SLinus Torvalds DMEMIT("%c ", state); 17941da177e4SLinus Torvalds 17951da177e4SLinus Torvalds if (pg->ps.type->status) 17961da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 17971da177e4SLinus Torvalds result + sz, 17981da177e4SLinus Torvalds maxlen - sz); 17991da177e4SLinus Torvalds else 18001da177e4SLinus Torvalds DMEMIT("0 "); 18011da177e4SLinus Torvalds 18021da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 18031da177e4SLinus Torvalds pg->ps.type->info_args); 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 18061da177e4SLinus Torvalds DMEMIT("%s %s %u ", p->path.dev->name, 18076680073dSKiyoshi Ueda p->is_active ? "A" : "F", 18081da177e4SLinus Torvalds p->fail_count); 18091da177e4SLinus Torvalds if (pg->ps.type->status) 18101da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 18111da177e4SLinus Torvalds &p->path, type, result + sz, 18121da177e4SLinus Torvalds maxlen - sz); 18131da177e4SLinus Torvalds } 18141da177e4SLinus Torvalds } 18151da177e4SLinus Torvalds break; 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds case STATUSTYPE_TABLE: 18181da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 18191da177e4SLinus Torvalds DMEMIT("%s ", pg->ps.type->name); 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds if (pg->ps.type->status) 18221da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 18231da177e4SLinus Torvalds result + sz, 18241da177e4SLinus Torvalds maxlen - sz); 18251da177e4SLinus Torvalds else 18261da177e4SLinus Torvalds DMEMIT("0 "); 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 18291da177e4SLinus Torvalds pg->ps.type->table_args); 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 18321da177e4SLinus Torvalds DMEMIT("%s ", p->path.dev->name); 18331da177e4SLinus Torvalds if (pg->ps.type->status) 18341da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 18351da177e4SLinus Torvalds &p->path, type, result + sz, 18361da177e4SLinus Torvalds maxlen - sz); 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds break; 18401da177e4SLinus Torvalds } 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 18431da177e4SLinus Torvalds } 18441da177e4SLinus Torvalds 18451da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv) 18461da177e4SLinus Torvalds { 18476380f26fSMike Anderson int r = -EINVAL; 18481da177e4SLinus Torvalds struct dm_dev *dev; 18497943bd6dSMike Snitzer struct multipath *m = ti->private; 18501da177e4SLinus Torvalds action_fn action; 18511da177e4SLinus Torvalds 18526380f26fSMike Anderson mutex_lock(&m->work_mutex); 18536380f26fSMike Anderson 1854c2f3d24bSKiyoshi Ueda if (dm_suspended(ti)) { 1855c2f3d24bSKiyoshi Ueda r = -EBUSY; 1856c2f3d24bSKiyoshi Ueda goto out; 1857c2f3d24bSKiyoshi Ueda } 1858c2f3d24bSKiyoshi Ueda 18591da177e4SLinus Torvalds if (argc == 1) { 1860498f0103SMike Snitzer if (!strcasecmp(argv[0], "queue_if_no_path")) { 1861be7d31ccSMike Snitzer r = queue_if_no_path(m, true, false); 18626380f26fSMike Anderson goto out; 1863498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "fail_if_no_path")) { 1864be7d31ccSMike Snitzer r = queue_if_no_path(m, false, false); 18656380f26fSMike Anderson goto out; 18666380f26fSMike Anderson } 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 18696380f26fSMike Anderson if (argc != 2) { 1870a356e426SJose Castillo DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc); 18716380f26fSMike Anderson goto out; 18726380f26fSMike Anderson } 18731da177e4SLinus Torvalds 1874498f0103SMike Snitzer if (!strcasecmp(argv[0], "disable_group")) { 1875be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], true); 18766380f26fSMike Anderson goto out; 1877498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "enable_group")) { 1878be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], false); 18796380f26fSMike Anderson goto out; 1880498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "switch_group")) { 18816380f26fSMike Anderson r = switch_pg_num(m, argv[1]); 18826380f26fSMike Anderson goto out; 1883498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "reinstate_path")) 18841da177e4SLinus Torvalds action = reinstate_path; 1885498f0103SMike Snitzer else if (!strcasecmp(argv[0], "fail_path")) 18861da177e4SLinus Torvalds action = fail_path; 18876380f26fSMike Anderson else { 1888a356e426SJose Castillo DMWARN("Unrecognised multipath message received: %s", argv[0]); 18896380f26fSMike Anderson goto out; 18906380f26fSMike Anderson } 18911da177e4SLinus Torvalds 18928215d6ecSNikanth Karthikesan r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev); 18931da177e4SLinus Torvalds if (r) { 189472d94861SAlasdair G Kergon DMWARN("message: error getting device %s", 18951da177e4SLinus Torvalds argv[1]); 18966380f26fSMike Anderson goto out; 18971da177e4SLinus Torvalds } 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds r = action_dev(m, dev, action); 19001da177e4SLinus Torvalds 19011da177e4SLinus Torvalds dm_put_device(ti, dev); 19021da177e4SLinus Torvalds 19036380f26fSMike Anderson out: 19046380f26fSMike Anderson mutex_unlock(&m->work_mutex); 19051da177e4SLinus Torvalds return r; 19061da177e4SLinus Torvalds } 19071da177e4SLinus Torvalds 1908e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti, 1909e56f81e0SChristoph Hellwig struct block_device **bdev, fmode_t *mode) 19109af4aa30SMilan Broz { 191135991652SMikulas Patocka struct multipath *m = ti->private; 19122da1610aSMike Snitzer struct pgpath *current_pgpath; 191335991652SMikulas Patocka int r; 191435991652SMikulas Patocka 19152da1610aSMike Snitzer current_pgpath = lockless_dereference(m->current_pgpath); 19162da1610aSMike Snitzer if (!current_pgpath) 19172da1610aSMike Snitzer current_pgpath = choose_pgpath(m, 0); 19189af4aa30SMilan Broz 19192da1610aSMike Snitzer if (current_pgpath) { 1920518257b1SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) { 19212da1610aSMike Snitzer *bdev = current_pgpath->path.dev->bdev; 19222da1610aSMike Snitzer *mode = current_pgpath->path.dev->mode; 192343e43c9eSJunichi Nomura r = 0; 192443e43c9eSJunichi Nomura } else { 192543e43c9eSJunichi Nomura /* pg_init has not started or completed */ 19266c182cd8SHannes Reinecke r = -ENOTCONN; 192743e43c9eSJunichi Nomura } 192843e43c9eSJunichi Nomura } else { 192943e43c9eSJunichi Nomura /* No path is available */ 1930518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 193143e43c9eSJunichi Nomura r = -ENOTCONN; 193243e43c9eSJunichi Nomura else 19339af4aa30SMilan Broz r = -EIO; 193443e43c9eSJunichi Nomura } 19359af4aa30SMilan Broz 19365bbbfdf6SJunichi Nomura if (r == -ENOTCONN) { 19372da1610aSMike Snitzer if (!lockless_dereference(m->current_pg)) { 19383e9f1be1SHannes Reinecke /* Path status changed, redo selection */ 19392da1610aSMike Snitzer (void) choose_pgpath(m, 0); 19403e9f1be1SHannes Reinecke } 1941518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 19422da1610aSMike Snitzer pg_init_all_paths(m); 194363d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 194476e33fe4SMike Snitzer process_queued_bios_list(m); 19453e9f1be1SHannes Reinecke } 194635991652SMikulas Patocka 1947e56f81e0SChristoph Hellwig /* 1948e56f81e0SChristoph Hellwig * Only pass ioctls through if the device sizes match exactly. 1949e56f81e0SChristoph Hellwig */ 1950e56f81e0SChristoph Hellwig if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT) 1951e56f81e0SChristoph Hellwig return 1; 1952e56f81e0SChristoph Hellwig return r; 19539af4aa30SMilan Broz } 19549af4aa30SMilan Broz 1955af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti, 1956af4874e0SMike Snitzer iterate_devices_callout_fn fn, void *data) 1957af4874e0SMike Snitzer { 1958af4874e0SMike Snitzer struct multipath *m = ti->private; 1959af4874e0SMike Snitzer struct priority_group *pg; 1960af4874e0SMike Snitzer struct pgpath *p; 1961af4874e0SMike Snitzer int ret = 0; 1962af4874e0SMike Snitzer 1963af4874e0SMike Snitzer list_for_each_entry(pg, &m->priority_groups, list) { 1964af4874e0SMike Snitzer list_for_each_entry(p, &pg->pgpaths, list) { 19655dea271bSMike Snitzer ret = fn(ti, p->path.dev, ti->begin, ti->len, data); 1966af4874e0SMike Snitzer if (ret) 1967af4874e0SMike Snitzer goto out; 1968af4874e0SMike Snitzer } 1969af4874e0SMike Snitzer } 1970af4874e0SMike Snitzer 1971af4874e0SMike Snitzer out: 1972af4874e0SMike Snitzer return ret; 1973af4874e0SMike Snitzer } 1974af4874e0SMike Snitzer 19759f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath) 1976f40c67f0SKiyoshi Ueda { 1977f40c67f0SKiyoshi Ueda struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1978f40c67f0SKiyoshi Ueda 197952b09914SMike Snitzer return blk_lld_busy(q); 1980f40c67f0SKiyoshi Ueda } 1981f40c67f0SKiyoshi Ueda 1982f40c67f0SKiyoshi Ueda /* 1983f40c67f0SKiyoshi Ueda * We return "busy", only when we can map I/Os but underlying devices 1984f40c67f0SKiyoshi Ueda * are busy (so even if we map I/Os now, the I/Os will wait on 1985f40c67f0SKiyoshi Ueda * the underlying queue). 1986f40c67f0SKiyoshi Ueda * In other words, if we want to kill I/Os or queue them inside us 1987f40c67f0SKiyoshi Ueda * due to map unavailability, we don't return "busy". Otherwise, 1988f40c67f0SKiyoshi Ueda * dm core won't give us the I/Os and we can't do what we want. 1989f40c67f0SKiyoshi Ueda */ 1990f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti) 1991f40c67f0SKiyoshi Ueda { 1992be7d31ccSMike Snitzer bool busy = false, has_active = false; 1993f40c67f0SKiyoshi Ueda struct multipath *m = ti->private; 19942da1610aSMike Snitzer struct priority_group *pg, *next_pg; 1995f40c67f0SKiyoshi Ueda struct pgpath *pgpath; 1996f40c67f0SKiyoshi Ueda 19977a7a3b45SJun'ichi Nomura /* pg_init in progress or no paths available */ 199891e968aaSMike Snitzer if (atomic_read(&m->pg_init_in_progress) || 19992da1610aSMike Snitzer (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) 20002da1610aSMike Snitzer return true; 20012da1610aSMike Snitzer 2002f40c67f0SKiyoshi Ueda /* Guess which priority_group will be used at next mapping time */ 20032da1610aSMike Snitzer pg = lockless_dereference(m->current_pg); 20042da1610aSMike Snitzer next_pg = lockless_dereference(m->next_pg); 20052da1610aSMike Snitzer if (unlikely(!lockless_dereference(m->current_pgpath) && next_pg)) 20062da1610aSMike Snitzer pg = next_pg; 20072da1610aSMike Snitzer 20082da1610aSMike Snitzer if (!pg) { 2009f40c67f0SKiyoshi Ueda /* 2010f40c67f0SKiyoshi Ueda * We don't know which pg will be used at next mapping time. 20112da1610aSMike Snitzer * We don't call choose_pgpath() here to avoid to trigger 2012f40c67f0SKiyoshi Ueda * pg_init just by busy checking. 2013f40c67f0SKiyoshi Ueda * So we don't know whether underlying devices we will be using 2014f40c67f0SKiyoshi Ueda * at next mapping time are busy or not. Just try mapping. 2015f40c67f0SKiyoshi Ueda */ 20162da1610aSMike Snitzer return busy; 20172da1610aSMike Snitzer } 2018f40c67f0SKiyoshi Ueda 2019f40c67f0SKiyoshi Ueda /* 2020f40c67f0SKiyoshi Ueda * If there is one non-busy active path at least, the path selector 2021f40c67f0SKiyoshi Ueda * will be able to select it. So we consider such a pg as not busy. 2022f40c67f0SKiyoshi Ueda */ 2023be7d31ccSMike Snitzer busy = true; 20242da1610aSMike Snitzer list_for_each_entry(pgpath, &pg->pgpaths, list) { 2025f40c67f0SKiyoshi Ueda if (pgpath->is_active) { 2026be7d31ccSMike Snitzer has_active = true; 20279f54cec5SMike Snitzer if (!pgpath_busy(pgpath)) { 2028be7d31ccSMike Snitzer busy = false; 2029f40c67f0SKiyoshi Ueda break; 2030f40c67f0SKiyoshi Ueda } 2031f40c67f0SKiyoshi Ueda } 20322da1610aSMike Snitzer } 2033f40c67f0SKiyoshi Ueda 20342da1610aSMike Snitzer if (!has_active) { 2035f40c67f0SKiyoshi Ueda /* 2036f40c67f0SKiyoshi Ueda * No active path in this pg, so this pg won't be used and 2037f40c67f0SKiyoshi Ueda * the current_pg will be changed at next mapping time. 2038f40c67f0SKiyoshi Ueda * We need to try mapping to determine it. 2039f40c67f0SKiyoshi Ueda */ 2040be7d31ccSMike Snitzer busy = false; 20412da1610aSMike Snitzer } 2042f40c67f0SKiyoshi Ueda 2043f40c67f0SKiyoshi Ueda return busy; 2044f40c67f0SKiyoshi Ueda } 2045f40c67f0SKiyoshi Ueda 20461da177e4SLinus Torvalds /*----------------------------------------------------------------- 20471da177e4SLinus Torvalds * Module setup 20481da177e4SLinus Torvalds *---------------------------------------------------------------*/ 20491da177e4SLinus Torvalds static struct target_type multipath_target = { 20501da177e4SLinus Torvalds .name = "multipath", 2051e83068a5SMike Snitzer .version = {1, 12, 0}, 205216f12266SMike Snitzer .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE, 20531da177e4SLinus Torvalds .module = THIS_MODULE, 20541da177e4SLinus Torvalds .ctr = multipath_ctr, 20551da177e4SLinus Torvalds .dtr = multipath_dtr, 2056f40c67f0SKiyoshi Ueda .map_rq = multipath_map, 2057e5863d9aSMike Snitzer .clone_and_map_rq = multipath_clone_and_map, 2058e5863d9aSMike Snitzer .release_clone_rq = multipath_release_clone, 2059f40c67f0SKiyoshi Ueda .rq_end_io = multipath_end_io, 206076e33fe4SMike Snitzer .map = multipath_map_bio, 206176e33fe4SMike Snitzer .end_io = multipath_end_io_bio, 206276e33fe4SMike Snitzer .presuspend = multipath_presuspend, 206376e33fe4SMike Snitzer .postsuspend = multipath_postsuspend, 206476e33fe4SMike Snitzer .resume = multipath_resume, 206576e33fe4SMike Snitzer .status = multipath_status, 206676e33fe4SMike Snitzer .message = multipath_message, 206776e33fe4SMike Snitzer .prepare_ioctl = multipath_prepare_ioctl, 206876e33fe4SMike Snitzer .iterate_devices = multipath_iterate_devices, 206976e33fe4SMike Snitzer .busy = multipath_busy, 207076e33fe4SMike Snitzer }; 207176e33fe4SMike Snitzer 20721da177e4SLinus Torvalds static int __init dm_multipath_init(void) 20731da177e4SLinus Torvalds { 20741da177e4SLinus Torvalds int r; 20751da177e4SLinus Torvalds 207676e33fe4SMike Snitzer /* allocate a slab for the dm_mpath_ios */ 2077028867acSAlasdair G Kergon _mpio_cache = KMEM_CACHE(dm_mpath_io, 0); 20781da177e4SLinus Torvalds if (!_mpio_cache) 20791da177e4SLinus Torvalds return -ENOMEM; 20801da177e4SLinus Torvalds 20811da177e4SLinus Torvalds r = dm_register_target(&multipath_target); 20821da177e4SLinus Torvalds if (r < 0) { 208376e33fe4SMike Snitzer DMERR("request-based register failed %d", r); 2084ff658e9cSJohannes Thumshirn r = -EINVAL; 2085ff658e9cSJohannes Thumshirn goto bad_register_target; 20861da177e4SLinus Torvalds } 20871da177e4SLinus Torvalds 20884d4d66abSTejun Heo kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0); 2089c557308eSAlasdair G Kergon if (!kmultipathd) { 20900cd33124SAlasdair G Kergon DMERR("failed to create workqueue kmpathd"); 2091ff658e9cSJohannes Thumshirn r = -ENOMEM; 2092ff658e9cSJohannes Thumshirn goto bad_alloc_kmultipathd; 2093c557308eSAlasdair G Kergon } 2094c557308eSAlasdair G Kergon 2095bab7cfc7SChandra Seetharaman /* 2096bab7cfc7SChandra Seetharaman * A separate workqueue is used to handle the device handlers 2097bab7cfc7SChandra Seetharaman * to avoid overloading existing workqueue. Overloading the 2098bab7cfc7SChandra Seetharaman * old workqueue would also create a bottleneck in the 2099bab7cfc7SChandra Seetharaman * path of the storage hardware device activation. 2100bab7cfc7SChandra Seetharaman */ 21014d4d66abSTejun Heo kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd", 21024d4d66abSTejun Heo WQ_MEM_RECLAIM); 2103bab7cfc7SChandra Seetharaman if (!kmpath_handlerd) { 2104bab7cfc7SChandra Seetharaman DMERR("failed to create workqueue kmpath_handlerd"); 2105ff658e9cSJohannes Thumshirn r = -ENOMEM; 2106ff658e9cSJohannes Thumshirn goto bad_alloc_kmpath_handlerd; 2107bab7cfc7SChandra Seetharaman } 2108bab7cfc7SChandra Seetharaman 2109ff658e9cSJohannes Thumshirn return 0; 2110ff658e9cSJohannes Thumshirn 2111ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd: 2112ff658e9cSJohannes Thumshirn destroy_workqueue(kmultipathd); 2113ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd: 2114ff658e9cSJohannes Thumshirn dm_unregister_target(&multipath_target); 2115ff658e9cSJohannes Thumshirn bad_register_target: 2116ff658e9cSJohannes Thumshirn kmem_cache_destroy(_mpio_cache); 2117ff658e9cSJohannes Thumshirn 21181da177e4SLinus Torvalds return r; 21191da177e4SLinus Torvalds } 21201da177e4SLinus Torvalds 21211da177e4SLinus Torvalds static void __exit dm_multipath_exit(void) 21221da177e4SLinus Torvalds { 2123bab7cfc7SChandra Seetharaman destroy_workqueue(kmpath_handlerd); 2124c557308eSAlasdair G Kergon destroy_workqueue(kmultipathd); 2125c557308eSAlasdair G Kergon 212610d3bd09SMikulas Patocka dm_unregister_target(&multipath_target); 21271da177e4SLinus Torvalds kmem_cache_destroy(_mpio_cache); 21281da177e4SLinus Torvalds } 21291da177e4SLinus Torvalds 21301da177e4SLinus Torvalds module_init(dm_multipath_init); 21311da177e4SLinus Torvalds module_exit(dm_multipath_exit); 21321da177e4SLinus Torvalds 21331da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target"); 21341da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); 21351da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2136