11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (C) 2003 Sistina Software Limited. 31da177e4SLinus Torvalds * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This file is released under the GPL. 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 8586e80e6SMikulas Patocka #include <linux/device-mapper.h> 9586e80e6SMikulas Patocka 104cc96131SMike Snitzer #include "dm-rq.h" 1176e33fe4SMike Snitzer #include "dm-bio-record.h" 121da177e4SLinus Torvalds #include "dm-path-selector.h" 13b15546f9SMike Anderson #include "dm-uevent.h" 141da177e4SLinus Torvalds 15e5863d9aSMike Snitzer #include <linux/blkdev.h> 161da177e4SLinus Torvalds #include <linux/ctype.h> 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/mempool.h> 191da177e4SLinus Torvalds #include <linux/module.h> 201da177e4SLinus Torvalds #include <linux/pagemap.h> 211da177e4SLinus Torvalds #include <linux/slab.h> 221da177e4SLinus Torvalds #include <linux/time.h> 231da177e4SLinus Torvalds #include <linux/workqueue.h> 2435991652SMikulas Patocka #include <linux/delay.h> 25cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h> 2660063497SArun Sharma #include <linux/atomic.h> 2778ce23b5SMike Snitzer #include <linux/blk-mq.h> 281da177e4SLinus Torvalds 2972d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath" 304e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000 314e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1) 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds /* Path properties */ 341da177e4SLinus Torvalds struct pgpath { 351da177e4SLinus Torvalds struct list_head list; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds struct priority_group *pg; /* Owning PG */ 381da177e4SLinus Torvalds unsigned fail_count; /* Cumulative failure count */ 391da177e4SLinus Torvalds 40c922d5f7SJosef "Jeff" Sipek struct dm_path path; 414e2d19e4SChandra Seetharaman struct delayed_work activate_path; 42be7d31ccSMike Snitzer 43be7d31ccSMike Snitzer bool is_active:1; /* Path status */ 441da177e4SLinus Torvalds }; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path) 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds /* 491da177e4SLinus Torvalds * Paths are grouped into Priority Groups and numbered from 1 upwards. 501da177e4SLinus Torvalds * Each has a path selector which controls which path gets used. 511da177e4SLinus Torvalds */ 521da177e4SLinus Torvalds struct priority_group { 531da177e4SLinus Torvalds struct list_head list; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds struct multipath *m; /* Owning multipath instance */ 561da177e4SLinus Torvalds struct path_selector ps; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds unsigned pg_num; /* Reference number */ 591da177e4SLinus Torvalds unsigned nr_pgpaths; /* Number of paths in PG */ 601da177e4SLinus Torvalds struct list_head pgpaths; 61be7d31ccSMike Snitzer 62be7d31ccSMike Snitzer bool bypassed:1; /* Temporarily bypass this PG? */ 631da177e4SLinus Torvalds }; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* Multipath context */ 661da177e4SLinus Torvalds struct multipath { 67848b8aefSMike Snitzer unsigned long flags; /* Multipath state flags */ 684e2d19e4SChandra Seetharaman 691fbdd2b3SMike Snitzer spinlock_t lock; 70848b8aefSMike Snitzer enum dm_queue_mode queue_mode; 714e2d19e4SChandra Seetharaman 721da177e4SLinus Torvalds struct pgpath *current_pgpath; 731da177e4SLinus Torvalds struct priority_group *current_pg; 741da177e4SLinus Torvalds struct priority_group *next_pg; /* Switch to this PG if set */ 751da177e4SLinus Torvalds 76848b8aefSMike Snitzer atomic_t nr_valid_paths; /* Total number of usable paths */ 77848b8aefSMike Snitzer unsigned nr_priority_groups; 78848b8aefSMike Snitzer struct list_head priority_groups; 791fbdd2b3SMike Snitzer 80848b8aefSMike Snitzer const char *hw_handler_name; 81848b8aefSMike Snitzer char *hw_handler_params; 82848b8aefSMike Snitzer wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */ 83c9e45581SDave Wysochanski unsigned pg_init_retries; /* Number of times to retry pg_init */ 844e2d19e4SChandra Seetharaman unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */ 8591e968aaSMike Snitzer atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */ 8691e968aaSMike Snitzer atomic_t pg_init_count; /* Number of times pg_init called */ 8791e968aaSMike Snitzer 886380f26fSMike Anderson struct mutex work_mutex; 8920800cb3SMike Snitzer struct work_struct trigger_event; 90848b8aefSMike Snitzer struct dm_target *ti; 9176e33fe4SMike Snitzer 9276e33fe4SMike Snitzer struct work_struct process_queued_bios; 9376e33fe4SMike Snitzer struct bio_list queued_bios; 941da177e4SLinus Torvalds }; 951da177e4SLinus Torvalds 961da177e4SLinus Torvalds /* 9776e33fe4SMike Snitzer * Context information attached to each io we process. 981da177e4SLinus Torvalds */ 99028867acSAlasdair G Kergon struct dm_mpath_io { 1001da177e4SLinus Torvalds struct pgpath *pgpath; 10102ab823fSKiyoshi Ueda size_t nr_bytes; 1021da177e4SLinus Torvalds }; 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath); 1051da177e4SLinus Torvalds 106bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd; 107c4028958SDavid Howells static void trigger_event(struct work_struct *work); 10889bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath); 10989bfce76SBart Van Assche static void activate_path_work(struct work_struct *work); 11076e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work); 1111da177e4SLinus Torvalds 112518257b1SMike Snitzer /*----------------------------------------------- 113518257b1SMike Snitzer * Multipath state flags. 114518257b1SMike Snitzer *-----------------------------------------------*/ 115518257b1SMike Snitzer 116518257b1SMike Snitzer #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */ 117518257b1SMike Snitzer #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */ 118518257b1SMike Snitzer #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */ 119518257b1SMike Snitzer #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */ 120518257b1SMike Snitzer #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */ 121518257b1SMike Snitzer #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */ 122518257b1SMike Snitzer #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */ 1231da177e4SLinus Torvalds 1241da177e4SLinus Torvalds /*----------------------------------------------- 1251da177e4SLinus Torvalds * Allocation routines 1261da177e4SLinus Torvalds *-----------------------------------------------*/ 1271da177e4SLinus Torvalds 1281da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void) 1291da177e4SLinus Torvalds { 130e69fae56SMicha³ Miros³aw struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL); 1311da177e4SLinus Torvalds 132848b8aefSMike Snitzer if (!pgpath) 133848b8aefSMike Snitzer return NULL; 134848b8aefSMike Snitzer 135be7d31ccSMike Snitzer pgpath->is_active = true; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds return pgpath; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 140028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath) 1411da177e4SLinus Torvalds { 1421da177e4SLinus Torvalds kfree(pgpath); 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void) 1461da177e4SLinus Torvalds { 1471da177e4SLinus Torvalds struct priority_group *pg; 1481da177e4SLinus Torvalds 149e69fae56SMicha³ Miros³aw pg = kzalloc(sizeof(*pg), GFP_KERNEL); 1501da177e4SLinus Torvalds 151e69fae56SMicha³ Miros³aw if (pg) 1521da177e4SLinus Torvalds INIT_LIST_HEAD(&pg->pgpaths); 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds return pg; 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti) 1581da177e4SLinus Torvalds { 1591da177e4SLinus Torvalds struct pgpath *pgpath, *tmp; 1601da177e4SLinus Torvalds 1611da177e4SLinus Torvalds list_for_each_entry_safe(pgpath, tmp, pgpaths, list) { 1621da177e4SLinus Torvalds list_del(&pgpath->list); 1631da177e4SLinus Torvalds dm_put_device(ti, pgpath->path.dev); 1641da177e4SLinus Torvalds free_pgpath(pgpath); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg, 1691da177e4SLinus Torvalds struct dm_target *ti) 1701da177e4SLinus Torvalds { 1711da177e4SLinus Torvalds struct path_selector *ps = &pg->ps; 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds if (ps->type) { 1741da177e4SLinus Torvalds ps->type->destroy(ps); 1751da177e4SLinus Torvalds dm_put_path_selector(ps->type); 1761da177e4SLinus Torvalds } 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds free_pgpaths(&pg->pgpaths, ti); 1791da177e4SLinus Torvalds kfree(pg); 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 182e83068a5SMike Snitzer static struct multipath *alloc_multipath(struct dm_target *ti) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds struct multipath *m; 1851da177e4SLinus Torvalds 186e69fae56SMicha³ Miros³aw m = kzalloc(sizeof(*m), GFP_KERNEL); 1871da177e4SLinus Torvalds if (m) { 1881da177e4SLinus Torvalds INIT_LIST_HEAD(&m->priority_groups); 1891da177e4SLinus Torvalds spin_lock_init(&m->lock); 19091e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, 0); 191c4028958SDavid Howells INIT_WORK(&m->trigger_event, trigger_event); 1926380f26fSMike Anderson mutex_init(&m->work_mutex); 1938637a6bfSMike Snitzer 194e83068a5SMike Snitzer m->queue_mode = DM_TYPE_NONE; 195e83068a5SMike Snitzer 196e83068a5SMike Snitzer m->ti = ti; 197e83068a5SMike Snitzer ti->private = m; 198e83068a5SMike Snitzer } 199e83068a5SMike Snitzer 200e83068a5SMike Snitzer return m; 201e83068a5SMike Snitzer } 202e83068a5SMike Snitzer 203e83068a5SMike Snitzer static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m) 204e83068a5SMike Snitzer { 205e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_NONE) { 206e83068a5SMike Snitzer /* 207e83068a5SMike Snitzer * Default to request-based. 208e83068a5SMike Snitzer */ 209e83068a5SMike Snitzer if (dm_use_blk_mq(dm_table_get_md(ti->table))) 210e83068a5SMike Snitzer m->queue_mode = DM_TYPE_MQ_REQUEST_BASED; 211e83068a5SMike Snitzer else 212e83068a5SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 213cd025384SMike Snitzer 2148d47e659SMike Snitzer } else if (m->queue_mode == DM_TYPE_BIO_BASED) { 21576e33fe4SMike Snitzer INIT_WORK(&m->process_queued_bios, process_queued_bios); 21676e33fe4SMike Snitzer /* 21776e33fe4SMike Snitzer * bio-based doesn't support any direct scsi_dh management; 21876e33fe4SMike Snitzer * it just discovers if a scsi_dh is attached. 21976e33fe4SMike Snitzer */ 22076e33fe4SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 22176e33fe4SMike Snitzer } 22276e33fe4SMike Snitzer 223e83068a5SMike Snitzer dm_table_set_type(ti->table, m->queue_mode); 2241da177e4SLinus Torvalds 225c3736674SMike Snitzer /* 226c3736674SMike Snitzer * Init fields that are only used when a scsi_dh is attached 227c3736674SMike Snitzer * - must do this unconditionally (really doesn't hurt non-SCSI uses) 228c3736674SMike Snitzer */ 229c3736674SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 230c3736674SMike Snitzer atomic_set(&m->pg_init_in_progress, 0); 231c3736674SMike Snitzer atomic_set(&m->pg_init_count, 0); 232c3736674SMike Snitzer m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT; 233c3736674SMike Snitzer init_waitqueue_head(&m->pg_init_wait); 234c3736674SMike Snitzer 235e83068a5SMike Snitzer return 0; 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds static void free_multipath(struct multipath *m) 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds struct priority_group *pg, *tmp; 2411da177e4SLinus Torvalds 2421da177e4SLinus Torvalds list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { 2431da177e4SLinus Torvalds list_del(&pg->list); 2441da177e4SLinus Torvalds free_priority_group(pg, m->ti); 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds 247cfae5c9bSChandra Seetharaman kfree(m->hw_handler_name); 2482bfd2e13SChandra Seetharaman kfree(m->hw_handler_params); 249d5ffebddSMike Snitzer mutex_destroy(&m->work_mutex); 2501da177e4SLinus Torvalds kfree(m); 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds 2532eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info) 2542eff1924SMike Snitzer { 2552eff1924SMike Snitzer return info->ptr; 2562eff1924SMike Snitzer } 2572eff1924SMike Snitzer 258bf661be1SMike Snitzer static size_t multipath_per_bio_data_size(void) 25976e33fe4SMike Snitzer { 260bf661be1SMike Snitzer return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details); 26176e33fe4SMike Snitzer } 26276e33fe4SMike Snitzer 263bf661be1SMike Snitzer static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio) 264bf661be1SMike Snitzer { 265bf661be1SMike Snitzer return dm_per_bio_data(bio, multipath_per_bio_data_size()); 266bf661be1SMike Snitzer } 267bf661be1SMike Snitzer 268d07a241dSMike Snitzer static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio) 269bf661be1SMike Snitzer { 270bf661be1SMike Snitzer /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */ 271bf661be1SMike Snitzer void *bio_details = mpio + 1; 272bf661be1SMike Snitzer return bio_details; 273bf661be1SMike Snitzer } 274bf661be1SMike Snitzer 27563f6e6fdSMike Snitzer static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p) 27676e33fe4SMike Snitzer { 27776e33fe4SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 278d07a241dSMike Snitzer struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio); 27976e33fe4SMike Snitzer 280d0442f80SMike Snitzer mpio->nr_bytes = bio->bi_iter.bi_size; 281d0442f80SMike Snitzer mpio->pgpath = NULL; 282bf661be1SMike Snitzer *mpio_p = mpio; 283d0442f80SMike Snitzer 284d0442f80SMike Snitzer dm_bio_record(bio_details, bio); 28576e33fe4SMike Snitzer } 28676e33fe4SMike Snitzer 2871da177e4SLinus Torvalds /*----------------------------------------------- 2881da177e4SLinus Torvalds * Path selection 2891da177e4SLinus Torvalds *-----------------------------------------------*/ 2901da177e4SLinus Torvalds 2913e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m) 292fb612642SKiyoshi Ueda { 293fb612642SKiyoshi Ueda struct pgpath *pgpath; 2944e2d19e4SChandra Seetharaman unsigned long pg_init_delay = 0; 295fb612642SKiyoshi Ueda 296b194679fSBart Van Assche lockdep_assert_held(&m->lock); 297b194679fSBart Van Assche 29891e968aaSMike Snitzer if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 2993e9f1be1SHannes Reinecke return 0; 30017f4ff45SHannes Reinecke 30191e968aaSMike Snitzer atomic_inc(&m->pg_init_count); 302518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 3033e9f1be1SHannes Reinecke 3043e9f1be1SHannes Reinecke /* Check here to reset pg_init_required */ 3053e9f1be1SHannes Reinecke if (!m->current_pg) 3063e9f1be1SHannes Reinecke return 0; 3073e9f1be1SHannes Reinecke 308518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags)) 3094e2d19e4SChandra Seetharaman pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ? 3104e2d19e4SChandra Seetharaman m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS); 311fb612642SKiyoshi Ueda list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) { 312fb612642SKiyoshi Ueda /* Skip failed paths */ 313fb612642SKiyoshi Ueda if (!pgpath->is_active) 314fb612642SKiyoshi Ueda continue; 3154e2d19e4SChandra Seetharaman if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path, 3164e2d19e4SChandra Seetharaman pg_init_delay)) 31791e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 318fb612642SKiyoshi Ueda } 31991e968aaSMike Snitzer return atomic_read(&m->pg_init_in_progress); 320fb612642SKiyoshi Ueda } 321fb612642SKiyoshi Ueda 322c1d7ecf7SBart Van Assche static int pg_init_all_paths(struct multipath *m) 3231da177e4SLinus Torvalds { 324c1d7ecf7SBart Van Assche int ret; 3252da1610aSMike Snitzer unsigned long flags; 3262da1610aSMike Snitzer 3272da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 328c1d7ecf7SBart Van Assche ret = __pg_init_all_paths(m); 3292da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 330c1d7ecf7SBart Van Assche 331c1d7ecf7SBart Van Assche return ret; 3322da1610aSMike Snitzer } 3332da1610aSMike Snitzer 3342da1610aSMike Snitzer static void __switch_pg(struct multipath *m, struct priority_group *pg) 3352da1610aSMike Snitzer { 3362da1610aSMike Snitzer m->current_pg = pg; 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds /* Must we initialise the PG first, and queue I/O till it's ready? */ 339cfae5c9bSChandra Seetharaman if (m->hw_handler_name) { 340518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 341518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 3421da177e4SLinus Torvalds } else { 343518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 344518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 3451da177e4SLinus Torvalds } 346c9e45581SDave Wysochanski 34791e968aaSMike Snitzer atomic_set(&m->pg_init_count, 0); 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds 3502da1610aSMike Snitzer static struct pgpath *choose_path_in_pg(struct multipath *m, 3512da1610aSMike Snitzer struct priority_group *pg, 35202ab823fSKiyoshi Ueda size_t nr_bytes) 3531da177e4SLinus Torvalds { 3542da1610aSMike Snitzer unsigned long flags; 355c922d5f7SJosef "Jeff" Sipek struct dm_path *path; 3562da1610aSMike Snitzer struct pgpath *pgpath; 3571da177e4SLinus Torvalds 35890a4323cSMike Snitzer path = pg->ps.type->select_path(&pg->ps, nr_bytes); 3591da177e4SLinus Torvalds if (!path) 3602da1610aSMike Snitzer return ERR_PTR(-ENXIO); 3611da177e4SLinus Torvalds 3622da1610aSMike Snitzer pgpath = path_to_pgpath(path); 3631da177e4SLinus Torvalds 364506458efSWill Deacon if (unlikely(READ_ONCE(m->current_pg) != pg)) { 3652da1610aSMike Snitzer /* Only update current_pgpath if pg changed */ 3662da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 3672da1610aSMike Snitzer m->current_pgpath = pgpath; 3682da1610aSMike Snitzer __switch_pg(m, pg); 3692da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3701da177e4SLinus Torvalds } 3711da177e4SLinus Torvalds 3722da1610aSMike Snitzer return pgpath; 3732da1610aSMike Snitzer } 3742da1610aSMike Snitzer 3752da1610aSMike Snitzer static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes) 3761da177e4SLinus Torvalds { 3772da1610aSMike Snitzer unsigned long flags; 3781da177e4SLinus Torvalds struct priority_group *pg; 3792da1610aSMike Snitzer struct pgpath *pgpath; 380d19a55ccSMike Snitzer unsigned bypassed = 1; 3811da177e4SLinus Torvalds 38291e968aaSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 383518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 3841da177e4SLinus Torvalds goto failed; 3851f271972SBenjamin Marzinski } 3861da177e4SLinus Torvalds 3871da177e4SLinus Torvalds /* Were we instructed to switch PG? */ 388506458efSWill Deacon if (READ_ONCE(m->next_pg)) { 3892da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 3901da177e4SLinus Torvalds pg = m->next_pg; 3912da1610aSMike Snitzer if (!pg) { 3922da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3932da1610aSMike Snitzer goto check_current_pg; 3942da1610aSMike Snitzer } 3951da177e4SLinus Torvalds m->next_pg = NULL; 3962da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3972da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 3982da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 3992da1610aSMike Snitzer return pgpath; 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds /* Don't change PG until it has no remaining paths */ 4032da1610aSMike Snitzer check_current_pg: 404506458efSWill Deacon pg = READ_ONCE(m->current_pg); 4052da1610aSMike Snitzer if (pg) { 4062da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4072da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 4082da1610aSMike Snitzer return pgpath; 4092da1610aSMike Snitzer } 4101da177e4SLinus Torvalds 4111da177e4SLinus Torvalds /* 4121da177e4SLinus Torvalds * Loop through priority groups until we find a valid path. 4131da177e4SLinus Torvalds * First time we skip PGs marked 'bypassed'. 414f220fd4eSMike Christie * Second time we only try the ones we skipped, but set 415f220fd4eSMike Christie * pg_init_delay_retry so we do not hammer controllers. 4161da177e4SLinus Torvalds */ 4171da177e4SLinus Torvalds do { 4181da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 419d19a55ccSMike Snitzer if (pg->bypassed == !!bypassed) 4201da177e4SLinus Torvalds continue; 4212da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4222da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) { 423f220fd4eSMike Christie if (!bypassed) 424518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 4252da1610aSMike Snitzer return pgpath; 4261da177e4SLinus Torvalds } 427f220fd4eSMike Christie } 4281da177e4SLinus Torvalds } while (bypassed--); 4291da177e4SLinus Torvalds 4301da177e4SLinus Torvalds failed: 4312da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 4321da177e4SLinus Torvalds m->current_pgpath = NULL; 4331da177e4SLinus Torvalds m->current_pg = NULL; 4342da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4352da1610aSMike Snitzer 4362da1610aSMike Snitzer return NULL; 4371da177e4SLinus Torvalds } 4381da177e4SLinus Torvalds 43945e15720SKiyoshi Ueda /* 44086331f39SBart Van Assche * dm_report_EIO() is a macro instead of a function to make pr_debug() 44186331f39SBart Van Assche * report the function name and line number of the function from which 44286331f39SBart Van Assche * it has been invoked. 44345e15720SKiyoshi Ueda */ 44486331f39SBart Van Assche #define dm_report_EIO(m) \ 44518a482f5SChristoph Hellwig do { \ 44686331f39SBart Van Assche struct mapped_device *md = dm_table_get_md((m)->ti->table); \ 44786331f39SBart Van Assche \ 44886331f39SBart Van Assche pr_debug("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d\n", \ 44986331f39SBart Van Assche dm_device_name(md), \ 45086331f39SBart Van Assche test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \ 45186331f39SBart Van Assche test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \ 45286331f39SBart Van Assche dm_noflush_suspending((m)->ti)); \ 45318a482f5SChristoph Hellwig } while (0) 45445e15720SKiyoshi Ueda 45536fcffccSHannes Reinecke /* 456c1fd0abeSMike Snitzer * Check whether bios must be queued in the device-mapper core rather 457c1fd0abeSMike Snitzer * than here in the target. 458c1fd0abeSMike Snitzer * 459c1fd0abeSMike Snitzer * If MPATHF_QUEUE_IF_NO_PATH and MPATHF_SAVED_QUEUE_IF_NO_PATH hold 460c1fd0abeSMike Snitzer * the same value then we are not between multipath_presuspend() 461c1fd0abeSMike Snitzer * and multipath_resume() calls and we have no need to check 462c1fd0abeSMike Snitzer * for the DMF_NOFLUSH_SUSPENDING flag. 463c1fd0abeSMike Snitzer */ 464c1fd0abeSMike Snitzer static bool __must_push_back(struct multipath *m, unsigned long flags) 465c1fd0abeSMike Snitzer { 466c1fd0abeSMike Snitzer return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) != 467c1fd0abeSMike Snitzer test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &flags)) && 468c1fd0abeSMike Snitzer dm_noflush_suspending(m->ti)); 469c1fd0abeSMike Snitzer } 470c1fd0abeSMike Snitzer 471c1fd0abeSMike Snitzer /* 472c1fd0abeSMike Snitzer * Following functions use READ_ONCE to get atomic access to 473c1fd0abeSMike Snitzer * all m->flags to avoid taking spinlock 474c1fd0abeSMike Snitzer */ 475c1fd0abeSMike Snitzer static bool must_push_back_rq(struct multipath *m) 476c1fd0abeSMike Snitzer { 477c1fd0abeSMike Snitzer unsigned long flags = READ_ONCE(m->flags); 478c1fd0abeSMike Snitzer return test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) || __must_push_back(m, flags); 479c1fd0abeSMike Snitzer } 480c1fd0abeSMike Snitzer 481c1fd0abeSMike Snitzer static bool must_push_back_bio(struct multipath *m) 482c1fd0abeSMike Snitzer { 483c1fd0abeSMike Snitzer unsigned long flags = READ_ONCE(m->flags); 484c1fd0abeSMike Snitzer return __must_push_back(m, flags); 485c1fd0abeSMike Snitzer } 486c1fd0abeSMike Snitzer 487c1fd0abeSMike Snitzer /* 48876e33fe4SMike Snitzer * Map cloned requests (request-based multipath) 48936fcffccSHannes Reinecke */ 490eb8db831SChristoph Hellwig static int multipath_clone_and_map(struct dm_target *ti, struct request *rq, 491e5863d9aSMike Snitzer union map_info *map_context, 492eb8db831SChristoph Hellwig struct request **__clone) 4931da177e4SLinus Torvalds { 4947943bd6dSMike Snitzer struct multipath *m = ti->private; 495eb8db831SChristoph Hellwig size_t nr_bytes = blk_rq_bytes(rq); 4961da177e4SLinus Torvalds struct pgpath *pgpath; 497f40c67f0SKiyoshi Ueda struct block_device *bdev; 498eb8db831SChristoph Hellwig struct dm_mpath_io *mpio = get_mpio(map_context); 4997083abbbSBart Van Assche struct request_queue *q; 500eb8db831SChristoph Hellwig struct request *clone; 5011da177e4SLinus Torvalds 5021da177e4SLinus Torvalds /* Do we need to select a new pgpath? */ 503506458efSWill Deacon pgpath = READ_ONCE(m->current_pgpath); 5042da1610aSMike Snitzer if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags)) 5052da1610aSMike Snitzer pgpath = choose_pgpath(m, nr_bytes); 5061da177e4SLinus Torvalds 5079bf59a61SMike Snitzer if (!pgpath) { 508c1fd0abeSMike Snitzer if (must_push_back_rq(m)) 509b88efd43SMike Snitzer return DM_MAPIO_DELAY_REQUEUE; 51018a482f5SChristoph Hellwig dm_report_EIO(m); /* Failed */ 511f98e0eb6SChristoph Hellwig return DM_MAPIO_KILL; 512518257b1SMike Snitzer } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) || 513518257b1SMike Snitzer test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 514459b5401SMing Lei pg_init_all_paths(m); 515c1d7ecf7SBart Van Assche return DM_MAPIO_DELAY_REQUEUE; 5169bf59a61SMike Snitzer } 5176afbc01dSMike Snitzer 518e8099177SHannes Reinecke mpio->pgpath = pgpath; 519e8099177SHannes Reinecke mpio->nr_bytes = nr_bytes; 5202eb6e1e3SKeith Busch 5212eb6e1e3SKeith Busch bdev = pgpath->path.dev->bdev; 5227083abbbSBart Van Assche q = bdev_get_queue(bdev); 523ff005a06SChristoph Hellwig clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE, 524ff005a06SChristoph Hellwig BLK_MQ_REQ_NOWAIT); 5256599c84eSBart Van Assche if (IS_ERR(clone)) { 5266599c84eSBart Van Assche /* EBUSY, ENODEV or EWOULDBLOCK: requeue */ 527848b8aefSMike Snitzer if (blk_queue_dying(q)) { 5287083abbbSBart Van Assche atomic_inc(&m->pg_init_in_progress); 5297083abbbSBart Van Assche activate_or_offline_path(pgpath); 530050af08fSMing Lei return DM_MAPIO_DELAY_REQUEUE; 5317083abbbSBart Van Assche } 532050af08fSMing Lei 533050af08fSMing Lei /* 534050af08fSMing Lei * blk-mq's SCHED_RESTART can cover this requeue, so we 535050af08fSMing Lei * needn't deal with it by DELAY_REQUEUE. More importantly, 536050af08fSMing Lei * we have to return DM_MAPIO_REQUEUE so that blk-mq can 537050af08fSMing Lei * get the queue busy feedback (via BLK_STS_RESOURCE), 538050af08fSMing Lei * otherwise I/O merging can suffer. 539050af08fSMing Lei */ 540050af08fSMing Lei if (q->mq_ops) 541050af08fSMing Lei return DM_MAPIO_REQUEUE; 542050af08fSMing Lei else 54306eb061fSBart Van Assche return DM_MAPIO_DELAY_REQUEUE; 5444c6dd53dSMike Snitzer } 5456599c84eSBart Van Assche clone->bio = clone->biotail = NULL; 5466599c84eSBart Van Assche clone->rq_disk = bdev->bd_disk; 5476599c84eSBart Van Assche clone->cmd_flags |= REQ_FAILFAST_TRANSPORT; 5486599c84eSBart Van Assche *__clone = clone; 5492eb6e1e3SKeith Busch 550e8099177SHannes Reinecke if (pgpath->pg->ps.type->start_io) 551e8099177SHannes Reinecke pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 552e8099177SHannes Reinecke &pgpath->path, 553e8099177SHannes Reinecke nr_bytes); 5542eb6e1e3SKeith Busch return DM_MAPIO_REMAPPED; 5551da177e4SLinus Torvalds } 5561da177e4SLinus Torvalds 557e5863d9aSMike Snitzer static void multipath_release_clone(struct request *clone) 558e5863d9aSMike Snitzer { 559eb8db831SChristoph Hellwig blk_put_request(clone); 560e5863d9aSMike Snitzer } 561e5863d9aSMike Snitzer 5621da177e4SLinus Torvalds /* 56376e33fe4SMike Snitzer * Map cloned bios (bio-based multipath) 56476e33fe4SMike Snitzer */ 5650001ec56SMike Snitzer 5660001ec56SMike Snitzer static struct pgpath *__map_bio(struct multipath *m, struct bio *bio) 56776e33fe4SMike Snitzer { 56876e33fe4SMike Snitzer struct pgpath *pgpath; 56976e33fe4SMike Snitzer unsigned long flags; 57076e33fe4SMike Snitzer bool queue_io; 57176e33fe4SMike Snitzer 57276e33fe4SMike Snitzer /* Do we need to select a new pgpath? */ 573506458efSWill Deacon pgpath = READ_ONCE(m->current_pgpath); 57476e33fe4SMike Snitzer queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags); 57576e33fe4SMike Snitzer if (!pgpath || !queue_io) 5760001ec56SMike Snitzer pgpath = choose_pgpath(m, bio->bi_iter.bi_size); 57776e33fe4SMike Snitzer 57876e33fe4SMike Snitzer if ((pgpath && queue_io) || 57976e33fe4SMike Snitzer (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) { 58076e33fe4SMike Snitzer /* Queue for the daemon to resubmit */ 58176e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 58276e33fe4SMike Snitzer bio_list_add(&m->queued_bios, bio); 58376e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 584848b8aefSMike Snitzer 58576e33fe4SMike Snitzer /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */ 58676e33fe4SMike Snitzer if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 58776e33fe4SMike Snitzer pg_init_all_paths(m); 58876e33fe4SMike Snitzer else if (!queue_io) 58976e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 5900001ec56SMike Snitzer 5910001ec56SMike Snitzer return ERR_PTR(-EAGAIN); 59276e33fe4SMike Snitzer } 59376e33fe4SMike Snitzer 5940001ec56SMike Snitzer return pgpath; 59576e33fe4SMike Snitzer } 59676e33fe4SMike Snitzer 5978d47e659SMike Snitzer static struct pgpath *__map_bio_fast(struct multipath *m, struct bio *bio) 5980001ec56SMike Snitzer { 5990001ec56SMike Snitzer struct pgpath *pgpath; 6000001ec56SMike Snitzer unsigned long flags; 6010001ec56SMike Snitzer 6020001ec56SMike Snitzer /* Do we need to select a new pgpath? */ 6030001ec56SMike Snitzer /* 6040001ec56SMike Snitzer * FIXME: currently only switching path if no path (due to failure, etc) 6050001ec56SMike Snitzer * - which negates the point of using a path selector 6060001ec56SMike Snitzer */ 6070001ec56SMike Snitzer pgpath = READ_ONCE(m->current_pgpath); 6080001ec56SMike Snitzer if (!pgpath) 6090001ec56SMike Snitzer pgpath = choose_pgpath(m, bio->bi_iter.bi_size); 6100001ec56SMike Snitzer 6110001ec56SMike Snitzer if (!pgpath) { 6120001ec56SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 6130001ec56SMike Snitzer /* Queue for the daemon to resubmit */ 6140001ec56SMike Snitzer spin_lock_irqsave(&m->lock, flags); 6150001ec56SMike Snitzer bio_list_add(&m->queued_bios, bio); 6160001ec56SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 6170001ec56SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 6180001ec56SMike Snitzer 6190001ec56SMike Snitzer return ERR_PTR(-EAGAIN); 6200001ec56SMike Snitzer } 6210001ec56SMike Snitzer return NULL; 6220001ec56SMike Snitzer } 6230001ec56SMike Snitzer 6240001ec56SMike Snitzer return pgpath; 6250001ec56SMike Snitzer } 6260001ec56SMike Snitzer 6270001ec56SMike Snitzer static int __multipath_map_bio(struct multipath *m, struct bio *bio, 6280001ec56SMike Snitzer struct dm_mpath_io *mpio) 6290001ec56SMike Snitzer { 6300001ec56SMike Snitzer struct pgpath *pgpath; 6310001ec56SMike Snitzer 6328d47e659SMike Snitzer if (!m->hw_handler_name) 6338d47e659SMike Snitzer pgpath = __map_bio_fast(m, bio); 6340001ec56SMike Snitzer else 6350001ec56SMike Snitzer pgpath = __map_bio(m, bio); 6360001ec56SMike Snitzer 6370001ec56SMike Snitzer if (IS_ERR(pgpath)) 6380001ec56SMike Snitzer return DM_MAPIO_SUBMITTED; 6390001ec56SMike Snitzer 64076e33fe4SMike Snitzer if (!pgpath) { 641c1fd0abeSMike Snitzer if (must_push_back_bio(m)) 64276e33fe4SMike Snitzer return DM_MAPIO_REQUEUE; 64318a482f5SChristoph Hellwig dm_report_EIO(m); 644846785e6SChristoph Hellwig return DM_MAPIO_KILL; 64576e33fe4SMike Snitzer } 64676e33fe4SMike Snitzer 64776e33fe4SMike Snitzer mpio->pgpath = pgpath; 64876e33fe4SMike Snitzer 6494e4cbee9SChristoph Hellwig bio->bi_status = 0; 65074d46992SChristoph Hellwig bio_set_dev(bio, pgpath->path.dev->bdev); 6511eff9d32SJens Axboe bio->bi_opf |= REQ_FAILFAST_TRANSPORT; 65276e33fe4SMike Snitzer 65376e33fe4SMike Snitzer if (pgpath->pg->ps.type->start_io) 65476e33fe4SMike Snitzer pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 65576e33fe4SMike Snitzer &pgpath->path, 656d0442f80SMike Snitzer mpio->nr_bytes); 65776e33fe4SMike Snitzer return DM_MAPIO_REMAPPED; 65876e33fe4SMike Snitzer } 65976e33fe4SMike Snitzer 66076e33fe4SMike Snitzer static int multipath_map_bio(struct dm_target *ti, struct bio *bio) 66176e33fe4SMike Snitzer { 66276e33fe4SMike Snitzer struct multipath *m = ti->private; 663bf661be1SMike Snitzer struct dm_mpath_io *mpio = NULL; 664bf661be1SMike Snitzer 66563f6e6fdSMike Snitzer multipath_init_per_bio_data(bio, &mpio); 66676e33fe4SMike Snitzer return __multipath_map_bio(m, bio, mpio); 66776e33fe4SMike Snitzer } 66876e33fe4SMike Snitzer 6697e48c768SMike Snitzer static void process_queued_io_list(struct multipath *m) 67076e33fe4SMike Snitzer { 6717e48c768SMike Snitzer if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED) 6727e48c768SMike Snitzer dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table)); 6738d47e659SMike Snitzer else if (m->queue_mode == DM_TYPE_BIO_BASED) 67476e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 67576e33fe4SMike Snitzer } 67676e33fe4SMike Snitzer 67776e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work) 67876e33fe4SMike Snitzer { 67976e33fe4SMike Snitzer int r; 68076e33fe4SMike Snitzer unsigned long flags; 68176e33fe4SMike Snitzer struct bio *bio; 68276e33fe4SMike Snitzer struct bio_list bios; 68376e33fe4SMike Snitzer struct blk_plug plug; 68476e33fe4SMike Snitzer struct multipath *m = 68576e33fe4SMike Snitzer container_of(work, struct multipath, process_queued_bios); 68676e33fe4SMike Snitzer 68776e33fe4SMike Snitzer bio_list_init(&bios); 68876e33fe4SMike Snitzer 68976e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 69076e33fe4SMike Snitzer 69176e33fe4SMike Snitzer if (bio_list_empty(&m->queued_bios)) { 69276e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 69376e33fe4SMike Snitzer return; 69476e33fe4SMike Snitzer } 69576e33fe4SMike Snitzer 69676e33fe4SMike Snitzer bio_list_merge(&bios, &m->queued_bios); 69776e33fe4SMike Snitzer bio_list_init(&m->queued_bios); 69876e33fe4SMike Snitzer 69976e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 70076e33fe4SMike Snitzer 70176e33fe4SMike Snitzer blk_start_plug(&plug); 70276e33fe4SMike Snitzer while ((bio = bio_list_pop(&bios))) { 7031836df08SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 7041836df08SMike Snitzer dm_bio_restore(get_bio_details_from_mpio(mpio), bio); 7051836df08SMike Snitzer r = __multipath_map_bio(m, bio, mpio); 706846785e6SChristoph Hellwig switch (r) { 707846785e6SChristoph Hellwig case DM_MAPIO_KILL: 7084e4cbee9SChristoph Hellwig bio->bi_status = BLK_STS_IOERR; 7094e4cbee9SChristoph Hellwig bio_endio(bio); 710047385b3SDan Carpenter break; 711846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE: 7124e4cbee9SChristoph Hellwig bio->bi_status = BLK_STS_DM_REQUEUE; 71376e33fe4SMike Snitzer bio_endio(bio); 714846785e6SChristoph Hellwig break; 715846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED: 71676e33fe4SMike Snitzer generic_make_request(bio); 717846785e6SChristoph Hellwig break; 7188192a0cdSWang Sheng-Hui case DM_MAPIO_SUBMITTED: 7199157c8d3SBart Van Assche break; 7209157c8d3SBart Van Assche default: 7219157c8d3SBart Van Assche WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r); 722846785e6SChristoph Hellwig } 72376e33fe4SMike Snitzer } 72476e33fe4SMike Snitzer blk_finish_plug(&plug); 72576e33fe4SMike Snitzer } 72676e33fe4SMike Snitzer 72776e33fe4SMike Snitzer /* 7281da177e4SLinus Torvalds * If we run out of usable paths, should we queue I/O or error it? 7291da177e4SLinus Torvalds */ 730be7d31ccSMike Snitzer static int queue_if_no_path(struct multipath *m, bool queue_if_no_path, 731be7d31ccSMike Snitzer bool save_old_value) 7321da177e4SLinus Torvalds { 7331da177e4SLinus Torvalds unsigned long flags; 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 7365307e2adSLukas Wunner assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags, 7375307e2adSLukas Wunner (save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) || 7385307e2adSLukas Wunner (!save_old_value && queue_if_no_path)); 739c1fd0abeSMike Snitzer assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path); 7401da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 7411da177e4SLinus Torvalds 74276e33fe4SMike Snitzer if (!queue_if_no_path) { 74363d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 7447e48c768SMike Snitzer process_queued_io_list(m); 74576e33fe4SMike Snitzer } 74663d832c3SHannes Reinecke 7471da177e4SLinus Torvalds return 0; 7481da177e4SLinus Torvalds } 7491da177e4SLinus Torvalds 7501da177e4SLinus Torvalds /* 7511da177e4SLinus Torvalds * An event is triggered whenever a path is taken out of use. 7521da177e4SLinus Torvalds * Includes path failure and PG bypass. 7531da177e4SLinus Torvalds */ 754c4028958SDavid Howells static void trigger_event(struct work_struct *work) 7551da177e4SLinus Torvalds { 756c4028958SDavid Howells struct multipath *m = 757c4028958SDavid Howells container_of(work, struct multipath, trigger_event); 7581da177e4SLinus Torvalds 7591da177e4SLinus Torvalds dm_table_event(m->ti->table); 7601da177e4SLinus Torvalds } 7611da177e4SLinus Torvalds 7621da177e4SLinus Torvalds /*----------------------------------------------------------------- 7631da177e4SLinus Torvalds * Constructor/argument parsing: 7641da177e4SLinus Torvalds * <#multipath feature args> [<arg>]* 7651da177e4SLinus Torvalds * <#hw_handler args> [hw_handler [<arg>]*] 7661da177e4SLinus Torvalds * <#priority groups> 7671da177e4SLinus Torvalds * <initial priority group> 7681da177e4SLinus Torvalds * [<selector> <#selector args> [<arg>]* 7691da177e4SLinus Torvalds * <#paths> <#per-path selector args> 7701da177e4SLinus Torvalds * [<path> [<arg>]* ]+ ]+ 7711da177e4SLinus Torvalds *---------------------------------------------------------------*/ 772498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg, 7731da177e4SLinus Torvalds struct dm_target *ti) 7741da177e4SLinus Torvalds { 7751da177e4SLinus Torvalds int r; 7761da177e4SLinus Torvalds struct path_selector_type *pst; 7771da177e4SLinus Torvalds unsigned ps_argc; 7781da177e4SLinus Torvalds 7795916a22bSEric Biggers static const struct dm_arg _args[] = { 78072d94861SAlasdair G Kergon {0, 1024, "invalid number of path selector args"}, 7811da177e4SLinus Torvalds }; 7821da177e4SLinus Torvalds 783498f0103SMike Snitzer pst = dm_get_path_selector(dm_shift_arg(as)); 7841da177e4SLinus Torvalds if (!pst) { 78572d94861SAlasdair G Kergon ti->error = "unknown path selector type"; 7861da177e4SLinus Torvalds return -EINVAL; 7871da177e4SLinus Torvalds } 7881da177e4SLinus Torvalds 789498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &ps_argc, &ti->error); 790371b2e34SMikulas Patocka if (r) { 791371b2e34SMikulas Patocka dm_put_path_selector(pst); 7921da177e4SLinus Torvalds return -EINVAL; 793371b2e34SMikulas Patocka } 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds r = pst->create(&pg->ps, ps_argc, as->argv); 7961da177e4SLinus Torvalds if (r) { 7971da177e4SLinus Torvalds dm_put_path_selector(pst); 79872d94861SAlasdair G Kergon ti->error = "path selector constructor failed"; 7991da177e4SLinus Torvalds return r; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 8021da177e4SLinus Torvalds pg->ps.type = pst; 803498f0103SMike Snitzer dm_consume_args(as, ps_argc); 8041da177e4SLinus Torvalds 8051da177e4SLinus Torvalds return 0; 8061da177e4SLinus Torvalds } 8071da177e4SLinus Torvalds 808e8f74a0fSMike Snitzer static int setup_scsi_dh(struct block_device *bdev, struct multipath *m, 809*b592211cSMike Snitzer const char **attached_handler_name, char **error) 8101da177e4SLinus Torvalds { 811848b8aefSMike Snitzer struct request_queue *q = bdev_get_queue(bdev); 812848b8aefSMike Snitzer int r; 813a0cf7ea9SHannes Reinecke 814518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) { 8151bab0de0SChristoph Hellwig retain: 816*b592211cSMike Snitzer if (*attached_handler_name) { 817a58a935dSMike Snitzer /* 81854cd640dStang.junhui * Clear any hw_handler_params associated with a 81954cd640dStang.junhui * handler that isn't already attached. 82054cd640dStang.junhui */ 821*b592211cSMike Snitzer if (m->hw_handler_name && strcmp(*attached_handler_name, m->hw_handler_name)) { 82254cd640dStang.junhui kfree(m->hw_handler_params); 82354cd640dStang.junhui m->hw_handler_params = NULL; 82454cd640dStang.junhui } 82554cd640dStang.junhui 82654cd640dStang.junhui /* 827a58a935dSMike Snitzer * Reset hw_handler_name to match the attached handler 828a58a935dSMike Snitzer * 829a58a935dSMike Snitzer * NB. This modifies the table line to show the actual 830a58a935dSMike Snitzer * handler instead of the original table passed in. 831a58a935dSMike Snitzer */ 832a58a935dSMike Snitzer kfree(m->hw_handler_name); 833*b592211cSMike Snitzer m->hw_handler_name = *attached_handler_name; 834*b592211cSMike Snitzer *attached_handler_name = NULL; 835a58a935dSMike Snitzer } 836a58a935dSMike Snitzer } 837a58a935dSMike Snitzer 838a58a935dSMike Snitzer if (m->hw_handler_name) { 839a0cf7ea9SHannes Reinecke r = scsi_dh_attach(q, m->hw_handler_name); 840a0cf7ea9SHannes Reinecke if (r == -EBUSY) { 8411bab0de0SChristoph Hellwig char b[BDEVNAME_SIZE]; 842a0cf7ea9SHannes Reinecke 8431bab0de0SChristoph Hellwig printk(KERN_INFO "dm-mpath: retaining handler on device %s\n", 844848b8aefSMike Snitzer bdevname(bdev, b)); 8451bab0de0SChristoph Hellwig goto retain; 8461bab0de0SChristoph Hellwig } 847ae11b1b3SHannes Reinecke if (r < 0) { 848848b8aefSMike Snitzer *error = "error attaching hardware handler"; 849848b8aefSMike Snitzer return r; 850ae11b1b3SHannes Reinecke } 8512bfd2e13SChandra Seetharaman 8522bfd2e13SChandra Seetharaman if (m->hw_handler_params) { 8532bfd2e13SChandra Seetharaman r = scsi_dh_set_params(q, m->hw_handler_params); 8542bfd2e13SChandra Seetharaman if (r < 0) { 855848b8aefSMike Snitzer *error = "unable to set hardware handler parameters"; 856848b8aefSMike Snitzer return r; 857848b8aefSMike Snitzer } 858848b8aefSMike Snitzer } 859848b8aefSMike Snitzer } 860848b8aefSMike Snitzer 861848b8aefSMike Snitzer return 0; 862848b8aefSMike Snitzer } 863848b8aefSMike Snitzer 864848b8aefSMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps, 865848b8aefSMike Snitzer struct dm_target *ti) 866848b8aefSMike Snitzer { 867848b8aefSMike Snitzer int r; 868848b8aefSMike Snitzer struct pgpath *p; 869848b8aefSMike Snitzer struct multipath *m = ti->private; 870e8f74a0fSMike Snitzer struct request_queue *q; 871*b592211cSMike Snitzer const char *attached_handler_name = NULL; 872848b8aefSMike Snitzer 873848b8aefSMike Snitzer /* we need at least a path arg */ 874848b8aefSMike Snitzer if (as->argc < 1) { 875848b8aefSMike Snitzer ti->error = "no device given"; 876848b8aefSMike Snitzer return ERR_PTR(-EINVAL); 877848b8aefSMike Snitzer } 878848b8aefSMike Snitzer 879848b8aefSMike Snitzer p = alloc_pgpath(); 880848b8aefSMike Snitzer if (!p) 881848b8aefSMike Snitzer return ERR_PTR(-ENOMEM); 882848b8aefSMike Snitzer 883848b8aefSMike Snitzer r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table), 884848b8aefSMike Snitzer &p->path.dev); 885848b8aefSMike Snitzer if (r) { 886848b8aefSMike Snitzer ti->error = "error getting device"; 8872bfd2e13SChandra Seetharaman goto bad; 8882bfd2e13SChandra Seetharaman } 889848b8aefSMike Snitzer 890e8f74a0fSMike Snitzer q = bdev_get_queue(p->path.dev->bdev); 891e8f74a0fSMike Snitzer attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL); 892e457edf0SMike Snitzer if (attached_handler_name || m->hw_handler_name) { 893848b8aefSMike Snitzer INIT_DELAYED_WORK(&p->activate_path, activate_path_work); 894*b592211cSMike Snitzer r = setup_scsi_dh(p->path.dev->bdev, m, &attached_handler_name, &ti->error); 895848b8aefSMike Snitzer if (r) { 896848b8aefSMike Snitzer dm_put_device(ti, p->path.dev); 897848b8aefSMike Snitzer goto bad; 8982bfd2e13SChandra Seetharaman } 899ae11b1b3SHannes Reinecke } 900ae11b1b3SHannes Reinecke 9011da177e4SLinus Torvalds r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); 9021da177e4SLinus Torvalds if (r) { 9031da177e4SLinus Torvalds dm_put_device(ti, p->path.dev); 9041da177e4SLinus Torvalds goto bad; 9051da177e4SLinus Torvalds } 9061da177e4SLinus Torvalds 9071da177e4SLinus Torvalds return p; 9081da177e4SLinus Torvalds bad: 909*b592211cSMike Snitzer kfree(attached_handler_name); 9101da177e4SLinus Torvalds free_pgpath(p); 91101460f35SBenjamin Marzinski return ERR_PTR(r); 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds 914498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as, 91528f16c20SMicha³ Miros³aw struct multipath *m) 9161da177e4SLinus Torvalds { 9175916a22bSEric Biggers static const struct dm_arg _args[] = { 91872d94861SAlasdair G Kergon {1, 1024, "invalid number of paths"}, 91972d94861SAlasdair G Kergon {0, 1024, "invalid number of selector args"} 9201da177e4SLinus Torvalds }; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds int r; 923498f0103SMike Snitzer unsigned i, nr_selector_args, nr_args; 9241da177e4SLinus Torvalds struct priority_group *pg; 92528f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds if (as->argc < 2) { 9281da177e4SLinus Torvalds as->argc = 0; 92901460f35SBenjamin Marzinski ti->error = "not enough priority group arguments"; 93001460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 9311da177e4SLinus Torvalds } 9321da177e4SLinus Torvalds 9331da177e4SLinus Torvalds pg = alloc_priority_group(); 9341da177e4SLinus Torvalds if (!pg) { 93572d94861SAlasdair G Kergon ti->error = "couldn't allocate priority group"; 93601460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 9371da177e4SLinus Torvalds } 9381da177e4SLinus Torvalds pg->m = m; 9391da177e4SLinus Torvalds 9401da177e4SLinus Torvalds r = parse_path_selector(as, pg, ti); 9411da177e4SLinus Torvalds if (r) 9421da177e4SLinus Torvalds goto bad; 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds /* 9451da177e4SLinus Torvalds * read the paths 9461da177e4SLinus Torvalds */ 947498f0103SMike Snitzer r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error); 9481da177e4SLinus Torvalds if (r) 9491da177e4SLinus Torvalds goto bad; 9501da177e4SLinus Torvalds 951498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error); 9521da177e4SLinus Torvalds if (r) 9531da177e4SLinus Torvalds goto bad; 9541da177e4SLinus Torvalds 955498f0103SMike Snitzer nr_args = 1 + nr_selector_args; 9561da177e4SLinus Torvalds for (i = 0; i < pg->nr_pgpaths; i++) { 9571da177e4SLinus Torvalds struct pgpath *pgpath; 958498f0103SMike Snitzer struct dm_arg_set path_args; 9591da177e4SLinus Torvalds 960498f0103SMike Snitzer if (as->argc < nr_args) { 961148acff6SMikulas Patocka ti->error = "not enough path parameters"; 9626bbf79a1SAlasdair G Kergon r = -EINVAL; 9631da177e4SLinus Torvalds goto bad; 964148acff6SMikulas Patocka } 9651da177e4SLinus Torvalds 966498f0103SMike Snitzer path_args.argc = nr_args; 9671da177e4SLinus Torvalds path_args.argv = as->argv; 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds pgpath = parse_path(&path_args, &pg->ps, ti); 97001460f35SBenjamin Marzinski if (IS_ERR(pgpath)) { 97101460f35SBenjamin Marzinski r = PTR_ERR(pgpath); 9721da177e4SLinus Torvalds goto bad; 97301460f35SBenjamin Marzinski } 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds pgpath->pg = pg; 9761da177e4SLinus Torvalds list_add_tail(&pgpath->list, &pg->pgpaths); 977498f0103SMike Snitzer dm_consume_args(as, nr_args); 9781da177e4SLinus Torvalds } 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds return pg; 9811da177e4SLinus Torvalds 9821da177e4SLinus Torvalds bad: 9831da177e4SLinus Torvalds free_priority_group(pg, ti); 98401460f35SBenjamin Marzinski return ERR_PTR(r); 9851da177e4SLinus Torvalds } 9861da177e4SLinus Torvalds 987498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m) 9881da177e4SLinus Torvalds { 9891da177e4SLinus Torvalds unsigned hw_argc; 9902bfd2e13SChandra Seetharaman int ret; 99128f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 9921da177e4SLinus Torvalds 9935916a22bSEric Biggers static const struct dm_arg _args[] = { 99472d94861SAlasdair G Kergon {0, 1024, "invalid number of hardware handler args"}, 9951da177e4SLinus Torvalds }; 9961da177e4SLinus Torvalds 997498f0103SMike Snitzer if (dm_read_arg_group(_args, as, &hw_argc, &ti->error)) 9981da177e4SLinus Torvalds return -EINVAL; 9991da177e4SLinus Torvalds 10001da177e4SLinus Torvalds if (!hw_argc) 10011da177e4SLinus Torvalds return 0; 10021da177e4SLinus Torvalds 10038d47e659SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) { 100476e33fe4SMike Snitzer dm_consume_args(as, hw_argc); 100576e33fe4SMike Snitzer DMERR("bio-based multipath doesn't allow hardware handler args"); 100676e33fe4SMike Snitzer return 0; 100776e33fe4SMike Snitzer } 100876e33fe4SMike Snitzer 1009498f0103SMike Snitzer m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL); 1010f97dc421Stang.junhui if (!m->hw_handler_name) 1011f97dc421Stang.junhui return -EINVAL; 101214e98c5cSChandra Seetharaman 10132bfd2e13SChandra Seetharaman if (hw_argc > 1) { 10142bfd2e13SChandra Seetharaman char *p; 10152bfd2e13SChandra Seetharaman int i, j, len = 4; 10162bfd2e13SChandra Seetharaman 10172bfd2e13SChandra Seetharaman for (i = 0; i <= hw_argc - 2; i++) 10182bfd2e13SChandra Seetharaman len += strlen(as->argv[i]) + 1; 10192bfd2e13SChandra Seetharaman p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); 10202bfd2e13SChandra Seetharaman if (!p) { 10212bfd2e13SChandra Seetharaman ti->error = "memory allocation failed"; 10222bfd2e13SChandra Seetharaman ret = -ENOMEM; 10232bfd2e13SChandra Seetharaman goto fail; 10242bfd2e13SChandra Seetharaman } 10252bfd2e13SChandra Seetharaman j = sprintf(p, "%d", hw_argc - 1); 10262bfd2e13SChandra Seetharaman for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1) 10272bfd2e13SChandra Seetharaman j = sprintf(p, "%s", as->argv[i]); 10282bfd2e13SChandra Seetharaman } 1029498f0103SMike Snitzer dm_consume_args(as, hw_argc - 1); 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds return 0; 10322bfd2e13SChandra Seetharaman fail: 10332bfd2e13SChandra Seetharaman kfree(m->hw_handler_name); 10342bfd2e13SChandra Seetharaman m->hw_handler_name = NULL; 10352bfd2e13SChandra Seetharaman return ret; 10361da177e4SLinus Torvalds } 10371da177e4SLinus Torvalds 1038498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m) 10391da177e4SLinus Torvalds { 10401da177e4SLinus Torvalds int r; 10411da177e4SLinus Torvalds unsigned argc; 104228f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 1043498f0103SMike Snitzer const char *arg_name; 10441da177e4SLinus Torvalds 10455916a22bSEric Biggers static const struct dm_arg _args[] = { 1046e83068a5SMike Snitzer {0, 8, "invalid number of feature args"}, 1047c9e45581SDave Wysochanski {1, 50, "pg_init_retries must be between 1 and 50"}, 10484e2d19e4SChandra Seetharaman {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"}, 10491da177e4SLinus Torvalds }; 10501da177e4SLinus Torvalds 1051498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &argc, &ti->error); 10521da177e4SLinus Torvalds if (r) 10531da177e4SLinus Torvalds return -EINVAL; 10541da177e4SLinus Torvalds 10551da177e4SLinus Torvalds if (!argc) 10561da177e4SLinus Torvalds return 0; 10571da177e4SLinus Torvalds 1058c9e45581SDave Wysochanski do { 1059498f0103SMike Snitzer arg_name = dm_shift_arg(as); 1060c9e45581SDave Wysochanski argc--; 1061c9e45581SDave Wysochanski 1062498f0103SMike Snitzer if (!strcasecmp(arg_name, "queue_if_no_path")) { 1063be7d31ccSMike Snitzer r = queue_if_no_path(m, true, false); 1064c9e45581SDave Wysochanski continue; 10651da177e4SLinus Torvalds } 1066c9e45581SDave Wysochanski 1067a58a935dSMike Snitzer if (!strcasecmp(arg_name, "retain_attached_hw_handler")) { 1068518257b1SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 1069a58a935dSMike Snitzer continue; 1070a58a935dSMike Snitzer } 1071a58a935dSMike Snitzer 1072498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_retries") && 1073c9e45581SDave Wysochanski (argc >= 1)) { 1074498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error); 1075c9e45581SDave Wysochanski argc--; 1076c9e45581SDave Wysochanski continue; 1077c9e45581SDave Wysochanski } 1078c9e45581SDave Wysochanski 1079498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_delay_msecs") && 10804e2d19e4SChandra Seetharaman (argc >= 1)) { 1081498f0103SMike Snitzer r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error); 10824e2d19e4SChandra Seetharaman argc--; 10834e2d19e4SChandra Seetharaman continue; 10844e2d19e4SChandra Seetharaman } 10854e2d19e4SChandra Seetharaman 1086e83068a5SMike Snitzer if (!strcasecmp(arg_name, "queue_mode") && 1087e83068a5SMike Snitzer (argc >= 1)) { 1088e83068a5SMike Snitzer const char *queue_mode_name = dm_shift_arg(as); 1089e83068a5SMike Snitzer 1090e83068a5SMike Snitzer if (!strcasecmp(queue_mode_name, "bio")) 1091e83068a5SMike Snitzer m->queue_mode = DM_TYPE_BIO_BASED; 1092e83068a5SMike Snitzer else if (!strcasecmp(queue_mode_name, "rq")) 1093e83068a5SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 1094e83068a5SMike Snitzer else if (!strcasecmp(queue_mode_name, "mq")) 1095e83068a5SMike Snitzer m->queue_mode = DM_TYPE_MQ_REQUEST_BASED; 1096e83068a5SMike Snitzer else { 1097e83068a5SMike Snitzer ti->error = "Unknown 'queue_mode' requested"; 1098e83068a5SMike Snitzer r = -EINVAL; 1099e83068a5SMike Snitzer } 1100e83068a5SMike Snitzer argc--; 1101e83068a5SMike Snitzer continue; 1102e83068a5SMike Snitzer } 1103e83068a5SMike Snitzer 1104c9e45581SDave Wysochanski ti->error = "Unrecognised multipath feature request"; 1105c9e45581SDave Wysochanski r = -EINVAL; 1106c9e45581SDave Wysochanski } while (argc && !r); 1107c9e45581SDave Wysochanski 1108c9e45581SDave Wysochanski return r; 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds 1111e83068a5SMike Snitzer static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv) 11121da177e4SLinus Torvalds { 1113498f0103SMike Snitzer /* target arguments */ 11145916a22bSEric Biggers static const struct dm_arg _args[] = { 1115a490a07aSMike Snitzer {0, 1024, "invalid number of priority groups"}, 1116a490a07aSMike Snitzer {0, 1024, "invalid initial priority group number"}, 11171da177e4SLinus Torvalds }; 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds int r; 11201da177e4SLinus Torvalds struct multipath *m; 1121498f0103SMike Snitzer struct dm_arg_set as; 11221da177e4SLinus Torvalds unsigned pg_count = 0; 11231da177e4SLinus Torvalds unsigned next_pg_num; 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds as.argc = argc; 11261da177e4SLinus Torvalds as.argv = argv; 11271da177e4SLinus Torvalds 1128e83068a5SMike Snitzer m = alloc_multipath(ti); 11291da177e4SLinus Torvalds if (!m) { 113072d94861SAlasdair G Kergon ti->error = "can't allocate multipath"; 11311da177e4SLinus Torvalds return -EINVAL; 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds 113428f16c20SMicha³ Miros³aw r = parse_features(&as, m); 11351da177e4SLinus Torvalds if (r) 11361da177e4SLinus Torvalds goto bad; 11371da177e4SLinus Torvalds 1138e83068a5SMike Snitzer r = alloc_multipath_stage2(ti, m); 1139e83068a5SMike Snitzer if (r) 1140e83068a5SMike Snitzer goto bad; 1141e83068a5SMike Snitzer 114228f16c20SMicha³ Miros³aw r = parse_hw_handler(&as, m); 11431da177e4SLinus Torvalds if (r) 11441da177e4SLinus Torvalds goto bad; 11451da177e4SLinus Torvalds 1146498f0103SMike Snitzer r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error); 11471da177e4SLinus Torvalds if (r) 11481da177e4SLinus Torvalds goto bad; 11491da177e4SLinus Torvalds 1150498f0103SMike Snitzer r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error); 11511da177e4SLinus Torvalds if (r) 11521da177e4SLinus Torvalds goto bad; 11531da177e4SLinus Torvalds 1154a490a07aSMike Snitzer if ((!m->nr_priority_groups && next_pg_num) || 1155a490a07aSMike Snitzer (m->nr_priority_groups && !next_pg_num)) { 1156a490a07aSMike Snitzer ti->error = "invalid initial priority group"; 1157a490a07aSMike Snitzer r = -EINVAL; 1158a490a07aSMike Snitzer goto bad; 1159a490a07aSMike Snitzer } 1160a490a07aSMike Snitzer 11611da177e4SLinus Torvalds /* parse the priority groups */ 11621da177e4SLinus Torvalds while (as.argc) { 11631da177e4SLinus Torvalds struct priority_group *pg; 116491e968aaSMike Snitzer unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths); 11651da177e4SLinus Torvalds 116628f16c20SMicha³ Miros³aw pg = parse_priority_group(&as, m); 116701460f35SBenjamin Marzinski if (IS_ERR(pg)) { 116801460f35SBenjamin Marzinski r = PTR_ERR(pg); 11691da177e4SLinus Torvalds goto bad; 11701da177e4SLinus Torvalds } 11711da177e4SLinus Torvalds 117291e968aaSMike Snitzer nr_valid_paths += pg->nr_pgpaths; 117391e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, nr_valid_paths); 117491e968aaSMike Snitzer 11751da177e4SLinus Torvalds list_add_tail(&pg->list, &m->priority_groups); 11761da177e4SLinus Torvalds pg_count++; 11771da177e4SLinus Torvalds pg->pg_num = pg_count; 11781da177e4SLinus Torvalds if (!--next_pg_num) 11791da177e4SLinus Torvalds m->next_pg = pg; 11801da177e4SLinus Torvalds } 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds if (pg_count != m->nr_priority_groups) { 118372d94861SAlasdair G Kergon ti->error = "priority group count mismatch"; 11841da177e4SLinus Torvalds r = -EINVAL; 11851da177e4SLinus Torvalds goto bad; 11861da177e4SLinus Torvalds } 11871da177e4SLinus Torvalds 118855a62eefSAlasdair G Kergon ti->num_flush_bios = 1; 118955a62eefSAlasdair G Kergon ti->num_discard_bios = 1; 1190042bcef8SMike Snitzer ti->num_write_same_bios = 1; 1191ac62d620SChristoph Hellwig ti->num_write_zeroes_bios = 1; 11928d47e659SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) 1193bf661be1SMike Snitzer ti->per_io_data_size = multipath_per_bio_data_size(); 1194eb8db831SChristoph Hellwig else 11958637a6bfSMike Snitzer ti->per_io_data_size = sizeof(struct dm_mpath_io); 11968627921fSMikulas Patocka 11971da177e4SLinus Torvalds return 0; 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds bad: 12001da177e4SLinus Torvalds free_multipath(m); 12011da177e4SLinus Torvalds return r; 12021da177e4SLinus Torvalds } 12031da177e4SLinus Torvalds 12042bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m) 12052bded7bdSKiyoshi Ueda { 12069f4c3f87SBart Van Assche DEFINE_WAIT(wait); 12072bded7bdSKiyoshi Ueda 12082bded7bdSKiyoshi Ueda while (1) { 12099f4c3f87SBart Van Assche prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE); 12102bded7bdSKiyoshi Ueda 121191e968aaSMike Snitzer if (!atomic_read(&m->pg_init_in_progress)) 12122bded7bdSKiyoshi Ueda break; 12132bded7bdSKiyoshi Ueda 12142bded7bdSKiyoshi Ueda io_schedule(); 12152bded7bdSKiyoshi Ueda } 12169f4c3f87SBart Van Assche finish_wait(&m->pg_init_wait, &wait); 12172bded7bdSKiyoshi Ueda } 12182bded7bdSKiyoshi Ueda 12192bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m) 12201da177e4SLinus Torvalds { 1221848b8aefSMike Snitzer if (m->hw_handler_name) { 1222518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1223518257b1SMike Snitzer smp_mb__after_atomic(); 1224954a73d5SShiva Krishna Merla 1225bab7cfc7SChandra Seetharaman flush_workqueue(kmpath_handlerd); 12262bded7bdSKiyoshi Ueda multipath_wait_for_pg_init_completion(m); 1227954a73d5SShiva Krishna Merla 1228518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1229518257b1SMike Snitzer smp_mb__after_atomic(); 12306df400abSKiyoshi Ueda } 12316df400abSKiyoshi Ueda 1232848b8aefSMike Snitzer flush_workqueue(kmultipathd); 1233848b8aefSMike Snitzer flush_work(&m->trigger_event); 1234848b8aefSMike Snitzer } 1235848b8aefSMike Snitzer 12366df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti) 12376df400abSKiyoshi Ueda { 12386df400abSKiyoshi Ueda struct multipath *m = ti->private; 12396df400abSKiyoshi Ueda 12402bded7bdSKiyoshi Ueda flush_multipath_work(m); 12411da177e4SLinus Torvalds free_multipath(m); 12421da177e4SLinus Torvalds } 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds /* 12451da177e4SLinus Torvalds * Take a path out of use. 12461da177e4SLinus Torvalds */ 12471da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath) 12481da177e4SLinus Torvalds { 12491da177e4SLinus Torvalds unsigned long flags; 12501da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 12511da177e4SLinus Torvalds 12521da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 12531da177e4SLinus Torvalds 12546680073dSKiyoshi Ueda if (!pgpath->is_active) 12551da177e4SLinus Torvalds goto out; 12561da177e4SLinus Torvalds 125772d94861SAlasdair G Kergon DMWARN("Failing path %s.", pgpath->path.dev->name); 12581da177e4SLinus Torvalds 12591da177e4SLinus Torvalds pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); 1260be7d31ccSMike Snitzer pgpath->is_active = false; 12611da177e4SLinus Torvalds pgpath->fail_count++; 12621da177e4SLinus Torvalds 126391e968aaSMike Snitzer atomic_dec(&m->nr_valid_paths); 12641da177e4SLinus Torvalds 12651da177e4SLinus Torvalds if (pgpath == m->current_pgpath) 12661da177e4SLinus Torvalds m->current_pgpath = NULL; 12671da177e4SLinus Torvalds 1268b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti, 126991e968aaSMike Snitzer pgpath->path.dev->name, atomic_read(&m->nr_valid_paths)); 1270b15546f9SMike Anderson 1271fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 12721da177e4SLinus Torvalds 12731da177e4SLinus Torvalds out: 12741da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 12751da177e4SLinus Torvalds 12761da177e4SLinus Torvalds return 0; 12771da177e4SLinus Torvalds } 12781da177e4SLinus Torvalds 12791da177e4SLinus Torvalds /* 12801da177e4SLinus Torvalds * Reinstate a previously-failed path 12811da177e4SLinus Torvalds */ 12821da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath) 12831da177e4SLinus Torvalds { 128463d832c3SHannes Reinecke int r = 0, run_queue = 0; 12851da177e4SLinus Torvalds unsigned long flags; 12861da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 128791e968aaSMike Snitzer unsigned nr_valid_paths; 12881da177e4SLinus Torvalds 12891da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 12901da177e4SLinus Torvalds 12916680073dSKiyoshi Ueda if (pgpath->is_active) 12921da177e4SLinus Torvalds goto out; 12931da177e4SLinus Torvalds 1294ec31f3f7SMike Snitzer DMWARN("Reinstating path %s.", pgpath->path.dev->name); 12951da177e4SLinus Torvalds 12961da177e4SLinus Torvalds r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); 12971da177e4SLinus Torvalds if (r) 12981da177e4SLinus Torvalds goto out; 12991da177e4SLinus Torvalds 1300be7d31ccSMike Snitzer pgpath->is_active = true; 13011da177e4SLinus Torvalds 130291e968aaSMike Snitzer nr_valid_paths = atomic_inc_return(&m->nr_valid_paths); 130391e968aaSMike Snitzer if (nr_valid_paths == 1) { 13041da177e4SLinus Torvalds m->current_pgpath = NULL; 130563d832c3SHannes Reinecke run_queue = 1; 1306e54f77ddSChandra Seetharaman } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) { 13074e2d19e4SChandra Seetharaman if (queue_work(kmpath_handlerd, &pgpath->activate_path.work)) 130891e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 1309e54f77ddSChandra Seetharaman } 13101da177e4SLinus Torvalds 1311b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti, 131291e968aaSMike Snitzer pgpath->path.dev->name, nr_valid_paths); 1313b15546f9SMike Anderson 1314fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13151da177e4SLinus Torvalds 13161da177e4SLinus Torvalds out: 13171da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 131876e33fe4SMike Snitzer if (run_queue) { 131963d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 13207e48c768SMike Snitzer process_queued_io_list(m); 132176e33fe4SMike Snitzer } 13221da177e4SLinus Torvalds 13231da177e4SLinus Torvalds return r; 13241da177e4SLinus Torvalds } 13251da177e4SLinus Torvalds 13261da177e4SLinus Torvalds /* 13271da177e4SLinus Torvalds * Fail or reinstate all paths that match the provided struct dm_dev. 13281da177e4SLinus Torvalds */ 13291da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev, 13301da177e4SLinus Torvalds action_fn action) 13311da177e4SLinus Torvalds { 133219040c0bSMike Snitzer int r = -EINVAL; 13331da177e4SLinus Torvalds struct pgpath *pgpath; 13341da177e4SLinus Torvalds struct priority_group *pg; 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 13371da177e4SLinus Torvalds list_for_each_entry(pgpath, &pg->pgpaths, list) { 13381da177e4SLinus Torvalds if (pgpath->path.dev == dev) 13391da177e4SLinus Torvalds r = action(pgpath); 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds } 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds return r; 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds 13461da177e4SLinus Torvalds /* 13471da177e4SLinus Torvalds * Temporarily try to avoid having to use the specified PG 13481da177e4SLinus Torvalds */ 13491da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg, 1350be7d31ccSMike Snitzer bool bypassed) 13511da177e4SLinus Torvalds { 13521da177e4SLinus Torvalds unsigned long flags; 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13551da177e4SLinus Torvalds 13561da177e4SLinus Torvalds pg->bypassed = bypassed; 13571da177e4SLinus Torvalds m->current_pgpath = NULL; 13581da177e4SLinus Torvalds m->current_pg = NULL; 13591da177e4SLinus Torvalds 13601da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13611da177e4SLinus Torvalds 1362fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13631da177e4SLinus Torvalds } 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds /* 13661da177e4SLinus Torvalds * Switch to using the specified PG from the next I/O that gets mapped 13671da177e4SLinus Torvalds */ 13681da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr) 13691da177e4SLinus Torvalds { 13701da177e4SLinus Torvalds struct priority_group *pg; 13711da177e4SLinus Torvalds unsigned pgnum; 13721da177e4SLinus Torvalds unsigned long flags; 137331998ef1SMikulas Patocka char dummy; 13741da177e4SLinus Torvalds 137531998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 1376cc5bd925Stang.junhui !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) { 13771da177e4SLinus Torvalds DMWARN("invalid PG number supplied to switch_pg_num"); 13781da177e4SLinus Torvalds return -EINVAL; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13821da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 1383be7d31ccSMike Snitzer pg->bypassed = false; 13841da177e4SLinus Torvalds if (--pgnum) 13851da177e4SLinus Torvalds continue; 13861da177e4SLinus Torvalds 13871da177e4SLinus Torvalds m->current_pgpath = NULL; 13881da177e4SLinus Torvalds m->current_pg = NULL; 13891da177e4SLinus Torvalds m->next_pg = pg; 13901da177e4SLinus Torvalds } 13911da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13921da177e4SLinus Torvalds 1393fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13941da177e4SLinus Torvalds return 0; 13951da177e4SLinus Torvalds } 13961da177e4SLinus Torvalds 13971da177e4SLinus Torvalds /* 13981da177e4SLinus Torvalds * Set/clear bypassed status of a PG. 13991da177e4SLinus Torvalds * PGs are numbered upwards from 1 in the order they were declared. 14001da177e4SLinus Torvalds */ 1401be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed) 14021da177e4SLinus Torvalds { 14031da177e4SLinus Torvalds struct priority_group *pg; 14041da177e4SLinus Torvalds unsigned pgnum; 140531998ef1SMikulas Patocka char dummy; 14061da177e4SLinus Torvalds 140731998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 1408cc5bd925Stang.junhui !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) { 14091da177e4SLinus Torvalds DMWARN("invalid PG number supplied to bypass_pg"); 14101da177e4SLinus Torvalds return -EINVAL; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 14141da177e4SLinus Torvalds if (!--pgnum) 14151da177e4SLinus Torvalds break; 14161da177e4SLinus Torvalds } 14171da177e4SLinus Torvalds 14181da177e4SLinus Torvalds bypass_pg(m, pg, bypassed); 14191da177e4SLinus Torvalds return 0; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds 14221da177e4SLinus Torvalds /* 1423c9e45581SDave Wysochanski * Should we retry pg_init immediately? 1424c9e45581SDave Wysochanski */ 1425be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath) 1426c9e45581SDave Wysochanski { 1427c9e45581SDave Wysochanski unsigned long flags; 1428be7d31ccSMike Snitzer bool limit_reached = false; 1429c9e45581SDave Wysochanski 1430c9e45581SDave Wysochanski spin_lock_irqsave(&m->lock, flags); 1431c9e45581SDave Wysochanski 143291e968aaSMike Snitzer if (atomic_read(&m->pg_init_count) <= m->pg_init_retries && 143391e968aaSMike Snitzer !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 1434518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 1435c9e45581SDave Wysochanski else 1436be7d31ccSMike Snitzer limit_reached = true; 1437c9e45581SDave Wysochanski 1438c9e45581SDave Wysochanski spin_unlock_irqrestore(&m->lock, flags); 1439c9e45581SDave Wysochanski 1440c9e45581SDave Wysochanski return limit_reached; 1441c9e45581SDave Wysochanski } 1442c9e45581SDave Wysochanski 14433ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors) 1444cfae5c9bSChandra Seetharaman { 144583c0d5d5SMoger, Babu struct pgpath *pgpath = data; 1446cfae5c9bSChandra Seetharaman struct priority_group *pg = pgpath->pg; 1447cfae5c9bSChandra Seetharaman struct multipath *m = pg->m; 1448cfae5c9bSChandra Seetharaman unsigned long flags; 1449be7d31ccSMike Snitzer bool delay_retry = false; 1450cfae5c9bSChandra Seetharaman 1451cfae5c9bSChandra Seetharaman /* device or driver problems */ 1452cfae5c9bSChandra Seetharaman switch (errors) { 1453cfae5c9bSChandra Seetharaman case SCSI_DH_OK: 1454cfae5c9bSChandra Seetharaman break; 1455cfae5c9bSChandra Seetharaman case SCSI_DH_NOSYS: 1456cfae5c9bSChandra Seetharaman if (!m->hw_handler_name) { 1457cfae5c9bSChandra Seetharaman errors = 0; 1458cfae5c9bSChandra Seetharaman break; 1459cfae5c9bSChandra Seetharaman } 1460f7b934c8SMoger, Babu DMERR("Could not failover the device: Handler scsi_dh_%s " 1461f7b934c8SMoger, Babu "Error %d.", m->hw_handler_name, errors); 1462cfae5c9bSChandra Seetharaman /* 1463cfae5c9bSChandra Seetharaman * Fail path for now, so we do not ping pong 1464cfae5c9bSChandra Seetharaman */ 1465cfae5c9bSChandra Seetharaman fail_path(pgpath); 1466cfae5c9bSChandra Seetharaman break; 1467cfae5c9bSChandra Seetharaman case SCSI_DH_DEV_TEMP_BUSY: 1468cfae5c9bSChandra Seetharaman /* 1469cfae5c9bSChandra Seetharaman * Probably doing something like FW upgrade on the 1470cfae5c9bSChandra Seetharaman * controller so try the other pg. 1471cfae5c9bSChandra Seetharaman */ 1472be7d31ccSMike Snitzer bypass_pg(m, pg, true); 1473cfae5c9bSChandra Seetharaman break; 1474cfae5c9bSChandra Seetharaman case SCSI_DH_RETRY: 14754e2d19e4SChandra Seetharaman /* Wait before retrying. */ 14764e2d19e4SChandra Seetharaman delay_retry = 1; 14777b06e09aSBart Van Assche /* fall through */ 1478cfae5c9bSChandra Seetharaman case SCSI_DH_IMM_RETRY: 1479cfae5c9bSChandra Seetharaman case SCSI_DH_RES_TEMP_UNAVAIL: 1480cfae5c9bSChandra Seetharaman if (pg_init_limit_reached(m, pgpath)) 1481cfae5c9bSChandra Seetharaman fail_path(pgpath); 1482cfae5c9bSChandra Seetharaman errors = 0; 1483cfae5c9bSChandra Seetharaman break; 1484ec31f3f7SMike Snitzer case SCSI_DH_DEV_OFFLINED: 1485cfae5c9bSChandra Seetharaman default: 1486cfae5c9bSChandra Seetharaman /* 1487cfae5c9bSChandra Seetharaman * We probably do not want to fail the path for a device 1488cfae5c9bSChandra Seetharaman * error, but this is what the old dm did. In future 1489cfae5c9bSChandra Seetharaman * patches we can do more advanced handling. 1490cfae5c9bSChandra Seetharaman */ 1491cfae5c9bSChandra Seetharaman fail_path(pgpath); 1492cfae5c9bSChandra Seetharaman } 1493cfae5c9bSChandra Seetharaman 1494cfae5c9bSChandra Seetharaman spin_lock_irqsave(&m->lock, flags); 1495cfae5c9bSChandra Seetharaman if (errors) { 1496e54f77ddSChandra Seetharaman if (pgpath == m->current_pgpath) { 1497cfae5c9bSChandra Seetharaman DMERR("Could not failover device. Error %d.", errors); 1498cfae5c9bSChandra Seetharaman m->current_pgpath = NULL; 1499cfae5c9bSChandra Seetharaman m->current_pg = NULL; 1500e54f77ddSChandra Seetharaman } 1501518257b1SMike Snitzer } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 1502be7d31ccSMike Snitzer pg->bypassed = false; 1503cfae5c9bSChandra Seetharaman 150491e968aaSMike Snitzer if (atomic_dec_return(&m->pg_init_in_progress) > 0) 1505d0259bf0SKiyoshi Ueda /* Activations of other paths are still on going */ 1506d0259bf0SKiyoshi Ueda goto out; 1507d0259bf0SKiyoshi Ueda 1508518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 1509518257b1SMike Snitzer if (delay_retry) 1510518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1511518257b1SMike Snitzer else 1512518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1513518257b1SMike Snitzer 15143e9f1be1SHannes Reinecke if (__pg_init_all_paths(m)) 15153e9f1be1SHannes Reinecke goto out; 15163e9f1be1SHannes Reinecke } 1517518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 1518d0259bf0SKiyoshi Ueda 15197e48c768SMike Snitzer process_queued_io_list(m); 152076e33fe4SMike Snitzer 15212bded7bdSKiyoshi Ueda /* 15222bded7bdSKiyoshi Ueda * Wake up any thread waiting to suspend. 15232bded7bdSKiyoshi Ueda */ 15242bded7bdSKiyoshi Ueda wake_up(&m->pg_init_wait); 15252bded7bdSKiyoshi Ueda 1526d0259bf0SKiyoshi Ueda out: 1527cfae5c9bSChandra Seetharaman spin_unlock_irqrestore(&m->lock, flags); 1528cfae5c9bSChandra Seetharaman } 1529cfae5c9bSChandra Seetharaman 153089bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath) 1531bab7cfc7SChandra Seetharaman { 1532f10e06b7SMike Snitzer struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1533bab7cfc7SChandra Seetharaman 1534f10e06b7SMike Snitzer if (pgpath->is_active && !blk_queue_dying(q)) 1535f10e06b7SMike Snitzer scsi_dh_activate(q, pg_init_done, pgpath); 15363a017509SHannes Reinecke else 15373a017509SHannes Reinecke pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED); 1538bab7cfc7SChandra Seetharaman } 1539bab7cfc7SChandra Seetharaman 154089bfce76SBart Van Assche static void activate_path_work(struct work_struct *work) 154189bfce76SBart Van Assche { 154289bfce76SBart Van Assche struct pgpath *pgpath = 154389bfce76SBart Van Assche container_of(work, struct pgpath, activate_path.work); 154489bfce76SBart Van Assche 154589bfce76SBart Van Assche activate_or_offline_path(pgpath); 154689bfce76SBart Van Assche } 154789bfce76SBart Van Assche 1548b79f10eeSChristoph Hellwig static int multipath_end_io(struct dm_target *ti, struct request *clone, 15492a842acaSChristoph Hellwig blk_status_t error, union map_info *map_context) 15501da177e4SLinus Torvalds { 1551b79f10eeSChristoph Hellwig struct dm_mpath_io *mpio = get_mpio(map_context); 1552b79f10eeSChristoph Hellwig struct pgpath *pgpath = mpio->pgpath; 15537ed8578aSChristoph Hellwig int r = DM_ENDIO_DONE; 1554b79f10eeSChristoph Hellwig 1555f40c67f0SKiyoshi Ueda /* 1556f40c67f0SKiyoshi Ueda * We don't queue any clone request inside the multipath target 1557f40c67f0SKiyoshi Ueda * during end I/O handling, since those clone requests don't have 1558f40c67f0SKiyoshi Ueda * bio clones. If we queue them inside the multipath target, 1559f40c67f0SKiyoshi Ueda * we need to make bio clones, that requires memory allocation. 15604cc96131SMike Snitzer * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests 1561f40c67f0SKiyoshi Ueda * don't have bio clones.) 1562f40c67f0SKiyoshi Ueda * Instead of queueing the clone request here, we queue the original 1563f40c67f0SKiyoshi Ueda * request into dm core, which will remake a clone request and 1564f40c67f0SKiyoshi Ueda * clone bios for it and resubmit it later. 1565f40c67f0SKiyoshi Ueda */ 1566a1275677SKeith Busch if (error && blk_path_error(error)) { 1567b79f10eeSChristoph Hellwig struct multipath *m = ti->private; 15681da177e4SLinus Torvalds 1569ac514ffcSMike Snitzer if (error == BLK_STS_RESOURCE) 1570ac514ffcSMike Snitzer r = DM_ENDIO_DELAY_REQUEUE; 1571ac514ffcSMike Snitzer else 15727ed8578aSChristoph Hellwig r = DM_ENDIO_REQUEUE; 15731da177e4SLinus Torvalds 1574b79f10eeSChristoph Hellwig if (pgpath) 1575b79f10eeSChristoph Hellwig fail_path(pgpath); 15761da177e4SLinus Torvalds 1577ca5beb76SBart Van Assche if (atomic_read(&m->nr_valid_paths) == 0 && 1578c1fd0abeSMike Snitzer !must_push_back_rq(m)) { 15792a842acaSChristoph Hellwig if (error == BLK_STS_IOERR) 158018a482f5SChristoph Hellwig dm_report_EIO(m); 15817ed8578aSChristoph Hellwig /* complete with the original error */ 15827ed8578aSChristoph Hellwig r = DM_ENDIO_DONE; 15837ed8578aSChristoph Hellwig } 15841da177e4SLinus Torvalds } 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds if (pgpath) { 1587b79f10eeSChristoph Hellwig struct path_selector *ps = &pgpath->pg->ps; 1588b79f10eeSChristoph Hellwig 15891da177e4SLinus Torvalds if (ps->type->end_io) 159002ab823fSKiyoshi Ueda ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); 15911da177e4SLinus Torvalds } 15921da177e4SLinus Torvalds 15937ed8578aSChristoph Hellwig return r; 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds 15964e4cbee9SChristoph Hellwig static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, 15974e4cbee9SChristoph Hellwig blk_status_t *error) 159876e33fe4SMike Snitzer { 159914ef1e48SChristoph Hellwig struct multipath *m = ti->private; 160014ef1e48SChristoph Hellwig struct dm_mpath_io *mpio = get_mpio_from_bio(clone); 160114ef1e48SChristoph Hellwig struct pgpath *pgpath = mpio->pgpath; 160276e33fe4SMike Snitzer unsigned long flags; 16031be56909SChristoph Hellwig int r = DM_ENDIO_DONE; 160476e33fe4SMike Snitzer 1605a1275677SKeith Busch if (!*error || !blk_path_error(*error)) 160614ef1e48SChristoph Hellwig goto done; 160776e33fe4SMike Snitzer 160814ef1e48SChristoph Hellwig if (pgpath) 160914ef1e48SChristoph Hellwig fail_path(pgpath); 161076e33fe4SMike Snitzer 1611ca5beb76SBart Van Assche if (atomic_read(&m->nr_valid_paths) == 0 && 161218a482f5SChristoph Hellwig !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 1613c1fd0abeSMike Snitzer if (must_push_back_bio(m)) { 1614c1fd0abeSMike Snitzer r = DM_ENDIO_REQUEUE; 1615c1fd0abeSMike Snitzer } else { 161618a482f5SChristoph Hellwig dm_report_EIO(m); 16174e4cbee9SChristoph Hellwig *error = BLK_STS_IOERR; 1618c1fd0abeSMike Snitzer } 161914ef1e48SChristoph Hellwig goto done; 162018a482f5SChristoph Hellwig } 162176e33fe4SMike Snitzer 162276e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 162376e33fe4SMike Snitzer bio_list_add(&m->queued_bios, clone); 162476e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 162576e33fe4SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) 162676e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 162776e33fe4SMike Snitzer 16281be56909SChristoph Hellwig r = DM_ENDIO_INCOMPLETE; 162914ef1e48SChristoph Hellwig done: 163076e33fe4SMike Snitzer if (pgpath) { 163114ef1e48SChristoph Hellwig struct path_selector *ps = &pgpath->pg->ps; 163214ef1e48SChristoph Hellwig 163376e33fe4SMike Snitzer if (ps->type->end_io) 163476e33fe4SMike Snitzer ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); 163576e33fe4SMike Snitzer } 163676e33fe4SMike Snitzer 16371be56909SChristoph Hellwig return r; 163876e33fe4SMike Snitzer } 163976e33fe4SMike Snitzer 16401da177e4SLinus Torvalds /* 16411da177e4SLinus Torvalds * Suspend can't complete until all the I/O is processed so if 1642436d4108SAlasdair G Kergon * the last path fails we must error any remaining I/O. 1643436d4108SAlasdair G Kergon * Note that if the freeze_bdev fails while suspending, the 1644436d4108SAlasdair G Kergon * queue_if_no_path state is lost - userspace should reset it. 16451da177e4SLinus Torvalds */ 16461da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti) 16471da177e4SLinus Torvalds { 16487943bd6dSMike Snitzer struct multipath *m = ti->private; 16491da177e4SLinus Torvalds 1650be7d31ccSMike Snitzer queue_if_no_path(m, false, true); 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds 16536df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti) 16546df400abSKiyoshi Ueda { 16556380f26fSMike Anderson struct multipath *m = ti->private; 16566380f26fSMike Anderson 16576380f26fSMike Anderson mutex_lock(&m->work_mutex); 16582bded7bdSKiyoshi Ueda flush_multipath_work(m); 16596380f26fSMike Anderson mutex_unlock(&m->work_mutex); 16606df400abSKiyoshi Ueda } 16616df400abSKiyoshi Ueda 1662436d4108SAlasdair G Kergon /* 1663436d4108SAlasdair G Kergon * Restore the queue_if_no_path setting. 1664436d4108SAlasdair G Kergon */ 16651da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti) 16661da177e4SLinus Torvalds { 16677943bd6dSMike Snitzer struct multipath *m = ti->private; 16681814f2e3SMike Snitzer unsigned long flags; 16691da177e4SLinus Torvalds 16701814f2e3SMike Snitzer spin_lock_irqsave(&m->lock, flags); 16715307e2adSLukas Wunner assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, 16725307e2adSLukas Wunner test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)); 16731814f2e3SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 16741da177e4SLinus Torvalds } 16751da177e4SLinus Torvalds 16761da177e4SLinus Torvalds /* 16771da177e4SLinus Torvalds * Info output has the following format: 16781da177e4SLinus Torvalds * num_multipath_feature_args [multipath_feature_args]* 16791da177e4SLinus Torvalds * num_handler_status_args [handler_status_args]* 16801da177e4SLinus Torvalds * num_groups init_group_number 16811da177e4SLinus Torvalds * [A|D|E num_ps_status_args [ps_status_args]* 16821da177e4SLinus Torvalds * num_paths num_selector_args 16831da177e4SLinus Torvalds * [path_dev A|F fail_count [selector_args]* ]+ ]+ 16841da177e4SLinus Torvalds * 16851da177e4SLinus Torvalds * Table output has the following format (identical to the constructor string): 16861da177e4SLinus Torvalds * num_feature_args [features_args]* 16871da177e4SLinus Torvalds * num_handler_args hw_handler [hw_handler_args]* 16881da177e4SLinus Torvalds * num_groups init_group_number 16891da177e4SLinus Torvalds * [priority selector-name num_ps_args [ps_args]* 16901da177e4SLinus Torvalds * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ 16911da177e4SLinus Torvalds */ 1692fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type, 16931f4e0ff0SAlasdair G Kergon unsigned status_flags, char *result, unsigned maxlen) 16941da177e4SLinus Torvalds { 16951da177e4SLinus Torvalds int sz = 0; 16961da177e4SLinus Torvalds unsigned long flags; 16977943bd6dSMike Snitzer struct multipath *m = ti->private; 16981da177e4SLinus Torvalds struct priority_group *pg; 16991da177e4SLinus Torvalds struct pgpath *p; 17001da177e4SLinus Torvalds unsigned pg_num; 17011da177e4SLinus Torvalds char state; 17021da177e4SLinus Torvalds 17031da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds /* Features */ 17061da177e4SLinus Torvalds if (type == STATUSTYPE_INFO) 170791e968aaSMike Snitzer DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags), 170891e968aaSMike Snitzer atomic_read(&m->pg_init_count)); 1709c9e45581SDave Wysochanski else { 1710518257b1SMike Snitzer DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) + 17114e2d19e4SChandra Seetharaman (m->pg_init_retries > 0) * 2 + 1712a58a935dSMike Snitzer (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 + 1713e83068a5SMike Snitzer test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) + 1714e83068a5SMike Snitzer (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2); 1715e83068a5SMike Snitzer 1716518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 1717c9e45581SDave Wysochanski DMEMIT("queue_if_no_path "); 1718c9e45581SDave Wysochanski if (m->pg_init_retries) 1719c9e45581SDave Wysochanski DMEMIT("pg_init_retries %u ", m->pg_init_retries); 17204e2d19e4SChandra Seetharaman if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) 17214e2d19e4SChandra Seetharaman DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs); 1722518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) 1723a58a935dSMike Snitzer DMEMIT("retain_attached_hw_handler "); 1724e83068a5SMike Snitzer if (m->queue_mode != DM_TYPE_REQUEST_BASED) { 1725e83068a5SMike Snitzer switch(m->queue_mode) { 1726e83068a5SMike Snitzer case DM_TYPE_BIO_BASED: 1727e83068a5SMike Snitzer DMEMIT("queue_mode bio "); 1728e83068a5SMike Snitzer break; 1729e83068a5SMike Snitzer case DM_TYPE_MQ_REQUEST_BASED: 1730e83068a5SMike Snitzer DMEMIT("queue_mode mq "); 1731e83068a5SMike Snitzer break; 17327e0d574fSBart Van Assche default: 17337e0d574fSBart Van Assche WARN_ON_ONCE(true); 17347e0d574fSBart Van Assche break; 1735e83068a5SMike Snitzer } 1736e83068a5SMike Snitzer } 1737c9e45581SDave Wysochanski } 17381da177e4SLinus Torvalds 1739cfae5c9bSChandra Seetharaman if (!m->hw_handler_name || type == STATUSTYPE_INFO) 17401da177e4SLinus Torvalds DMEMIT("0 "); 17411da177e4SLinus Torvalds else 1742cfae5c9bSChandra Seetharaman DMEMIT("1 %s ", m->hw_handler_name); 17431da177e4SLinus Torvalds 17441da177e4SLinus Torvalds DMEMIT("%u ", m->nr_priority_groups); 17451da177e4SLinus Torvalds 17461da177e4SLinus Torvalds if (m->next_pg) 17471da177e4SLinus Torvalds pg_num = m->next_pg->pg_num; 17481da177e4SLinus Torvalds else if (m->current_pg) 17491da177e4SLinus Torvalds pg_num = m->current_pg->pg_num; 17501da177e4SLinus Torvalds else 1751a490a07aSMike Snitzer pg_num = (m->nr_priority_groups ? 1 : 0); 17521da177e4SLinus Torvalds 17531da177e4SLinus Torvalds DMEMIT("%u ", pg_num); 17541da177e4SLinus Torvalds 17551da177e4SLinus Torvalds switch (type) { 17561da177e4SLinus Torvalds case STATUSTYPE_INFO: 17571da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 17581da177e4SLinus Torvalds if (pg->bypassed) 17591da177e4SLinus Torvalds state = 'D'; /* Disabled */ 17601da177e4SLinus Torvalds else if (pg == m->current_pg) 17611da177e4SLinus Torvalds state = 'A'; /* Currently Active */ 17621da177e4SLinus Torvalds else 17631da177e4SLinus Torvalds state = 'E'; /* Enabled */ 17641da177e4SLinus Torvalds 17651da177e4SLinus Torvalds DMEMIT("%c ", state); 17661da177e4SLinus Torvalds 17671da177e4SLinus Torvalds if (pg->ps.type->status) 17681da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 17691da177e4SLinus Torvalds result + sz, 17701da177e4SLinus Torvalds maxlen - sz); 17711da177e4SLinus Torvalds else 17721da177e4SLinus Torvalds DMEMIT("0 "); 17731da177e4SLinus Torvalds 17741da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 17751da177e4SLinus Torvalds pg->ps.type->info_args); 17761da177e4SLinus Torvalds 17771da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 17781da177e4SLinus Torvalds DMEMIT("%s %s %u ", p->path.dev->name, 17796680073dSKiyoshi Ueda p->is_active ? "A" : "F", 17801da177e4SLinus Torvalds p->fail_count); 17811da177e4SLinus Torvalds if (pg->ps.type->status) 17821da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 17831da177e4SLinus Torvalds &p->path, type, result + sz, 17841da177e4SLinus Torvalds maxlen - sz); 17851da177e4SLinus Torvalds } 17861da177e4SLinus Torvalds } 17871da177e4SLinus Torvalds break; 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds case STATUSTYPE_TABLE: 17901da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 17911da177e4SLinus Torvalds DMEMIT("%s ", pg->ps.type->name); 17921da177e4SLinus Torvalds 17931da177e4SLinus Torvalds if (pg->ps.type->status) 17941da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 17951da177e4SLinus Torvalds result + sz, 17961da177e4SLinus Torvalds maxlen - sz); 17971da177e4SLinus Torvalds else 17981da177e4SLinus Torvalds DMEMIT("0 "); 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 18011da177e4SLinus Torvalds pg->ps.type->table_args); 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 18041da177e4SLinus Torvalds DMEMIT("%s ", p->path.dev->name); 18051da177e4SLinus Torvalds if (pg->ps.type->status) 18061da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 18071da177e4SLinus Torvalds &p->path, type, result + sz, 18081da177e4SLinus Torvalds maxlen - sz); 18091da177e4SLinus Torvalds } 18101da177e4SLinus Torvalds } 18111da177e4SLinus Torvalds break; 18121da177e4SLinus Torvalds } 18131da177e4SLinus Torvalds 18141da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 18151da177e4SLinus Torvalds } 18161da177e4SLinus Torvalds 18171eb5fa84SMike Snitzer static int multipath_message(struct dm_target *ti, unsigned argc, char **argv, 18181eb5fa84SMike Snitzer char *result, unsigned maxlen) 18191da177e4SLinus Torvalds { 18206380f26fSMike Anderson int r = -EINVAL; 18211da177e4SLinus Torvalds struct dm_dev *dev; 18227943bd6dSMike Snitzer struct multipath *m = ti->private; 18231da177e4SLinus Torvalds action_fn action; 18241da177e4SLinus Torvalds 18256380f26fSMike Anderson mutex_lock(&m->work_mutex); 18266380f26fSMike Anderson 1827c2f3d24bSKiyoshi Ueda if (dm_suspended(ti)) { 1828c2f3d24bSKiyoshi Ueda r = -EBUSY; 1829c2f3d24bSKiyoshi Ueda goto out; 1830c2f3d24bSKiyoshi Ueda } 1831c2f3d24bSKiyoshi Ueda 18321da177e4SLinus Torvalds if (argc == 1) { 1833498f0103SMike Snitzer if (!strcasecmp(argv[0], "queue_if_no_path")) { 1834be7d31ccSMike Snitzer r = queue_if_no_path(m, true, false); 18356380f26fSMike Anderson goto out; 1836498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "fail_if_no_path")) { 1837be7d31ccSMike Snitzer r = queue_if_no_path(m, false, false); 18386380f26fSMike Anderson goto out; 18396380f26fSMike Anderson } 18401da177e4SLinus Torvalds } 18411da177e4SLinus Torvalds 18426380f26fSMike Anderson if (argc != 2) { 1843a356e426SJose Castillo DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc); 18446380f26fSMike Anderson goto out; 18456380f26fSMike Anderson } 18461da177e4SLinus Torvalds 1847498f0103SMike Snitzer if (!strcasecmp(argv[0], "disable_group")) { 1848be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], true); 18496380f26fSMike Anderson goto out; 1850498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "enable_group")) { 1851be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], false); 18526380f26fSMike Anderson goto out; 1853498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "switch_group")) { 18546380f26fSMike Anderson r = switch_pg_num(m, argv[1]); 18556380f26fSMike Anderson goto out; 1856498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "reinstate_path")) 18571da177e4SLinus Torvalds action = reinstate_path; 1858498f0103SMike Snitzer else if (!strcasecmp(argv[0], "fail_path")) 18591da177e4SLinus Torvalds action = fail_path; 18606380f26fSMike Anderson else { 1861a356e426SJose Castillo DMWARN("Unrecognised multipath message received: %s", argv[0]); 18626380f26fSMike Anderson goto out; 18636380f26fSMike Anderson } 18641da177e4SLinus Torvalds 18658215d6ecSNikanth Karthikesan r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev); 18661da177e4SLinus Torvalds if (r) { 186772d94861SAlasdair G Kergon DMWARN("message: error getting device %s", 18681da177e4SLinus Torvalds argv[1]); 18696380f26fSMike Anderson goto out; 18701da177e4SLinus Torvalds } 18711da177e4SLinus Torvalds 18721da177e4SLinus Torvalds r = action_dev(m, dev, action); 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds dm_put_device(ti, dev); 18751da177e4SLinus Torvalds 18766380f26fSMike Anderson out: 18776380f26fSMike Anderson mutex_unlock(&m->work_mutex); 18781da177e4SLinus Torvalds return r; 18791da177e4SLinus Torvalds } 18801da177e4SLinus Torvalds 1881e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti, 18825bd5e8d8SMike Snitzer struct block_device **bdev) 18839af4aa30SMilan Broz { 188435991652SMikulas Patocka struct multipath *m = ti->private; 18852da1610aSMike Snitzer struct pgpath *current_pgpath; 188635991652SMikulas Patocka int r; 188735991652SMikulas Patocka 1888506458efSWill Deacon current_pgpath = READ_ONCE(m->current_pgpath); 18892da1610aSMike Snitzer if (!current_pgpath) 18902da1610aSMike Snitzer current_pgpath = choose_pgpath(m, 0); 18919af4aa30SMilan Broz 18922da1610aSMike Snitzer if (current_pgpath) { 1893518257b1SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) { 18942da1610aSMike Snitzer *bdev = current_pgpath->path.dev->bdev; 189543e43c9eSJunichi Nomura r = 0; 189643e43c9eSJunichi Nomura } else { 189743e43c9eSJunichi Nomura /* pg_init has not started or completed */ 18986c182cd8SHannes Reinecke r = -ENOTCONN; 189943e43c9eSJunichi Nomura } 190043e43c9eSJunichi Nomura } else { 190143e43c9eSJunichi Nomura /* No path is available */ 1902518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 190343e43c9eSJunichi Nomura r = -ENOTCONN; 190443e43c9eSJunichi Nomura else 19059af4aa30SMilan Broz r = -EIO; 190643e43c9eSJunichi Nomura } 19079af4aa30SMilan Broz 19085bbbfdf6SJunichi Nomura if (r == -ENOTCONN) { 1909506458efSWill Deacon if (!READ_ONCE(m->current_pg)) { 19103e9f1be1SHannes Reinecke /* Path status changed, redo selection */ 19112da1610aSMike Snitzer (void) choose_pgpath(m, 0); 19123e9f1be1SHannes Reinecke } 1913518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 19142da1610aSMike Snitzer pg_init_all_paths(m); 191563d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 19167e48c768SMike Snitzer process_queued_io_list(m); 19173e9f1be1SHannes Reinecke } 191835991652SMikulas Patocka 1919e56f81e0SChristoph Hellwig /* 1920e56f81e0SChristoph Hellwig * Only pass ioctls through if the device sizes match exactly. 1921e56f81e0SChristoph Hellwig */ 1922e56f81e0SChristoph Hellwig if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT) 1923e56f81e0SChristoph Hellwig return 1; 1924e56f81e0SChristoph Hellwig return r; 19259af4aa30SMilan Broz } 19269af4aa30SMilan Broz 1927af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti, 1928af4874e0SMike Snitzer iterate_devices_callout_fn fn, void *data) 1929af4874e0SMike Snitzer { 1930af4874e0SMike Snitzer struct multipath *m = ti->private; 1931af4874e0SMike Snitzer struct priority_group *pg; 1932af4874e0SMike Snitzer struct pgpath *p; 1933af4874e0SMike Snitzer int ret = 0; 1934af4874e0SMike Snitzer 1935af4874e0SMike Snitzer list_for_each_entry(pg, &m->priority_groups, list) { 1936af4874e0SMike Snitzer list_for_each_entry(p, &pg->pgpaths, list) { 19375dea271bSMike Snitzer ret = fn(ti, p->path.dev, ti->begin, ti->len, data); 1938af4874e0SMike Snitzer if (ret) 1939af4874e0SMike Snitzer goto out; 1940af4874e0SMike Snitzer } 1941af4874e0SMike Snitzer } 1942af4874e0SMike Snitzer 1943af4874e0SMike Snitzer out: 1944af4874e0SMike Snitzer return ret; 1945af4874e0SMike Snitzer } 1946af4874e0SMike Snitzer 19479f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath) 1948f40c67f0SKiyoshi Ueda { 1949f40c67f0SKiyoshi Ueda struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1950f40c67f0SKiyoshi Ueda 195152b09914SMike Snitzer return blk_lld_busy(q); 1952f40c67f0SKiyoshi Ueda } 1953f40c67f0SKiyoshi Ueda 1954f40c67f0SKiyoshi Ueda /* 1955f40c67f0SKiyoshi Ueda * We return "busy", only when we can map I/Os but underlying devices 1956f40c67f0SKiyoshi Ueda * are busy (so even if we map I/Os now, the I/Os will wait on 1957f40c67f0SKiyoshi Ueda * the underlying queue). 1958f40c67f0SKiyoshi Ueda * In other words, if we want to kill I/Os or queue them inside us 1959f40c67f0SKiyoshi Ueda * due to map unavailability, we don't return "busy". Otherwise, 1960f40c67f0SKiyoshi Ueda * dm core won't give us the I/Os and we can't do what we want. 1961f40c67f0SKiyoshi Ueda */ 1962f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti) 1963f40c67f0SKiyoshi Ueda { 1964be7d31ccSMike Snitzer bool busy = false, has_active = false; 1965f40c67f0SKiyoshi Ueda struct multipath *m = ti->private; 19662da1610aSMike Snitzer struct priority_group *pg, *next_pg; 1967f40c67f0SKiyoshi Ueda struct pgpath *pgpath; 1968f40c67f0SKiyoshi Ueda 1969b88efd43SMike Snitzer /* pg_init in progress */ 1970b88efd43SMike Snitzer if (atomic_read(&m->pg_init_in_progress)) 19712da1610aSMike Snitzer return true; 19722da1610aSMike Snitzer 1973b88efd43SMike Snitzer /* no paths available, for blk-mq: rely on IO mapping to delay requeue */ 1974b88efd43SMike Snitzer if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 1975b88efd43SMike Snitzer return (m->queue_mode != DM_TYPE_MQ_REQUEST_BASED); 1976b88efd43SMike Snitzer 1977f40c67f0SKiyoshi Ueda /* Guess which priority_group will be used at next mapping time */ 1978506458efSWill Deacon pg = READ_ONCE(m->current_pg); 1979506458efSWill Deacon next_pg = READ_ONCE(m->next_pg); 1980506458efSWill Deacon if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg)) 19812da1610aSMike Snitzer pg = next_pg; 19822da1610aSMike Snitzer 19832da1610aSMike Snitzer if (!pg) { 1984f40c67f0SKiyoshi Ueda /* 1985f40c67f0SKiyoshi Ueda * We don't know which pg will be used at next mapping time. 19862da1610aSMike Snitzer * We don't call choose_pgpath() here to avoid to trigger 1987f40c67f0SKiyoshi Ueda * pg_init just by busy checking. 1988f40c67f0SKiyoshi Ueda * So we don't know whether underlying devices we will be using 1989f40c67f0SKiyoshi Ueda * at next mapping time are busy or not. Just try mapping. 1990f40c67f0SKiyoshi Ueda */ 19912da1610aSMike Snitzer return busy; 19922da1610aSMike Snitzer } 1993f40c67f0SKiyoshi Ueda 1994f40c67f0SKiyoshi Ueda /* 1995f40c67f0SKiyoshi Ueda * If there is one non-busy active path at least, the path selector 1996f40c67f0SKiyoshi Ueda * will be able to select it. So we consider such a pg as not busy. 1997f40c67f0SKiyoshi Ueda */ 1998be7d31ccSMike Snitzer busy = true; 19992da1610aSMike Snitzer list_for_each_entry(pgpath, &pg->pgpaths, list) { 2000f40c67f0SKiyoshi Ueda if (pgpath->is_active) { 2001be7d31ccSMike Snitzer has_active = true; 20029f54cec5SMike Snitzer if (!pgpath_busy(pgpath)) { 2003be7d31ccSMike Snitzer busy = false; 2004f40c67f0SKiyoshi Ueda break; 2005f40c67f0SKiyoshi Ueda } 2006f40c67f0SKiyoshi Ueda } 20072da1610aSMike Snitzer } 2008f40c67f0SKiyoshi Ueda 20092da1610aSMike Snitzer if (!has_active) { 2010f40c67f0SKiyoshi Ueda /* 2011f40c67f0SKiyoshi Ueda * No active path in this pg, so this pg won't be used and 2012f40c67f0SKiyoshi Ueda * the current_pg will be changed at next mapping time. 2013f40c67f0SKiyoshi Ueda * We need to try mapping to determine it. 2014f40c67f0SKiyoshi Ueda */ 2015be7d31ccSMike Snitzer busy = false; 20162da1610aSMike Snitzer } 2017f40c67f0SKiyoshi Ueda 2018f40c67f0SKiyoshi Ueda return busy; 2019f40c67f0SKiyoshi Ueda } 2020f40c67f0SKiyoshi Ueda 20211da177e4SLinus Torvalds /*----------------------------------------------------------------- 20221da177e4SLinus Torvalds * Module setup 20231da177e4SLinus Torvalds *---------------------------------------------------------------*/ 20241da177e4SLinus Torvalds static struct target_type multipath_target = { 20251da177e4SLinus Torvalds .name = "multipath", 20268c5c1473SSteffen Maier .version = {1, 13, 0}, 20278c5c1473SSteffen Maier .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE | 20288c5c1473SSteffen Maier DM_TARGET_PASSES_INTEGRITY, 20291da177e4SLinus Torvalds .module = THIS_MODULE, 20301da177e4SLinus Torvalds .ctr = multipath_ctr, 20311da177e4SLinus Torvalds .dtr = multipath_dtr, 2032e5863d9aSMike Snitzer .clone_and_map_rq = multipath_clone_and_map, 2033e5863d9aSMike Snitzer .release_clone_rq = multipath_release_clone, 2034f40c67f0SKiyoshi Ueda .rq_end_io = multipath_end_io, 203576e33fe4SMike Snitzer .map = multipath_map_bio, 203676e33fe4SMike Snitzer .end_io = multipath_end_io_bio, 203776e33fe4SMike Snitzer .presuspend = multipath_presuspend, 203876e33fe4SMike Snitzer .postsuspend = multipath_postsuspend, 203976e33fe4SMike Snitzer .resume = multipath_resume, 204076e33fe4SMike Snitzer .status = multipath_status, 204176e33fe4SMike Snitzer .message = multipath_message, 204276e33fe4SMike Snitzer .prepare_ioctl = multipath_prepare_ioctl, 204376e33fe4SMike Snitzer .iterate_devices = multipath_iterate_devices, 204476e33fe4SMike Snitzer .busy = multipath_busy, 204576e33fe4SMike Snitzer }; 204676e33fe4SMike Snitzer 20471da177e4SLinus Torvalds static int __init dm_multipath_init(void) 20481da177e4SLinus Torvalds { 20491da177e4SLinus Torvalds int r; 20501da177e4SLinus Torvalds 20514d4d66abSTejun Heo kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0); 2052c557308eSAlasdair G Kergon if (!kmultipathd) { 20530cd33124SAlasdair G Kergon DMERR("failed to create workqueue kmpathd"); 2054ff658e9cSJohannes Thumshirn r = -ENOMEM; 2055ff658e9cSJohannes Thumshirn goto bad_alloc_kmultipathd; 2056c557308eSAlasdair G Kergon } 2057c557308eSAlasdair G Kergon 2058bab7cfc7SChandra Seetharaman /* 2059bab7cfc7SChandra Seetharaman * A separate workqueue is used to handle the device handlers 2060bab7cfc7SChandra Seetharaman * to avoid overloading existing workqueue. Overloading the 2061bab7cfc7SChandra Seetharaman * old workqueue would also create a bottleneck in the 2062bab7cfc7SChandra Seetharaman * path of the storage hardware device activation. 2063bab7cfc7SChandra Seetharaman */ 20644d4d66abSTejun Heo kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd", 20654d4d66abSTejun Heo WQ_MEM_RECLAIM); 2066bab7cfc7SChandra Seetharaman if (!kmpath_handlerd) { 2067bab7cfc7SChandra Seetharaman DMERR("failed to create workqueue kmpath_handlerd"); 2068ff658e9cSJohannes Thumshirn r = -ENOMEM; 2069ff658e9cSJohannes Thumshirn goto bad_alloc_kmpath_handlerd; 2070bab7cfc7SChandra Seetharaman } 2071bab7cfc7SChandra Seetharaman 20727e6358d2Smonty_pavel@sina.com r = dm_register_target(&multipath_target); 20737e6358d2Smonty_pavel@sina.com if (r < 0) { 20747e6358d2Smonty_pavel@sina.com DMERR("request-based register failed %d", r); 20757e6358d2Smonty_pavel@sina.com r = -EINVAL; 20767e6358d2Smonty_pavel@sina.com goto bad_register_target; 20777e6358d2Smonty_pavel@sina.com } 20787e6358d2Smonty_pavel@sina.com 2079ff658e9cSJohannes Thumshirn return 0; 2080ff658e9cSJohannes Thumshirn 20817e6358d2Smonty_pavel@sina.com bad_register_target: 20827e6358d2Smonty_pavel@sina.com destroy_workqueue(kmpath_handlerd); 2083ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd: 2084ff658e9cSJohannes Thumshirn destroy_workqueue(kmultipathd); 2085ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd: 20861da177e4SLinus Torvalds return r; 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds static void __exit dm_multipath_exit(void) 20901da177e4SLinus Torvalds { 2091bab7cfc7SChandra Seetharaman destroy_workqueue(kmpath_handlerd); 2092c557308eSAlasdair G Kergon destroy_workqueue(kmultipathd); 2093c557308eSAlasdair G Kergon 209410d3bd09SMikulas Patocka dm_unregister_target(&multipath_target); 20951da177e4SLinus Torvalds } 20961da177e4SLinus Torvalds 20971da177e4SLinus Torvalds module_init(dm_multipath_init); 20981da177e4SLinus Torvalds module_exit(dm_multipath_exit); 20991da177e4SLinus Torvalds 21001da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target"); 21011da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); 21021da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2103