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 937e0d574fSBart Van Assche enum dm_queue_mode queue_mode; 94e83068a5SMike Snitzer 956380f26fSMike Anderson struct mutex work_mutex; 9620800cb3SMike Snitzer struct work_struct trigger_event; 9776e33fe4SMike Snitzer 9876e33fe4SMike Snitzer struct work_struct process_queued_bios; 9976e33fe4SMike Snitzer struct bio_list queued_bios; 1001da177e4SLinus Torvalds }; 1011da177e4SLinus Torvalds 1021da177e4SLinus Torvalds /* 10376e33fe4SMike Snitzer * Context information attached to each io we process. 1041da177e4SLinus Torvalds */ 105028867acSAlasdair G Kergon struct dm_mpath_io { 1061da177e4SLinus Torvalds struct pgpath *pgpath; 10702ab823fSKiyoshi Ueda size_t nr_bytes; 1081da177e4SLinus Torvalds }; 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath); 1111da177e4SLinus Torvalds 112bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd; 113c4028958SDavid Howells static void trigger_event(struct work_struct *work); 11489bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath); 11589bfce76SBart Van Assche static void activate_path_work(struct work_struct *work); 11676e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work); 1171da177e4SLinus Torvalds 118518257b1SMike Snitzer /*----------------------------------------------- 119518257b1SMike Snitzer * Multipath state flags. 120518257b1SMike Snitzer *-----------------------------------------------*/ 121518257b1SMike Snitzer 122518257b1SMike Snitzer #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */ 123518257b1SMike Snitzer #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */ 124518257b1SMike Snitzer #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */ 125518257b1SMike Snitzer #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */ 126518257b1SMike Snitzer #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */ 127518257b1SMike Snitzer #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */ 128518257b1SMike Snitzer #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */ 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds /*----------------------------------------------- 1311da177e4SLinus Torvalds * Allocation routines 1321da177e4SLinus Torvalds *-----------------------------------------------*/ 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void) 1351da177e4SLinus Torvalds { 136e69fae56SMicha³ Miros³aw struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL); 1371da177e4SLinus Torvalds 138224cb3e9SMike Anderson if (pgpath) { 139be7d31ccSMike Snitzer pgpath->is_active = true; 14089bfce76SBart Van Assche INIT_DELAYED_WORK(&pgpath->activate_path, activate_path_work); 141224cb3e9SMike Anderson } 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds return pgpath; 1441da177e4SLinus Torvalds } 1451da177e4SLinus Torvalds 146028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath) 1471da177e4SLinus Torvalds { 1481da177e4SLinus Torvalds kfree(pgpath); 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void) 1521da177e4SLinus Torvalds { 1531da177e4SLinus Torvalds struct priority_group *pg; 1541da177e4SLinus Torvalds 155e69fae56SMicha³ Miros³aw pg = kzalloc(sizeof(*pg), GFP_KERNEL); 1561da177e4SLinus Torvalds 157e69fae56SMicha³ Miros³aw if (pg) 1581da177e4SLinus Torvalds INIT_LIST_HEAD(&pg->pgpaths); 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds return pg; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti) 1641da177e4SLinus Torvalds { 1651da177e4SLinus Torvalds struct pgpath *pgpath, *tmp; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds list_for_each_entry_safe(pgpath, tmp, pgpaths, list) { 1681da177e4SLinus Torvalds list_del(&pgpath->list); 1691da177e4SLinus Torvalds dm_put_device(ti, pgpath->path.dev); 1701da177e4SLinus Torvalds free_pgpath(pgpath); 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg, 1751da177e4SLinus Torvalds struct dm_target *ti) 1761da177e4SLinus Torvalds { 1771da177e4SLinus Torvalds struct path_selector *ps = &pg->ps; 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds if (ps->type) { 1801da177e4SLinus Torvalds ps->type->destroy(ps); 1811da177e4SLinus Torvalds dm_put_path_selector(ps->type); 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds free_pgpaths(&pg->pgpaths, ti); 1851da177e4SLinus Torvalds kfree(pg); 1861da177e4SLinus Torvalds } 1871da177e4SLinus Torvalds 188e83068a5SMike Snitzer static struct multipath *alloc_multipath(struct dm_target *ti) 1891da177e4SLinus Torvalds { 1901da177e4SLinus Torvalds struct multipath *m; 1911da177e4SLinus Torvalds 192e69fae56SMicha³ Miros³aw m = kzalloc(sizeof(*m), GFP_KERNEL); 1931da177e4SLinus Torvalds if (m) { 1941da177e4SLinus Torvalds INIT_LIST_HEAD(&m->priority_groups); 1951da177e4SLinus Torvalds spin_lock_init(&m->lock); 196518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 19791e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, 0); 19891e968aaSMike Snitzer atomic_set(&m->pg_init_in_progress, 0); 19991e968aaSMike Snitzer atomic_set(&m->pg_init_count, 0); 2004e2d19e4SChandra Seetharaman m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT; 201c4028958SDavid Howells INIT_WORK(&m->trigger_event, trigger_event); 2022bded7bdSKiyoshi Ueda init_waitqueue_head(&m->pg_init_wait); 2036380f26fSMike Anderson mutex_init(&m->work_mutex); 2048637a6bfSMike Snitzer 205e83068a5SMike Snitzer m->queue_mode = DM_TYPE_NONE; 206e83068a5SMike Snitzer 207e83068a5SMike Snitzer m->ti = ti; 208e83068a5SMike Snitzer ti->private = m; 209e83068a5SMike Snitzer } 210e83068a5SMike Snitzer 211e83068a5SMike Snitzer return m; 212e83068a5SMike Snitzer } 213e83068a5SMike Snitzer 214e83068a5SMike Snitzer static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m) 215e83068a5SMike Snitzer { 216e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_NONE) { 217e83068a5SMike Snitzer /* 218e83068a5SMike Snitzer * Default to request-based. 219e83068a5SMike Snitzer */ 220e83068a5SMike Snitzer if (dm_use_blk_mq(dm_table_get_md(ti->table))) 221e83068a5SMike Snitzer m->queue_mode = DM_TYPE_MQ_REQUEST_BASED; 222e83068a5SMike Snitzer else 223e83068a5SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 224*cd025384SMike Snitzer 225*cd025384SMike Snitzer } else if (m->queue_mode == DM_TYPE_BIO_BASED || 226*cd025384SMike Snitzer m->queue_mode == DM_TYPE_NVME_BIO_BASED) { 22776e33fe4SMike Snitzer INIT_WORK(&m->process_queued_bios, process_queued_bios); 228*cd025384SMike Snitzer 229*cd025384SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) { 23076e33fe4SMike Snitzer /* 23176e33fe4SMike Snitzer * bio-based doesn't support any direct scsi_dh management; 23276e33fe4SMike Snitzer * it just discovers if a scsi_dh is attached. 23376e33fe4SMike Snitzer */ 23476e33fe4SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 23576e33fe4SMike Snitzer } 236*cd025384SMike Snitzer } 23776e33fe4SMike Snitzer 238e83068a5SMike Snitzer dm_table_set_type(ti->table, m->queue_mode); 2391da177e4SLinus Torvalds 240e83068a5SMike Snitzer return 0; 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds static void free_multipath(struct multipath *m) 2441da177e4SLinus Torvalds { 2451da177e4SLinus Torvalds struct priority_group *pg, *tmp; 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { 2481da177e4SLinus Torvalds list_del(&pg->list); 2491da177e4SLinus Torvalds free_priority_group(pg, m->ti); 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 252cfae5c9bSChandra Seetharaman kfree(m->hw_handler_name); 2532bfd2e13SChandra Seetharaman kfree(m->hw_handler_params); 2541da177e4SLinus Torvalds kfree(m); 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds 2572eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info) 2582eff1924SMike Snitzer { 2592eff1924SMike Snitzer return info->ptr; 2602eff1924SMike Snitzer } 2612eff1924SMike Snitzer 262bf661be1SMike Snitzer static size_t multipath_per_bio_data_size(void) 26376e33fe4SMike Snitzer { 264bf661be1SMike Snitzer return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details); 26576e33fe4SMike Snitzer } 26676e33fe4SMike Snitzer 267bf661be1SMike Snitzer static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio) 268bf661be1SMike Snitzer { 269bf661be1SMike Snitzer return dm_per_bio_data(bio, multipath_per_bio_data_size()); 270bf661be1SMike Snitzer } 271bf661be1SMike Snitzer 272d07a241dSMike Snitzer static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio) 273bf661be1SMike Snitzer { 274bf661be1SMike Snitzer /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */ 275bf661be1SMike Snitzer void *bio_details = mpio + 1; 276bf661be1SMike Snitzer return bio_details; 277bf661be1SMike Snitzer } 278bf661be1SMike Snitzer 27963f6e6fdSMike Snitzer static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p) 28076e33fe4SMike Snitzer { 28176e33fe4SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 282d07a241dSMike Snitzer struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio); 28376e33fe4SMike Snitzer 284d0442f80SMike Snitzer mpio->nr_bytes = bio->bi_iter.bi_size; 285d0442f80SMike Snitzer mpio->pgpath = NULL; 286bf661be1SMike Snitzer *mpio_p = mpio; 287d0442f80SMike Snitzer 288d0442f80SMike Snitzer dm_bio_record(bio_details, bio); 28976e33fe4SMike Snitzer } 29076e33fe4SMike Snitzer 2911da177e4SLinus Torvalds /*----------------------------------------------- 2921da177e4SLinus Torvalds * Path selection 2931da177e4SLinus Torvalds *-----------------------------------------------*/ 2941da177e4SLinus Torvalds 2953e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m) 296fb612642SKiyoshi Ueda { 297fb612642SKiyoshi Ueda struct pgpath *pgpath; 2984e2d19e4SChandra Seetharaman unsigned long pg_init_delay = 0; 299fb612642SKiyoshi Ueda 300b194679fSBart Van Assche lockdep_assert_held(&m->lock); 301b194679fSBart Van Assche 30291e968aaSMike Snitzer if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 3033e9f1be1SHannes Reinecke return 0; 30417f4ff45SHannes Reinecke 30591e968aaSMike Snitzer atomic_inc(&m->pg_init_count); 306518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 3073e9f1be1SHannes Reinecke 3083e9f1be1SHannes Reinecke /* Check here to reset pg_init_required */ 3093e9f1be1SHannes Reinecke if (!m->current_pg) 3103e9f1be1SHannes Reinecke return 0; 3113e9f1be1SHannes Reinecke 312518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags)) 3134e2d19e4SChandra Seetharaman pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ? 3144e2d19e4SChandra Seetharaman m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS); 315fb612642SKiyoshi Ueda list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) { 316fb612642SKiyoshi Ueda /* Skip failed paths */ 317fb612642SKiyoshi Ueda if (!pgpath->is_active) 318fb612642SKiyoshi Ueda continue; 3194e2d19e4SChandra Seetharaman if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path, 3204e2d19e4SChandra Seetharaman pg_init_delay)) 32191e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 322fb612642SKiyoshi Ueda } 32391e968aaSMike Snitzer return atomic_read(&m->pg_init_in_progress); 324fb612642SKiyoshi Ueda } 325fb612642SKiyoshi Ueda 326c1d7ecf7SBart Van Assche static int pg_init_all_paths(struct multipath *m) 3271da177e4SLinus Torvalds { 328c1d7ecf7SBart Van Assche int ret; 3292da1610aSMike Snitzer unsigned long flags; 3302da1610aSMike Snitzer 3312da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 332c1d7ecf7SBart Van Assche ret = __pg_init_all_paths(m); 3332da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 334c1d7ecf7SBart Van Assche 335c1d7ecf7SBart Van Assche return ret; 3362da1610aSMike Snitzer } 3372da1610aSMike Snitzer 3382da1610aSMike Snitzer static void __switch_pg(struct multipath *m, struct priority_group *pg) 3392da1610aSMike Snitzer { 3402da1610aSMike Snitzer m->current_pg = pg; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds /* Must we initialise the PG first, and queue I/O till it's ready? */ 343cfae5c9bSChandra Seetharaman if (m->hw_handler_name) { 344518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 345518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 3461da177e4SLinus Torvalds } else { 347518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 348518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 3491da177e4SLinus Torvalds } 350c9e45581SDave Wysochanski 35191e968aaSMike Snitzer atomic_set(&m->pg_init_count, 0); 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 3542da1610aSMike Snitzer static struct pgpath *choose_path_in_pg(struct multipath *m, 3552da1610aSMike Snitzer struct priority_group *pg, 35602ab823fSKiyoshi Ueda size_t nr_bytes) 3571da177e4SLinus Torvalds { 3582da1610aSMike Snitzer unsigned long flags; 359c922d5f7SJosef "Jeff" Sipek struct dm_path *path; 3602da1610aSMike Snitzer struct pgpath *pgpath; 3611da177e4SLinus Torvalds 36290a4323cSMike Snitzer path = pg->ps.type->select_path(&pg->ps, nr_bytes); 3631da177e4SLinus Torvalds if (!path) 3642da1610aSMike Snitzer return ERR_PTR(-ENXIO); 3651da177e4SLinus Torvalds 3662da1610aSMike Snitzer pgpath = path_to_pgpath(path); 3671da177e4SLinus Torvalds 368506458efSWill Deacon if (unlikely(READ_ONCE(m->current_pg) != pg)) { 3692da1610aSMike Snitzer /* Only update current_pgpath if pg changed */ 3702da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 3712da1610aSMike Snitzer m->current_pgpath = pgpath; 3722da1610aSMike Snitzer __switch_pg(m, pg); 3732da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3741da177e4SLinus Torvalds } 3751da177e4SLinus Torvalds 3762da1610aSMike Snitzer return pgpath; 3772da1610aSMike Snitzer } 3782da1610aSMike Snitzer 3792da1610aSMike Snitzer static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes) 3801da177e4SLinus Torvalds { 3812da1610aSMike Snitzer unsigned long flags; 3821da177e4SLinus Torvalds struct priority_group *pg; 3832da1610aSMike Snitzer struct pgpath *pgpath; 384d19a55ccSMike Snitzer unsigned bypassed = 1; 3851da177e4SLinus Torvalds 38691e968aaSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 387518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 3881da177e4SLinus Torvalds goto failed; 3891f271972SBenjamin Marzinski } 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds /* Were we instructed to switch PG? */ 392506458efSWill Deacon if (READ_ONCE(m->next_pg)) { 3932da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 3941da177e4SLinus Torvalds pg = m->next_pg; 3952da1610aSMike Snitzer if (!pg) { 3962da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3972da1610aSMike Snitzer goto check_current_pg; 3982da1610aSMike Snitzer } 3991da177e4SLinus Torvalds m->next_pg = NULL; 4002da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4012da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4022da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 4032da1610aSMike Snitzer return pgpath; 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 4061da177e4SLinus Torvalds /* Don't change PG until it has no remaining paths */ 4072da1610aSMike Snitzer check_current_pg: 408506458efSWill Deacon pg = READ_ONCE(m->current_pg); 4092da1610aSMike Snitzer if (pg) { 4102da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4112da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 4122da1610aSMike Snitzer return pgpath; 4132da1610aSMike Snitzer } 4141da177e4SLinus Torvalds 4151da177e4SLinus Torvalds /* 4161da177e4SLinus Torvalds * Loop through priority groups until we find a valid path. 4171da177e4SLinus Torvalds * First time we skip PGs marked 'bypassed'. 418f220fd4eSMike Christie * Second time we only try the ones we skipped, but set 419f220fd4eSMike Christie * pg_init_delay_retry so we do not hammer controllers. 4201da177e4SLinus Torvalds */ 4211da177e4SLinus Torvalds do { 4221da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 423d19a55ccSMike Snitzer if (pg->bypassed == !!bypassed) 4241da177e4SLinus Torvalds continue; 4252da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4262da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) { 427f220fd4eSMike Christie if (!bypassed) 428518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 4292da1610aSMike Snitzer return pgpath; 4301da177e4SLinus Torvalds } 431f220fd4eSMike Christie } 4321da177e4SLinus Torvalds } while (bypassed--); 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds failed: 4352da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 4361da177e4SLinus Torvalds m->current_pgpath = NULL; 4371da177e4SLinus Torvalds m->current_pg = NULL; 4382da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4392da1610aSMike Snitzer 4402da1610aSMike Snitzer return NULL; 4411da177e4SLinus Torvalds } 4421da177e4SLinus Torvalds 44345e15720SKiyoshi Ueda /* 44486331f39SBart Van Assche * dm_report_EIO() is a macro instead of a function to make pr_debug() 44586331f39SBart Van Assche * report the function name and line number of the function from which 44686331f39SBart Van Assche * it has been invoked. 44745e15720SKiyoshi Ueda */ 44886331f39SBart Van Assche #define dm_report_EIO(m) \ 44918a482f5SChristoph Hellwig do { \ 45086331f39SBart Van Assche struct mapped_device *md = dm_table_get_md((m)->ti->table); \ 45186331f39SBart Van Assche \ 45286331f39SBart Van Assche pr_debug("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d\n", \ 45386331f39SBart Van Assche dm_device_name(md), \ 45486331f39SBart Van Assche test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \ 45586331f39SBart Van Assche test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \ 45686331f39SBart Van Assche dm_noflush_suspending((m)->ti)); \ 45718a482f5SChristoph Hellwig } while (0) 45845e15720SKiyoshi Ueda 45936fcffccSHannes Reinecke /* 460c1fd0abeSMike Snitzer * Check whether bios must be queued in the device-mapper core rather 461c1fd0abeSMike Snitzer * than here in the target. 462c1fd0abeSMike Snitzer * 463c1fd0abeSMike Snitzer * If MPATHF_QUEUE_IF_NO_PATH and MPATHF_SAVED_QUEUE_IF_NO_PATH hold 464c1fd0abeSMike Snitzer * the same value then we are not between multipath_presuspend() 465c1fd0abeSMike Snitzer * and multipath_resume() calls and we have no need to check 466c1fd0abeSMike Snitzer * for the DMF_NOFLUSH_SUSPENDING flag. 467c1fd0abeSMike Snitzer */ 468c1fd0abeSMike Snitzer static bool __must_push_back(struct multipath *m, unsigned long flags) 469c1fd0abeSMike Snitzer { 470c1fd0abeSMike Snitzer return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) != 471c1fd0abeSMike Snitzer test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &flags)) && 472c1fd0abeSMike Snitzer dm_noflush_suspending(m->ti)); 473c1fd0abeSMike Snitzer } 474c1fd0abeSMike Snitzer 475c1fd0abeSMike Snitzer /* 476c1fd0abeSMike Snitzer * Following functions use READ_ONCE to get atomic access to 477c1fd0abeSMike Snitzer * all m->flags to avoid taking spinlock 478c1fd0abeSMike Snitzer */ 479c1fd0abeSMike Snitzer static bool must_push_back_rq(struct multipath *m) 480c1fd0abeSMike Snitzer { 481c1fd0abeSMike Snitzer unsigned long flags = READ_ONCE(m->flags); 482c1fd0abeSMike Snitzer return test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) || __must_push_back(m, flags); 483c1fd0abeSMike Snitzer } 484c1fd0abeSMike Snitzer 485c1fd0abeSMike Snitzer static bool must_push_back_bio(struct multipath *m) 486c1fd0abeSMike Snitzer { 487c1fd0abeSMike Snitzer unsigned long flags = READ_ONCE(m->flags); 488c1fd0abeSMike Snitzer return __must_push_back(m, flags); 489c1fd0abeSMike Snitzer } 490c1fd0abeSMike Snitzer 491c1fd0abeSMike Snitzer /* 49276e33fe4SMike Snitzer * Map cloned requests (request-based multipath) 49336fcffccSHannes Reinecke */ 494eb8db831SChristoph Hellwig static int multipath_clone_and_map(struct dm_target *ti, struct request *rq, 495e5863d9aSMike Snitzer union map_info *map_context, 496eb8db831SChristoph Hellwig struct request **__clone) 4971da177e4SLinus Torvalds { 4987943bd6dSMike Snitzer struct multipath *m = ti->private; 499eb8db831SChristoph Hellwig size_t nr_bytes = blk_rq_bytes(rq); 5001da177e4SLinus Torvalds struct pgpath *pgpath; 501f40c67f0SKiyoshi Ueda struct block_device *bdev; 502eb8db831SChristoph Hellwig struct dm_mpath_io *mpio = get_mpio(map_context); 5037083abbbSBart Van Assche struct request_queue *q; 504eb8db831SChristoph Hellwig struct request *clone; 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds /* Do we need to select a new pgpath? */ 507506458efSWill Deacon pgpath = READ_ONCE(m->current_pgpath); 5082da1610aSMike Snitzer if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags)) 5092da1610aSMike Snitzer pgpath = choose_pgpath(m, nr_bytes); 5101da177e4SLinus Torvalds 5119bf59a61SMike Snitzer if (!pgpath) { 512c1fd0abeSMike Snitzer if (must_push_back_rq(m)) 513b88efd43SMike Snitzer return DM_MAPIO_DELAY_REQUEUE; 51418a482f5SChristoph Hellwig dm_report_EIO(m); /* Failed */ 515f98e0eb6SChristoph Hellwig return DM_MAPIO_KILL; 516518257b1SMike Snitzer } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) || 517518257b1SMike Snitzer test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 518c1d7ecf7SBart Van Assche if (pg_init_all_paths(m)) 519c1d7ecf7SBart Van Assche return DM_MAPIO_DELAY_REQUEUE; 52006eb061fSBart Van Assche return DM_MAPIO_REQUEUE; 5219bf59a61SMike Snitzer } 5226afbc01dSMike Snitzer 523e8099177SHannes Reinecke mpio->pgpath = pgpath; 524e8099177SHannes Reinecke mpio->nr_bytes = nr_bytes; 5252eb6e1e3SKeith Busch 5262eb6e1e3SKeith Busch bdev = pgpath->path.dev->bdev; 5277083abbbSBart Van Assche q = bdev_get_queue(bdev); 5287083abbbSBart Van Assche clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE, GFP_ATOMIC); 5296599c84eSBart Van Assche if (IS_ERR(clone)) { 5306599c84eSBart Van Assche /* EBUSY, ENODEV or EWOULDBLOCK: requeue */ 5317083abbbSBart Van Assche bool queue_dying = blk_queue_dying(q); 5327083abbbSBart Van Assche if (queue_dying) { 5337083abbbSBart Van Assche atomic_inc(&m->pg_init_in_progress); 5347083abbbSBart Van Assche activate_or_offline_path(pgpath); 5357083abbbSBart Van Assche } 53606eb061fSBart Van Assche return DM_MAPIO_DELAY_REQUEUE; 5374c6dd53dSMike Snitzer } 5386599c84eSBart Van Assche clone->bio = clone->biotail = NULL; 5396599c84eSBart Van Assche clone->rq_disk = bdev->bd_disk; 5406599c84eSBart Van Assche clone->cmd_flags |= REQ_FAILFAST_TRANSPORT; 5416599c84eSBart Van Assche *__clone = clone; 5422eb6e1e3SKeith Busch 543e8099177SHannes Reinecke if (pgpath->pg->ps.type->start_io) 544e8099177SHannes Reinecke pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 545e8099177SHannes Reinecke &pgpath->path, 546e8099177SHannes Reinecke nr_bytes); 5472eb6e1e3SKeith Busch return DM_MAPIO_REMAPPED; 5481da177e4SLinus Torvalds } 5491da177e4SLinus Torvalds 550e5863d9aSMike Snitzer static void multipath_release_clone(struct request *clone) 551e5863d9aSMike Snitzer { 552eb8db831SChristoph Hellwig blk_put_request(clone); 553e5863d9aSMike Snitzer } 554e5863d9aSMike Snitzer 5551da177e4SLinus Torvalds /* 55676e33fe4SMike Snitzer * Map cloned bios (bio-based multipath) 55776e33fe4SMike Snitzer */ 55876e33fe4SMike Snitzer static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio) 55976e33fe4SMike Snitzer { 56076e33fe4SMike Snitzer struct pgpath *pgpath; 56176e33fe4SMike Snitzer unsigned long flags; 56276e33fe4SMike Snitzer bool queue_io; 56376e33fe4SMike Snitzer 56476e33fe4SMike Snitzer /* Do we need to select a new pgpath? */ 565506458efSWill Deacon pgpath = READ_ONCE(m->current_pgpath); 56676e33fe4SMike Snitzer queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags); 56776e33fe4SMike Snitzer if (!pgpath || !queue_io) 568d0442f80SMike Snitzer pgpath = choose_pgpath(m, mpio->nr_bytes); 56976e33fe4SMike Snitzer 57076e33fe4SMike Snitzer if ((pgpath && queue_io) || 57176e33fe4SMike Snitzer (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) { 57276e33fe4SMike Snitzer /* Queue for the daemon to resubmit */ 57376e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 57476e33fe4SMike Snitzer bio_list_add(&m->queued_bios, bio); 57576e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 57676e33fe4SMike Snitzer /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */ 57776e33fe4SMike Snitzer if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 57876e33fe4SMike Snitzer pg_init_all_paths(m); 57976e33fe4SMike Snitzer else if (!queue_io) 58076e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 58176e33fe4SMike Snitzer return DM_MAPIO_SUBMITTED; 58276e33fe4SMike Snitzer } 58376e33fe4SMike Snitzer 58476e33fe4SMike Snitzer if (!pgpath) { 585c1fd0abeSMike Snitzer if (must_push_back_bio(m)) 58676e33fe4SMike Snitzer return DM_MAPIO_REQUEUE; 58718a482f5SChristoph Hellwig dm_report_EIO(m); 588846785e6SChristoph Hellwig return DM_MAPIO_KILL; 58976e33fe4SMike Snitzer } 59076e33fe4SMike Snitzer 59176e33fe4SMike Snitzer mpio->pgpath = pgpath; 59276e33fe4SMike Snitzer 5934e4cbee9SChristoph Hellwig bio->bi_status = 0; 59474d46992SChristoph Hellwig bio_set_dev(bio, pgpath->path.dev->bdev); 5951eff9d32SJens Axboe bio->bi_opf |= REQ_FAILFAST_TRANSPORT; 59676e33fe4SMike Snitzer 59776e33fe4SMike Snitzer if (pgpath->pg->ps.type->start_io) 59876e33fe4SMike Snitzer pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 59976e33fe4SMike Snitzer &pgpath->path, 600d0442f80SMike Snitzer mpio->nr_bytes); 60176e33fe4SMike Snitzer return DM_MAPIO_REMAPPED; 60276e33fe4SMike Snitzer } 60376e33fe4SMike Snitzer 60476e33fe4SMike Snitzer static int multipath_map_bio(struct dm_target *ti, struct bio *bio) 60576e33fe4SMike Snitzer { 60676e33fe4SMike Snitzer struct multipath *m = ti->private; 607bf661be1SMike Snitzer struct dm_mpath_io *mpio = NULL; 608bf661be1SMike Snitzer 60963f6e6fdSMike Snitzer multipath_init_per_bio_data(bio, &mpio); 61076e33fe4SMike Snitzer return __multipath_map_bio(m, bio, mpio); 61176e33fe4SMike Snitzer } 61276e33fe4SMike Snitzer 6137e48c768SMike Snitzer static void process_queued_io_list(struct multipath *m) 61476e33fe4SMike Snitzer { 6157e48c768SMike Snitzer if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED) 6167e48c768SMike Snitzer dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table)); 617*cd025384SMike Snitzer else if (m->queue_mode == DM_TYPE_BIO_BASED || 618*cd025384SMike Snitzer m->queue_mode == DM_TYPE_NVME_BIO_BASED) 61976e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 62076e33fe4SMike Snitzer } 62176e33fe4SMike Snitzer 62276e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work) 62376e33fe4SMike Snitzer { 62476e33fe4SMike Snitzer int r; 62576e33fe4SMike Snitzer unsigned long flags; 62676e33fe4SMike Snitzer struct bio *bio; 62776e33fe4SMike Snitzer struct bio_list bios; 62876e33fe4SMike Snitzer struct blk_plug plug; 62976e33fe4SMike Snitzer struct multipath *m = 63076e33fe4SMike Snitzer container_of(work, struct multipath, process_queued_bios); 63176e33fe4SMike Snitzer 63276e33fe4SMike Snitzer bio_list_init(&bios); 63376e33fe4SMike Snitzer 63476e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 63576e33fe4SMike Snitzer 63676e33fe4SMike Snitzer if (bio_list_empty(&m->queued_bios)) { 63776e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 63876e33fe4SMike Snitzer return; 63976e33fe4SMike Snitzer } 64076e33fe4SMike Snitzer 64176e33fe4SMike Snitzer bio_list_merge(&bios, &m->queued_bios); 64276e33fe4SMike Snitzer bio_list_init(&m->queued_bios); 64376e33fe4SMike Snitzer 64476e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 64576e33fe4SMike Snitzer 64676e33fe4SMike Snitzer blk_start_plug(&plug); 64776e33fe4SMike Snitzer while ((bio = bio_list_pop(&bios))) { 6481836df08SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 6491836df08SMike Snitzer dm_bio_restore(get_bio_details_from_mpio(mpio), bio); 6501836df08SMike Snitzer r = __multipath_map_bio(m, bio, mpio); 651846785e6SChristoph Hellwig switch (r) { 652846785e6SChristoph Hellwig case DM_MAPIO_KILL: 6534e4cbee9SChristoph Hellwig bio->bi_status = BLK_STS_IOERR; 6544e4cbee9SChristoph Hellwig bio_endio(bio); 655047385b3SDan Carpenter break; 656846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE: 6574e4cbee9SChristoph Hellwig bio->bi_status = BLK_STS_DM_REQUEUE; 65876e33fe4SMike Snitzer bio_endio(bio); 659846785e6SChristoph Hellwig break; 660846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED: 66176e33fe4SMike Snitzer generic_make_request(bio); 662846785e6SChristoph Hellwig break; 6639157c8d3SBart Van Assche case 0: 6649157c8d3SBart Van Assche break; 6659157c8d3SBart Van Assche default: 6669157c8d3SBart Van Assche WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r); 667846785e6SChristoph Hellwig } 66876e33fe4SMike Snitzer } 66976e33fe4SMike Snitzer blk_finish_plug(&plug); 67076e33fe4SMike Snitzer } 67176e33fe4SMike Snitzer 67276e33fe4SMike Snitzer /* 6731da177e4SLinus Torvalds * If we run out of usable paths, should we queue I/O or error it? 6741da177e4SLinus Torvalds */ 675be7d31ccSMike Snitzer static int queue_if_no_path(struct multipath *m, bool queue_if_no_path, 676be7d31ccSMike Snitzer bool save_old_value) 6771da177e4SLinus Torvalds { 6781da177e4SLinus Torvalds unsigned long flags; 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 6815307e2adSLukas Wunner assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags, 6825307e2adSLukas Wunner (save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) || 6835307e2adSLukas Wunner (!save_old_value && queue_if_no_path)); 684c1fd0abeSMike Snitzer assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path); 6851da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 6861da177e4SLinus Torvalds 68776e33fe4SMike Snitzer if (!queue_if_no_path) { 68863d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 6897e48c768SMike Snitzer process_queued_io_list(m); 69076e33fe4SMike Snitzer } 69163d832c3SHannes Reinecke 6921da177e4SLinus Torvalds return 0; 6931da177e4SLinus Torvalds } 6941da177e4SLinus Torvalds 6951da177e4SLinus Torvalds /* 6961da177e4SLinus Torvalds * An event is triggered whenever a path is taken out of use. 6971da177e4SLinus Torvalds * Includes path failure and PG bypass. 6981da177e4SLinus Torvalds */ 699c4028958SDavid Howells static void trigger_event(struct work_struct *work) 7001da177e4SLinus Torvalds { 701c4028958SDavid Howells struct multipath *m = 702c4028958SDavid Howells container_of(work, struct multipath, trigger_event); 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds dm_table_event(m->ti->table); 7051da177e4SLinus Torvalds } 7061da177e4SLinus Torvalds 7071da177e4SLinus Torvalds /*----------------------------------------------------------------- 7081da177e4SLinus Torvalds * Constructor/argument parsing: 7091da177e4SLinus Torvalds * <#multipath feature args> [<arg>]* 7101da177e4SLinus Torvalds * <#hw_handler args> [hw_handler [<arg>]*] 7111da177e4SLinus Torvalds * <#priority groups> 7121da177e4SLinus Torvalds * <initial priority group> 7131da177e4SLinus Torvalds * [<selector> <#selector args> [<arg>]* 7141da177e4SLinus Torvalds * <#paths> <#per-path selector args> 7151da177e4SLinus Torvalds * [<path> [<arg>]* ]+ ]+ 7161da177e4SLinus Torvalds *---------------------------------------------------------------*/ 717498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg, 7181da177e4SLinus Torvalds struct dm_target *ti) 7191da177e4SLinus Torvalds { 7201da177e4SLinus Torvalds int r; 7211da177e4SLinus Torvalds struct path_selector_type *pst; 7221da177e4SLinus Torvalds unsigned ps_argc; 7231da177e4SLinus Torvalds 7245916a22bSEric Biggers static const struct dm_arg _args[] = { 72572d94861SAlasdair G Kergon {0, 1024, "invalid number of path selector args"}, 7261da177e4SLinus Torvalds }; 7271da177e4SLinus Torvalds 728498f0103SMike Snitzer pst = dm_get_path_selector(dm_shift_arg(as)); 7291da177e4SLinus Torvalds if (!pst) { 73072d94861SAlasdair G Kergon ti->error = "unknown path selector type"; 7311da177e4SLinus Torvalds return -EINVAL; 7321da177e4SLinus Torvalds } 7331da177e4SLinus Torvalds 734498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &ps_argc, &ti->error); 735371b2e34SMikulas Patocka if (r) { 736371b2e34SMikulas Patocka dm_put_path_selector(pst); 7371da177e4SLinus Torvalds return -EINVAL; 738371b2e34SMikulas Patocka } 7391da177e4SLinus Torvalds 7401da177e4SLinus Torvalds r = pst->create(&pg->ps, ps_argc, as->argv); 7411da177e4SLinus Torvalds if (r) { 7421da177e4SLinus Torvalds dm_put_path_selector(pst); 74372d94861SAlasdair G Kergon ti->error = "path selector constructor failed"; 7441da177e4SLinus Torvalds return r; 7451da177e4SLinus Torvalds } 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds pg->ps.type = pst; 748498f0103SMike Snitzer dm_consume_args(as, ps_argc); 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds return 0; 7511da177e4SLinus Torvalds } 7521da177e4SLinus Torvalds 753498f0103SMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps, 7541da177e4SLinus Torvalds struct dm_target *ti) 7551da177e4SLinus Torvalds { 7561da177e4SLinus Torvalds int r; 7571da177e4SLinus Torvalds struct pgpath *p; 758ae11b1b3SHannes Reinecke struct multipath *m = ti->private; 759a58a935dSMike Snitzer struct request_queue *q = NULL; 760a58a935dSMike Snitzer const char *attached_handler_name; 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds /* we need at least a path arg */ 7631da177e4SLinus Torvalds if (as->argc < 1) { 76472d94861SAlasdair G Kergon ti->error = "no device given"; 76501460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 7661da177e4SLinus Torvalds } 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds p = alloc_pgpath(); 7691da177e4SLinus Torvalds if (!p) 77001460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 7711da177e4SLinus Torvalds 772498f0103SMike Snitzer r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table), 7738215d6ecSNikanth Karthikesan &p->path.dev); 7741da177e4SLinus Torvalds if (r) { 77572d94861SAlasdair G Kergon ti->error = "error getting device"; 7761da177e4SLinus Torvalds goto bad; 7771da177e4SLinus Torvalds } 7781da177e4SLinus Torvalds 779518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name) 780a58a935dSMike Snitzer q = bdev_get_queue(p->path.dev->bdev); 781a0cf7ea9SHannes Reinecke 782518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) { 7831bab0de0SChristoph Hellwig retain: 784a58a935dSMike Snitzer attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL); 785a58a935dSMike Snitzer if (attached_handler_name) { 786a58a935dSMike Snitzer /* 78754cd640dStang.junhui * Clear any hw_handler_params associated with a 78854cd640dStang.junhui * handler that isn't already attached. 78954cd640dStang.junhui */ 79054cd640dStang.junhui if (m->hw_handler_name && strcmp(attached_handler_name, m->hw_handler_name)) { 79154cd640dStang.junhui kfree(m->hw_handler_params); 79254cd640dStang.junhui m->hw_handler_params = NULL; 79354cd640dStang.junhui } 79454cd640dStang.junhui 79554cd640dStang.junhui /* 796a58a935dSMike Snitzer * Reset hw_handler_name to match the attached handler 797a58a935dSMike Snitzer * 798a58a935dSMike Snitzer * NB. This modifies the table line to show the actual 799a58a935dSMike Snitzer * handler instead of the original table passed in. 800a58a935dSMike Snitzer */ 801a58a935dSMike Snitzer kfree(m->hw_handler_name); 802a58a935dSMike Snitzer m->hw_handler_name = attached_handler_name; 803a58a935dSMike Snitzer } 804a58a935dSMike Snitzer } 805a58a935dSMike Snitzer 806a58a935dSMike Snitzer if (m->hw_handler_name) { 807a0cf7ea9SHannes Reinecke r = scsi_dh_attach(q, m->hw_handler_name); 808a0cf7ea9SHannes Reinecke if (r == -EBUSY) { 8091bab0de0SChristoph Hellwig char b[BDEVNAME_SIZE]; 810a0cf7ea9SHannes Reinecke 8111bab0de0SChristoph Hellwig printk(KERN_INFO "dm-mpath: retaining handler on device %s\n", 8121bab0de0SChristoph Hellwig bdevname(p->path.dev->bdev, b)); 8131bab0de0SChristoph Hellwig goto retain; 8141bab0de0SChristoph Hellwig } 815ae11b1b3SHannes Reinecke if (r < 0) { 816a0cf7ea9SHannes Reinecke ti->error = "error attaching hardware handler"; 817ae11b1b3SHannes Reinecke dm_put_device(ti, p->path.dev); 818ae11b1b3SHannes Reinecke goto bad; 819ae11b1b3SHannes Reinecke } 8202bfd2e13SChandra Seetharaman 8212bfd2e13SChandra Seetharaman if (m->hw_handler_params) { 8222bfd2e13SChandra Seetharaman r = scsi_dh_set_params(q, m->hw_handler_params); 8232bfd2e13SChandra Seetharaman if (r < 0) { 8242bfd2e13SChandra Seetharaman ti->error = "unable to set hardware " 8252bfd2e13SChandra Seetharaman "handler parameters"; 8262bfd2e13SChandra Seetharaman dm_put_device(ti, p->path.dev); 8272bfd2e13SChandra Seetharaman goto bad; 8282bfd2e13SChandra Seetharaman } 8292bfd2e13SChandra Seetharaman } 830ae11b1b3SHannes Reinecke } 831ae11b1b3SHannes Reinecke 8321da177e4SLinus Torvalds r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); 8331da177e4SLinus Torvalds if (r) { 8341da177e4SLinus Torvalds dm_put_device(ti, p->path.dev); 8351da177e4SLinus Torvalds goto bad; 8361da177e4SLinus Torvalds } 8371da177e4SLinus Torvalds 8381da177e4SLinus Torvalds return p; 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds bad: 8411da177e4SLinus Torvalds free_pgpath(p); 84201460f35SBenjamin Marzinski return ERR_PTR(r); 8431da177e4SLinus Torvalds } 8441da177e4SLinus Torvalds 845498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as, 84628f16c20SMicha³ Miros³aw struct multipath *m) 8471da177e4SLinus Torvalds { 8485916a22bSEric Biggers static const struct dm_arg _args[] = { 84972d94861SAlasdair G Kergon {1, 1024, "invalid number of paths"}, 85072d94861SAlasdair G Kergon {0, 1024, "invalid number of selector args"} 8511da177e4SLinus Torvalds }; 8521da177e4SLinus Torvalds 8531da177e4SLinus Torvalds int r; 854498f0103SMike Snitzer unsigned i, nr_selector_args, nr_args; 8551da177e4SLinus Torvalds struct priority_group *pg; 85628f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 8571da177e4SLinus Torvalds 8581da177e4SLinus Torvalds if (as->argc < 2) { 8591da177e4SLinus Torvalds as->argc = 0; 86001460f35SBenjamin Marzinski ti->error = "not enough priority group arguments"; 86101460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 8641da177e4SLinus Torvalds pg = alloc_priority_group(); 8651da177e4SLinus Torvalds if (!pg) { 86672d94861SAlasdair G Kergon ti->error = "couldn't allocate priority group"; 86701460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 8681da177e4SLinus Torvalds } 8691da177e4SLinus Torvalds pg->m = m; 8701da177e4SLinus Torvalds 8711da177e4SLinus Torvalds r = parse_path_selector(as, pg, ti); 8721da177e4SLinus Torvalds if (r) 8731da177e4SLinus Torvalds goto bad; 8741da177e4SLinus Torvalds 8751da177e4SLinus Torvalds /* 8761da177e4SLinus Torvalds * read the paths 8771da177e4SLinus Torvalds */ 878498f0103SMike Snitzer r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error); 8791da177e4SLinus Torvalds if (r) 8801da177e4SLinus Torvalds goto bad; 8811da177e4SLinus Torvalds 882498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error); 8831da177e4SLinus Torvalds if (r) 8841da177e4SLinus Torvalds goto bad; 8851da177e4SLinus Torvalds 886498f0103SMike Snitzer nr_args = 1 + nr_selector_args; 8871da177e4SLinus Torvalds for (i = 0; i < pg->nr_pgpaths; i++) { 8881da177e4SLinus Torvalds struct pgpath *pgpath; 889498f0103SMike Snitzer struct dm_arg_set path_args; 8901da177e4SLinus Torvalds 891498f0103SMike Snitzer if (as->argc < nr_args) { 892148acff6SMikulas Patocka ti->error = "not enough path parameters"; 8936bbf79a1SAlasdair G Kergon r = -EINVAL; 8941da177e4SLinus Torvalds goto bad; 895148acff6SMikulas Patocka } 8961da177e4SLinus Torvalds 897498f0103SMike Snitzer path_args.argc = nr_args; 8981da177e4SLinus Torvalds path_args.argv = as->argv; 8991da177e4SLinus Torvalds 9001da177e4SLinus Torvalds pgpath = parse_path(&path_args, &pg->ps, ti); 90101460f35SBenjamin Marzinski if (IS_ERR(pgpath)) { 90201460f35SBenjamin Marzinski r = PTR_ERR(pgpath); 9031da177e4SLinus Torvalds goto bad; 90401460f35SBenjamin Marzinski } 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds pgpath->pg = pg; 9071da177e4SLinus Torvalds list_add_tail(&pgpath->list, &pg->pgpaths); 908498f0103SMike Snitzer dm_consume_args(as, nr_args); 9091da177e4SLinus Torvalds } 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds return pg; 9121da177e4SLinus Torvalds 9131da177e4SLinus Torvalds bad: 9141da177e4SLinus Torvalds free_priority_group(pg, ti); 91501460f35SBenjamin Marzinski return ERR_PTR(r); 9161da177e4SLinus Torvalds } 9171da177e4SLinus Torvalds 918498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m) 9191da177e4SLinus Torvalds { 9201da177e4SLinus Torvalds unsigned hw_argc; 9212bfd2e13SChandra Seetharaman int ret; 92228f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 9231da177e4SLinus Torvalds 9245916a22bSEric Biggers static const struct dm_arg _args[] = { 92572d94861SAlasdair G Kergon {0, 1024, "invalid number of hardware handler args"}, 9261da177e4SLinus Torvalds }; 9271da177e4SLinus Torvalds 928498f0103SMike Snitzer if (dm_read_arg_group(_args, as, &hw_argc, &ti->error)) 9291da177e4SLinus Torvalds return -EINVAL; 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds if (!hw_argc) 9321da177e4SLinus Torvalds return 0; 9331da177e4SLinus Torvalds 934*cd025384SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED || 935*cd025384SMike Snitzer m->queue_mode == DM_TYPE_NVME_BIO_BASED) { 93676e33fe4SMike Snitzer dm_consume_args(as, hw_argc); 93776e33fe4SMike Snitzer DMERR("bio-based multipath doesn't allow hardware handler args"); 93876e33fe4SMike Snitzer return 0; 93976e33fe4SMike Snitzer } 94076e33fe4SMike Snitzer 941498f0103SMike Snitzer m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL); 942f97dc421Stang.junhui if (!m->hw_handler_name) 943f97dc421Stang.junhui return -EINVAL; 94414e98c5cSChandra Seetharaman 9452bfd2e13SChandra Seetharaman if (hw_argc > 1) { 9462bfd2e13SChandra Seetharaman char *p; 9472bfd2e13SChandra Seetharaman int i, j, len = 4; 9482bfd2e13SChandra Seetharaman 9492bfd2e13SChandra Seetharaman for (i = 0; i <= hw_argc - 2; i++) 9502bfd2e13SChandra Seetharaman len += strlen(as->argv[i]) + 1; 9512bfd2e13SChandra Seetharaman p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); 9522bfd2e13SChandra Seetharaman if (!p) { 9532bfd2e13SChandra Seetharaman ti->error = "memory allocation failed"; 9542bfd2e13SChandra Seetharaman ret = -ENOMEM; 9552bfd2e13SChandra Seetharaman goto fail; 9562bfd2e13SChandra Seetharaman } 9572bfd2e13SChandra Seetharaman j = sprintf(p, "%d", hw_argc - 1); 9582bfd2e13SChandra Seetharaman for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1) 9592bfd2e13SChandra Seetharaman j = sprintf(p, "%s", as->argv[i]); 9602bfd2e13SChandra Seetharaman } 961498f0103SMike Snitzer dm_consume_args(as, hw_argc - 1); 9621da177e4SLinus Torvalds 9631da177e4SLinus Torvalds return 0; 9642bfd2e13SChandra Seetharaman fail: 9652bfd2e13SChandra Seetharaman kfree(m->hw_handler_name); 9662bfd2e13SChandra Seetharaman m->hw_handler_name = NULL; 9672bfd2e13SChandra Seetharaman return ret; 9681da177e4SLinus Torvalds } 9691da177e4SLinus Torvalds 970498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m) 9711da177e4SLinus Torvalds { 9721da177e4SLinus Torvalds int r; 9731da177e4SLinus Torvalds unsigned argc; 97428f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 975498f0103SMike Snitzer const char *arg_name; 9761da177e4SLinus Torvalds 9775916a22bSEric Biggers static const struct dm_arg _args[] = { 978e83068a5SMike Snitzer {0, 8, "invalid number of feature args"}, 979c9e45581SDave Wysochanski {1, 50, "pg_init_retries must be between 1 and 50"}, 9804e2d19e4SChandra Seetharaman {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"}, 9811da177e4SLinus Torvalds }; 9821da177e4SLinus Torvalds 983498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &argc, &ti->error); 9841da177e4SLinus Torvalds if (r) 9851da177e4SLinus Torvalds return -EINVAL; 9861da177e4SLinus Torvalds 9871da177e4SLinus Torvalds if (!argc) 9881da177e4SLinus Torvalds return 0; 9891da177e4SLinus Torvalds 990c9e45581SDave Wysochanski do { 991498f0103SMike Snitzer arg_name = dm_shift_arg(as); 992c9e45581SDave Wysochanski argc--; 993c9e45581SDave Wysochanski 994498f0103SMike Snitzer if (!strcasecmp(arg_name, "queue_if_no_path")) { 995be7d31ccSMike Snitzer r = queue_if_no_path(m, true, false); 996c9e45581SDave Wysochanski continue; 9971da177e4SLinus Torvalds } 998c9e45581SDave Wysochanski 999a58a935dSMike Snitzer if (!strcasecmp(arg_name, "retain_attached_hw_handler")) { 1000518257b1SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 1001a58a935dSMike Snitzer continue; 1002a58a935dSMike Snitzer } 1003a58a935dSMike Snitzer 1004498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_retries") && 1005c9e45581SDave Wysochanski (argc >= 1)) { 1006498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error); 1007c9e45581SDave Wysochanski argc--; 1008c9e45581SDave Wysochanski continue; 1009c9e45581SDave Wysochanski } 1010c9e45581SDave Wysochanski 1011498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_delay_msecs") && 10124e2d19e4SChandra Seetharaman (argc >= 1)) { 1013498f0103SMike Snitzer r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error); 10144e2d19e4SChandra Seetharaman argc--; 10154e2d19e4SChandra Seetharaman continue; 10164e2d19e4SChandra Seetharaman } 10174e2d19e4SChandra Seetharaman 1018e83068a5SMike Snitzer if (!strcasecmp(arg_name, "queue_mode") && 1019e83068a5SMike Snitzer (argc >= 1)) { 1020e83068a5SMike Snitzer const char *queue_mode_name = dm_shift_arg(as); 1021e83068a5SMike Snitzer 1022e83068a5SMike Snitzer if (!strcasecmp(queue_mode_name, "bio")) 1023e83068a5SMike Snitzer m->queue_mode = DM_TYPE_BIO_BASED; 1024*cd025384SMike Snitzer else if (!strcasecmp(queue_mode_name, "nvme")) 1025*cd025384SMike Snitzer m->queue_mode = DM_TYPE_NVME_BIO_BASED; 1026e83068a5SMike Snitzer else if (!strcasecmp(queue_mode_name, "rq")) 1027e83068a5SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 1028e83068a5SMike Snitzer else if (!strcasecmp(queue_mode_name, "mq")) 1029e83068a5SMike Snitzer m->queue_mode = DM_TYPE_MQ_REQUEST_BASED; 1030e83068a5SMike Snitzer else { 1031e83068a5SMike Snitzer ti->error = "Unknown 'queue_mode' requested"; 1032e83068a5SMike Snitzer r = -EINVAL; 1033e83068a5SMike Snitzer } 1034e83068a5SMike Snitzer argc--; 1035e83068a5SMike Snitzer continue; 1036e83068a5SMike Snitzer } 1037e83068a5SMike Snitzer 1038c9e45581SDave Wysochanski ti->error = "Unrecognised multipath feature request"; 1039c9e45581SDave Wysochanski r = -EINVAL; 1040c9e45581SDave Wysochanski } while (argc && !r); 1041c9e45581SDave Wysochanski 1042c9e45581SDave Wysochanski return r; 10431da177e4SLinus Torvalds } 10441da177e4SLinus Torvalds 1045e83068a5SMike Snitzer static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv) 10461da177e4SLinus Torvalds { 1047498f0103SMike Snitzer /* target arguments */ 10485916a22bSEric Biggers static const struct dm_arg _args[] = { 1049a490a07aSMike Snitzer {0, 1024, "invalid number of priority groups"}, 1050a490a07aSMike Snitzer {0, 1024, "invalid initial priority group number"}, 10511da177e4SLinus Torvalds }; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds int r; 10541da177e4SLinus Torvalds struct multipath *m; 1055498f0103SMike Snitzer struct dm_arg_set as; 10561da177e4SLinus Torvalds unsigned pg_count = 0; 10571da177e4SLinus Torvalds unsigned next_pg_num; 10581da177e4SLinus Torvalds 10591da177e4SLinus Torvalds as.argc = argc; 10601da177e4SLinus Torvalds as.argv = argv; 10611da177e4SLinus Torvalds 1062e83068a5SMike Snitzer m = alloc_multipath(ti); 10631da177e4SLinus Torvalds if (!m) { 106472d94861SAlasdair G Kergon ti->error = "can't allocate multipath"; 10651da177e4SLinus Torvalds return -EINVAL; 10661da177e4SLinus Torvalds } 10671da177e4SLinus Torvalds 106828f16c20SMicha³ Miros³aw r = parse_features(&as, m); 10691da177e4SLinus Torvalds if (r) 10701da177e4SLinus Torvalds goto bad; 10711da177e4SLinus Torvalds 1072e83068a5SMike Snitzer r = alloc_multipath_stage2(ti, m); 1073e83068a5SMike Snitzer if (r) 1074e83068a5SMike Snitzer goto bad; 1075e83068a5SMike Snitzer 107628f16c20SMicha³ Miros³aw r = parse_hw_handler(&as, m); 10771da177e4SLinus Torvalds if (r) 10781da177e4SLinus Torvalds goto bad; 10791da177e4SLinus Torvalds 1080498f0103SMike Snitzer r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error); 10811da177e4SLinus Torvalds if (r) 10821da177e4SLinus Torvalds goto bad; 10831da177e4SLinus Torvalds 1084498f0103SMike Snitzer r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error); 10851da177e4SLinus Torvalds if (r) 10861da177e4SLinus Torvalds goto bad; 10871da177e4SLinus Torvalds 1088a490a07aSMike Snitzer if ((!m->nr_priority_groups && next_pg_num) || 1089a490a07aSMike Snitzer (m->nr_priority_groups && !next_pg_num)) { 1090a490a07aSMike Snitzer ti->error = "invalid initial priority group"; 1091a490a07aSMike Snitzer r = -EINVAL; 1092a490a07aSMike Snitzer goto bad; 1093a490a07aSMike Snitzer } 1094a490a07aSMike Snitzer 10951da177e4SLinus Torvalds /* parse the priority groups */ 10961da177e4SLinus Torvalds while (as.argc) { 10971da177e4SLinus Torvalds struct priority_group *pg; 109891e968aaSMike Snitzer unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths); 10991da177e4SLinus Torvalds 110028f16c20SMicha³ Miros³aw pg = parse_priority_group(&as, m); 110101460f35SBenjamin Marzinski if (IS_ERR(pg)) { 110201460f35SBenjamin Marzinski r = PTR_ERR(pg); 11031da177e4SLinus Torvalds goto bad; 11041da177e4SLinus Torvalds } 11051da177e4SLinus Torvalds 110691e968aaSMike Snitzer nr_valid_paths += pg->nr_pgpaths; 110791e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, nr_valid_paths); 110891e968aaSMike Snitzer 11091da177e4SLinus Torvalds list_add_tail(&pg->list, &m->priority_groups); 11101da177e4SLinus Torvalds pg_count++; 11111da177e4SLinus Torvalds pg->pg_num = pg_count; 11121da177e4SLinus Torvalds if (!--next_pg_num) 11131da177e4SLinus Torvalds m->next_pg = pg; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds if (pg_count != m->nr_priority_groups) { 111772d94861SAlasdair G Kergon ti->error = "priority group count mismatch"; 11181da177e4SLinus Torvalds r = -EINVAL; 11191da177e4SLinus Torvalds goto bad; 11201da177e4SLinus Torvalds } 11211da177e4SLinus Torvalds 112255a62eefSAlasdair G Kergon ti->num_flush_bios = 1; 112355a62eefSAlasdair G Kergon ti->num_discard_bios = 1; 1124042bcef8SMike Snitzer ti->num_write_same_bios = 1; 1125ac62d620SChristoph Hellwig ti->num_write_zeroes_bios = 1; 1126*cd025384SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED || m->queue_mode == DM_TYPE_NVME_BIO_BASED) 1127bf661be1SMike Snitzer ti->per_io_data_size = multipath_per_bio_data_size(); 1128eb8db831SChristoph Hellwig else 11298637a6bfSMike Snitzer ti->per_io_data_size = sizeof(struct dm_mpath_io); 11308627921fSMikulas Patocka 11311da177e4SLinus Torvalds return 0; 11321da177e4SLinus Torvalds 11331da177e4SLinus Torvalds bad: 11341da177e4SLinus Torvalds free_multipath(m); 11351da177e4SLinus Torvalds return r; 11361da177e4SLinus Torvalds } 11371da177e4SLinus Torvalds 11382bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m) 11392bded7bdSKiyoshi Ueda { 11409f4c3f87SBart Van Assche DEFINE_WAIT(wait); 11412bded7bdSKiyoshi Ueda 11422bded7bdSKiyoshi Ueda while (1) { 11439f4c3f87SBart Van Assche prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE); 11442bded7bdSKiyoshi Ueda 114591e968aaSMike Snitzer if (!atomic_read(&m->pg_init_in_progress)) 11462bded7bdSKiyoshi Ueda break; 11472bded7bdSKiyoshi Ueda 11482bded7bdSKiyoshi Ueda io_schedule(); 11492bded7bdSKiyoshi Ueda } 11509f4c3f87SBart Van Assche finish_wait(&m->pg_init_wait, &wait); 11512bded7bdSKiyoshi Ueda } 11522bded7bdSKiyoshi Ueda 11532bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m) 11541da177e4SLinus Torvalds { 1155518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1156518257b1SMike Snitzer smp_mb__after_atomic(); 1157954a73d5SShiva Krishna Merla 1158bab7cfc7SChandra Seetharaman flush_workqueue(kmpath_handlerd); 11592bded7bdSKiyoshi Ueda multipath_wait_for_pg_init_completion(m); 1160a044d016SAlasdair G Kergon flush_workqueue(kmultipathd); 116143829731STejun Heo flush_work(&m->trigger_event); 1162954a73d5SShiva Krishna Merla 1163518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1164518257b1SMike Snitzer smp_mb__after_atomic(); 11656df400abSKiyoshi Ueda } 11666df400abSKiyoshi Ueda 11676df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti) 11686df400abSKiyoshi Ueda { 11696df400abSKiyoshi Ueda struct multipath *m = ti->private; 11706df400abSKiyoshi Ueda 11712bded7bdSKiyoshi Ueda flush_multipath_work(m); 11721da177e4SLinus Torvalds free_multipath(m); 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds /* 11761da177e4SLinus Torvalds * Take a path out of use. 11771da177e4SLinus Torvalds */ 11781da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath) 11791da177e4SLinus Torvalds { 11801da177e4SLinus Torvalds unsigned long flags; 11811da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 11841da177e4SLinus Torvalds 11856680073dSKiyoshi Ueda if (!pgpath->is_active) 11861da177e4SLinus Torvalds goto out; 11871da177e4SLinus Torvalds 118872d94861SAlasdair G Kergon DMWARN("Failing path %s.", pgpath->path.dev->name); 11891da177e4SLinus Torvalds 11901da177e4SLinus Torvalds pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); 1191be7d31ccSMike Snitzer pgpath->is_active = false; 11921da177e4SLinus Torvalds pgpath->fail_count++; 11931da177e4SLinus Torvalds 119491e968aaSMike Snitzer atomic_dec(&m->nr_valid_paths); 11951da177e4SLinus Torvalds 11961da177e4SLinus Torvalds if (pgpath == m->current_pgpath) 11971da177e4SLinus Torvalds m->current_pgpath = NULL; 11981da177e4SLinus Torvalds 1199b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti, 120091e968aaSMike Snitzer pgpath->path.dev->name, atomic_read(&m->nr_valid_paths)); 1201b15546f9SMike Anderson 1202fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 12031da177e4SLinus Torvalds 12041da177e4SLinus Torvalds out: 12051da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 12061da177e4SLinus Torvalds 12071da177e4SLinus Torvalds return 0; 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds 12101da177e4SLinus Torvalds /* 12111da177e4SLinus Torvalds * Reinstate a previously-failed path 12121da177e4SLinus Torvalds */ 12131da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath) 12141da177e4SLinus Torvalds { 121563d832c3SHannes Reinecke int r = 0, run_queue = 0; 12161da177e4SLinus Torvalds unsigned long flags; 12171da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 121891e968aaSMike Snitzer unsigned nr_valid_paths; 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 12211da177e4SLinus Torvalds 12226680073dSKiyoshi Ueda if (pgpath->is_active) 12231da177e4SLinus Torvalds goto out; 12241da177e4SLinus Torvalds 1225ec31f3f7SMike Snitzer DMWARN("Reinstating path %s.", pgpath->path.dev->name); 12261da177e4SLinus Torvalds 12271da177e4SLinus Torvalds r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); 12281da177e4SLinus Torvalds if (r) 12291da177e4SLinus Torvalds goto out; 12301da177e4SLinus Torvalds 1231be7d31ccSMike Snitzer pgpath->is_active = true; 12321da177e4SLinus Torvalds 123391e968aaSMike Snitzer nr_valid_paths = atomic_inc_return(&m->nr_valid_paths); 123491e968aaSMike Snitzer if (nr_valid_paths == 1) { 12351da177e4SLinus Torvalds m->current_pgpath = NULL; 123663d832c3SHannes Reinecke run_queue = 1; 1237e54f77ddSChandra Seetharaman } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) { 12384e2d19e4SChandra Seetharaman if (queue_work(kmpath_handlerd, &pgpath->activate_path.work)) 123991e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 1240e54f77ddSChandra Seetharaman } 12411da177e4SLinus Torvalds 1242b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti, 124391e968aaSMike Snitzer pgpath->path.dev->name, nr_valid_paths); 1244b15546f9SMike Anderson 1245fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds out: 12481da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 124976e33fe4SMike Snitzer if (run_queue) { 125063d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 12517e48c768SMike Snitzer process_queued_io_list(m); 125276e33fe4SMike Snitzer } 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds return r; 12551da177e4SLinus Torvalds } 12561da177e4SLinus Torvalds 12571da177e4SLinus Torvalds /* 12581da177e4SLinus Torvalds * Fail or reinstate all paths that match the provided struct dm_dev. 12591da177e4SLinus Torvalds */ 12601da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev, 12611da177e4SLinus Torvalds action_fn action) 12621da177e4SLinus Torvalds { 126319040c0bSMike Snitzer int r = -EINVAL; 12641da177e4SLinus Torvalds struct pgpath *pgpath; 12651da177e4SLinus Torvalds struct priority_group *pg; 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 12681da177e4SLinus Torvalds list_for_each_entry(pgpath, &pg->pgpaths, list) { 12691da177e4SLinus Torvalds if (pgpath->path.dev == dev) 12701da177e4SLinus Torvalds r = action(pgpath); 12711da177e4SLinus Torvalds } 12721da177e4SLinus Torvalds } 12731da177e4SLinus Torvalds 12741da177e4SLinus Torvalds return r; 12751da177e4SLinus Torvalds } 12761da177e4SLinus Torvalds 12771da177e4SLinus Torvalds /* 12781da177e4SLinus Torvalds * Temporarily try to avoid having to use the specified PG 12791da177e4SLinus Torvalds */ 12801da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg, 1281be7d31ccSMike Snitzer bool bypassed) 12821da177e4SLinus Torvalds { 12831da177e4SLinus Torvalds unsigned long flags; 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds pg->bypassed = bypassed; 12881da177e4SLinus Torvalds m->current_pgpath = NULL; 12891da177e4SLinus Torvalds m->current_pg = NULL; 12901da177e4SLinus Torvalds 12911da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 12921da177e4SLinus Torvalds 1293fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 12941da177e4SLinus Torvalds } 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds /* 12971da177e4SLinus Torvalds * Switch to using the specified PG from the next I/O that gets mapped 12981da177e4SLinus Torvalds */ 12991da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr) 13001da177e4SLinus Torvalds { 13011da177e4SLinus Torvalds struct priority_group *pg; 13021da177e4SLinus Torvalds unsigned pgnum; 13031da177e4SLinus Torvalds unsigned long flags; 130431998ef1SMikulas Patocka char dummy; 13051da177e4SLinus Torvalds 130631998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 1307cc5bd925Stang.junhui !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) { 13081da177e4SLinus Torvalds DMWARN("invalid PG number supplied to switch_pg_num"); 13091da177e4SLinus Torvalds return -EINVAL; 13101da177e4SLinus Torvalds } 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13131da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 1314be7d31ccSMike Snitzer pg->bypassed = false; 13151da177e4SLinus Torvalds if (--pgnum) 13161da177e4SLinus Torvalds continue; 13171da177e4SLinus Torvalds 13181da177e4SLinus Torvalds m->current_pgpath = NULL; 13191da177e4SLinus Torvalds m->current_pg = NULL; 13201da177e4SLinus Torvalds m->next_pg = pg; 13211da177e4SLinus Torvalds } 13221da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13231da177e4SLinus Torvalds 1324fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13251da177e4SLinus Torvalds return 0; 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds /* 13291da177e4SLinus Torvalds * Set/clear bypassed status of a PG. 13301da177e4SLinus Torvalds * PGs are numbered upwards from 1 in the order they were declared. 13311da177e4SLinus Torvalds */ 1332be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed) 13331da177e4SLinus Torvalds { 13341da177e4SLinus Torvalds struct priority_group *pg; 13351da177e4SLinus Torvalds unsigned pgnum; 133631998ef1SMikulas Patocka char dummy; 13371da177e4SLinus Torvalds 133831998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 1339cc5bd925Stang.junhui !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) { 13401da177e4SLinus Torvalds DMWARN("invalid PG number supplied to bypass_pg"); 13411da177e4SLinus Torvalds return -EINVAL; 13421da177e4SLinus Torvalds } 13431da177e4SLinus Torvalds 13441da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 13451da177e4SLinus Torvalds if (!--pgnum) 13461da177e4SLinus Torvalds break; 13471da177e4SLinus Torvalds } 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds bypass_pg(m, pg, bypassed); 13501da177e4SLinus Torvalds return 0; 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds /* 1354c9e45581SDave Wysochanski * Should we retry pg_init immediately? 1355c9e45581SDave Wysochanski */ 1356be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath) 1357c9e45581SDave Wysochanski { 1358c9e45581SDave Wysochanski unsigned long flags; 1359be7d31ccSMike Snitzer bool limit_reached = false; 1360c9e45581SDave Wysochanski 1361c9e45581SDave Wysochanski spin_lock_irqsave(&m->lock, flags); 1362c9e45581SDave Wysochanski 136391e968aaSMike Snitzer if (atomic_read(&m->pg_init_count) <= m->pg_init_retries && 136491e968aaSMike Snitzer !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 1365518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 1366c9e45581SDave Wysochanski else 1367be7d31ccSMike Snitzer limit_reached = true; 1368c9e45581SDave Wysochanski 1369c9e45581SDave Wysochanski spin_unlock_irqrestore(&m->lock, flags); 1370c9e45581SDave Wysochanski 1371c9e45581SDave Wysochanski return limit_reached; 1372c9e45581SDave Wysochanski } 1373c9e45581SDave Wysochanski 13743ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors) 1375cfae5c9bSChandra Seetharaman { 137683c0d5d5SMoger, Babu struct pgpath *pgpath = data; 1377cfae5c9bSChandra Seetharaman struct priority_group *pg = pgpath->pg; 1378cfae5c9bSChandra Seetharaman struct multipath *m = pg->m; 1379cfae5c9bSChandra Seetharaman unsigned long flags; 1380be7d31ccSMike Snitzer bool delay_retry = false; 1381cfae5c9bSChandra Seetharaman 1382cfae5c9bSChandra Seetharaman /* device or driver problems */ 1383cfae5c9bSChandra Seetharaman switch (errors) { 1384cfae5c9bSChandra Seetharaman case SCSI_DH_OK: 1385cfae5c9bSChandra Seetharaman break; 1386cfae5c9bSChandra Seetharaman case SCSI_DH_NOSYS: 1387cfae5c9bSChandra Seetharaman if (!m->hw_handler_name) { 1388cfae5c9bSChandra Seetharaman errors = 0; 1389cfae5c9bSChandra Seetharaman break; 1390cfae5c9bSChandra Seetharaman } 1391f7b934c8SMoger, Babu DMERR("Could not failover the device: Handler scsi_dh_%s " 1392f7b934c8SMoger, Babu "Error %d.", m->hw_handler_name, errors); 1393cfae5c9bSChandra Seetharaman /* 1394cfae5c9bSChandra Seetharaman * Fail path for now, so we do not ping pong 1395cfae5c9bSChandra Seetharaman */ 1396cfae5c9bSChandra Seetharaman fail_path(pgpath); 1397cfae5c9bSChandra Seetharaman break; 1398cfae5c9bSChandra Seetharaman case SCSI_DH_DEV_TEMP_BUSY: 1399cfae5c9bSChandra Seetharaman /* 1400cfae5c9bSChandra Seetharaman * Probably doing something like FW upgrade on the 1401cfae5c9bSChandra Seetharaman * controller so try the other pg. 1402cfae5c9bSChandra Seetharaman */ 1403be7d31ccSMike Snitzer bypass_pg(m, pg, true); 1404cfae5c9bSChandra Seetharaman break; 1405cfae5c9bSChandra Seetharaman case SCSI_DH_RETRY: 14064e2d19e4SChandra Seetharaman /* Wait before retrying. */ 14074e2d19e4SChandra Seetharaman delay_retry = 1; 14087b06e09aSBart Van Assche /* fall through */ 1409cfae5c9bSChandra Seetharaman case SCSI_DH_IMM_RETRY: 1410cfae5c9bSChandra Seetharaman case SCSI_DH_RES_TEMP_UNAVAIL: 1411cfae5c9bSChandra Seetharaman if (pg_init_limit_reached(m, pgpath)) 1412cfae5c9bSChandra Seetharaman fail_path(pgpath); 1413cfae5c9bSChandra Seetharaman errors = 0; 1414cfae5c9bSChandra Seetharaman break; 1415ec31f3f7SMike Snitzer case SCSI_DH_DEV_OFFLINED: 1416cfae5c9bSChandra Seetharaman default: 1417cfae5c9bSChandra Seetharaman /* 1418cfae5c9bSChandra Seetharaman * We probably do not want to fail the path for a device 1419cfae5c9bSChandra Seetharaman * error, but this is what the old dm did. In future 1420cfae5c9bSChandra Seetharaman * patches we can do more advanced handling. 1421cfae5c9bSChandra Seetharaman */ 1422cfae5c9bSChandra Seetharaman fail_path(pgpath); 1423cfae5c9bSChandra Seetharaman } 1424cfae5c9bSChandra Seetharaman 1425cfae5c9bSChandra Seetharaman spin_lock_irqsave(&m->lock, flags); 1426cfae5c9bSChandra Seetharaman if (errors) { 1427e54f77ddSChandra Seetharaman if (pgpath == m->current_pgpath) { 1428cfae5c9bSChandra Seetharaman DMERR("Could not failover device. Error %d.", errors); 1429cfae5c9bSChandra Seetharaman m->current_pgpath = NULL; 1430cfae5c9bSChandra Seetharaman m->current_pg = NULL; 1431e54f77ddSChandra Seetharaman } 1432518257b1SMike Snitzer } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 1433be7d31ccSMike Snitzer pg->bypassed = false; 1434cfae5c9bSChandra Seetharaman 143591e968aaSMike Snitzer if (atomic_dec_return(&m->pg_init_in_progress) > 0) 1436d0259bf0SKiyoshi Ueda /* Activations of other paths are still on going */ 1437d0259bf0SKiyoshi Ueda goto out; 1438d0259bf0SKiyoshi Ueda 1439518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 1440518257b1SMike Snitzer if (delay_retry) 1441518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1442518257b1SMike Snitzer else 1443518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1444518257b1SMike Snitzer 14453e9f1be1SHannes Reinecke if (__pg_init_all_paths(m)) 14463e9f1be1SHannes Reinecke goto out; 14473e9f1be1SHannes Reinecke } 1448518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 1449d0259bf0SKiyoshi Ueda 14507e48c768SMike Snitzer process_queued_io_list(m); 145176e33fe4SMike Snitzer 14522bded7bdSKiyoshi Ueda /* 14532bded7bdSKiyoshi Ueda * Wake up any thread waiting to suspend. 14542bded7bdSKiyoshi Ueda */ 14552bded7bdSKiyoshi Ueda wake_up(&m->pg_init_wait); 14562bded7bdSKiyoshi Ueda 1457d0259bf0SKiyoshi Ueda out: 1458cfae5c9bSChandra Seetharaman spin_unlock_irqrestore(&m->lock, flags); 1459cfae5c9bSChandra Seetharaman } 1460cfae5c9bSChandra Seetharaman 146189bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath) 1462bab7cfc7SChandra Seetharaman { 1463f10e06b7SMike Snitzer struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1464bab7cfc7SChandra Seetharaman 1465f10e06b7SMike Snitzer if (pgpath->is_active && !blk_queue_dying(q)) 1466f10e06b7SMike Snitzer scsi_dh_activate(q, pg_init_done, pgpath); 14673a017509SHannes Reinecke else 14683a017509SHannes Reinecke pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED); 1469bab7cfc7SChandra Seetharaman } 1470bab7cfc7SChandra Seetharaman 147189bfce76SBart Van Assche static void activate_path_work(struct work_struct *work) 147289bfce76SBart Van Assche { 147389bfce76SBart Van Assche struct pgpath *pgpath = 147489bfce76SBart Van Assche container_of(work, struct pgpath, activate_path.work); 147589bfce76SBart Van Assche 147689bfce76SBart Van Assche activate_or_offline_path(pgpath); 147789bfce76SBart Van Assche } 147889bfce76SBart Van Assche 14792a842acaSChristoph Hellwig static int noretry_error(blk_status_t error) 14807e782af5SHannes Reinecke { 14817e782af5SHannes Reinecke switch (error) { 14822a842acaSChristoph Hellwig case BLK_STS_NOTSUPP: 14832a842acaSChristoph Hellwig case BLK_STS_NOSPC: 14842a842acaSChristoph Hellwig case BLK_STS_TARGET: 14852a842acaSChristoph Hellwig case BLK_STS_NEXUS: 14862a842acaSChristoph Hellwig case BLK_STS_MEDIUM: 14877e782af5SHannes Reinecke return 1; 14887e782af5SHannes Reinecke } 14897e782af5SHannes Reinecke 14907e782af5SHannes Reinecke /* Anything else could be a path failure, so should be retried */ 14917e782af5SHannes Reinecke return 0; 14927e782af5SHannes Reinecke } 14937e782af5SHannes Reinecke 1494b79f10eeSChristoph Hellwig static int multipath_end_io(struct dm_target *ti, struct request *clone, 14952a842acaSChristoph Hellwig blk_status_t error, union map_info *map_context) 14961da177e4SLinus Torvalds { 1497b79f10eeSChristoph Hellwig struct dm_mpath_io *mpio = get_mpio(map_context); 1498b79f10eeSChristoph Hellwig struct pgpath *pgpath = mpio->pgpath; 14997ed8578aSChristoph Hellwig int r = DM_ENDIO_DONE; 1500b79f10eeSChristoph Hellwig 1501f40c67f0SKiyoshi Ueda /* 1502f40c67f0SKiyoshi Ueda * We don't queue any clone request inside the multipath target 1503f40c67f0SKiyoshi Ueda * during end I/O handling, since those clone requests don't have 1504f40c67f0SKiyoshi Ueda * bio clones. If we queue them inside the multipath target, 1505f40c67f0SKiyoshi Ueda * we need to make bio clones, that requires memory allocation. 15064cc96131SMike Snitzer * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests 1507f40c67f0SKiyoshi Ueda * don't have bio clones.) 1508f40c67f0SKiyoshi Ueda * Instead of queueing the clone request here, we queue the original 1509f40c67f0SKiyoshi Ueda * request into dm core, which will remake a clone request and 1510f40c67f0SKiyoshi Ueda * clone bios for it and resubmit it later. 1511f40c67f0SKiyoshi Ueda */ 1512b79f10eeSChristoph Hellwig if (error && !noretry_error(error)) { 1513b79f10eeSChristoph Hellwig struct multipath *m = ti->private; 15141da177e4SLinus Torvalds 15157ed8578aSChristoph Hellwig r = DM_ENDIO_REQUEUE; 15161da177e4SLinus Torvalds 1517b79f10eeSChristoph Hellwig if (pgpath) 1518b79f10eeSChristoph Hellwig fail_path(pgpath); 15191da177e4SLinus Torvalds 1520ca5beb76SBart Van Assche if (atomic_read(&m->nr_valid_paths) == 0 && 1521c1fd0abeSMike Snitzer !must_push_back_rq(m)) { 15222a842acaSChristoph Hellwig if (error == BLK_STS_IOERR) 152318a482f5SChristoph Hellwig dm_report_EIO(m); 15247ed8578aSChristoph Hellwig /* complete with the original error */ 15257ed8578aSChristoph Hellwig r = DM_ENDIO_DONE; 15267ed8578aSChristoph Hellwig } 15271da177e4SLinus Torvalds } 15281da177e4SLinus Torvalds 15291da177e4SLinus Torvalds if (pgpath) { 1530b79f10eeSChristoph Hellwig struct path_selector *ps = &pgpath->pg->ps; 1531b79f10eeSChristoph Hellwig 15321da177e4SLinus Torvalds if (ps->type->end_io) 153302ab823fSKiyoshi Ueda ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); 15341da177e4SLinus Torvalds } 15351da177e4SLinus Torvalds 15367ed8578aSChristoph Hellwig return r; 15371da177e4SLinus Torvalds } 15381da177e4SLinus Torvalds 15394e4cbee9SChristoph Hellwig static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, 15404e4cbee9SChristoph Hellwig blk_status_t *error) 154176e33fe4SMike Snitzer { 154214ef1e48SChristoph Hellwig struct multipath *m = ti->private; 154314ef1e48SChristoph Hellwig struct dm_mpath_io *mpio = get_mpio_from_bio(clone); 154414ef1e48SChristoph Hellwig struct pgpath *pgpath = mpio->pgpath; 154576e33fe4SMike Snitzer unsigned long flags; 15461be56909SChristoph Hellwig int r = DM_ENDIO_DONE; 154776e33fe4SMike Snitzer 15484e4cbee9SChristoph Hellwig if (!*error || noretry_error(*error)) 154914ef1e48SChristoph Hellwig goto done; 155076e33fe4SMike Snitzer 155114ef1e48SChristoph Hellwig if (pgpath) 155214ef1e48SChristoph Hellwig fail_path(pgpath); 155376e33fe4SMike Snitzer 1554ca5beb76SBart Van Assche if (atomic_read(&m->nr_valid_paths) == 0 && 155518a482f5SChristoph Hellwig !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 1556c1fd0abeSMike Snitzer if (must_push_back_bio(m)) { 1557c1fd0abeSMike Snitzer r = DM_ENDIO_REQUEUE; 1558c1fd0abeSMike Snitzer } else { 155918a482f5SChristoph Hellwig dm_report_EIO(m); 15604e4cbee9SChristoph Hellwig *error = BLK_STS_IOERR; 1561c1fd0abeSMike Snitzer } 156214ef1e48SChristoph Hellwig goto done; 156318a482f5SChristoph Hellwig } 156476e33fe4SMike Snitzer 156576e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 156676e33fe4SMike Snitzer bio_list_add(&m->queued_bios, clone); 156776e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 156876e33fe4SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) 156976e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 157076e33fe4SMike Snitzer 15711be56909SChristoph Hellwig r = DM_ENDIO_INCOMPLETE; 157214ef1e48SChristoph Hellwig done: 157376e33fe4SMike Snitzer if (pgpath) { 157414ef1e48SChristoph Hellwig struct path_selector *ps = &pgpath->pg->ps; 157514ef1e48SChristoph Hellwig 157676e33fe4SMike Snitzer if (ps->type->end_io) 157776e33fe4SMike Snitzer ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); 157876e33fe4SMike Snitzer } 157976e33fe4SMike Snitzer 15801be56909SChristoph Hellwig return r; 158176e33fe4SMike Snitzer } 158276e33fe4SMike Snitzer 15831da177e4SLinus Torvalds /* 15841da177e4SLinus Torvalds * Suspend can't complete until all the I/O is processed so if 1585436d4108SAlasdair G Kergon * the last path fails we must error any remaining I/O. 1586436d4108SAlasdair G Kergon * Note that if the freeze_bdev fails while suspending, the 1587436d4108SAlasdair G Kergon * queue_if_no_path state is lost - userspace should reset it. 15881da177e4SLinus Torvalds */ 15891da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti) 15901da177e4SLinus Torvalds { 15917943bd6dSMike Snitzer struct multipath *m = ti->private; 15921da177e4SLinus Torvalds 1593be7d31ccSMike Snitzer queue_if_no_path(m, false, true); 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds 15966df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti) 15976df400abSKiyoshi Ueda { 15986380f26fSMike Anderson struct multipath *m = ti->private; 15996380f26fSMike Anderson 16006380f26fSMike Anderson mutex_lock(&m->work_mutex); 16012bded7bdSKiyoshi Ueda flush_multipath_work(m); 16026380f26fSMike Anderson mutex_unlock(&m->work_mutex); 16036df400abSKiyoshi Ueda } 16046df400abSKiyoshi Ueda 1605436d4108SAlasdair G Kergon /* 1606436d4108SAlasdair G Kergon * Restore the queue_if_no_path setting. 1607436d4108SAlasdair G Kergon */ 16081da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti) 16091da177e4SLinus Torvalds { 16107943bd6dSMike Snitzer struct multipath *m = ti->private; 16111814f2e3SMike Snitzer unsigned long flags; 16121da177e4SLinus Torvalds 16131814f2e3SMike Snitzer spin_lock_irqsave(&m->lock, flags); 16145307e2adSLukas Wunner assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, 16155307e2adSLukas Wunner test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)); 16161814f2e3SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 16171da177e4SLinus Torvalds } 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds /* 16201da177e4SLinus Torvalds * Info output has the following format: 16211da177e4SLinus Torvalds * num_multipath_feature_args [multipath_feature_args]* 16221da177e4SLinus Torvalds * num_handler_status_args [handler_status_args]* 16231da177e4SLinus Torvalds * num_groups init_group_number 16241da177e4SLinus Torvalds * [A|D|E num_ps_status_args [ps_status_args]* 16251da177e4SLinus Torvalds * num_paths num_selector_args 16261da177e4SLinus Torvalds * [path_dev A|F fail_count [selector_args]* ]+ ]+ 16271da177e4SLinus Torvalds * 16281da177e4SLinus Torvalds * Table output has the following format (identical to the constructor string): 16291da177e4SLinus Torvalds * num_feature_args [features_args]* 16301da177e4SLinus Torvalds * num_handler_args hw_handler [hw_handler_args]* 16311da177e4SLinus Torvalds * num_groups init_group_number 16321da177e4SLinus Torvalds * [priority selector-name num_ps_args [ps_args]* 16331da177e4SLinus Torvalds * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ 16341da177e4SLinus Torvalds */ 1635fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type, 16361f4e0ff0SAlasdair G Kergon unsigned status_flags, char *result, unsigned maxlen) 16371da177e4SLinus Torvalds { 16381da177e4SLinus Torvalds int sz = 0; 16391da177e4SLinus Torvalds unsigned long flags; 16407943bd6dSMike Snitzer struct multipath *m = ti->private; 16411da177e4SLinus Torvalds struct priority_group *pg; 16421da177e4SLinus Torvalds struct pgpath *p; 16431da177e4SLinus Torvalds unsigned pg_num; 16441da177e4SLinus Torvalds char state; 16451da177e4SLinus Torvalds 16461da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 16471da177e4SLinus Torvalds 16481da177e4SLinus Torvalds /* Features */ 16491da177e4SLinus Torvalds if (type == STATUSTYPE_INFO) 165091e968aaSMike Snitzer DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags), 165191e968aaSMike Snitzer atomic_read(&m->pg_init_count)); 1652c9e45581SDave Wysochanski else { 1653518257b1SMike Snitzer DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) + 16544e2d19e4SChandra Seetharaman (m->pg_init_retries > 0) * 2 + 1655a58a935dSMike Snitzer (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 + 1656e83068a5SMike Snitzer test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) + 1657e83068a5SMike Snitzer (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2); 1658e83068a5SMike Snitzer 1659518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 1660c9e45581SDave Wysochanski DMEMIT("queue_if_no_path "); 1661c9e45581SDave Wysochanski if (m->pg_init_retries) 1662c9e45581SDave Wysochanski DMEMIT("pg_init_retries %u ", m->pg_init_retries); 16634e2d19e4SChandra Seetharaman if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) 16644e2d19e4SChandra Seetharaman DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs); 1665518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) 1666a58a935dSMike Snitzer DMEMIT("retain_attached_hw_handler "); 1667e83068a5SMike Snitzer if (m->queue_mode != DM_TYPE_REQUEST_BASED) { 1668e83068a5SMike Snitzer switch(m->queue_mode) { 1669e83068a5SMike Snitzer case DM_TYPE_BIO_BASED: 1670e83068a5SMike Snitzer DMEMIT("queue_mode bio "); 1671e83068a5SMike Snitzer break; 1672*cd025384SMike Snitzer case DM_TYPE_NVME_BIO_BASED: 1673*cd025384SMike Snitzer DMEMIT("queue_mode nvme "); 1674*cd025384SMike Snitzer break; 1675e83068a5SMike Snitzer case DM_TYPE_MQ_REQUEST_BASED: 1676e83068a5SMike Snitzer DMEMIT("queue_mode mq "); 1677e83068a5SMike Snitzer break; 16787e0d574fSBart Van Assche default: 16797e0d574fSBart Van Assche WARN_ON_ONCE(true); 16807e0d574fSBart Van Assche break; 1681e83068a5SMike Snitzer } 1682e83068a5SMike Snitzer } 1683c9e45581SDave Wysochanski } 16841da177e4SLinus Torvalds 1685cfae5c9bSChandra Seetharaman if (!m->hw_handler_name || type == STATUSTYPE_INFO) 16861da177e4SLinus Torvalds DMEMIT("0 "); 16871da177e4SLinus Torvalds else 1688cfae5c9bSChandra Seetharaman DMEMIT("1 %s ", m->hw_handler_name); 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds DMEMIT("%u ", m->nr_priority_groups); 16911da177e4SLinus Torvalds 16921da177e4SLinus Torvalds if (m->next_pg) 16931da177e4SLinus Torvalds pg_num = m->next_pg->pg_num; 16941da177e4SLinus Torvalds else if (m->current_pg) 16951da177e4SLinus Torvalds pg_num = m->current_pg->pg_num; 16961da177e4SLinus Torvalds else 1697a490a07aSMike Snitzer pg_num = (m->nr_priority_groups ? 1 : 0); 16981da177e4SLinus Torvalds 16991da177e4SLinus Torvalds DMEMIT("%u ", pg_num); 17001da177e4SLinus Torvalds 17011da177e4SLinus Torvalds switch (type) { 17021da177e4SLinus Torvalds case STATUSTYPE_INFO: 17031da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 17041da177e4SLinus Torvalds if (pg->bypassed) 17051da177e4SLinus Torvalds state = 'D'; /* Disabled */ 17061da177e4SLinus Torvalds else if (pg == m->current_pg) 17071da177e4SLinus Torvalds state = 'A'; /* Currently Active */ 17081da177e4SLinus Torvalds else 17091da177e4SLinus Torvalds state = 'E'; /* Enabled */ 17101da177e4SLinus Torvalds 17111da177e4SLinus Torvalds DMEMIT("%c ", state); 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds if (pg->ps.type->status) 17141da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 17151da177e4SLinus Torvalds result + sz, 17161da177e4SLinus Torvalds maxlen - sz); 17171da177e4SLinus Torvalds else 17181da177e4SLinus Torvalds DMEMIT("0 "); 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 17211da177e4SLinus Torvalds pg->ps.type->info_args); 17221da177e4SLinus Torvalds 17231da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 17241da177e4SLinus Torvalds DMEMIT("%s %s %u ", p->path.dev->name, 17256680073dSKiyoshi Ueda p->is_active ? "A" : "F", 17261da177e4SLinus Torvalds p->fail_count); 17271da177e4SLinus Torvalds if (pg->ps.type->status) 17281da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 17291da177e4SLinus Torvalds &p->path, type, result + sz, 17301da177e4SLinus Torvalds maxlen - sz); 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds } 17331da177e4SLinus Torvalds break; 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds case STATUSTYPE_TABLE: 17361da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 17371da177e4SLinus Torvalds DMEMIT("%s ", pg->ps.type->name); 17381da177e4SLinus Torvalds 17391da177e4SLinus Torvalds if (pg->ps.type->status) 17401da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 17411da177e4SLinus Torvalds result + sz, 17421da177e4SLinus Torvalds maxlen - sz); 17431da177e4SLinus Torvalds else 17441da177e4SLinus Torvalds DMEMIT("0 "); 17451da177e4SLinus Torvalds 17461da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 17471da177e4SLinus Torvalds pg->ps.type->table_args); 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 17501da177e4SLinus Torvalds DMEMIT("%s ", p->path.dev->name); 17511da177e4SLinus Torvalds if (pg->ps.type->status) 17521da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 17531da177e4SLinus Torvalds &p->path, type, result + sz, 17541da177e4SLinus Torvalds maxlen - sz); 17551da177e4SLinus Torvalds } 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds break; 17581da177e4SLinus Torvalds } 17591da177e4SLinus Torvalds 17601da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 17611da177e4SLinus Torvalds } 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv) 17641da177e4SLinus Torvalds { 17656380f26fSMike Anderson int r = -EINVAL; 17661da177e4SLinus Torvalds struct dm_dev *dev; 17677943bd6dSMike Snitzer struct multipath *m = ti->private; 17681da177e4SLinus Torvalds action_fn action; 17691da177e4SLinus Torvalds 17706380f26fSMike Anderson mutex_lock(&m->work_mutex); 17716380f26fSMike Anderson 1772c2f3d24bSKiyoshi Ueda if (dm_suspended(ti)) { 1773c2f3d24bSKiyoshi Ueda r = -EBUSY; 1774c2f3d24bSKiyoshi Ueda goto out; 1775c2f3d24bSKiyoshi Ueda } 1776c2f3d24bSKiyoshi Ueda 17771da177e4SLinus Torvalds if (argc == 1) { 1778498f0103SMike Snitzer if (!strcasecmp(argv[0], "queue_if_no_path")) { 1779be7d31ccSMike Snitzer r = queue_if_no_path(m, true, false); 17806380f26fSMike Anderson goto out; 1781498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "fail_if_no_path")) { 1782be7d31ccSMike Snitzer r = queue_if_no_path(m, false, false); 17836380f26fSMike Anderson goto out; 17846380f26fSMike Anderson } 17851da177e4SLinus Torvalds } 17861da177e4SLinus Torvalds 17876380f26fSMike Anderson if (argc != 2) { 1788a356e426SJose Castillo DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc); 17896380f26fSMike Anderson goto out; 17906380f26fSMike Anderson } 17911da177e4SLinus Torvalds 1792498f0103SMike Snitzer if (!strcasecmp(argv[0], "disable_group")) { 1793be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], true); 17946380f26fSMike Anderson goto out; 1795498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "enable_group")) { 1796be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], false); 17976380f26fSMike Anderson goto out; 1798498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "switch_group")) { 17996380f26fSMike Anderson r = switch_pg_num(m, argv[1]); 18006380f26fSMike Anderson goto out; 1801498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "reinstate_path")) 18021da177e4SLinus Torvalds action = reinstate_path; 1803498f0103SMike Snitzer else if (!strcasecmp(argv[0], "fail_path")) 18041da177e4SLinus Torvalds action = fail_path; 18056380f26fSMike Anderson else { 1806a356e426SJose Castillo DMWARN("Unrecognised multipath message received: %s", argv[0]); 18076380f26fSMike Anderson goto out; 18086380f26fSMike Anderson } 18091da177e4SLinus Torvalds 18108215d6ecSNikanth Karthikesan r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev); 18111da177e4SLinus Torvalds if (r) { 181272d94861SAlasdair G Kergon DMWARN("message: error getting device %s", 18131da177e4SLinus Torvalds argv[1]); 18146380f26fSMike Anderson goto out; 18151da177e4SLinus Torvalds } 18161da177e4SLinus Torvalds 18171da177e4SLinus Torvalds r = action_dev(m, dev, action); 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds dm_put_device(ti, dev); 18201da177e4SLinus Torvalds 18216380f26fSMike Anderson out: 18226380f26fSMike Anderson mutex_unlock(&m->work_mutex); 18231da177e4SLinus Torvalds return r; 18241da177e4SLinus Torvalds } 18251da177e4SLinus Torvalds 1826e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti, 1827e56f81e0SChristoph Hellwig struct block_device **bdev, fmode_t *mode) 18289af4aa30SMilan Broz { 182935991652SMikulas Patocka struct multipath *m = ti->private; 18302da1610aSMike Snitzer struct pgpath *current_pgpath; 183135991652SMikulas Patocka int r; 183235991652SMikulas Patocka 1833506458efSWill Deacon current_pgpath = READ_ONCE(m->current_pgpath); 18342da1610aSMike Snitzer if (!current_pgpath) 18352da1610aSMike Snitzer current_pgpath = choose_pgpath(m, 0); 18369af4aa30SMilan Broz 18372da1610aSMike Snitzer if (current_pgpath) { 1838518257b1SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) { 18392da1610aSMike Snitzer *bdev = current_pgpath->path.dev->bdev; 18402da1610aSMike Snitzer *mode = current_pgpath->path.dev->mode; 184143e43c9eSJunichi Nomura r = 0; 184243e43c9eSJunichi Nomura } else { 184343e43c9eSJunichi Nomura /* pg_init has not started or completed */ 18446c182cd8SHannes Reinecke r = -ENOTCONN; 184543e43c9eSJunichi Nomura } 184643e43c9eSJunichi Nomura } else { 184743e43c9eSJunichi Nomura /* No path is available */ 1848518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 184943e43c9eSJunichi Nomura r = -ENOTCONN; 185043e43c9eSJunichi Nomura else 18519af4aa30SMilan Broz r = -EIO; 185243e43c9eSJunichi Nomura } 18539af4aa30SMilan Broz 18545bbbfdf6SJunichi Nomura if (r == -ENOTCONN) { 1855506458efSWill Deacon if (!READ_ONCE(m->current_pg)) { 18563e9f1be1SHannes Reinecke /* Path status changed, redo selection */ 18572da1610aSMike Snitzer (void) choose_pgpath(m, 0); 18583e9f1be1SHannes Reinecke } 1859518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 18602da1610aSMike Snitzer pg_init_all_paths(m); 186163d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 18627e48c768SMike Snitzer process_queued_io_list(m); 18633e9f1be1SHannes Reinecke } 186435991652SMikulas Patocka 1865e56f81e0SChristoph Hellwig /* 1866e56f81e0SChristoph Hellwig * Only pass ioctls through if the device sizes match exactly. 1867e56f81e0SChristoph Hellwig */ 1868e56f81e0SChristoph Hellwig if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT) 1869e56f81e0SChristoph Hellwig return 1; 1870e56f81e0SChristoph Hellwig return r; 18719af4aa30SMilan Broz } 18729af4aa30SMilan Broz 1873af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti, 1874af4874e0SMike Snitzer iterate_devices_callout_fn fn, void *data) 1875af4874e0SMike Snitzer { 1876af4874e0SMike Snitzer struct multipath *m = ti->private; 1877af4874e0SMike Snitzer struct priority_group *pg; 1878af4874e0SMike Snitzer struct pgpath *p; 1879af4874e0SMike Snitzer int ret = 0; 1880af4874e0SMike Snitzer 1881af4874e0SMike Snitzer list_for_each_entry(pg, &m->priority_groups, list) { 1882af4874e0SMike Snitzer list_for_each_entry(p, &pg->pgpaths, list) { 18835dea271bSMike Snitzer ret = fn(ti, p->path.dev, ti->begin, ti->len, data); 1884af4874e0SMike Snitzer if (ret) 1885af4874e0SMike Snitzer goto out; 1886af4874e0SMike Snitzer } 1887af4874e0SMike Snitzer } 1888af4874e0SMike Snitzer 1889af4874e0SMike Snitzer out: 1890af4874e0SMike Snitzer return ret; 1891af4874e0SMike Snitzer } 1892af4874e0SMike Snitzer 18939f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath) 1894f40c67f0SKiyoshi Ueda { 1895f40c67f0SKiyoshi Ueda struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1896f40c67f0SKiyoshi Ueda 189752b09914SMike Snitzer return blk_lld_busy(q); 1898f40c67f0SKiyoshi Ueda } 1899f40c67f0SKiyoshi Ueda 1900f40c67f0SKiyoshi Ueda /* 1901f40c67f0SKiyoshi Ueda * We return "busy", only when we can map I/Os but underlying devices 1902f40c67f0SKiyoshi Ueda * are busy (so even if we map I/Os now, the I/Os will wait on 1903f40c67f0SKiyoshi Ueda * the underlying queue). 1904f40c67f0SKiyoshi Ueda * In other words, if we want to kill I/Os or queue them inside us 1905f40c67f0SKiyoshi Ueda * due to map unavailability, we don't return "busy". Otherwise, 1906f40c67f0SKiyoshi Ueda * dm core won't give us the I/Os and we can't do what we want. 1907f40c67f0SKiyoshi Ueda */ 1908f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti) 1909f40c67f0SKiyoshi Ueda { 1910be7d31ccSMike Snitzer bool busy = false, has_active = false; 1911f40c67f0SKiyoshi Ueda struct multipath *m = ti->private; 19122da1610aSMike Snitzer struct priority_group *pg, *next_pg; 1913f40c67f0SKiyoshi Ueda struct pgpath *pgpath; 1914f40c67f0SKiyoshi Ueda 1915b88efd43SMike Snitzer /* pg_init in progress */ 1916b88efd43SMike Snitzer if (atomic_read(&m->pg_init_in_progress)) 19172da1610aSMike Snitzer return true; 19182da1610aSMike Snitzer 1919b88efd43SMike Snitzer /* no paths available, for blk-mq: rely on IO mapping to delay requeue */ 1920b88efd43SMike Snitzer if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 1921b88efd43SMike Snitzer return (m->queue_mode != DM_TYPE_MQ_REQUEST_BASED); 1922b88efd43SMike Snitzer 1923f40c67f0SKiyoshi Ueda /* Guess which priority_group will be used at next mapping time */ 1924506458efSWill Deacon pg = READ_ONCE(m->current_pg); 1925506458efSWill Deacon next_pg = READ_ONCE(m->next_pg); 1926506458efSWill Deacon if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg)) 19272da1610aSMike Snitzer pg = next_pg; 19282da1610aSMike Snitzer 19292da1610aSMike Snitzer if (!pg) { 1930f40c67f0SKiyoshi Ueda /* 1931f40c67f0SKiyoshi Ueda * We don't know which pg will be used at next mapping time. 19322da1610aSMike Snitzer * We don't call choose_pgpath() here to avoid to trigger 1933f40c67f0SKiyoshi Ueda * pg_init just by busy checking. 1934f40c67f0SKiyoshi Ueda * So we don't know whether underlying devices we will be using 1935f40c67f0SKiyoshi Ueda * at next mapping time are busy or not. Just try mapping. 1936f40c67f0SKiyoshi Ueda */ 19372da1610aSMike Snitzer return busy; 19382da1610aSMike Snitzer } 1939f40c67f0SKiyoshi Ueda 1940f40c67f0SKiyoshi Ueda /* 1941f40c67f0SKiyoshi Ueda * If there is one non-busy active path at least, the path selector 1942f40c67f0SKiyoshi Ueda * will be able to select it. So we consider such a pg as not busy. 1943f40c67f0SKiyoshi Ueda */ 1944be7d31ccSMike Snitzer busy = true; 19452da1610aSMike Snitzer list_for_each_entry(pgpath, &pg->pgpaths, list) { 1946f40c67f0SKiyoshi Ueda if (pgpath->is_active) { 1947be7d31ccSMike Snitzer has_active = true; 19489f54cec5SMike Snitzer if (!pgpath_busy(pgpath)) { 1949be7d31ccSMike Snitzer busy = false; 1950f40c67f0SKiyoshi Ueda break; 1951f40c67f0SKiyoshi Ueda } 1952f40c67f0SKiyoshi Ueda } 19532da1610aSMike Snitzer } 1954f40c67f0SKiyoshi Ueda 19552da1610aSMike Snitzer if (!has_active) { 1956f40c67f0SKiyoshi Ueda /* 1957f40c67f0SKiyoshi Ueda * No active path in this pg, so this pg won't be used and 1958f40c67f0SKiyoshi Ueda * the current_pg will be changed at next mapping time. 1959f40c67f0SKiyoshi Ueda * We need to try mapping to determine it. 1960f40c67f0SKiyoshi Ueda */ 1961be7d31ccSMike Snitzer busy = false; 19622da1610aSMike Snitzer } 1963f40c67f0SKiyoshi Ueda 1964f40c67f0SKiyoshi Ueda return busy; 1965f40c67f0SKiyoshi Ueda } 1966f40c67f0SKiyoshi Ueda 19671da177e4SLinus Torvalds /*----------------------------------------------------------------- 19681da177e4SLinus Torvalds * Module setup 19691da177e4SLinus Torvalds *---------------------------------------------------------------*/ 19701da177e4SLinus Torvalds static struct target_type multipath_target = { 19711da177e4SLinus Torvalds .name = "multipath", 1972e83068a5SMike Snitzer .version = {1, 12, 0}, 197316f12266SMike Snitzer .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE, 19741da177e4SLinus Torvalds .module = THIS_MODULE, 19751da177e4SLinus Torvalds .ctr = multipath_ctr, 19761da177e4SLinus Torvalds .dtr = multipath_dtr, 1977e5863d9aSMike Snitzer .clone_and_map_rq = multipath_clone_and_map, 1978e5863d9aSMike Snitzer .release_clone_rq = multipath_release_clone, 1979f40c67f0SKiyoshi Ueda .rq_end_io = multipath_end_io, 198076e33fe4SMike Snitzer .map = multipath_map_bio, 198176e33fe4SMike Snitzer .end_io = multipath_end_io_bio, 198276e33fe4SMike Snitzer .presuspend = multipath_presuspend, 198376e33fe4SMike Snitzer .postsuspend = multipath_postsuspend, 198476e33fe4SMike Snitzer .resume = multipath_resume, 198576e33fe4SMike Snitzer .status = multipath_status, 198676e33fe4SMike Snitzer .message = multipath_message, 198776e33fe4SMike Snitzer .prepare_ioctl = multipath_prepare_ioctl, 198876e33fe4SMike Snitzer .iterate_devices = multipath_iterate_devices, 198976e33fe4SMike Snitzer .busy = multipath_busy, 199076e33fe4SMike Snitzer }; 199176e33fe4SMike Snitzer 19921da177e4SLinus Torvalds static int __init dm_multipath_init(void) 19931da177e4SLinus Torvalds { 19941da177e4SLinus Torvalds int r; 19951da177e4SLinus Torvalds 19964d4d66abSTejun Heo kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0); 1997c557308eSAlasdair G Kergon if (!kmultipathd) { 19980cd33124SAlasdair G Kergon DMERR("failed to create workqueue kmpathd"); 1999ff658e9cSJohannes Thumshirn r = -ENOMEM; 2000ff658e9cSJohannes Thumshirn goto bad_alloc_kmultipathd; 2001c557308eSAlasdair G Kergon } 2002c557308eSAlasdair G Kergon 2003bab7cfc7SChandra Seetharaman /* 2004bab7cfc7SChandra Seetharaman * A separate workqueue is used to handle the device handlers 2005bab7cfc7SChandra Seetharaman * to avoid overloading existing workqueue. Overloading the 2006bab7cfc7SChandra Seetharaman * old workqueue would also create a bottleneck in the 2007bab7cfc7SChandra Seetharaman * path of the storage hardware device activation. 2008bab7cfc7SChandra Seetharaman */ 20094d4d66abSTejun Heo kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd", 20104d4d66abSTejun Heo WQ_MEM_RECLAIM); 2011bab7cfc7SChandra Seetharaman if (!kmpath_handlerd) { 2012bab7cfc7SChandra Seetharaman DMERR("failed to create workqueue kmpath_handlerd"); 2013ff658e9cSJohannes Thumshirn r = -ENOMEM; 2014ff658e9cSJohannes Thumshirn goto bad_alloc_kmpath_handlerd; 2015bab7cfc7SChandra Seetharaman } 2016bab7cfc7SChandra Seetharaman 20177e6358d2Smonty_pavel@sina.com r = dm_register_target(&multipath_target); 20187e6358d2Smonty_pavel@sina.com if (r < 0) { 20197e6358d2Smonty_pavel@sina.com DMERR("request-based register failed %d", r); 20207e6358d2Smonty_pavel@sina.com r = -EINVAL; 20217e6358d2Smonty_pavel@sina.com goto bad_register_target; 20227e6358d2Smonty_pavel@sina.com } 20237e6358d2Smonty_pavel@sina.com 2024ff658e9cSJohannes Thumshirn return 0; 2025ff658e9cSJohannes Thumshirn 20267e6358d2Smonty_pavel@sina.com bad_register_target: 20277e6358d2Smonty_pavel@sina.com destroy_workqueue(kmpath_handlerd); 2028ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd: 2029ff658e9cSJohannes Thumshirn destroy_workqueue(kmultipathd); 2030ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd: 20311da177e4SLinus Torvalds return r; 20321da177e4SLinus Torvalds } 20331da177e4SLinus Torvalds 20341da177e4SLinus Torvalds static void __exit dm_multipath_exit(void) 20351da177e4SLinus Torvalds { 2036bab7cfc7SChandra Seetharaman destroy_workqueue(kmpath_handlerd); 2037c557308eSAlasdair G Kergon destroy_workqueue(kmultipathd); 2038c557308eSAlasdair G Kergon 203910d3bd09SMikulas Patocka dm_unregister_target(&multipath_target); 20401da177e4SLinus Torvalds } 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds module_init(dm_multipath_init); 20431da177e4SLinus Torvalds module_exit(dm_multipath_exit); 20441da177e4SLinus Torvalds 20451da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target"); 20461da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); 20471da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2048