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> 23be240ff5SAnatol Pomazau #include <linux/timer.h> 241da177e4SLinus Torvalds #include <linux/workqueue.h> 2535991652SMikulas Patocka #include <linux/delay.h> 26cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h> 2760063497SArun Sharma #include <linux/atomic.h> 2878ce23b5SMike Snitzer #include <linux/blk-mq.h> 291da177e4SLinus Torvalds 3072d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath" 314e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000 324e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1) 33be240ff5SAnatol Pomazau #define QUEUE_IF_NO_PATH_TIMEOUT_DEFAULT 0 34be240ff5SAnatol Pomazau 35be240ff5SAnatol Pomazau static unsigned long queue_if_no_path_timeout_secs = QUEUE_IF_NO_PATH_TIMEOUT_DEFAULT; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds /* Path properties */ 381da177e4SLinus Torvalds struct pgpath { 391da177e4SLinus Torvalds struct list_head list; 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds struct priority_group *pg; /* Owning PG */ 421da177e4SLinus Torvalds unsigned fail_count; /* Cumulative failure count */ 431da177e4SLinus Torvalds 44c922d5f7SJosef "Jeff" Sipek struct dm_path path; 454e2d19e4SChandra Seetharaman struct delayed_work activate_path; 46be7d31ccSMike Snitzer 47be7d31ccSMike Snitzer bool is_active:1; /* Path status */ 481da177e4SLinus Torvalds }; 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path) 511da177e4SLinus Torvalds 521da177e4SLinus Torvalds /* 531da177e4SLinus Torvalds * Paths are grouped into Priority Groups and numbered from 1 upwards. 541da177e4SLinus Torvalds * Each has a path selector which controls which path gets used. 551da177e4SLinus Torvalds */ 561da177e4SLinus Torvalds struct priority_group { 571da177e4SLinus Torvalds struct list_head list; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds struct multipath *m; /* Owning multipath instance */ 601da177e4SLinus Torvalds struct path_selector ps; 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds unsigned pg_num; /* Reference number */ 631da177e4SLinus Torvalds unsigned nr_pgpaths; /* Number of paths in PG */ 641da177e4SLinus Torvalds struct list_head pgpaths; 65be7d31ccSMike Snitzer 66be7d31ccSMike Snitzer bool bypassed:1; /* Temporarily bypass this PG? */ 671da177e4SLinus Torvalds }; 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* Multipath context */ 701da177e4SLinus Torvalds struct multipath { 71848b8aefSMike Snitzer unsigned long flags; /* Multipath state flags */ 724e2d19e4SChandra Seetharaman 731fbdd2b3SMike Snitzer spinlock_t lock; 74848b8aefSMike Snitzer enum dm_queue_mode queue_mode; 754e2d19e4SChandra Seetharaman 761da177e4SLinus Torvalds struct pgpath *current_pgpath; 771da177e4SLinus Torvalds struct priority_group *current_pg; 781da177e4SLinus Torvalds struct priority_group *next_pg; /* Switch to this PG if set */ 791da177e4SLinus Torvalds 80848b8aefSMike Snitzer atomic_t nr_valid_paths; /* Total number of usable paths */ 81848b8aefSMike Snitzer unsigned nr_priority_groups; 82848b8aefSMike Snitzer struct list_head priority_groups; 831fbdd2b3SMike Snitzer 84848b8aefSMike Snitzer const char *hw_handler_name; 85848b8aefSMike Snitzer char *hw_handler_params; 86848b8aefSMike Snitzer wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */ 87c9e45581SDave Wysochanski unsigned pg_init_retries; /* Number of times to retry pg_init */ 884e2d19e4SChandra Seetharaman unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */ 8991e968aaSMike Snitzer atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */ 9091e968aaSMike Snitzer atomic_t pg_init_count; /* Number of times pg_init called */ 9191e968aaSMike Snitzer 926380f26fSMike Anderson struct mutex work_mutex; 9320800cb3SMike Snitzer struct work_struct trigger_event; 94848b8aefSMike Snitzer struct dm_target *ti; 9576e33fe4SMike Snitzer 9676e33fe4SMike Snitzer struct work_struct process_queued_bios; 9776e33fe4SMike Snitzer struct bio_list queued_bios; 98be240ff5SAnatol Pomazau 99be240ff5SAnatol Pomazau struct timer_list nopath_timer; /* Timeout for queue_if_no_path */ 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); 117be240ff5SAnatol Pomazau static void queue_if_no_path_timeout_work(struct timer_list *t); 1181da177e4SLinus Torvalds 119518257b1SMike Snitzer /*----------------------------------------------- 120518257b1SMike Snitzer * Multipath state flags. 121518257b1SMike Snitzer *-----------------------------------------------*/ 122518257b1SMike Snitzer 123518257b1SMike Snitzer #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */ 124518257b1SMike Snitzer #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */ 125518257b1SMike Snitzer #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */ 126518257b1SMike Snitzer #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */ 127518257b1SMike Snitzer #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */ 128518257b1SMike Snitzer #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */ 129518257b1SMike Snitzer #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */ 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /*----------------------------------------------- 1321da177e4SLinus Torvalds * Allocation routines 1331da177e4SLinus Torvalds *-----------------------------------------------*/ 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void) 1361da177e4SLinus Torvalds { 137e69fae56SMicha³ Miros³aw struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL); 1381da177e4SLinus Torvalds 139848b8aefSMike Snitzer if (!pgpath) 140848b8aefSMike Snitzer return NULL; 141848b8aefSMike Snitzer 142be7d31ccSMike Snitzer pgpath->is_active = true; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds return pgpath; 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 147028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath) 1481da177e4SLinus Torvalds { 1491da177e4SLinus Torvalds kfree(pgpath); 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds struct priority_group *pg; 1551da177e4SLinus Torvalds 156e69fae56SMicha³ Miros³aw pg = kzalloc(sizeof(*pg), GFP_KERNEL); 1571da177e4SLinus Torvalds 158e69fae56SMicha³ Miros³aw if (pg) 1591da177e4SLinus Torvalds INIT_LIST_HEAD(&pg->pgpaths); 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds return pg; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti) 1651da177e4SLinus Torvalds { 1661da177e4SLinus Torvalds struct pgpath *pgpath, *tmp; 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds list_for_each_entry_safe(pgpath, tmp, pgpaths, list) { 1691da177e4SLinus Torvalds list_del(&pgpath->list); 1701da177e4SLinus Torvalds dm_put_device(ti, pgpath->path.dev); 1711da177e4SLinus Torvalds free_pgpath(pgpath); 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds } 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg, 1761da177e4SLinus Torvalds struct dm_target *ti) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds struct path_selector *ps = &pg->ps; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds if (ps->type) { 1811da177e4SLinus Torvalds ps->type->destroy(ps); 1821da177e4SLinus Torvalds dm_put_path_selector(ps->type); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds free_pgpaths(&pg->pgpaths, ti); 1861da177e4SLinus Torvalds kfree(pg); 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 189e83068a5SMike Snitzer static struct multipath *alloc_multipath(struct dm_target *ti) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds struct multipath *m; 1921da177e4SLinus Torvalds 193e69fae56SMicha³ Miros³aw m = kzalloc(sizeof(*m), GFP_KERNEL); 1941da177e4SLinus Torvalds if (m) { 1951da177e4SLinus Torvalds INIT_LIST_HEAD(&m->priority_groups); 1961da177e4SLinus Torvalds spin_lock_init(&m->lock); 19791e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, 0); 198c4028958SDavid Howells INIT_WORK(&m->trigger_event, trigger_event); 1996380f26fSMike Anderson mutex_init(&m->work_mutex); 2008637a6bfSMike Snitzer 201e83068a5SMike Snitzer m->queue_mode = DM_TYPE_NONE; 202e83068a5SMike Snitzer 203e83068a5SMike Snitzer m->ti = ti; 204e83068a5SMike Snitzer ti->private = m; 205be240ff5SAnatol Pomazau 206be240ff5SAnatol Pomazau timer_setup(&m->nopath_timer, queue_if_no_path_timeout_work, 0); 207e83068a5SMike Snitzer } 208e83068a5SMike Snitzer 209e83068a5SMike Snitzer return m; 210e83068a5SMike Snitzer } 211e83068a5SMike Snitzer 212e83068a5SMike Snitzer static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m) 213e83068a5SMike Snitzer { 214e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_NONE) { 215953923c0SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 2168d47e659SMike Snitzer } else if (m->queue_mode == DM_TYPE_BIO_BASED) { 21776e33fe4SMike Snitzer INIT_WORK(&m->process_queued_bios, process_queued_bios); 21876e33fe4SMike Snitzer /* 21976e33fe4SMike Snitzer * bio-based doesn't support any direct scsi_dh management; 22076e33fe4SMike Snitzer * it just discovers if a scsi_dh is attached. 22176e33fe4SMike Snitzer */ 22276e33fe4SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 22376e33fe4SMike Snitzer } 22476e33fe4SMike Snitzer 225e83068a5SMike Snitzer dm_table_set_type(ti->table, m->queue_mode); 2261da177e4SLinus Torvalds 227c3736674SMike Snitzer /* 228c3736674SMike Snitzer * Init fields that are only used when a scsi_dh is attached 229c3736674SMike Snitzer * - must do this unconditionally (really doesn't hurt non-SCSI uses) 230c3736674SMike Snitzer */ 231c3736674SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 232c3736674SMike Snitzer atomic_set(&m->pg_init_in_progress, 0); 233c3736674SMike Snitzer atomic_set(&m->pg_init_count, 0); 234c3736674SMike Snitzer m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT; 235c3736674SMike Snitzer init_waitqueue_head(&m->pg_init_wait); 236c3736674SMike Snitzer 237e83068a5SMike Snitzer return 0; 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds static void free_multipath(struct multipath *m) 2411da177e4SLinus Torvalds { 2421da177e4SLinus Torvalds struct priority_group *pg, *tmp; 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { 2451da177e4SLinus Torvalds list_del(&pg->list); 2461da177e4SLinus Torvalds free_priority_group(pg, m->ti); 2471da177e4SLinus Torvalds } 2481da177e4SLinus Torvalds 249cfae5c9bSChandra Seetharaman kfree(m->hw_handler_name); 2502bfd2e13SChandra Seetharaman kfree(m->hw_handler_params); 251d5ffebddSMike Snitzer mutex_destroy(&m->work_mutex); 2521da177e4SLinus Torvalds kfree(m); 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2552eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info) 2562eff1924SMike Snitzer { 2572eff1924SMike Snitzer return info->ptr; 2582eff1924SMike Snitzer } 2592eff1924SMike Snitzer 260bf661be1SMike Snitzer static size_t multipath_per_bio_data_size(void) 26176e33fe4SMike Snitzer { 262bf661be1SMike Snitzer return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details); 26376e33fe4SMike Snitzer } 26476e33fe4SMike Snitzer 265bf661be1SMike Snitzer static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio) 266bf661be1SMike Snitzer { 267bf661be1SMike Snitzer return dm_per_bio_data(bio, multipath_per_bio_data_size()); 268bf661be1SMike Snitzer } 269bf661be1SMike Snitzer 270d07a241dSMike Snitzer static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio) 271bf661be1SMike Snitzer { 272bf661be1SMike Snitzer /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */ 273bf661be1SMike Snitzer void *bio_details = mpio + 1; 274bf661be1SMike Snitzer return bio_details; 275bf661be1SMike Snitzer } 276bf661be1SMike Snitzer 27763f6e6fdSMike Snitzer static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p) 27876e33fe4SMike Snitzer { 27976e33fe4SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 280d07a241dSMike Snitzer struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio); 28176e33fe4SMike Snitzer 282d0442f80SMike Snitzer mpio->nr_bytes = bio->bi_iter.bi_size; 283d0442f80SMike Snitzer mpio->pgpath = NULL; 284bf661be1SMike Snitzer *mpio_p = mpio; 285d0442f80SMike Snitzer 286d0442f80SMike Snitzer dm_bio_record(bio_details, bio); 28776e33fe4SMike Snitzer } 28876e33fe4SMike Snitzer 2891da177e4SLinus Torvalds /*----------------------------------------------- 2901da177e4SLinus Torvalds * Path selection 2911da177e4SLinus Torvalds *-----------------------------------------------*/ 2921da177e4SLinus Torvalds 2933e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m) 294fb612642SKiyoshi Ueda { 295fb612642SKiyoshi Ueda struct pgpath *pgpath; 2964e2d19e4SChandra Seetharaman unsigned long pg_init_delay = 0; 297fb612642SKiyoshi Ueda 298b194679fSBart Van Assche lockdep_assert_held(&m->lock); 299b194679fSBart Van Assche 30091e968aaSMike Snitzer if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 3013e9f1be1SHannes Reinecke return 0; 30217f4ff45SHannes Reinecke 30391e968aaSMike Snitzer atomic_inc(&m->pg_init_count); 304518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 3053e9f1be1SHannes Reinecke 3063e9f1be1SHannes Reinecke /* Check here to reset pg_init_required */ 3073e9f1be1SHannes Reinecke if (!m->current_pg) 3083e9f1be1SHannes Reinecke return 0; 3093e9f1be1SHannes Reinecke 310518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags)) 3114e2d19e4SChandra Seetharaman pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ? 3124e2d19e4SChandra Seetharaman m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS); 313fb612642SKiyoshi Ueda list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) { 314fb612642SKiyoshi Ueda /* Skip failed paths */ 315fb612642SKiyoshi Ueda if (!pgpath->is_active) 316fb612642SKiyoshi Ueda continue; 3174e2d19e4SChandra Seetharaman if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path, 3184e2d19e4SChandra Seetharaman pg_init_delay)) 31991e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 320fb612642SKiyoshi Ueda } 32191e968aaSMike Snitzer return atomic_read(&m->pg_init_in_progress); 322fb612642SKiyoshi Ueda } 323fb612642SKiyoshi Ueda 324c1d7ecf7SBart Van Assche static int pg_init_all_paths(struct multipath *m) 3251da177e4SLinus Torvalds { 326c1d7ecf7SBart Van Assche int ret; 3272da1610aSMike Snitzer unsigned long flags; 3282da1610aSMike Snitzer 3292da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 330c1d7ecf7SBart Van Assche ret = __pg_init_all_paths(m); 3312da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 332c1d7ecf7SBart Van Assche 333c1d7ecf7SBart Van Assche return ret; 3342da1610aSMike Snitzer } 3352da1610aSMike Snitzer 3362da1610aSMike Snitzer static void __switch_pg(struct multipath *m, struct priority_group *pg) 3372da1610aSMike Snitzer { 33869cea0d4SMike Snitzer lockdep_assert_held(&m->lock); 33969cea0d4SMike 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)) { 38769cea0d4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 388518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 38969cea0d4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3901da177e4SLinus Torvalds goto failed; 3911f271972SBenjamin Marzinski } 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds /* Were we instructed to switch PG? */ 394506458efSWill Deacon if (READ_ONCE(m->next_pg)) { 3952da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 3961da177e4SLinus Torvalds pg = m->next_pg; 3972da1610aSMike Snitzer if (!pg) { 3982da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3992da1610aSMike Snitzer goto check_current_pg; 4002da1610aSMike Snitzer } 4011da177e4SLinus Torvalds m->next_pg = NULL; 4022da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4032da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4042da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 4052da1610aSMike Snitzer return pgpath; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /* Don't change PG until it has no remaining paths */ 4092da1610aSMike Snitzer check_current_pg: 410506458efSWill Deacon pg = READ_ONCE(m->current_pg); 4112da1610aSMike Snitzer if (pg) { 4122da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4132da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 4142da1610aSMike Snitzer return pgpath; 4152da1610aSMike Snitzer } 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds /* 4181da177e4SLinus Torvalds * Loop through priority groups until we find a valid path. 4191da177e4SLinus Torvalds * First time we skip PGs marked 'bypassed'. 420f220fd4eSMike Christie * Second time we only try the ones we skipped, but set 421f220fd4eSMike Christie * pg_init_delay_retry so we do not hammer controllers. 4221da177e4SLinus Torvalds */ 4231da177e4SLinus Torvalds do { 4241da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 425d19a55ccSMike Snitzer if (pg->bypassed == !!bypassed) 4261da177e4SLinus Torvalds continue; 4272da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4282da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) { 42969cea0d4SMike Snitzer if (!bypassed) { 43069cea0d4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 431518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 43269cea0d4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 43369cea0d4SMike Snitzer } 4342da1610aSMike Snitzer return pgpath; 4351da177e4SLinus Torvalds } 436f220fd4eSMike Christie } 4371da177e4SLinus Torvalds } while (bypassed--); 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds failed: 4402da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 4411da177e4SLinus Torvalds m->current_pgpath = NULL; 4421da177e4SLinus Torvalds m->current_pg = NULL; 4432da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4442da1610aSMike Snitzer 4452da1610aSMike Snitzer return NULL; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 44845e15720SKiyoshi Ueda /* 449ac75b09fSMike Snitzer * dm_report_EIO() is a macro instead of a function to make pr_debug_ratelimited() 45086331f39SBart Van Assche * report the function name and line number of the function from which 45186331f39SBart Van Assche * it has been invoked. 45245e15720SKiyoshi Ueda */ 45386331f39SBart Van Assche #define dm_report_EIO(m) \ 45418a482f5SChristoph Hellwig do { \ 45586331f39SBart Van Assche struct mapped_device *md = dm_table_get_md((m)->ti->table); \ 45686331f39SBart Van Assche \ 457ac75b09fSMike Snitzer DMDEBUG_LIMIT("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d", \ 45886331f39SBart Van Assche dm_device_name(md), \ 45986331f39SBart Van Assche test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \ 46086331f39SBart Van Assche test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \ 46186331f39SBart Van Assche dm_noflush_suspending((m)->ti)); \ 46218a482f5SChristoph Hellwig } while (0) 46345e15720SKiyoshi Ueda 46436fcffccSHannes Reinecke /* 465c1fd0abeSMike Snitzer * Check whether bios must be queued in the device-mapper core rather 466c1fd0abeSMike Snitzer * than here in the target. 467c1fd0abeSMike Snitzer */ 468a862e4e2SMike Snitzer static bool __must_push_back(struct multipath *m) 469c1fd0abeSMike Snitzer { 470a862e4e2SMike Snitzer return dm_noflush_suspending(m->ti); 471c1fd0abeSMike Snitzer } 472c1fd0abeSMike Snitzer 473c1fd0abeSMike Snitzer static bool must_push_back_rq(struct multipath *m) 474c1fd0abeSMike Snitzer { 47573265f3fSMike Snitzer unsigned long flags; 47673265f3fSMike Snitzer bool ret; 47773265f3fSMike Snitzer 47873265f3fSMike Snitzer spin_lock_irqsave(&m->lock, flags); 47973265f3fSMike Snitzer ret = (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) || __must_push_back(m)); 48073265f3fSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 48173265f3fSMike Snitzer 48273265f3fSMike Snitzer return ret; 483c1fd0abeSMike Snitzer } 484c1fd0abeSMike Snitzer 485c1fd0abeSMike Snitzer /* 48676e33fe4SMike Snitzer * Map cloned requests (request-based multipath) 48736fcffccSHannes Reinecke */ 488eb8db831SChristoph Hellwig static int multipath_clone_and_map(struct dm_target *ti, struct request *rq, 489e5863d9aSMike Snitzer union map_info *map_context, 490eb8db831SChristoph Hellwig struct request **__clone) 4911da177e4SLinus Torvalds { 4927943bd6dSMike Snitzer struct multipath *m = ti->private; 493eb8db831SChristoph Hellwig size_t nr_bytes = blk_rq_bytes(rq); 4941da177e4SLinus Torvalds struct pgpath *pgpath; 495f40c67f0SKiyoshi Ueda struct block_device *bdev; 496eb8db831SChristoph Hellwig struct dm_mpath_io *mpio = get_mpio(map_context); 4977083abbbSBart Van Assche struct request_queue *q; 498eb8db831SChristoph Hellwig struct request *clone; 4991da177e4SLinus Torvalds 5001da177e4SLinus Torvalds /* Do we need to select a new pgpath? */ 501506458efSWill Deacon pgpath = READ_ONCE(m->current_pgpath); 5022da1610aSMike Snitzer if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags)) 5032da1610aSMike Snitzer pgpath = choose_pgpath(m, nr_bytes); 5041da177e4SLinus Torvalds 5059bf59a61SMike Snitzer if (!pgpath) { 506c1fd0abeSMike Snitzer if (must_push_back_rq(m)) 507b88efd43SMike Snitzer return DM_MAPIO_DELAY_REQUEUE; 50818a482f5SChristoph Hellwig dm_report_EIO(m); /* Failed */ 509f98e0eb6SChristoph Hellwig return DM_MAPIO_KILL; 510518257b1SMike Snitzer } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) || 511518257b1SMike Snitzer test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 512459b5401SMing Lei pg_init_all_paths(m); 513c1d7ecf7SBart Van Assche return DM_MAPIO_DELAY_REQUEUE; 5149bf59a61SMike Snitzer } 5156afbc01dSMike Snitzer 516e8099177SHannes Reinecke mpio->pgpath = pgpath; 517e8099177SHannes Reinecke mpio->nr_bytes = nr_bytes; 5182eb6e1e3SKeith Busch 5192eb6e1e3SKeith Busch bdev = pgpath->path.dev->bdev; 5207083abbbSBart Van Assche q = bdev_get_queue(bdev); 521ff005a06SChristoph Hellwig clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE, 522ff005a06SChristoph Hellwig BLK_MQ_REQ_NOWAIT); 5236599c84eSBart Van Assche if (IS_ERR(clone)) { 5246599c84eSBart Van Assche /* EBUSY, ENODEV or EWOULDBLOCK: requeue */ 525848b8aefSMike Snitzer if (blk_queue_dying(q)) { 5267083abbbSBart Van Assche atomic_inc(&m->pg_init_in_progress); 5277083abbbSBart Van Assche activate_or_offline_path(pgpath); 528050af08fSMing Lei return DM_MAPIO_DELAY_REQUEUE; 5297083abbbSBart Van Assche } 530050af08fSMing Lei 531050af08fSMing Lei /* 532050af08fSMing Lei * blk-mq's SCHED_RESTART can cover this requeue, so we 533050af08fSMing Lei * needn't deal with it by DELAY_REQUEUE. More importantly, 534050af08fSMing Lei * we have to return DM_MAPIO_REQUEUE so that blk-mq can 535050af08fSMing Lei * get the queue busy feedback (via BLK_STS_RESOURCE), 536050af08fSMing Lei * otherwise I/O merging can suffer. 537050af08fSMing Lei */ 538050af08fSMing Lei return DM_MAPIO_REQUEUE; 5394c6dd53dSMike Snitzer } 5406599c84eSBart Van Assche clone->bio = clone->biotail = NULL; 5416599c84eSBart Van Assche clone->rq_disk = bdev->bd_disk; 5426599c84eSBart Van Assche clone->cmd_flags |= REQ_FAILFAST_TRANSPORT; 5436599c84eSBart Van Assche *__clone = clone; 5442eb6e1e3SKeith Busch 545e8099177SHannes Reinecke if (pgpath->pg->ps.type->start_io) 546e8099177SHannes Reinecke pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 547e8099177SHannes Reinecke &pgpath->path, 548e8099177SHannes Reinecke nr_bytes); 5492eb6e1e3SKeith Busch return DM_MAPIO_REMAPPED; 5501da177e4SLinus Torvalds } 5511da177e4SLinus Torvalds 5525de719e3SYufen Yu static void multipath_release_clone(struct request *clone, 5535de719e3SYufen Yu union map_info *map_context) 554e5863d9aSMike Snitzer { 5555de719e3SYufen Yu if (unlikely(map_context)) { 5565de719e3SYufen Yu /* 5575de719e3SYufen Yu * non-NULL map_context means caller is still map 5585de719e3SYufen Yu * method; must undo multipath_clone_and_map() 5595de719e3SYufen Yu */ 5605de719e3SYufen Yu struct dm_mpath_io *mpio = get_mpio(map_context); 5615de719e3SYufen Yu struct pgpath *pgpath = mpio->pgpath; 5625de719e3SYufen Yu 5635de719e3SYufen Yu if (pgpath && pgpath->pg->ps.type->end_io) 5645de719e3SYufen Yu pgpath->pg->ps.type->end_io(&pgpath->pg->ps, 5655de719e3SYufen Yu &pgpath->path, 566087615bfSGabriel Krisman Bertazi mpio->nr_bytes, 567087615bfSGabriel Krisman Bertazi clone->io_start_time_ns); 5685de719e3SYufen Yu } 5695de719e3SYufen Yu 570eb8db831SChristoph Hellwig blk_put_request(clone); 571e5863d9aSMike Snitzer } 572e5863d9aSMike Snitzer 5731da177e4SLinus Torvalds /* 57476e33fe4SMike Snitzer * Map cloned bios (bio-based multipath) 57576e33fe4SMike Snitzer */ 5760001ec56SMike Snitzer 57717213ec1SMike Snitzer static void __multipath_queue_bio(struct multipath *m, struct bio *bio) 57817213ec1SMike Snitzer { 57917213ec1SMike Snitzer /* Queue for the daemon to resubmit */ 58017213ec1SMike Snitzer bio_list_add(&m->queued_bios, bio); 58117213ec1SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) 58217213ec1SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 58317213ec1SMike Snitzer } 58417213ec1SMike Snitzer 585f45f1186SMike Snitzer static void multipath_queue_bio(struct multipath *m, struct bio *bio) 586f45f1186SMike Snitzer { 587f45f1186SMike Snitzer unsigned long flags; 588f45f1186SMike Snitzer 589f45f1186SMike Snitzer spin_lock_irqsave(&m->lock, flags); 59017213ec1SMike Snitzer __multipath_queue_bio(m, bio); 591f45f1186SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 592f45f1186SMike Snitzer } 593f45f1186SMike Snitzer 5940001ec56SMike Snitzer static struct pgpath *__map_bio(struct multipath *m, struct bio *bio) 59576e33fe4SMike Snitzer { 59676e33fe4SMike Snitzer struct pgpath *pgpath; 59776e33fe4SMike Snitzer unsigned long flags; 59876e33fe4SMike Snitzer 59976e33fe4SMike Snitzer /* Do we need to select a new pgpath? */ 600506458efSWill Deacon pgpath = READ_ONCE(m->current_pgpath); 6015686dee3SGabriel Krisman Bertazi if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags)) 6020001ec56SMike Snitzer pgpath = choose_pgpath(m, bio->bi_iter.bi_size); 60376e33fe4SMike Snitzer 60417213ec1SMike Snitzer if (!pgpath) { 60517213ec1SMike Snitzer spin_lock_irqsave(&m->lock, flags); 60617213ec1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 60717213ec1SMike Snitzer __multipath_queue_bio(m, bio); 60817213ec1SMike Snitzer pgpath = ERR_PTR(-EAGAIN); 60917213ec1SMike Snitzer } 61017213ec1SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 6115686dee3SGabriel Krisman Bertazi 61217213ec1SMike Snitzer } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) || 61317213ec1SMike Snitzer test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 614f45f1186SMike Snitzer multipath_queue_bio(m, bio); 61576e33fe4SMike Snitzer pg_init_all_paths(m); 6160001ec56SMike Snitzer return ERR_PTR(-EAGAIN); 61776e33fe4SMike Snitzer } 61876e33fe4SMike Snitzer 6190001ec56SMike Snitzer return pgpath; 62076e33fe4SMike Snitzer } 62176e33fe4SMike Snitzer 6220001ec56SMike Snitzer static int __multipath_map_bio(struct multipath *m, struct bio *bio, 6230001ec56SMike Snitzer struct dm_mpath_io *mpio) 6240001ec56SMike Snitzer { 625dbaf971cSMike Snitzer struct pgpath *pgpath = __map_bio(m, bio); 6260001ec56SMike Snitzer 6270001ec56SMike Snitzer if (IS_ERR(pgpath)) 6280001ec56SMike Snitzer return DM_MAPIO_SUBMITTED; 6290001ec56SMike Snitzer 63076e33fe4SMike Snitzer if (!pgpath) { 631a862e4e2SMike Snitzer if (__must_push_back(m)) 63276e33fe4SMike Snitzer return DM_MAPIO_REQUEUE; 63318a482f5SChristoph Hellwig dm_report_EIO(m); 634846785e6SChristoph Hellwig return DM_MAPIO_KILL; 63576e33fe4SMike Snitzer } 63676e33fe4SMike Snitzer 63776e33fe4SMike Snitzer mpio->pgpath = pgpath; 63876e33fe4SMike Snitzer 6394e4cbee9SChristoph Hellwig bio->bi_status = 0; 64074d46992SChristoph Hellwig bio_set_dev(bio, pgpath->path.dev->bdev); 6411eff9d32SJens Axboe bio->bi_opf |= REQ_FAILFAST_TRANSPORT; 64276e33fe4SMike Snitzer 64376e33fe4SMike Snitzer if (pgpath->pg->ps.type->start_io) 64476e33fe4SMike Snitzer pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 64576e33fe4SMike Snitzer &pgpath->path, 646d0442f80SMike Snitzer mpio->nr_bytes); 64776e33fe4SMike Snitzer return DM_MAPIO_REMAPPED; 64876e33fe4SMike Snitzer } 64976e33fe4SMike Snitzer 65076e33fe4SMike Snitzer static int multipath_map_bio(struct dm_target *ti, struct bio *bio) 65176e33fe4SMike Snitzer { 65276e33fe4SMike Snitzer struct multipath *m = ti->private; 653bf661be1SMike Snitzer struct dm_mpath_io *mpio = NULL; 654bf661be1SMike Snitzer 65563f6e6fdSMike Snitzer multipath_init_per_bio_data(bio, &mpio); 65676e33fe4SMike Snitzer return __multipath_map_bio(m, bio, mpio); 65776e33fe4SMike Snitzer } 65876e33fe4SMike Snitzer 6597e48c768SMike Snitzer static void process_queued_io_list(struct multipath *m) 66076e33fe4SMike Snitzer { 661953923c0SMike Snitzer if (m->queue_mode == DM_TYPE_REQUEST_BASED) 6627e48c768SMike Snitzer dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table)); 6638d47e659SMike Snitzer else if (m->queue_mode == DM_TYPE_BIO_BASED) 66476e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 66576e33fe4SMike Snitzer } 66676e33fe4SMike Snitzer 66776e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work) 66876e33fe4SMike Snitzer { 66976e33fe4SMike Snitzer int r; 67076e33fe4SMike Snitzer unsigned long flags; 67176e33fe4SMike Snitzer struct bio *bio; 67276e33fe4SMike Snitzer struct bio_list bios; 67376e33fe4SMike Snitzer struct blk_plug plug; 67476e33fe4SMike Snitzer struct multipath *m = 67576e33fe4SMike Snitzer container_of(work, struct multipath, process_queued_bios); 67676e33fe4SMike Snitzer 67776e33fe4SMike Snitzer bio_list_init(&bios); 67876e33fe4SMike Snitzer 67976e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 68076e33fe4SMike Snitzer 68176e33fe4SMike Snitzer if (bio_list_empty(&m->queued_bios)) { 68276e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 68376e33fe4SMike Snitzer return; 68476e33fe4SMike Snitzer } 68576e33fe4SMike Snitzer 68676e33fe4SMike Snitzer bio_list_merge(&bios, &m->queued_bios); 68776e33fe4SMike Snitzer bio_list_init(&m->queued_bios); 68876e33fe4SMike Snitzer 68976e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 69076e33fe4SMike Snitzer 69176e33fe4SMike Snitzer blk_start_plug(&plug); 69276e33fe4SMike Snitzer while ((bio = bio_list_pop(&bios))) { 6931836df08SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 6941836df08SMike Snitzer dm_bio_restore(get_bio_details_from_mpio(mpio), bio); 6951836df08SMike Snitzer r = __multipath_map_bio(m, bio, mpio); 696846785e6SChristoph Hellwig switch (r) { 697846785e6SChristoph Hellwig case DM_MAPIO_KILL: 6984e4cbee9SChristoph Hellwig bio->bi_status = BLK_STS_IOERR; 6994e4cbee9SChristoph Hellwig bio_endio(bio); 700047385b3SDan Carpenter break; 701846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE: 7024e4cbee9SChristoph Hellwig bio->bi_status = BLK_STS_DM_REQUEUE; 70376e33fe4SMike Snitzer bio_endio(bio); 704846785e6SChristoph Hellwig break; 705846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED: 70676e33fe4SMike Snitzer generic_make_request(bio); 707846785e6SChristoph Hellwig break; 7088192a0cdSWang Sheng-Hui case DM_MAPIO_SUBMITTED: 7099157c8d3SBart Van Assche break; 7109157c8d3SBart Van Assche default: 7119157c8d3SBart Van Assche WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r); 712846785e6SChristoph Hellwig } 71376e33fe4SMike Snitzer } 71476e33fe4SMike Snitzer blk_finish_plug(&plug); 71576e33fe4SMike Snitzer } 71676e33fe4SMike Snitzer 71776e33fe4SMike Snitzer /* 7181da177e4SLinus Torvalds * If we run out of usable paths, should we queue I/O or error it? 7191da177e4SLinus Torvalds */ 720be7d31ccSMike Snitzer static int queue_if_no_path(struct multipath *m, bool queue_if_no_path, 7214c3f4838SMike Snitzer bool save_old_value, const char *caller) 7221da177e4SLinus Torvalds { 7231da177e4SLinus Torvalds unsigned long flags; 724553ec94cSMike Snitzer bool queue_if_no_path_bit, saved_queue_if_no_path_bit; 7254c3f4838SMike Snitzer const char *dm_dev_name = dm_device_name(dm_table_get_md(m->ti->table)); 7264c3f4838SMike Snitzer 7274c3f4838SMike Snitzer DMDEBUG("%s: %s caller=%s queue_if_no_path=%d save_old_value=%d", 7284c3f4838SMike Snitzer dm_dev_name, __func__, caller, queue_if_no_path, save_old_value); 7291da177e4SLinus Torvalds 7301da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 731553ec94cSMike Snitzer 732553ec94cSMike Snitzer queue_if_no_path_bit = test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 733553ec94cSMike Snitzer saved_queue_if_no_path_bit = test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 734553ec94cSMike Snitzer 735553ec94cSMike Snitzer if (save_old_value) { 736553ec94cSMike Snitzer if (unlikely(!queue_if_no_path_bit && saved_queue_if_no_path_bit)) { 737553ec94cSMike Snitzer DMERR("%s: QIFNP disabled but saved as enabled, saving again loses state, not saving!", 7384c3f4838SMike Snitzer dm_dev_name); 739553ec94cSMike Snitzer } else 740553ec94cSMike Snitzer assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path_bit); 741553ec94cSMike Snitzer } else if (!queue_if_no_path && saved_queue_if_no_path_bit) { 742553ec94cSMike Snitzer /* due to "fail_if_no_path" message, need to honor it. */ 743553ec94cSMike Snitzer clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 744553ec94cSMike Snitzer } 745c1fd0abeSMike Snitzer assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path); 746553ec94cSMike Snitzer 7474c3f4838SMike Snitzer DMDEBUG("%s: after %s changes; QIFNP = %d; SQIFNP = %d; DNFS = %d", 7484c3f4838SMike Snitzer dm_dev_name, __func__, 7494c3f4838SMike Snitzer test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags), 7504c3f4838SMike Snitzer test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags), 7514c3f4838SMike Snitzer dm_noflush_suspending(m->ti)); 7524c3f4838SMike Snitzer 7531da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 7541da177e4SLinus Torvalds 75576e33fe4SMike Snitzer if (!queue_if_no_path) { 75663d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 7577e48c768SMike Snitzer process_queued_io_list(m); 75876e33fe4SMike Snitzer } 75963d832c3SHannes Reinecke 7601da177e4SLinus Torvalds return 0; 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /* 764be240ff5SAnatol Pomazau * If the queue_if_no_path timeout fires, turn off queue_if_no_path and 765be240ff5SAnatol Pomazau * process any queued I/O. 766be240ff5SAnatol Pomazau */ 767be240ff5SAnatol Pomazau static void queue_if_no_path_timeout_work(struct timer_list *t) 768be240ff5SAnatol Pomazau { 769be240ff5SAnatol Pomazau struct multipath *m = from_timer(m, t, nopath_timer); 770be240ff5SAnatol Pomazau struct mapped_device *md = dm_table_get_md(m->ti->table); 771be240ff5SAnatol Pomazau 772be240ff5SAnatol Pomazau DMWARN("queue_if_no_path timeout on %s, failing queued IO", dm_device_name(md)); 7734c3f4838SMike Snitzer queue_if_no_path(m, false, false, __func__); 774be240ff5SAnatol Pomazau } 775be240ff5SAnatol Pomazau 776be240ff5SAnatol Pomazau /* 777be240ff5SAnatol Pomazau * Enable the queue_if_no_path timeout if necessary. 778be240ff5SAnatol Pomazau * Called with m->lock held. 779be240ff5SAnatol Pomazau */ 780be240ff5SAnatol Pomazau static void enable_nopath_timeout(struct multipath *m) 781be240ff5SAnatol Pomazau { 782be240ff5SAnatol Pomazau unsigned long queue_if_no_path_timeout = 783be240ff5SAnatol Pomazau READ_ONCE(queue_if_no_path_timeout_secs) * HZ; 784be240ff5SAnatol Pomazau 785be240ff5SAnatol Pomazau lockdep_assert_held(&m->lock); 786be240ff5SAnatol Pomazau 787be240ff5SAnatol Pomazau if (queue_if_no_path_timeout > 0 && 788be240ff5SAnatol Pomazau atomic_read(&m->nr_valid_paths) == 0 && 789be240ff5SAnatol Pomazau test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 790be240ff5SAnatol Pomazau mod_timer(&m->nopath_timer, 791be240ff5SAnatol Pomazau jiffies + queue_if_no_path_timeout); 792be240ff5SAnatol Pomazau } 793be240ff5SAnatol Pomazau } 794be240ff5SAnatol Pomazau 795be240ff5SAnatol Pomazau static void disable_nopath_timeout(struct multipath *m) 796be240ff5SAnatol Pomazau { 797be240ff5SAnatol Pomazau del_timer_sync(&m->nopath_timer); 798be240ff5SAnatol Pomazau } 799be240ff5SAnatol Pomazau 800be240ff5SAnatol Pomazau /* 8011da177e4SLinus Torvalds * An event is triggered whenever a path is taken out of use. 8021da177e4SLinus Torvalds * Includes path failure and PG bypass. 8031da177e4SLinus Torvalds */ 804c4028958SDavid Howells static void trigger_event(struct work_struct *work) 8051da177e4SLinus Torvalds { 806c4028958SDavid Howells struct multipath *m = 807c4028958SDavid Howells container_of(work, struct multipath, trigger_event); 8081da177e4SLinus Torvalds 8091da177e4SLinus Torvalds dm_table_event(m->ti->table); 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds 8121da177e4SLinus Torvalds /*----------------------------------------------------------------- 8131da177e4SLinus Torvalds * Constructor/argument parsing: 8141da177e4SLinus Torvalds * <#multipath feature args> [<arg>]* 8151da177e4SLinus Torvalds * <#hw_handler args> [hw_handler [<arg>]*] 8161da177e4SLinus Torvalds * <#priority groups> 8171da177e4SLinus Torvalds * <initial priority group> 8181da177e4SLinus Torvalds * [<selector> <#selector args> [<arg>]* 8191da177e4SLinus Torvalds * <#paths> <#per-path selector args> 8201da177e4SLinus Torvalds * [<path> [<arg>]* ]+ ]+ 8211da177e4SLinus Torvalds *---------------------------------------------------------------*/ 822498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg, 8231da177e4SLinus Torvalds struct dm_target *ti) 8241da177e4SLinus Torvalds { 8251da177e4SLinus Torvalds int r; 8261da177e4SLinus Torvalds struct path_selector_type *pst; 8271da177e4SLinus Torvalds unsigned ps_argc; 8281da177e4SLinus Torvalds 8295916a22bSEric Biggers static const struct dm_arg _args[] = { 83072d94861SAlasdair G Kergon {0, 1024, "invalid number of path selector args"}, 8311da177e4SLinus Torvalds }; 8321da177e4SLinus Torvalds 833498f0103SMike Snitzer pst = dm_get_path_selector(dm_shift_arg(as)); 8341da177e4SLinus Torvalds if (!pst) { 83572d94861SAlasdair G Kergon ti->error = "unknown path selector type"; 8361da177e4SLinus Torvalds return -EINVAL; 8371da177e4SLinus Torvalds } 8381da177e4SLinus Torvalds 839498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &ps_argc, &ti->error); 840371b2e34SMikulas Patocka if (r) { 841371b2e34SMikulas Patocka dm_put_path_selector(pst); 8421da177e4SLinus Torvalds return -EINVAL; 843371b2e34SMikulas Patocka } 8441da177e4SLinus Torvalds 8451da177e4SLinus Torvalds r = pst->create(&pg->ps, ps_argc, as->argv); 8461da177e4SLinus Torvalds if (r) { 8471da177e4SLinus Torvalds dm_put_path_selector(pst); 84872d94861SAlasdair G Kergon ti->error = "path selector constructor failed"; 8491da177e4SLinus Torvalds return r; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 8521da177e4SLinus Torvalds pg->ps.type = pst; 853498f0103SMike Snitzer dm_consume_args(as, ps_argc); 8541da177e4SLinus Torvalds 8551da177e4SLinus Torvalds return 0; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 858e8f74a0fSMike Snitzer static int setup_scsi_dh(struct block_device *bdev, struct multipath *m, 859b592211cSMike Snitzer const char **attached_handler_name, char **error) 8601da177e4SLinus Torvalds { 861848b8aefSMike Snitzer struct request_queue *q = bdev_get_queue(bdev); 862848b8aefSMike Snitzer int r; 863a0cf7ea9SHannes Reinecke 864518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) { 8651bab0de0SChristoph Hellwig retain: 866b592211cSMike Snitzer if (*attached_handler_name) { 867a58a935dSMike Snitzer /* 86854cd640dStang.junhui * Clear any hw_handler_params associated with a 86954cd640dStang.junhui * handler that isn't already attached. 87054cd640dStang.junhui */ 871b592211cSMike Snitzer if (m->hw_handler_name && strcmp(*attached_handler_name, m->hw_handler_name)) { 87254cd640dStang.junhui kfree(m->hw_handler_params); 87354cd640dStang.junhui m->hw_handler_params = NULL; 87454cd640dStang.junhui } 87554cd640dStang.junhui 87654cd640dStang.junhui /* 877a58a935dSMike Snitzer * Reset hw_handler_name to match the attached handler 878a58a935dSMike Snitzer * 879a58a935dSMike Snitzer * NB. This modifies the table line to show the actual 880a58a935dSMike Snitzer * handler instead of the original table passed in. 881a58a935dSMike Snitzer */ 882a58a935dSMike Snitzer kfree(m->hw_handler_name); 883b592211cSMike Snitzer m->hw_handler_name = *attached_handler_name; 884b592211cSMike Snitzer *attached_handler_name = NULL; 885a58a935dSMike Snitzer } 886a58a935dSMike Snitzer } 887a58a935dSMike Snitzer 888a58a935dSMike Snitzer if (m->hw_handler_name) { 889a0cf7ea9SHannes Reinecke r = scsi_dh_attach(q, m->hw_handler_name); 890a0cf7ea9SHannes Reinecke if (r == -EBUSY) { 8911bab0de0SChristoph Hellwig char b[BDEVNAME_SIZE]; 892a0cf7ea9SHannes Reinecke 8931bab0de0SChristoph Hellwig printk(KERN_INFO "dm-mpath: retaining handler on device %s\n", 894848b8aefSMike Snitzer bdevname(bdev, b)); 8951bab0de0SChristoph Hellwig goto retain; 8961bab0de0SChristoph Hellwig } 897ae11b1b3SHannes Reinecke if (r < 0) { 898848b8aefSMike Snitzer *error = "error attaching hardware handler"; 899848b8aefSMike Snitzer return r; 900ae11b1b3SHannes Reinecke } 9012bfd2e13SChandra Seetharaman 9022bfd2e13SChandra Seetharaman if (m->hw_handler_params) { 9032bfd2e13SChandra Seetharaman r = scsi_dh_set_params(q, m->hw_handler_params); 9042bfd2e13SChandra Seetharaman if (r < 0) { 905848b8aefSMike Snitzer *error = "unable to set hardware handler parameters"; 906848b8aefSMike Snitzer return r; 907848b8aefSMike Snitzer } 908848b8aefSMike Snitzer } 909848b8aefSMike Snitzer } 910848b8aefSMike Snitzer 911848b8aefSMike Snitzer return 0; 912848b8aefSMike Snitzer } 913848b8aefSMike Snitzer 914848b8aefSMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps, 915848b8aefSMike Snitzer struct dm_target *ti) 916848b8aefSMike Snitzer { 917848b8aefSMike Snitzer int r; 918848b8aefSMike Snitzer struct pgpath *p; 919848b8aefSMike Snitzer struct multipath *m = ti->private; 920e8f74a0fSMike Snitzer struct request_queue *q; 921b592211cSMike Snitzer const char *attached_handler_name = NULL; 922848b8aefSMike Snitzer 923848b8aefSMike Snitzer /* we need at least a path arg */ 924848b8aefSMike Snitzer if (as->argc < 1) { 925848b8aefSMike Snitzer ti->error = "no device given"; 926848b8aefSMike Snitzer return ERR_PTR(-EINVAL); 927848b8aefSMike Snitzer } 928848b8aefSMike Snitzer 929848b8aefSMike Snitzer p = alloc_pgpath(); 930848b8aefSMike Snitzer if (!p) 931848b8aefSMike Snitzer return ERR_PTR(-ENOMEM); 932848b8aefSMike Snitzer 933848b8aefSMike Snitzer r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table), 934848b8aefSMike Snitzer &p->path.dev); 935848b8aefSMike Snitzer if (r) { 936848b8aefSMike Snitzer ti->error = "error getting device"; 9372bfd2e13SChandra Seetharaman goto bad; 9382bfd2e13SChandra Seetharaman } 939848b8aefSMike Snitzer 940e8f74a0fSMike Snitzer q = bdev_get_queue(p->path.dev->bdev); 941e8f74a0fSMike Snitzer attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL); 942e457edf0SMike Snitzer if (attached_handler_name || m->hw_handler_name) { 943848b8aefSMike Snitzer INIT_DELAYED_WORK(&p->activate_path, activate_path_work); 944b592211cSMike Snitzer r = setup_scsi_dh(p->path.dev->bdev, m, &attached_handler_name, &ti->error); 945940bc471SMartin Wilck kfree(attached_handler_name); 946848b8aefSMike Snitzer if (r) { 947848b8aefSMike Snitzer dm_put_device(ti, p->path.dev); 948848b8aefSMike Snitzer goto bad; 9492bfd2e13SChandra Seetharaman } 950ae11b1b3SHannes Reinecke } 951ae11b1b3SHannes Reinecke 9521da177e4SLinus Torvalds r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); 9531da177e4SLinus Torvalds if (r) { 9541da177e4SLinus Torvalds dm_put_device(ti, p->path.dev); 9551da177e4SLinus Torvalds goto bad; 9561da177e4SLinus Torvalds } 9571da177e4SLinus Torvalds 9581da177e4SLinus Torvalds return p; 9591da177e4SLinus Torvalds bad: 9601da177e4SLinus Torvalds free_pgpath(p); 96101460f35SBenjamin Marzinski return ERR_PTR(r); 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds 964498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as, 96528f16c20SMicha³ Miros³aw struct multipath *m) 9661da177e4SLinus Torvalds { 9675916a22bSEric Biggers static const struct dm_arg _args[] = { 96872d94861SAlasdair G Kergon {1, 1024, "invalid number of paths"}, 96972d94861SAlasdair G Kergon {0, 1024, "invalid number of selector args"} 9701da177e4SLinus Torvalds }; 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds int r; 973498f0103SMike Snitzer unsigned i, nr_selector_args, nr_args; 9741da177e4SLinus Torvalds struct priority_group *pg; 97528f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds if (as->argc < 2) { 9781da177e4SLinus Torvalds as->argc = 0; 97901460f35SBenjamin Marzinski ti->error = "not enough priority group arguments"; 98001460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 9811da177e4SLinus Torvalds } 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds pg = alloc_priority_group(); 9841da177e4SLinus Torvalds if (!pg) { 98572d94861SAlasdair G Kergon ti->error = "couldn't allocate priority group"; 98601460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 9871da177e4SLinus Torvalds } 9881da177e4SLinus Torvalds pg->m = m; 9891da177e4SLinus Torvalds 9901da177e4SLinus Torvalds r = parse_path_selector(as, pg, ti); 9911da177e4SLinus Torvalds if (r) 9921da177e4SLinus Torvalds goto bad; 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds /* 9951da177e4SLinus Torvalds * read the paths 9961da177e4SLinus Torvalds */ 997498f0103SMike Snitzer r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error); 9981da177e4SLinus Torvalds if (r) 9991da177e4SLinus Torvalds goto bad; 10001da177e4SLinus Torvalds 1001498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error); 10021da177e4SLinus Torvalds if (r) 10031da177e4SLinus Torvalds goto bad; 10041da177e4SLinus Torvalds 1005498f0103SMike Snitzer nr_args = 1 + nr_selector_args; 10061da177e4SLinus Torvalds for (i = 0; i < pg->nr_pgpaths; i++) { 10071da177e4SLinus Torvalds struct pgpath *pgpath; 1008498f0103SMike Snitzer struct dm_arg_set path_args; 10091da177e4SLinus Torvalds 1010498f0103SMike Snitzer if (as->argc < nr_args) { 1011148acff6SMikulas Patocka ti->error = "not enough path parameters"; 10126bbf79a1SAlasdair G Kergon r = -EINVAL; 10131da177e4SLinus Torvalds goto bad; 1014148acff6SMikulas Patocka } 10151da177e4SLinus Torvalds 1016498f0103SMike Snitzer path_args.argc = nr_args; 10171da177e4SLinus Torvalds path_args.argv = as->argv; 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds pgpath = parse_path(&path_args, &pg->ps, ti); 102001460f35SBenjamin Marzinski if (IS_ERR(pgpath)) { 102101460f35SBenjamin Marzinski r = PTR_ERR(pgpath); 10221da177e4SLinus Torvalds goto bad; 102301460f35SBenjamin Marzinski } 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds pgpath->pg = pg; 10261da177e4SLinus Torvalds list_add_tail(&pgpath->list, &pg->pgpaths); 1027498f0103SMike Snitzer dm_consume_args(as, nr_args); 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds 10301da177e4SLinus Torvalds return pg; 10311da177e4SLinus Torvalds 10321da177e4SLinus Torvalds bad: 10331da177e4SLinus Torvalds free_priority_group(pg, ti); 103401460f35SBenjamin Marzinski return ERR_PTR(r); 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds 1037498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m) 10381da177e4SLinus Torvalds { 10391da177e4SLinus Torvalds unsigned hw_argc; 10402bfd2e13SChandra Seetharaman int ret; 104128f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 10421da177e4SLinus Torvalds 10435916a22bSEric Biggers static const struct dm_arg _args[] = { 104472d94861SAlasdair G Kergon {0, 1024, "invalid number of hardware handler args"}, 10451da177e4SLinus Torvalds }; 10461da177e4SLinus Torvalds 1047498f0103SMike Snitzer if (dm_read_arg_group(_args, as, &hw_argc, &ti->error)) 10481da177e4SLinus Torvalds return -EINVAL; 10491da177e4SLinus Torvalds 10501da177e4SLinus Torvalds if (!hw_argc) 10511da177e4SLinus Torvalds return 0; 10521da177e4SLinus Torvalds 10538d47e659SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) { 105476e33fe4SMike Snitzer dm_consume_args(as, hw_argc); 105576e33fe4SMike Snitzer DMERR("bio-based multipath doesn't allow hardware handler args"); 105676e33fe4SMike Snitzer return 0; 105776e33fe4SMike Snitzer } 105876e33fe4SMike Snitzer 1059498f0103SMike Snitzer m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL); 1060f97dc421Stang.junhui if (!m->hw_handler_name) 1061f97dc421Stang.junhui return -EINVAL; 106214e98c5cSChandra Seetharaman 10632bfd2e13SChandra Seetharaman if (hw_argc > 1) { 10642bfd2e13SChandra Seetharaman char *p; 10652bfd2e13SChandra Seetharaman int i, j, len = 4; 10662bfd2e13SChandra Seetharaman 10672bfd2e13SChandra Seetharaman for (i = 0; i <= hw_argc - 2; i++) 10682bfd2e13SChandra Seetharaman len += strlen(as->argv[i]) + 1; 10692bfd2e13SChandra Seetharaman p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); 10702bfd2e13SChandra Seetharaman if (!p) { 10712bfd2e13SChandra Seetharaman ti->error = "memory allocation failed"; 10722bfd2e13SChandra Seetharaman ret = -ENOMEM; 10732bfd2e13SChandra Seetharaman goto fail; 10742bfd2e13SChandra Seetharaman } 10752bfd2e13SChandra Seetharaman j = sprintf(p, "%d", hw_argc - 1); 10762bfd2e13SChandra Seetharaman for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1) 10772bfd2e13SChandra Seetharaman j = sprintf(p, "%s", as->argv[i]); 10782bfd2e13SChandra Seetharaman } 1079498f0103SMike Snitzer dm_consume_args(as, hw_argc - 1); 10801da177e4SLinus Torvalds 10811da177e4SLinus Torvalds return 0; 10822bfd2e13SChandra Seetharaman fail: 10832bfd2e13SChandra Seetharaman kfree(m->hw_handler_name); 10842bfd2e13SChandra Seetharaman m->hw_handler_name = NULL; 10852bfd2e13SChandra Seetharaman return ret; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 1088498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m) 10891da177e4SLinus Torvalds { 10901da177e4SLinus Torvalds int r; 10911da177e4SLinus Torvalds unsigned argc; 109228f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 1093498f0103SMike Snitzer const char *arg_name; 10941da177e4SLinus Torvalds 10955916a22bSEric Biggers static const struct dm_arg _args[] = { 1096e83068a5SMike Snitzer {0, 8, "invalid number of feature args"}, 1097c9e45581SDave Wysochanski {1, 50, "pg_init_retries must be between 1 and 50"}, 10984e2d19e4SChandra Seetharaman {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"}, 10991da177e4SLinus Torvalds }; 11001da177e4SLinus Torvalds 1101498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &argc, &ti->error); 11021da177e4SLinus Torvalds if (r) 11031da177e4SLinus Torvalds return -EINVAL; 11041da177e4SLinus Torvalds 11051da177e4SLinus Torvalds if (!argc) 11061da177e4SLinus Torvalds return 0; 11071da177e4SLinus Torvalds 1108c9e45581SDave Wysochanski do { 1109498f0103SMike Snitzer arg_name = dm_shift_arg(as); 1110c9e45581SDave Wysochanski argc--; 1111c9e45581SDave Wysochanski 1112498f0103SMike Snitzer if (!strcasecmp(arg_name, "queue_if_no_path")) { 11134c3f4838SMike Snitzer r = queue_if_no_path(m, true, false, __func__); 1114c9e45581SDave Wysochanski continue; 11151da177e4SLinus Torvalds } 1116c9e45581SDave Wysochanski 1117a58a935dSMike Snitzer if (!strcasecmp(arg_name, "retain_attached_hw_handler")) { 1118518257b1SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 1119a58a935dSMike Snitzer continue; 1120a58a935dSMike Snitzer } 1121a58a935dSMike Snitzer 1122498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_retries") && 1123c9e45581SDave Wysochanski (argc >= 1)) { 1124498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error); 1125c9e45581SDave Wysochanski argc--; 1126c9e45581SDave Wysochanski continue; 1127c9e45581SDave Wysochanski } 1128c9e45581SDave Wysochanski 1129498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_delay_msecs") && 11304e2d19e4SChandra Seetharaman (argc >= 1)) { 1131498f0103SMike Snitzer r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error); 11324e2d19e4SChandra Seetharaman argc--; 11334e2d19e4SChandra Seetharaman continue; 11344e2d19e4SChandra Seetharaman } 11354e2d19e4SChandra Seetharaman 1136e83068a5SMike Snitzer if (!strcasecmp(arg_name, "queue_mode") && 1137e83068a5SMike Snitzer (argc >= 1)) { 1138e83068a5SMike Snitzer const char *queue_mode_name = dm_shift_arg(as); 1139e83068a5SMike Snitzer 1140e83068a5SMike Snitzer if (!strcasecmp(queue_mode_name, "bio")) 1141e83068a5SMike Snitzer m->queue_mode = DM_TYPE_BIO_BASED; 1142953923c0SMike Snitzer else if (!strcasecmp(queue_mode_name, "rq") || 1143953923c0SMike Snitzer !strcasecmp(queue_mode_name, "mq")) 1144e83068a5SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 1145e83068a5SMike Snitzer else { 1146e83068a5SMike Snitzer ti->error = "Unknown 'queue_mode' requested"; 1147e83068a5SMike Snitzer r = -EINVAL; 1148e83068a5SMike Snitzer } 1149e83068a5SMike Snitzer argc--; 1150e83068a5SMike Snitzer continue; 1151e83068a5SMike Snitzer } 1152e83068a5SMike Snitzer 1153c9e45581SDave Wysochanski ti->error = "Unrecognised multipath feature request"; 1154c9e45581SDave Wysochanski r = -EINVAL; 1155c9e45581SDave Wysochanski } while (argc && !r); 1156c9e45581SDave Wysochanski 1157c9e45581SDave Wysochanski return r; 11581da177e4SLinus Torvalds } 11591da177e4SLinus Torvalds 1160e83068a5SMike Snitzer static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv) 11611da177e4SLinus Torvalds { 1162498f0103SMike Snitzer /* target arguments */ 11635916a22bSEric Biggers static const struct dm_arg _args[] = { 1164a490a07aSMike Snitzer {0, 1024, "invalid number of priority groups"}, 1165a490a07aSMike Snitzer {0, 1024, "invalid initial priority group number"}, 11661da177e4SLinus Torvalds }; 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds int r; 11691da177e4SLinus Torvalds struct multipath *m; 1170498f0103SMike Snitzer struct dm_arg_set as; 11711da177e4SLinus Torvalds unsigned pg_count = 0; 11721da177e4SLinus Torvalds unsigned next_pg_num; 1173be240ff5SAnatol Pomazau unsigned long flags; 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds as.argc = argc; 11761da177e4SLinus Torvalds as.argv = argv; 11771da177e4SLinus Torvalds 1178e83068a5SMike Snitzer m = alloc_multipath(ti); 11791da177e4SLinus Torvalds if (!m) { 118072d94861SAlasdair G Kergon ti->error = "can't allocate multipath"; 11811da177e4SLinus Torvalds return -EINVAL; 11821da177e4SLinus Torvalds } 11831da177e4SLinus Torvalds 118428f16c20SMicha³ Miros³aw r = parse_features(&as, m); 11851da177e4SLinus Torvalds if (r) 11861da177e4SLinus Torvalds goto bad; 11871da177e4SLinus Torvalds 1188e83068a5SMike Snitzer r = alloc_multipath_stage2(ti, m); 1189e83068a5SMike Snitzer if (r) 1190e83068a5SMike Snitzer goto bad; 1191e83068a5SMike Snitzer 119228f16c20SMicha³ Miros³aw r = parse_hw_handler(&as, m); 11931da177e4SLinus Torvalds if (r) 11941da177e4SLinus Torvalds goto bad; 11951da177e4SLinus Torvalds 1196498f0103SMike Snitzer r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error); 11971da177e4SLinus Torvalds if (r) 11981da177e4SLinus Torvalds goto bad; 11991da177e4SLinus Torvalds 1200498f0103SMike Snitzer r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error); 12011da177e4SLinus Torvalds if (r) 12021da177e4SLinus Torvalds goto bad; 12031da177e4SLinus Torvalds 1204a490a07aSMike Snitzer if ((!m->nr_priority_groups && next_pg_num) || 1205a490a07aSMike Snitzer (m->nr_priority_groups && !next_pg_num)) { 1206a490a07aSMike Snitzer ti->error = "invalid initial priority group"; 1207a490a07aSMike Snitzer r = -EINVAL; 1208a490a07aSMike Snitzer goto bad; 1209a490a07aSMike Snitzer } 1210a490a07aSMike Snitzer 12111da177e4SLinus Torvalds /* parse the priority groups */ 12121da177e4SLinus Torvalds while (as.argc) { 12131da177e4SLinus Torvalds struct priority_group *pg; 121491e968aaSMike Snitzer unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths); 12151da177e4SLinus Torvalds 121628f16c20SMicha³ Miros³aw pg = parse_priority_group(&as, m); 121701460f35SBenjamin Marzinski if (IS_ERR(pg)) { 121801460f35SBenjamin Marzinski r = PTR_ERR(pg); 12191da177e4SLinus Torvalds goto bad; 12201da177e4SLinus Torvalds } 12211da177e4SLinus Torvalds 122291e968aaSMike Snitzer nr_valid_paths += pg->nr_pgpaths; 122391e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, nr_valid_paths); 122491e968aaSMike Snitzer 12251da177e4SLinus Torvalds list_add_tail(&pg->list, &m->priority_groups); 12261da177e4SLinus Torvalds pg_count++; 12271da177e4SLinus Torvalds pg->pg_num = pg_count; 12281da177e4SLinus Torvalds if (!--next_pg_num) 12291da177e4SLinus Torvalds m->next_pg = pg; 12301da177e4SLinus Torvalds } 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds if (pg_count != m->nr_priority_groups) { 123372d94861SAlasdair G Kergon ti->error = "priority group count mismatch"; 12341da177e4SLinus Torvalds r = -EINVAL; 12351da177e4SLinus Torvalds goto bad; 12361da177e4SLinus Torvalds } 12371da177e4SLinus Torvalds 1238be240ff5SAnatol Pomazau spin_lock_irqsave(&m->lock, flags); 1239be240ff5SAnatol Pomazau enable_nopath_timeout(m); 1240be240ff5SAnatol Pomazau spin_unlock_irqrestore(&m->lock, flags); 1241be240ff5SAnatol Pomazau 124255a62eefSAlasdair G Kergon ti->num_flush_bios = 1; 124355a62eefSAlasdair G Kergon ti->num_discard_bios = 1; 1244042bcef8SMike Snitzer ti->num_write_same_bios = 1; 1245ac62d620SChristoph Hellwig ti->num_write_zeroes_bios = 1; 12468d47e659SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) 1247bf661be1SMike Snitzer ti->per_io_data_size = multipath_per_bio_data_size(); 1248eb8db831SChristoph Hellwig else 12498637a6bfSMike Snitzer ti->per_io_data_size = sizeof(struct dm_mpath_io); 12508627921fSMikulas Patocka 12511da177e4SLinus Torvalds return 0; 12521da177e4SLinus Torvalds 12531da177e4SLinus Torvalds bad: 12541da177e4SLinus Torvalds free_multipath(m); 12551da177e4SLinus Torvalds return r; 12561da177e4SLinus Torvalds } 12571da177e4SLinus Torvalds 12582bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m) 12592bded7bdSKiyoshi Ueda { 12609f4c3f87SBart Van Assche DEFINE_WAIT(wait); 12612bded7bdSKiyoshi Ueda 12622bded7bdSKiyoshi Ueda while (1) { 12639f4c3f87SBart Van Assche prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE); 12642bded7bdSKiyoshi Ueda 126591e968aaSMike Snitzer if (!atomic_read(&m->pg_init_in_progress)) 12662bded7bdSKiyoshi Ueda break; 12672bded7bdSKiyoshi Ueda 12682bded7bdSKiyoshi Ueda io_schedule(); 12692bded7bdSKiyoshi Ueda } 12709f4c3f87SBart Van Assche finish_wait(&m->pg_init_wait, &wait); 12712bded7bdSKiyoshi Ueda } 12722bded7bdSKiyoshi Ueda 12732bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m) 12741da177e4SLinus Torvalds { 1275848b8aefSMike Snitzer if (m->hw_handler_name) { 1276518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1277518257b1SMike Snitzer smp_mb__after_atomic(); 1278954a73d5SShiva Krishna Merla 1279935fcc56Swuzhouhui if (atomic_read(&m->pg_init_in_progress)) 1280bab7cfc7SChandra Seetharaman flush_workqueue(kmpath_handlerd); 12812bded7bdSKiyoshi Ueda multipath_wait_for_pg_init_completion(m); 1282954a73d5SShiva Krishna Merla 1283518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1284518257b1SMike Snitzer smp_mb__after_atomic(); 12856df400abSKiyoshi Ueda } 12866df400abSKiyoshi Ueda 1287935fcc56Swuzhouhui if (m->queue_mode == DM_TYPE_BIO_BASED) 1288935fcc56Swuzhouhui flush_work(&m->process_queued_bios); 1289848b8aefSMike Snitzer flush_work(&m->trigger_event); 1290848b8aefSMike Snitzer } 1291848b8aefSMike Snitzer 12926df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti) 12936df400abSKiyoshi Ueda { 12946df400abSKiyoshi Ueda struct multipath *m = ti->private; 12956df400abSKiyoshi Ueda 1296be240ff5SAnatol Pomazau disable_nopath_timeout(m); 12972bded7bdSKiyoshi Ueda flush_multipath_work(m); 12981da177e4SLinus Torvalds free_multipath(m); 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds /* 13021da177e4SLinus Torvalds * Take a path out of use. 13031da177e4SLinus Torvalds */ 13041da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath) 13051da177e4SLinus Torvalds { 13061da177e4SLinus Torvalds unsigned long flags; 13071da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13101da177e4SLinus Torvalds 13116680073dSKiyoshi Ueda if (!pgpath->is_active) 13121da177e4SLinus Torvalds goto out; 13131da177e4SLinus Torvalds 131404867370SMike Snitzer DMWARN("%s: Failing path %s.", 131504867370SMike Snitzer dm_device_name(dm_table_get_md(m->ti->table)), 131604867370SMike Snitzer pgpath->path.dev->name); 13171da177e4SLinus Torvalds 13181da177e4SLinus Torvalds pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); 1319be7d31ccSMike Snitzer pgpath->is_active = false; 13201da177e4SLinus Torvalds pgpath->fail_count++; 13211da177e4SLinus Torvalds 132291e968aaSMike Snitzer atomic_dec(&m->nr_valid_paths); 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds if (pgpath == m->current_pgpath) 13251da177e4SLinus Torvalds m->current_pgpath = NULL; 13261da177e4SLinus Torvalds 1327b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti, 132891e968aaSMike Snitzer pgpath->path.dev->name, atomic_read(&m->nr_valid_paths)); 1329b15546f9SMike Anderson 1330fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13311da177e4SLinus Torvalds 1332be240ff5SAnatol Pomazau enable_nopath_timeout(m); 1333be240ff5SAnatol Pomazau 13341da177e4SLinus Torvalds out: 13351da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13361da177e4SLinus Torvalds 13371da177e4SLinus Torvalds return 0; 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds /* 13411da177e4SLinus Torvalds * Reinstate a previously-failed path 13421da177e4SLinus Torvalds */ 13431da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath) 13441da177e4SLinus Torvalds { 134563d832c3SHannes Reinecke int r = 0, run_queue = 0; 13461da177e4SLinus Torvalds unsigned long flags; 13471da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 134891e968aaSMike Snitzer unsigned nr_valid_paths; 13491da177e4SLinus Torvalds 13501da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13511da177e4SLinus Torvalds 13526680073dSKiyoshi Ueda if (pgpath->is_active) 13531da177e4SLinus Torvalds goto out; 13541da177e4SLinus Torvalds 135504867370SMike Snitzer DMWARN("%s: Reinstating path %s.", 135604867370SMike Snitzer dm_device_name(dm_table_get_md(m->ti->table)), 135704867370SMike Snitzer pgpath->path.dev->name); 13581da177e4SLinus Torvalds 13591da177e4SLinus Torvalds r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); 13601da177e4SLinus Torvalds if (r) 13611da177e4SLinus Torvalds goto out; 13621da177e4SLinus Torvalds 1363be7d31ccSMike Snitzer pgpath->is_active = true; 13641da177e4SLinus Torvalds 136591e968aaSMike Snitzer nr_valid_paths = atomic_inc_return(&m->nr_valid_paths); 136691e968aaSMike Snitzer if (nr_valid_paths == 1) { 13671da177e4SLinus Torvalds m->current_pgpath = NULL; 136863d832c3SHannes Reinecke run_queue = 1; 1369e54f77ddSChandra Seetharaman } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) { 13704e2d19e4SChandra Seetharaman if (queue_work(kmpath_handlerd, &pgpath->activate_path.work)) 137191e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 1372e54f77ddSChandra Seetharaman } 13731da177e4SLinus Torvalds 1374b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti, 137591e968aaSMike Snitzer pgpath->path.dev->name, nr_valid_paths); 1376b15546f9SMike Anderson 1377fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13781da177e4SLinus Torvalds 13791da177e4SLinus Torvalds out: 13801da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 138176e33fe4SMike Snitzer if (run_queue) { 138263d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 13837e48c768SMike Snitzer process_queued_io_list(m); 138476e33fe4SMike Snitzer } 13851da177e4SLinus Torvalds 1386be240ff5SAnatol Pomazau if (pgpath->is_active) 1387be240ff5SAnatol Pomazau disable_nopath_timeout(m); 1388be240ff5SAnatol Pomazau 13891da177e4SLinus Torvalds return r; 13901da177e4SLinus Torvalds } 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds /* 13931da177e4SLinus Torvalds * Fail or reinstate all paths that match the provided struct dm_dev. 13941da177e4SLinus Torvalds */ 13951da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev, 13961da177e4SLinus Torvalds action_fn action) 13971da177e4SLinus Torvalds { 139819040c0bSMike Snitzer int r = -EINVAL; 13991da177e4SLinus Torvalds struct pgpath *pgpath; 14001da177e4SLinus Torvalds struct priority_group *pg; 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 14031da177e4SLinus Torvalds list_for_each_entry(pgpath, &pg->pgpaths, list) { 14041da177e4SLinus Torvalds if (pgpath->path.dev == dev) 14051da177e4SLinus Torvalds r = action(pgpath); 14061da177e4SLinus Torvalds } 14071da177e4SLinus Torvalds } 14081da177e4SLinus Torvalds 14091da177e4SLinus Torvalds return r; 14101da177e4SLinus Torvalds } 14111da177e4SLinus Torvalds 14121da177e4SLinus Torvalds /* 14131da177e4SLinus Torvalds * Temporarily try to avoid having to use the specified PG 14141da177e4SLinus Torvalds */ 14151da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg, 1416be7d31ccSMike Snitzer bool bypassed) 14171da177e4SLinus Torvalds { 14181da177e4SLinus Torvalds unsigned long flags; 14191da177e4SLinus Torvalds 14201da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds pg->bypassed = bypassed; 14231da177e4SLinus Torvalds m->current_pgpath = NULL; 14241da177e4SLinus Torvalds m->current_pg = NULL; 14251da177e4SLinus Torvalds 14261da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 14271da177e4SLinus Torvalds 1428fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 14291da177e4SLinus Torvalds } 14301da177e4SLinus Torvalds 14311da177e4SLinus Torvalds /* 14321da177e4SLinus Torvalds * Switch to using the specified PG from the next I/O that gets mapped 14331da177e4SLinus Torvalds */ 14341da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr) 14351da177e4SLinus Torvalds { 14361da177e4SLinus Torvalds struct priority_group *pg; 14371da177e4SLinus Torvalds unsigned pgnum; 14381da177e4SLinus Torvalds unsigned long flags; 143931998ef1SMikulas Patocka char dummy; 14401da177e4SLinus Torvalds 144131998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 1442cc5bd925Stang.junhui !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) { 14431da177e4SLinus Torvalds DMWARN("invalid PG number supplied to switch_pg_num"); 14441da177e4SLinus Torvalds return -EINVAL; 14451da177e4SLinus Torvalds } 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 14481da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 1449be7d31ccSMike Snitzer pg->bypassed = false; 14501da177e4SLinus Torvalds if (--pgnum) 14511da177e4SLinus Torvalds continue; 14521da177e4SLinus Torvalds 14531da177e4SLinus Torvalds m->current_pgpath = NULL; 14541da177e4SLinus Torvalds m->current_pg = NULL; 14551da177e4SLinus Torvalds m->next_pg = pg; 14561da177e4SLinus Torvalds } 14571da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 14581da177e4SLinus Torvalds 1459fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 14601da177e4SLinus Torvalds return 0; 14611da177e4SLinus Torvalds } 14621da177e4SLinus Torvalds 14631da177e4SLinus Torvalds /* 14641da177e4SLinus Torvalds * Set/clear bypassed status of a PG. 14651da177e4SLinus Torvalds * PGs are numbered upwards from 1 in the order they were declared. 14661da177e4SLinus Torvalds */ 1467be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed) 14681da177e4SLinus Torvalds { 14691da177e4SLinus Torvalds struct priority_group *pg; 14701da177e4SLinus Torvalds unsigned pgnum; 147131998ef1SMikulas Patocka char dummy; 14721da177e4SLinus Torvalds 147331998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 1474cc5bd925Stang.junhui !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) { 14751da177e4SLinus Torvalds DMWARN("invalid PG number supplied to bypass_pg"); 14761da177e4SLinus Torvalds return -EINVAL; 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds 14791da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 14801da177e4SLinus Torvalds if (!--pgnum) 14811da177e4SLinus Torvalds break; 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds 14841da177e4SLinus Torvalds bypass_pg(m, pg, bypassed); 14851da177e4SLinus Torvalds return 0; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 14881da177e4SLinus Torvalds /* 1489c9e45581SDave Wysochanski * Should we retry pg_init immediately? 1490c9e45581SDave Wysochanski */ 1491be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath) 1492c9e45581SDave Wysochanski { 1493c9e45581SDave Wysochanski unsigned long flags; 1494be7d31ccSMike Snitzer bool limit_reached = false; 1495c9e45581SDave Wysochanski 1496c9e45581SDave Wysochanski spin_lock_irqsave(&m->lock, flags); 1497c9e45581SDave Wysochanski 149891e968aaSMike Snitzer if (atomic_read(&m->pg_init_count) <= m->pg_init_retries && 149991e968aaSMike Snitzer !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 1500518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 1501c9e45581SDave Wysochanski else 1502be7d31ccSMike Snitzer limit_reached = true; 1503c9e45581SDave Wysochanski 1504c9e45581SDave Wysochanski spin_unlock_irqrestore(&m->lock, flags); 1505c9e45581SDave Wysochanski 1506c9e45581SDave Wysochanski return limit_reached; 1507c9e45581SDave Wysochanski } 1508c9e45581SDave Wysochanski 15093ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors) 1510cfae5c9bSChandra Seetharaman { 151183c0d5d5SMoger, Babu struct pgpath *pgpath = data; 1512cfae5c9bSChandra Seetharaman struct priority_group *pg = pgpath->pg; 1513cfae5c9bSChandra Seetharaman struct multipath *m = pg->m; 1514cfae5c9bSChandra Seetharaman unsigned long flags; 1515be7d31ccSMike Snitzer bool delay_retry = false; 1516cfae5c9bSChandra Seetharaman 1517cfae5c9bSChandra Seetharaman /* device or driver problems */ 1518cfae5c9bSChandra Seetharaman switch (errors) { 1519cfae5c9bSChandra Seetharaman case SCSI_DH_OK: 1520cfae5c9bSChandra Seetharaman break; 1521cfae5c9bSChandra Seetharaman case SCSI_DH_NOSYS: 1522cfae5c9bSChandra Seetharaman if (!m->hw_handler_name) { 1523cfae5c9bSChandra Seetharaman errors = 0; 1524cfae5c9bSChandra Seetharaman break; 1525cfae5c9bSChandra Seetharaman } 1526f7b934c8SMoger, Babu DMERR("Could not failover the device: Handler scsi_dh_%s " 1527f7b934c8SMoger, Babu "Error %d.", m->hw_handler_name, errors); 1528cfae5c9bSChandra Seetharaman /* 1529cfae5c9bSChandra Seetharaman * Fail path for now, so we do not ping pong 1530cfae5c9bSChandra Seetharaman */ 1531cfae5c9bSChandra Seetharaman fail_path(pgpath); 1532cfae5c9bSChandra Seetharaman break; 1533cfae5c9bSChandra Seetharaman case SCSI_DH_DEV_TEMP_BUSY: 1534cfae5c9bSChandra Seetharaman /* 1535cfae5c9bSChandra Seetharaman * Probably doing something like FW upgrade on the 1536cfae5c9bSChandra Seetharaman * controller so try the other pg. 1537cfae5c9bSChandra Seetharaman */ 1538be7d31ccSMike Snitzer bypass_pg(m, pg, true); 1539cfae5c9bSChandra Seetharaman break; 1540cfae5c9bSChandra Seetharaman case SCSI_DH_RETRY: 15414e2d19e4SChandra Seetharaman /* Wait before retrying. */ 15424ecc5081Szhengbin delay_retry = true; 15437b06e09aSBart Van Assche /* fall through */ 1544cfae5c9bSChandra Seetharaman case SCSI_DH_IMM_RETRY: 1545cfae5c9bSChandra Seetharaman case SCSI_DH_RES_TEMP_UNAVAIL: 1546cfae5c9bSChandra Seetharaman if (pg_init_limit_reached(m, pgpath)) 1547cfae5c9bSChandra Seetharaman fail_path(pgpath); 1548cfae5c9bSChandra Seetharaman errors = 0; 1549cfae5c9bSChandra Seetharaman break; 1550ec31f3f7SMike Snitzer case SCSI_DH_DEV_OFFLINED: 1551cfae5c9bSChandra Seetharaman default: 1552cfae5c9bSChandra Seetharaman /* 1553cfae5c9bSChandra Seetharaman * We probably do not want to fail the path for a device 1554cfae5c9bSChandra Seetharaman * error, but this is what the old dm did. In future 1555cfae5c9bSChandra Seetharaman * patches we can do more advanced handling. 1556cfae5c9bSChandra Seetharaman */ 1557cfae5c9bSChandra Seetharaman fail_path(pgpath); 1558cfae5c9bSChandra Seetharaman } 1559cfae5c9bSChandra Seetharaman 1560cfae5c9bSChandra Seetharaman spin_lock_irqsave(&m->lock, flags); 1561cfae5c9bSChandra Seetharaman if (errors) { 1562e54f77ddSChandra Seetharaman if (pgpath == m->current_pgpath) { 1563cfae5c9bSChandra Seetharaman DMERR("Could not failover device. Error %d.", errors); 1564cfae5c9bSChandra Seetharaman m->current_pgpath = NULL; 1565cfae5c9bSChandra Seetharaman m->current_pg = NULL; 1566e54f77ddSChandra Seetharaman } 1567518257b1SMike Snitzer } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 1568be7d31ccSMike Snitzer pg->bypassed = false; 1569cfae5c9bSChandra Seetharaman 157091e968aaSMike Snitzer if (atomic_dec_return(&m->pg_init_in_progress) > 0) 1571d0259bf0SKiyoshi Ueda /* Activations of other paths are still on going */ 1572d0259bf0SKiyoshi Ueda goto out; 1573d0259bf0SKiyoshi Ueda 1574518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 1575518257b1SMike Snitzer if (delay_retry) 1576518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1577518257b1SMike Snitzer else 1578518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1579518257b1SMike Snitzer 15803e9f1be1SHannes Reinecke if (__pg_init_all_paths(m)) 15813e9f1be1SHannes Reinecke goto out; 15823e9f1be1SHannes Reinecke } 1583518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 1584d0259bf0SKiyoshi Ueda 15857e48c768SMike Snitzer process_queued_io_list(m); 158676e33fe4SMike Snitzer 15872bded7bdSKiyoshi Ueda /* 15882bded7bdSKiyoshi Ueda * Wake up any thread waiting to suspend. 15892bded7bdSKiyoshi Ueda */ 15902bded7bdSKiyoshi Ueda wake_up(&m->pg_init_wait); 15912bded7bdSKiyoshi Ueda 1592d0259bf0SKiyoshi Ueda out: 1593cfae5c9bSChandra Seetharaman spin_unlock_irqrestore(&m->lock, flags); 1594cfae5c9bSChandra Seetharaman } 1595cfae5c9bSChandra Seetharaman 159689bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath) 1597bab7cfc7SChandra Seetharaman { 1598f10e06b7SMike Snitzer struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1599bab7cfc7SChandra Seetharaman 1600f10e06b7SMike Snitzer if (pgpath->is_active && !blk_queue_dying(q)) 1601f10e06b7SMike Snitzer scsi_dh_activate(q, pg_init_done, pgpath); 16023a017509SHannes Reinecke else 16033a017509SHannes Reinecke pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED); 1604bab7cfc7SChandra Seetharaman } 1605bab7cfc7SChandra Seetharaman 160689bfce76SBart Van Assche static void activate_path_work(struct work_struct *work) 160789bfce76SBart Van Assche { 160889bfce76SBart Van Assche struct pgpath *pgpath = 160989bfce76SBart Van Assche container_of(work, struct pgpath, activate_path.work); 161089bfce76SBart Van Assche 161189bfce76SBart Van Assche activate_or_offline_path(pgpath); 161289bfce76SBart Van Assche } 161389bfce76SBart Van Assche 1614b79f10eeSChristoph Hellwig static int multipath_end_io(struct dm_target *ti, struct request *clone, 16152a842acaSChristoph Hellwig blk_status_t error, union map_info *map_context) 16161da177e4SLinus Torvalds { 1617b79f10eeSChristoph Hellwig struct dm_mpath_io *mpio = get_mpio(map_context); 1618b79f10eeSChristoph Hellwig struct pgpath *pgpath = mpio->pgpath; 16197ed8578aSChristoph Hellwig int r = DM_ENDIO_DONE; 1620b79f10eeSChristoph Hellwig 1621f40c67f0SKiyoshi Ueda /* 1622f40c67f0SKiyoshi Ueda * We don't queue any clone request inside the multipath target 1623f40c67f0SKiyoshi Ueda * during end I/O handling, since those clone requests don't have 1624f40c67f0SKiyoshi Ueda * bio clones. If we queue them inside the multipath target, 1625f40c67f0SKiyoshi Ueda * we need to make bio clones, that requires memory allocation. 16264cc96131SMike Snitzer * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests 1627f40c67f0SKiyoshi Ueda * don't have bio clones.) 1628f40c67f0SKiyoshi Ueda * Instead of queueing the clone request here, we queue the original 1629f40c67f0SKiyoshi Ueda * request into dm core, which will remake a clone request and 1630f40c67f0SKiyoshi Ueda * clone bios for it and resubmit it later. 1631f40c67f0SKiyoshi Ueda */ 1632a1275677SKeith Busch if (error && blk_path_error(error)) { 1633b79f10eeSChristoph Hellwig struct multipath *m = ti->private; 16341da177e4SLinus Torvalds 1635ac514ffcSMike Snitzer if (error == BLK_STS_RESOURCE) 1636ac514ffcSMike Snitzer r = DM_ENDIO_DELAY_REQUEUE; 1637ac514ffcSMike Snitzer else 16387ed8578aSChristoph Hellwig r = DM_ENDIO_REQUEUE; 16391da177e4SLinus Torvalds 1640b79f10eeSChristoph Hellwig if (pgpath) 1641b79f10eeSChristoph Hellwig fail_path(pgpath); 16421da177e4SLinus Torvalds 164373265f3fSMike Snitzer if (!atomic_read(&m->nr_valid_paths) && 164473265f3fSMike Snitzer !must_push_back_rq(m)) { 16452a842acaSChristoph Hellwig if (error == BLK_STS_IOERR) 164618a482f5SChristoph Hellwig dm_report_EIO(m); 16477ed8578aSChristoph Hellwig /* complete with the original error */ 16487ed8578aSChristoph Hellwig r = DM_ENDIO_DONE; 16497ed8578aSChristoph Hellwig } 16501da177e4SLinus Torvalds } 16511da177e4SLinus Torvalds 16521da177e4SLinus Torvalds if (pgpath) { 1653b79f10eeSChristoph Hellwig struct path_selector *ps = &pgpath->pg->ps; 1654b79f10eeSChristoph Hellwig 16551da177e4SLinus Torvalds if (ps->type->end_io) 1656087615bfSGabriel Krisman Bertazi ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes, 1657087615bfSGabriel Krisman Bertazi clone->io_start_time_ns); 16581da177e4SLinus Torvalds } 16591da177e4SLinus Torvalds 16607ed8578aSChristoph Hellwig return r; 16611da177e4SLinus Torvalds } 16621da177e4SLinus Torvalds 16634e4cbee9SChristoph Hellwig static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, 16644e4cbee9SChristoph Hellwig blk_status_t *error) 166576e33fe4SMike Snitzer { 166614ef1e48SChristoph Hellwig struct multipath *m = ti->private; 166714ef1e48SChristoph Hellwig struct dm_mpath_io *mpio = get_mpio_from_bio(clone); 166814ef1e48SChristoph Hellwig struct pgpath *pgpath = mpio->pgpath; 166976e33fe4SMike Snitzer unsigned long flags; 16701be56909SChristoph Hellwig int r = DM_ENDIO_DONE; 167176e33fe4SMike Snitzer 1672a1275677SKeith Busch if (!*error || !blk_path_error(*error)) 167314ef1e48SChristoph Hellwig goto done; 167476e33fe4SMike Snitzer 167514ef1e48SChristoph Hellwig if (pgpath) 167614ef1e48SChristoph Hellwig fail_path(pgpath); 167776e33fe4SMike Snitzer 1678a271a89cSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 1679a271a89cSMike Snitzer spin_lock_irqsave(&m->lock, flags); 1680a271a89cSMike Snitzer if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 1681a862e4e2SMike Snitzer if (__must_push_back(m)) { 1682c1fd0abeSMike Snitzer r = DM_ENDIO_REQUEUE; 1683c1fd0abeSMike Snitzer } else { 168418a482f5SChristoph Hellwig dm_report_EIO(m); 16854e4cbee9SChristoph Hellwig *error = BLK_STS_IOERR; 1686c1fd0abeSMike Snitzer } 1687a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 168814ef1e48SChristoph Hellwig goto done; 168918a482f5SChristoph Hellwig } 1690a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 1691a271a89cSMike Snitzer } 169276e33fe4SMike Snitzer 1693f45f1186SMike Snitzer multipath_queue_bio(m, clone); 16941be56909SChristoph Hellwig r = DM_ENDIO_INCOMPLETE; 169514ef1e48SChristoph Hellwig done: 169676e33fe4SMike Snitzer if (pgpath) { 169714ef1e48SChristoph Hellwig struct path_selector *ps = &pgpath->pg->ps; 169814ef1e48SChristoph Hellwig 169976e33fe4SMike Snitzer if (ps->type->end_io) 1700087615bfSGabriel Krisman Bertazi ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes, 1701087615bfSGabriel Krisman Bertazi dm_start_time_ns_from_clone(clone)); 170276e33fe4SMike Snitzer } 170376e33fe4SMike Snitzer 17041be56909SChristoph Hellwig return r; 170576e33fe4SMike Snitzer } 170676e33fe4SMike Snitzer 17071da177e4SLinus Torvalds /* 1708553ec94cSMike Snitzer * Suspend with flush can't complete until all the I/O is processed 1709553ec94cSMike Snitzer * so if the last path fails we must error any remaining I/O. 1710553ec94cSMike Snitzer * - Note that if the freeze_bdev fails while suspending, the 1711436d4108SAlasdair G Kergon * queue_if_no_path state is lost - userspace should reset it. 1712553ec94cSMike Snitzer * Otherwise, during noflush suspend, queue_if_no_path will not change. 17131da177e4SLinus Torvalds */ 17141da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti) 17151da177e4SLinus Torvalds { 17167943bd6dSMike Snitzer struct multipath *m = ti->private; 17171da177e4SLinus Torvalds 1718553ec94cSMike Snitzer /* FIXME: bio-based shouldn't need to always disable queue_if_no_path */ 1719553ec94cSMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED || !dm_noflush_suspending(m->ti)) 17204c3f4838SMike Snitzer queue_if_no_path(m, false, true, __func__); 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds 17236df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti) 17246df400abSKiyoshi Ueda { 17256380f26fSMike Anderson struct multipath *m = ti->private; 17266380f26fSMike Anderson 17276380f26fSMike Anderson mutex_lock(&m->work_mutex); 17282bded7bdSKiyoshi Ueda flush_multipath_work(m); 17296380f26fSMike Anderson mutex_unlock(&m->work_mutex); 17306df400abSKiyoshi Ueda } 17316df400abSKiyoshi Ueda 1732436d4108SAlasdair G Kergon /* 1733436d4108SAlasdair G Kergon * Restore the queue_if_no_path setting. 1734436d4108SAlasdair G Kergon */ 17351da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti) 17361da177e4SLinus Torvalds { 17377943bd6dSMike Snitzer struct multipath *m = ti->private; 17381814f2e3SMike Snitzer unsigned long flags; 17391da177e4SLinus Torvalds 17401814f2e3SMike Snitzer spin_lock_irqsave(&m->lock, flags); 1741553ec94cSMike Snitzer if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) { 1742553ec94cSMike Snitzer set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 1743553ec94cSMike Snitzer clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 1744553ec94cSMike Snitzer } 17454c3f4838SMike Snitzer 17464c3f4838SMike Snitzer DMDEBUG("%s: %s finished; QIFNP = %d; SQIFNP = %d", 17474c3f4838SMike Snitzer dm_device_name(dm_table_get_md(m->ti->table)), __func__, 17484c3f4838SMike Snitzer test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags), 17494c3f4838SMike Snitzer test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)); 17504c3f4838SMike Snitzer 17511814f2e3SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 17521da177e4SLinus Torvalds } 17531da177e4SLinus Torvalds 17541da177e4SLinus Torvalds /* 17551da177e4SLinus Torvalds * Info output has the following format: 17561da177e4SLinus Torvalds * num_multipath_feature_args [multipath_feature_args]* 17571da177e4SLinus Torvalds * num_handler_status_args [handler_status_args]* 17581da177e4SLinus Torvalds * num_groups init_group_number 17591da177e4SLinus Torvalds * [A|D|E num_ps_status_args [ps_status_args]* 17601da177e4SLinus Torvalds * num_paths num_selector_args 17611da177e4SLinus Torvalds * [path_dev A|F fail_count [selector_args]* ]+ ]+ 17621da177e4SLinus Torvalds * 17631da177e4SLinus Torvalds * Table output has the following format (identical to the constructor string): 17641da177e4SLinus Torvalds * num_feature_args [features_args]* 17651da177e4SLinus Torvalds * num_handler_args hw_handler [hw_handler_args]* 17661da177e4SLinus Torvalds * num_groups init_group_number 17671da177e4SLinus Torvalds * [priority selector-name num_ps_args [ps_args]* 17681da177e4SLinus Torvalds * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ 17691da177e4SLinus Torvalds */ 1770fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type, 17711f4e0ff0SAlasdair G Kergon unsigned status_flags, char *result, unsigned maxlen) 17721da177e4SLinus Torvalds { 17731da177e4SLinus Torvalds int sz = 0; 17741da177e4SLinus Torvalds unsigned long flags; 17757943bd6dSMike Snitzer struct multipath *m = ti->private; 17761da177e4SLinus Torvalds struct priority_group *pg; 17771da177e4SLinus Torvalds struct pgpath *p; 17781da177e4SLinus Torvalds unsigned pg_num; 17791da177e4SLinus Torvalds char state; 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 17821da177e4SLinus Torvalds 17831da177e4SLinus Torvalds /* Features */ 17841da177e4SLinus Torvalds if (type == STATUSTYPE_INFO) 178591e968aaSMike Snitzer DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags), 178691e968aaSMike Snitzer atomic_read(&m->pg_init_count)); 1787c9e45581SDave Wysochanski else { 1788518257b1SMike Snitzer DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) + 17894e2d19e4SChandra Seetharaman (m->pg_init_retries > 0) * 2 + 1790a58a935dSMike Snitzer (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 + 1791e83068a5SMike Snitzer test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) + 1792e83068a5SMike Snitzer (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2); 1793e83068a5SMike Snitzer 1794518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 1795c9e45581SDave Wysochanski DMEMIT("queue_if_no_path "); 1796c9e45581SDave Wysochanski if (m->pg_init_retries) 1797c9e45581SDave Wysochanski DMEMIT("pg_init_retries %u ", m->pg_init_retries); 17984e2d19e4SChandra Seetharaman if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) 17994e2d19e4SChandra Seetharaman DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs); 1800518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) 1801a58a935dSMike Snitzer DMEMIT("retain_attached_hw_handler "); 1802e83068a5SMike Snitzer if (m->queue_mode != DM_TYPE_REQUEST_BASED) { 1803e83068a5SMike Snitzer switch(m->queue_mode) { 1804e83068a5SMike Snitzer case DM_TYPE_BIO_BASED: 1805e83068a5SMike Snitzer DMEMIT("queue_mode bio "); 1806e83068a5SMike Snitzer break; 18077e0d574fSBart Van Assche default: 18087e0d574fSBart Van Assche WARN_ON_ONCE(true); 18097e0d574fSBart Van Assche break; 1810e83068a5SMike Snitzer } 1811e83068a5SMike Snitzer } 1812c9e45581SDave Wysochanski } 18131da177e4SLinus Torvalds 1814cfae5c9bSChandra Seetharaman if (!m->hw_handler_name || type == STATUSTYPE_INFO) 18151da177e4SLinus Torvalds DMEMIT("0 "); 18161da177e4SLinus Torvalds else 1817cfae5c9bSChandra Seetharaman DMEMIT("1 %s ", m->hw_handler_name); 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds DMEMIT("%u ", m->nr_priority_groups); 18201da177e4SLinus Torvalds 18211da177e4SLinus Torvalds if (m->next_pg) 18221da177e4SLinus Torvalds pg_num = m->next_pg->pg_num; 18231da177e4SLinus Torvalds else if (m->current_pg) 18241da177e4SLinus Torvalds pg_num = m->current_pg->pg_num; 18251da177e4SLinus Torvalds else 1826a490a07aSMike Snitzer pg_num = (m->nr_priority_groups ? 1 : 0); 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds DMEMIT("%u ", pg_num); 18291da177e4SLinus Torvalds 18301da177e4SLinus Torvalds switch (type) { 18311da177e4SLinus Torvalds case STATUSTYPE_INFO: 18321da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 18331da177e4SLinus Torvalds if (pg->bypassed) 18341da177e4SLinus Torvalds state = 'D'; /* Disabled */ 18351da177e4SLinus Torvalds else if (pg == m->current_pg) 18361da177e4SLinus Torvalds state = 'A'; /* Currently Active */ 18371da177e4SLinus Torvalds else 18381da177e4SLinus Torvalds state = 'E'; /* Enabled */ 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds DMEMIT("%c ", state); 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds if (pg->ps.type->status) 18431da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 18441da177e4SLinus Torvalds result + sz, 18451da177e4SLinus Torvalds maxlen - sz); 18461da177e4SLinus Torvalds else 18471da177e4SLinus Torvalds DMEMIT("0 "); 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 18501da177e4SLinus Torvalds pg->ps.type->info_args); 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 18531da177e4SLinus Torvalds DMEMIT("%s %s %u ", p->path.dev->name, 18546680073dSKiyoshi Ueda p->is_active ? "A" : "F", 18551da177e4SLinus Torvalds p->fail_count); 18561da177e4SLinus Torvalds if (pg->ps.type->status) 18571da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 18581da177e4SLinus Torvalds &p->path, type, result + sz, 18591da177e4SLinus Torvalds maxlen - sz); 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds } 18621da177e4SLinus Torvalds break; 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds case STATUSTYPE_TABLE: 18651da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 18661da177e4SLinus Torvalds DMEMIT("%s ", pg->ps.type->name); 18671da177e4SLinus Torvalds 18681da177e4SLinus Torvalds if (pg->ps.type->status) 18691da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 18701da177e4SLinus Torvalds result + sz, 18711da177e4SLinus Torvalds maxlen - sz); 18721da177e4SLinus Torvalds else 18731da177e4SLinus Torvalds DMEMIT("0 "); 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 18761da177e4SLinus Torvalds pg->ps.type->table_args); 18771da177e4SLinus Torvalds 18781da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 18791da177e4SLinus Torvalds DMEMIT("%s ", p->path.dev->name); 18801da177e4SLinus Torvalds if (pg->ps.type->status) 18811da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 18821da177e4SLinus Torvalds &p->path, type, result + sz, 18831da177e4SLinus Torvalds maxlen - sz); 18841da177e4SLinus Torvalds } 18851da177e4SLinus Torvalds } 18861da177e4SLinus Torvalds break; 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds 18921eb5fa84SMike Snitzer static int multipath_message(struct dm_target *ti, unsigned argc, char **argv, 18931eb5fa84SMike Snitzer char *result, unsigned maxlen) 18941da177e4SLinus Torvalds { 18956380f26fSMike Anderson int r = -EINVAL; 18961da177e4SLinus Torvalds struct dm_dev *dev; 18977943bd6dSMike Snitzer struct multipath *m = ti->private; 18981da177e4SLinus Torvalds action_fn action; 1899be240ff5SAnatol Pomazau unsigned long flags; 19001da177e4SLinus Torvalds 19016380f26fSMike Anderson mutex_lock(&m->work_mutex); 19026380f26fSMike Anderson 1903c2f3d24bSKiyoshi Ueda if (dm_suspended(ti)) { 1904c2f3d24bSKiyoshi Ueda r = -EBUSY; 1905c2f3d24bSKiyoshi Ueda goto out; 1906c2f3d24bSKiyoshi Ueda } 1907c2f3d24bSKiyoshi Ueda 19081da177e4SLinus Torvalds if (argc == 1) { 1909498f0103SMike Snitzer if (!strcasecmp(argv[0], "queue_if_no_path")) { 19104c3f4838SMike Snitzer r = queue_if_no_path(m, true, false, __func__); 1911be240ff5SAnatol Pomazau spin_lock_irqsave(&m->lock, flags); 1912be240ff5SAnatol Pomazau enable_nopath_timeout(m); 1913be240ff5SAnatol Pomazau spin_unlock_irqrestore(&m->lock, flags); 19146380f26fSMike Anderson goto out; 1915498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "fail_if_no_path")) { 19164c3f4838SMike Snitzer r = queue_if_no_path(m, false, false, __func__); 1917be240ff5SAnatol Pomazau disable_nopath_timeout(m); 19186380f26fSMike Anderson goto out; 19196380f26fSMike Anderson } 19201da177e4SLinus Torvalds } 19211da177e4SLinus Torvalds 19226380f26fSMike Anderson if (argc != 2) { 1923a356e426SJose Castillo DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc); 19246380f26fSMike Anderson goto out; 19256380f26fSMike Anderson } 19261da177e4SLinus Torvalds 1927498f0103SMike Snitzer if (!strcasecmp(argv[0], "disable_group")) { 1928be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], true); 19296380f26fSMike Anderson goto out; 1930498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "enable_group")) { 1931be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], false); 19326380f26fSMike Anderson goto out; 1933498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "switch_group")) { 19346380f26fSMike Anderson r = switch_pg_num(m, argv[1]); 19356380f26fSMike Anderson goto out; 1936498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "reinstate_path")) 19371da177e4SLinus Torvalds action = reinstate_path; 1938498f0103SMike Snitzer else if (!strcasecmp(argv[0], "fail_path")) 19391da177e4SLinus Torvalds action = fail_path; 19406380f26fSMike Anderson else { 1941a356e426SJose Castillo DMWARN("Unrecognised multipath message received: %s", argv[0]); 19426380f26fSMike Anderson goto out; 19436380f26fSMike Anderson } 19441da177e4SLinus Torvalds 19458215d6ecSNikanth Karthikesan r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev); 19461da177e4SLinus Torvalds if (r) { 194772d94861SAlasdair G Kergon DMWARN("message: error getting device %s", 19481da177e4SLinus Torvalds argv[1]); 19496380f26fSMike Anderson goto out; 19501da177e4SLinus Torvalds } 19511da177e4SLinus Torvalds 19521da177e4SLinus Torvalds r = action_dev(m, dev, action); 19531da177e4SLinus Torvalds 19541da177e4SLinus Torvalds dm_put_device(ti, dev); 19551da177e4SLinus Torvalds 19566380f26fSMike Anderson out: 19576380f26fSMike Anderson mutex_unlock(&m->work_mutex); 19581da177e4SLinus Torvalds return r; 19591da177e4SLinus Torvalds } 19601da177e4SLinus Torvalds 1961e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti, 19625bd5e8d8SMike Snitzer struct block_device **bdev) 19639af4aa30SMilan Broz { 196435991652SMikulas Patocka struct multipath *m = ti->private; 1965*564dbb13SMike Snitzer struct pgpath *pgpath; 196669cea0d4SMike Snitzer unsigned long flags; 196735991652SMikulas Patocka int r; 196835991652SMikulas Patocka 1969*564dbb13SMike Snitzer pgpath = READ_ONCE(m->current_pgpath); 1970*564dbb13SMike Snitzer if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags)) 1971*564dbb13SMike Snitzer pgpath = choose_pgpath(m, 0); 19729af4aa30SMilan Broz 1973*564dbb13SMike Snitzer if (pgpath) { 1974518257b1SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) { 1975*564dbb13SMike Snitzer *bdev = pgpath->path.dev->bdev; 197643e43c9eSJunichi Nomura r = 0; 197743e43c9eSJunichi Nomura } else { 197843e43c9eSJunichi Nomura /* pg_init has not started or completed */ 19796c182cd8SHannes Reinecke r = -ENOTCONN; 198043e43c9eSJunichi Nomura } 198143e43c9eSJunichi Nomura } else { 198243e43c9eSJunichi Nomura /* No path is available */ 1983a271a89cSMike Snitzer r = -EIO; 1984a271a89cSMike Snitzer spin_lock_irqsave(&m->lock, flags); 1985518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 198643e43c9eSJunichi Nomura r = -ENOTCONN; 1987a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 198843e43c9eSJunichi Nomura } 19899af4aa30SMilan Broz 19905bbbfdf6SJunichi Nomura if (r == -ENOTCONN) { 1991506458efSWill Deacon if (!READ_ONCE(m->current_pg)) { 19923e9f1be1SHannes Reinecke /* Path status changed, redo selection */ 19932da1610aSMike Snitzer (void) choose_pgpath(m, 0); 19943e9f1be1SHannes Reinecke } 199569cea0d4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 1996518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 199769cea0d4SMike Snitzer (void) __pg_init_all_paths(m); 199869cea0d4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 199963d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 20007e48c768SMike Snitzer process_queued_io_list(m); 20013e9f1be1SHannes Reinecke } 200235991652SMikulas Patocka 2003e56f81e0SChristoph Hellwig /* 2004e56f81e0SChristoph Hellwig * Only pass ioctls through if the device sizes match exactly. 2005e56f81e0SChristoph Hellwig */ 2006e56f81e0SChristoph Hellwig if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT) 2007e56f81e0SChristoph Hellwig return 1; 2008e56f81e0SChristoph Hellwig return r; 20099af4aa30SMilan Broz } 20109af4aa30SMilan Broz 2011af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti, 2012af4874e0SMike Snitzer iterate_devices_callout_fn fn, void *data) 2013af4874e0SMike Snitzer { 2014af4874e0SMike Snitzer struct multipath *m = ti->private; 2015af4874e0SMike Snitzer struct priority_group *pg; 2016af4874e0SMike Snitzer struct pgpath *p; 2017af4874e0SMike Snitzer int ret = 0; 2018af4874e0SMike Snitzer 2019af4874e0SMike Snitzer list_for_each_entry(pg, &m->priority_groups, list) { 2020af4874e0SMike Snitzer list_for_each_entry(p, &pg->pgpaths, list) { 20215dea271bSMike Snitzer ret = fn(ti, p->path.dev, ti->begin, ti->len, data); 2022af4874e0SMike Snitzer if (ret) 2023af4874e0SMike Snitzer goto out; 2024af4874e0SMike Snitzer } 2025af4874e0SMike Snitzer } 2026af4874e0SMike Snitzer 2027af4874e0SMike Snitzer out: 2028af4874e0SMike Snitzer return ret; 2029af4874e0SMike Snitzer } 2030af4874e0SMike Snitzer 20319f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath) 2032f40c67f0SKiyoshi Ueda { 2033f40c67f0SKiyoshi Ueda struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 2034f40c67f0SKiyoshi Ueda 203552b09914SMike Snitzer return blk_lld_busy(q); 2036f40c67f0SKiyoshi Ueda } 2037f40c67f0SKiyoshi Ueda 2038f40c67f0SKiyoshi Ueda /* 2039f40c67f0SKiyoshi Ueda * We return "busy", only when we can map I/Os but underlying devices 2040f40c67f0SKiyoshi Ueda * are busy (so even if we map I/Os now, the I/Os will wait on 2041f40c67f0SKiyoshi Ueda * the underlying queue). 2042f40c67f0SKiyoshi Ueda * In other words, if we want to kill I/Os or queue them inside us 2043f40c67f0SKiyoshi Ueda * due to map unavailability, we don't return "busy". Otherwise, 2044f40c67f0SKiyoshi Ueda * dm core won't give us the I/Os and we can't do what we want. 2045f40c67f0SKiyoshi Ueda */ 2046f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti) 2047f40c67f0SKiyoshi Ueda { 2048be7d31ccSMike Snitzer bool busy = false, has_active = false; 2049f40c67f0SKiyoshi Ueda struct multipath *m = ti->private; 20502da1610aSMike Snitzer struct priority_group *pg, *next_pg; 2051f40c67f0SKiyoshi Ueda struct pgpath *pgpath; 2052f40c67f0SKiyoshi Ueda 2053b88efd43SMike Snitzer /* pg_init in progress */ 2054b88efd43SMike Snitzer if (atomic_read(&m->pg_init_in_progress)) 20552da1610aSMike Snitzer return true; 20562da1610aSMike Snitzer 2057b88efd43SMike Snitzer /* no paths available, for blk-mq: rely on IO mapping to delay requeue */ 2058a271a89cSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 2059a271a89cSMike Snitzer unsigned long flags; 2060a271a89cSMike Snitzer spin_lock_irqsave(&m->lock, flags); 2061a271a89cSMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 2062a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 2063953923c0SMike Snitzer return (m->queue_mode != DM_TYPE_REQUEST_BASED); 2064a271a89cSMike Snitzer } 2065a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 2066a271a89cSMike Snitzer } 2067b88efd43SMike Snitzer 2068f40c67f0SKiyoshi Ueda /* Guess which priority_group will be used at next mapping time */ 2069506458efSWill Deacon pg = READ_ONCE(m->current_pg); 2070506458efSWill Deacon next_pg = READ_ONCE(m->next_pg); 2071506458efSWill Deacon if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg)) 20722da1610aSMike Snitzer pg = next_pg; 20732da1610aSMike Snitzer 20742da1610aSMike Snitzer if (!pg) { 2075f40c67f0SKiyoshi Ueda /* 2076f40c67f0SKiyoshi Ueda * We don't know which pg will be used at next mapping time. 20772da1610aSMike Snitzer * We don't call choose_pgpath() here to avoid to trigger 2078f40c67f0SKiyoshi Ueda * pg_init just by busy checking. 2079f40c67f0SKiyoshi Ueda * So we don't know whether underlying devices we will be using 2080f40c67f0SKiyoshi Ueda * at next mapping time are busy or not. Just try mapping. 2081f40c67f0SKiyoshi Ueda */ 20822da1610aSMike Snitzer return busy; 20832da1610aSMike Snitzer } 2084f40c67f0SKiyoshi Ueda 2085f40c67f0SKiyoshi Ueda /* 2086f40c67f0SKiyoshi Ueda * If there is one non-busy active path at least, the path selector 2087f40c67f0SKiyoshi Ueda * will be able to select it. So we consider such a pg as not busy. 2088f40c67f0SKiyoshi Ueda */ 2089be7d31ccSMike Snitzer busy = true; 20902da1610aSMike Snitzer list_for_each_entry(pgpath, &pg->pgpaths, list) { 2091f40c67f0SKiyoshi Ueda if (pgpath->is_active) { 2092be7d31ccSMike Snitzer has_active = true; 20939f54cec5SMike Snitzer if (!pgpath_busy(pgpath)) { 2094be7d31ccSMike Snitzer busy = false; 2095f40c67f0SKiyoshi Ueda break; 2096f40c67f0SKiyoshi Ueda } 2097f40c67f0SKiyoshi Ueda } 20982da1610aSMike Snitzer } 2099f40c67f0SKiyoshi Ueda 21002da1610aSMike Snitzer if (!has_active) { 2101f40c67f0SKiyoshi Ueda /* 2102f40c67f0SKiyoshi Ueda * No active path in this pg, so this pg won't be used and 2103f40c67f0SKiyoshi Ueda * the current_pg will be changed at next mapping time. 2104f40c67f0SKiyoshi Ueda * We need to try mapping to determine it. 2105f40c67f0SKiyoshi Ueda */ 2106be7d31ccSMike Snitzer busy = false; 21072da1610aSMike Snitzer } 2108f40c67f0SKiyoshi Ueda 2109f40c67f0SKiyoshi Ueda return busy; 2110f40c67f0SKiyoshi Ueda } 2111f40c67f0SKiyoshi Ueda 21121da177e4SLinus Torvalds /*----------------------------------------------------------------- 21131da177e4SLinus Torvalds * Module setup 21141da177e4SLinus Torvalds *---------------------------------------------------------------*/ 21151da177e4SLinus Torvalds static struct target_type multipath_target = { 21161da177e4SLinus Torvalds .name = "multipath", 2117636be424SMike Snitzer .version = {1, 14, 0}, 21188c5c1473SSteffen Maier .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE | 21198c5c1473SSteffen Maier DM_TARGET_PASSES_INTEGRITY, 21201da177e4SLinus Torvalds .module = THIS_MODULE, 21211da177e4SLinus Torvalds .ctr = multipath_ctr, 21221da177e4SLinus Torvalds .dtr = multipath_dtr, 2123e5863d9aSMike Snitzer .clone_and_map_rq = multipath_clone_and_map, 2124e5863d9aSMike Snitzer .release_clone_rq = multipath_release_clone, 2125f40c67f0SKiyoshi Ueda .rq_end_io = multipath_end_io, 212676e33fe4SMike Snitzer .map = multipath_map_bio, 212776e33fe4SMike Snitzer .end_io = multipath_end_io_bio, 212876e33fe4SMike Snitzer .presuspend = multipath_presuspend, 212976e33fe4SMike Snitzer .postsuspend = multipath_postsuspend, 213076e33fe4SMike Snitzer .resume = multipath_resume, 213176e33fe4SMike Snitzer .status = multipath_status, 213276e33fe4SMike Snitzer .message = multipath_message, 213376e33fe4SMike Snitzer .prepare_ioctl = multipath_prepare_ioctl, 213476e33fe4SMike Snitzer .iterate_devices = multipath_iterate_devices, 213576e33fe4SMike Snitzer .busy = multipath_busy, 213676e33fe4SMike Snitzer }; 213776e33fe4SMike Snitzer 21381da177e4SLinus Torvalds static int __init dm_multipath_init(void) 21391da177e4SLinus Torvalds { 21401da177e4SLinus Torvalds int r; 21411da177e4SLinus Torvalds 21424d4d66abSTejun Heo kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0); 2143c557308eSAlasdair G Kergon if (!kmultipathd) { 21440cd33124SAlasdair G Kergon DMERR("failed to create workqueue kmpathd"); 2145ff658e9cSJohannes Thumshirn r = -ENOMEM; 2146ff658e9cSJohannes Thumshirn goto bad_alloc_kmultipathd; 2147c557308eSAlasdair G Kergon } 2148c557308eSAlasdair G Kergon 2149bab7cfc7SChandra Seetharaman /* 2150bab7cfc7SChandra Seetharaman * A separate workqueue is used to handle the device handlers 2151bab7cfc7SChandra Seetharaman * to avoid overloading existing workqueue. Overloading the 2152bab7cfc7SChandra Seetharaman * old workqueue would also create a bottleneck in the 2153bab7cfc7SChandra Seetharaman * path of the storage hardware device activation. 2154bab7cfc7SChandra Seetharaman */ 21554d4d66abSTejun Heo kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd", 21564d4d66abSTejun Heo WQ_MEM_RECLAIM); 2157bab7cfc7SChandra Seetharaman if (!kmpath_handlerd) { 2158bab7cfc7SChandra Seetharaman DMERR("failed to create workqueue kmpath_handlerd"); 2159ff658e9cSJohannes Thumshirn r = -ENOMEM; 2160ff658e9cSJohannes Thumshirn goto bad_alloc_kmpath_handlerd; 2161bab7cfc7SChandra Seetharaman } 2162bab7cfc7SChandra Seetharaman 21637e6358d2Smonty_pavel@sina.com r = dm_register_target(&multipath_target); 21647e6358d2Smonty_pavel@sina.com if (r < 0) { 21657e6358d2Smonty_pavel@sina.com DMERR("request-based register failed %d", r); 21667e6358d2Smonty_pavel@sina.com r = -EINVAL; 21677e6358d2Smonty_pavel@sina.com goto bad_register_target; 21687e6358d2Smonty_pavel@sina.com } 21697e6358d2Smonty_pavel@sina.com 2170ff658e9cSJohannes Thumshirn return 0; 2171ff658e9cSJohannes Thumshirn 21727e6358d2Smonty_pavel@sina.com bad_register_target: 21737e6358d2Smonty_pavel@sina.com destroy_workqueue(kmpath_handlerd); 2174ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd: 2175ff658e9cSJohannes Thumshirn destroy_workqueue(kmultipathd); 2176ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd: 21771da177e4SLinus Torvalds return r; 21781da177e4SLinus Torvalds } 21791da177e4SLinus Torvalds 21801da177e4SLinus Torvalds static void __exit dm_multipath_exit(void) 21811da177e4SLinus Torvalds { 2182bab7cfc7SChandra Seetharaman destroy_workqueue(kmpath_handlerd); 2183c557308eSAlasdair G Kergon destroy_workqueue(kmultipathd); 2184c557308eSAlasdair G Kergon 218510d3bd09SMikulas Patocka dm_unregister_target(&multipath_target); 21861da177e4SLinus Torvalds } 21871da177e4SLinus Torvalds 21881da177e4SLinus Torvalds module_init(dm_multipath_init); 21891da177e4SLinus Torvalds module_exit(dm_multipath_exit); 21901da177e4SLinus Torvalds 2191be240ff5SAnatol Pomazau module_param_named(queue_if_no_path_timeout_secs, 2192be240ff5SAnatol Pomazau queue_if_no_path_timeout_secs, ulong, S_IRUGO | S_IWUSR); 2193be240ff5SAnatol Pomazau MODULE_PARM_DESC(queue_if_no_path_timeout_secs, "No available paths queue IO timeout in seconds"); 2194be240ff5SAnatol Pomazau 21951da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target"); 21961da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); 21971da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2198