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) { 206953923c0SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 2078d47e659SMike Snitzer } else if (m->queue_mode == DM_TYPE_BIO_BASED) { 20876e33fe4SMike Snitzer INIT_WORK(&m->process_queued_bios, process_queued_bios); 20976e33fe4SMike Snitzer /* 21076e33fe4SMike Snitzer * bio-based doesn't support any direct scsi_dh management; 21176e33fe4SMike Snitzer * it just discovers if a scsi_dh is attached. 21276e33fe4SMike Snitzer */ 21376e33fe4SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 21476e33fe4SMike Snitzer } 21576e33fe4SMike Snitzer 216e83068a5SMike Snitzer dm_table_set_type(ti->table, m->queue_mode); 2171da177e4SLinus Torvalds 218c3736674SMike Snitzer /* 219c3736674SMike Snitzer * Init fields that are only used when a scsi_dh is attached 220c3736674SMike Snitzer * - must do this unconditionally (really doesn't hurt non-SCSI uses) 221c3736674SMike Snitzer */ 222c3736674SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 223c3736674SMike Snitzer atomic_set(&m->pg_init_in_progress, 0); 224c3736674SMike Snitzer atomic_set(&m->pg_init_count, 0); 225c3736674SMike Snitzer m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT; 226c3736674SMike Snitzer init_waitqueue_head(&m->pg_init_wait); 227c3736674SMike Snitzer 228e83068a5SMike Snitzer return 0; 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds static void free_multipath(struct multipath *m) 2321da177e4SLinus Torvalds { 2331da177e4SLinus Torvalds struct priority_group *pg, *tmp; 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { 2361da177e4SLinus Torvalds list_del(&pg->list); 2371da177e4SLinus Torvalds free_priority_group(pg, m->ti); 2381da177e4SLinus Torvalds } 2391da177e4SLinus Torvalds 240cfae5c9bSChandra Seetharaman kfree(m->hw_handler_name); 2412bfd2e13SChandra Seetharaman kfree(m->hw_handler_params); 242d5ffebddSMike Snitzer mutex_destroy(&m->work_mutex); 2431da177e4SLinus Torvalds kfree(m); 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2462eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info) 2472eff1924SMike Snitzer { 2482eff1924SMike Snitzer return info->ptr; 2492eff1924SMike Snitzer } 2502eff1924SMike Snitzer 251bf661be1SMike Snitzer static size_t multipath_per_bio_data_size(void) 25276e33fe4SMike Snitzer { 253bf661be1SMike Snitzer return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details); 25476e33fe4SMike Snitzer } 25576e33fe4SMike Snitzer 256bf661be1SMike Snitzer static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio) 257bf661be1SMike Snitzer { 258bf661be1SMike Snitzer return dm_per_bio_data(bio, multipath_per_bio_data_size()); 259bf661be1SMike Snitzer } 260bf661be1SMike Snitzer 261d07a241dSMike Snitzer static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio) 262bf661be1SMike Snitzer { 263bf661be1SMike Snitzer /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */ 264bf661be1SMike Snitzer void *bio_details = mpio + 1; 265bf661be1SMike Snitzer return bio_details; 266bf661be1SMike Snitzer } 267bf661be1SMike Snitzer 26863f6e6fdSMike Snitzer static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p) 26976e33fe4SMike Snitzer { 27076e33fe4SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 271d07a241dSMike Snitzer struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio); 27276e33fe4SMike Snitzer 273d0442f80SMike Snitzer mpio->nr_bytes = bio->bi_iter.bi_size; 274d0442f80SMike Snitzer mpio->pgpath = NULL; 275bf661be1SMike Snitzer *mpio_p = mpio; 276d0442f80SMike Snitzer 277d0442f80SMike Snitzer dm_bio_record(bio_details, bio); 27876e33fe4SMike Snitzer } 27976e33fe4SMike Snitzer 2801da177e4SLinus Torvalds /*----------------------------------------------- 2811da177e4SLinus Torvalds * Path selection 2821da177e4SLinus Torvalds *-----------------------------------------------*/ 2831da177e4SLinus Torvalds 2843e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m) 285fb612642SKiyoshi Ueda { 286fb612642SKiyoshi Ueda struct pgpath *pgpath; 2874e2d19e4SChandra Seetharaman unsigned long pg_init_delay = 0; 288fb612642SKiyoshi Ueda 289b194679fSBart Van Assche lockdep_assert_held(&m->lock); 290b194679fSBart Van Assche 29191e968aaSMike Snitzer if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 2923e9f1be1SHannes Reinecke return 0; 29317f4ff45SHannes Reinecke 29491e968aaSMike Snitzer atomic_inc(&m->pg_init_count); 295518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 2963e9f1be1SHannes Reinecke 2973e9f1be1SHannes Reinecke /* Check here to reset pg_init_required */ 2983e9f1be1SHannes Reinecke if (!m->current_pg) 2993e9f1be1SHannes Reinecke return 0; 3003e9f1be1SHannes Reinecke 301518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags)) 3024e2d19e4SChandra Seetharaman pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ? 3034e2d19e4SChandra Seetharaman m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS); 304fb612642SKiyoshi Ueda list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) { 305fb612642SKiyoshi Ueda /* Skip failed paths */ 306fb612642SKiyoshi Ueda if (!pgpath->is_active) 307fb612642SKiyoshi Ueda continue; 3084e2d19e4SChandra Seetharaman if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path, 3094e2d19e4SChandra Seetharaman pg_init_delay)) 31091e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 311fb612642SKiyoshi Ueda } 31291e968aaSMike Snitzer return atomic_read(&m->pg_init_in_progress); 313fb612642SKiyoshi Ueda } 314fb612642SKiyoshi Ueda 315c1d7ecf7SBart Van Assche static int pg_init_all_paths(struct multipath *m) 3161da177e4SLinus Torvalds { 317c1d7ecf7SBart Van Assche int ret; 3182da1610aSMike Snitzer unsigned long flags; 3192da1610aSMike Snitzer 3202da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 321c1d7ecf7SBart Van Assche ret = __pg_init_all_paths(m); 3222da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 323c1d7ecf7SBart Van Assche 324c1d7ecf7SBart Van Assche return ret; 3252da1610aSMike Snitzer } 3262da1610aSMike Snitzer 3272da1610aSMike Snitzer static void __switch_pg(struct multipath *m, struct priority_group *pg) 3282da1610aSMike Snitzer { 3292da1610aSMike Snitzer m->current_pg = pg; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds /* Must we initialise the PG first, and queue I/O till it's ready? */ 332cfae5c9bSChandra Seetharaman if (m->hw_handler_name) { 333518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 334518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 3351da177e4SLinus Torvalds } else { 336518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 337518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 3381da177e4SLinus Torvalds } 339c9e45581SDave Wysochanski 34091e968aaSMike Snitzer atomic_set(&m->pg_init_count, 0); 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 3432da1610aSMike Snitzer static struct pgpath *choose_path_in_pg(struct multipath *m, 3442da1610aSMike Snitzer struct priority_group *pg, 34502ab823fSKiyoshi Ueda size_t nr_bytes) 3461da177e4SLinus Torvalds { 3472da1610aSMike Snitzer unsigned long flags; 348c922d5f7SJosef "Jeff" Sipek struct dm_path *path; 3492da1610aSMike Snitzer struct pgpath *pgpath; 3501da177e4SLinus Torvalds 35190a4323cSMike Snitzer path = pg->ps.type->select_path(&pg->ps, nr_bytes); 3521da177e4SLinus Torvalds if (!path) 3532da1610aSMike Snitzer return ERR_PTR(-ENXIO); 3541da177e4SLinus Torvalds 3552da1610aSMike Snitzer pgpath = path_to_pgpath(path); 3561da177e4SLinus Torvalds 357506458efSWill Deacon if (unlikely(READ_ONCE(m->current_pg) != pg)) { 3582da1610aSMike Snitzer /* Only update current_pgpath if pg changed */ 3592da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 3602da1610aSMike Snitzer m->current_pgpath = pgpath; 3612da1610aSMike Snitzer __switch_pg(m, pg); 3622da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds 3652da1610aSMike Snitzer return pgpath; 3662da1610aSMike Snitzer } 3672da1610aSMike Snitzer 3682da1610aSMike Snitzer static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes) 3691da177e4SLinus Torvalds { 3702da1610aSMike Snitzer unsigned long flags; 3711da177e4SLinus Torvalds struct priority_group *pg; 3722da1610aSMike Snitzer struct pgpath *pgpath; 373d19a55ccSMike Snitzer unsigned bypassed = 1; 3741da177e4SLinus Torvalds 37591e968aaSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 376518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 3771da177e4SLinus Torvalds goto failed; 3781f271972SBenjamin Marzinski } 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds /* Were we instructed to switch PG? */ 381506458efSWill Deacon if (READ_ONCE(m->next_pg)) { 3822da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 3831da177e4SLinus Torvalds pg = m->next_pg; 3842da1610aSMike Snitzer if (!pg) { 3852da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3862da1610aSMike Snitzer goto check_current_pg; 3872da1610aSMike Snitzer } 3881da177e4SLinus Torvalds m->next_pg = NULL; 3892da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3902da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 3912da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 3922da1610aSMike Snitzer return pgpath; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds /* Don't change PG until it has no remaining paths */ 3962da1610aSMike Snitzer check_current_pg: 397506458efSWill Deacon pg = READ_ONCE(m->current_pg); 3982da1610aSMike Snitzer if (pg) { 3992da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4002da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 4012da1610aSMike Snitzer return pgpath; 4022da1610aSMike Snitzer } 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds /* 4051da177e4SLinus Torvalds * Loop through priority groups until we find a valid path. 4061da177e4SLinus Torvalds * First time we skip PGs marked 'bypassed'. 407f220fd4eSMike Christie * Second time we only try the ones we skipped, but set 408f220fd4eSMike Christie * pg_init_delay_retry so we do not hammer controllers. 4091da177e4SLinus Torvalds */ 4101da177e4SLinus Torvalds do { 4111da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 412d19a55ccSMike Snitzer if (pg->bypassed == !!bypassed) 4131da177e4SLinus Torvalds continue; 4142da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4152da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) { 416f220fd4eSMike Christie if (!bypassed) 417518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 4182da1610aSMike Snitzer return pgpath; 4191da177e4SLinus Torvalds } 420f220fd4eSMike Christie } 4211da177e4SLinus Torvalds } while (bypassed--); 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds failed: 4242da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 4251da177e4SLinus Torvalds m->current_pgpath = NULL; 4261da177e4SLinus Torvalds m->current_pg = NULL; 4272da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4282da1610aSMike Snitzer 4292da1610aSMike Snitzer return NULL; 4301da177e4SLinus Torvalds } 4311da177e4SLinus Torvalds 43245e15720SKiyoshi Ueda /* 43386331f39SBart Van Assche * dm_report_EIO() is a macro instead of a function to make pr_debug() 43486331f39SBart Van Assche * report the function name and line number of the function from which 43586331f39SBart Van Assche * it has been invoked. 43645e15720SKiyoshi Ueda */ 43786331f39SBart Van Assche #define dm_report_EIO(m) \ 43818a482f5SChristoph Hellwig do { \ 43986331f39SBart Van Assche struct mapped_device *md = dm_table_get_md((m)->ti->table); \ 44086331f39SBart Van Assche \ 44186331f39SBart Van Assche pr_debug("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d\n", \ 44286331f39SBart Van Assche dm_device_name(md), \ 44386331f39SBart Van Assche test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \ 44486331f39SBart Van Assche test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \ 44586331f39SBart Van Assche dm_noflush_suspending((m)->ti)); \ 44618a482f5SChristoph Hellwig } while (0) 44745e15720SKiyoshi Ueda 44836fcffccSHannes Reinecke /* 449c1fd0abeSMike Snitzer * Check whether bios must be queued in the device-mapper core rather 450c1fd0abeSMike Snitzer * than here in the target. 451c1fd0abeSMike Snitzer * 452c1fd0abeSMike Snitzer * If MPATHF_QUEUE_IF_NO_PATH and MPATHF_SAVED_QUEUE_IF_NO_PATH hold 453c1fd0abeSMike Snitzer * the same value then we are not between multipath_presuspend() 454c1fd0abeSMike Snitzer * and multipath_resume() calls and we have no need to check 455c1fd0abeSMike Snitzer * for the DMF_NOFLUSH_SUSPENDING flag. 456c1fd0abeSMike Snitzer */ 457c1fd0abeSMike Snitzer static bool __must_push_back(struct multipath *m, unsigned long flags) 458c1fd0abeSMike Snitzer { 459c1fd0abeSMike Snitzer return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) != 460c1fd0abeSMike Snitzer test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &flags)) && 461c1fd0abeSMike Snitzer dm_noflush_suspending(m->ti)); 462c1fd0abeSMike Snitzer } 463c1fd0abeSMike Snitzer 464c1fd0abeSMike Snitzer /* 465c1fd0abeSMike Snitzer * Following functions use READ_ONCE to get atomic access to 466c1fd0abeSMike Snitzer * all m->flags to avoid taking spinlock 467c1fd0abeSMike Snitzer */ 468c1fd0abeSMike Snitzer static bool must_push_back_rq(struct multipath *m) 469c1fd0abeSMike Snitzer { 470c1fd0abeSMike Snitzer unsigned long flags = READ_ONCE(m->flags); 471c1fd0abeSMike Snitzer return test_bit(MPATHF_QUEUE_IF_NO_PATH, &flags) || __must_push_back(m, flags); 472c1fd0abeSMike Snitzer } 473c1fd0abeSMike Snitzer 474c1fd0abeSMike Snitzer static bool must_push_back_bio(struct multipath *m) 475c1fd0abeSMike Snitzer { 476c1fd0abeSMike Snitzer unsigned long flags = READ_ONCE(m->flags); 477c1fd0abeSMike Snitzer return __must_push_back(m, flags); 478c1fd0abeSMike Snitzer } 479c1fd0abeSMike Snitzer 480c1fd0abeSMike Snitzer /* 48176e33fe4SMike Snitzer * Map cloned requests (request-based multipath) 48236fcffccSHannes Reinecke */ 483eb8db831SChristoph Hellwig static int multipath_clone_and_map(struct dm_target *ti, struct request *rq, 484e5863d9aSMike Snitzer union map_info *map_context, 485eb8db831SChristoph Hellwig struct request **__clone) 4861da177e4SLinus Torvalds { 4877943bd6dSMike Snitzer struct multipath *m = ti->private; 488eb8db831SChristoph Hellwig size_t nr_bytes = blk_rq_bytes(rq); 4891da177e4SLinus Torvalds struct pgpath *pgpath; 490f40c67f0SKiyoshi Ueda struct block_device *bdev; 491eb8db831SChristoph Hellwig struct dm_mpath_io *mpio = get_mpio(map_context); 4927083abbbSBart Van Assche struct request_queue *q; 493eb8db831SChristoph Hellwig struct request *clone; 4941da177e4SLinus Torvalds 4951da177e4SLinus Torvalds /* Do we need to select a new pgpath? */ 496506458efSWill Deacon pgpath = READ_ONCE(m->current_pgpath); 4972da1610aSMike Snitzer if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags)) 4982da1610aSMike Snitzer pgpath = choose_pgpath(m, nr_bytes); 4991da177e4SLinus Torvalds 5009bf59a61SMike Snitzer if (!pgpath) { 501c1fd0abeSMike Snitzer if (must_push_back_rq(m)) 502b88efd43SMike Snitzer return DM_MAPIO_DELAY_REQUEUE; 50318a482f5SChristoph Hellwig dm_report_EIO(m); /* Failed */ 504f98e0eb6SChristoph Hellwig return DM_MAPIO_KILL; 505518257b1SMike Snitzer } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) || 506518257b1SMike Snitzer test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 507459b5401SMing Lei pg_init_all_paths(m); 508c1d7ecf7SBart Van Assche return DM_MAPIO_DELAY_REQUEUE; 5099bf59a61SMike Snitzer } 5106afbc01dSMike Snitzer 511e8099177SHannes Reinecke mpio->pgpath = pgpath; 512e8099177SHannes Reinecke mpio->nr_bytes = nr_bytes; 5132eb6e1e3SKeith Busch 5142eb6e1e3SKeith Busch bdev = pgpath->path.dev->bdev; 5157083abbbSBart Van Assche q = bdev_get_queue(bdev); 516ff005a06SChristoph Hellwig clone = blk_get_request(q, rq->cmd_flags | REQ_NOMERGE, 517ff005a06SChristoph Hellwig BLK_MQ_REQ_NOWAIT); 5186599c84eSBart Van Assche if (IS_ERR(clone)) { 5196599c84eSBart Van Assche /* EBUSY, ENODEV or EWOULDBLOCK: requeue */ 520848b8aefSMike Snitzer if (blk_queue_dying(q)) { 5217083abbbSBart Van Assche atomic_inc(&m->pg_init_in_progress); 5227083abbbSBart Van Assche activate_or_offline_path(pgpath); 523050af08fSMing Lei return DM_MAPIO_DELAY_REQUEUE; 5247083abbbSBart Van Assche } 525050af08fSMing Lei 526050af08fSMing Lei /* 527050af08fSMing Lei * blk-mq's SCHED_RESTART can cover this requeue, so we 528050af08fSMing Lei * needn't deal with it by DELAY_REQUEUE. More importantly, 529050af08fSMing Lei * we have to return DM_MAPIO_REQUEUE so that blk-mq can 530050af08fSMing Lei * get the queue busy feedback (via BLK_STS_RESOURCE), 531050af08fSMing Lei * otherwise I/O merging can suffer. 532050af08fSMing Lei */ 533050af08fSMing Lei return DM_MAPIO_REQUEUE; 5344c6dd53dSMike Snitzer } 5356599c84eSBart Van Assche clone->bio = clone->biotail = NULL; 5366599c84eSBart Van Assche clone->rq_disk = bdev->bd_disk; 5376599c84eSBart Van Assche clone->cmd_flags |= REQ_FAILFAST_TRANSPORT; 5386599c84eSBart Van Assche *__clone = clone; 5392eb6e1e3SKeith Busch 540e8099177SHannes Reinecke if (pgpath->pg->ps.type->start_io) 541e8099177SHannes Reinecke pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 542e8099177SHannes Reinecke &pgpath->path, 543e8099177SHannes Reinecke nr_bytes); 5442eb6e1e3SKeith Busch return DM_MAPIO_REMAPPED; 5451da177e4SLinus Torvalds } 5461da177e4SLinus Torvalds 5475de719e3SYufen Yu static void multipath_release_clone(struct request *clone, 5485de719e3SYufen Yu union map_info *map_context) 549e5863d9aSMike Snitzer { 5505de719e3SYufen Yu if (unlikely(map_context)) { 5515de719e3SYufen Yu /* 5525de719e3SYufen Yu * non-NULL map_context means caller is still map 5535de719e3SYufen Yu * method; must undo multipath_clone_and_map() 5545de719e3SYufen Yu */ 5555de719e3SYufen Yu struct dm_mpath_io *mpio = get_mpio(map_context); 5565de719e3SYufen Yu struct pgpath *pgpath = mpio->pgpath; 5575de719e3SYufen Yu 5585de719e3SYufen Yu if (pgpath && pgpath->pg->ps.type->end_io) 5595de719e3SYufen Yu pgpath->pg->ps.type->end_io(&pgpath->pg->ps, 5605de719e3SYufen Yu &pgpath->path, 5615de719e3SYufen Yu mpio->nr_bytes); 5625de719e3SYufen Yu } 5635de719e3SYufen Yu 564eb8db831SChristoph Hellwig blk_put_request(clone); 565e5863d9aSMike Snitzer } 566e5863d9aSMike Snitzer 5671da177e4SLinus Torvalds /* 56876e33fe4SMike Snitzer * Map cloned bios (bio-based multipath) 56976e33fe4SMike Snitzer */ 5700001ec56SMike Snitzer 5710001ec56SMike Snitzer static struct pgpath *__map_bio(struct multipath *m, struct bio *bio) 57276e33fe4SMike Snitzer { 57376e33fe4SMike Snitzer struct pgpath *pgpath; 57476e33fe4SMike Snitzer unsigned long flags; 57576e33fe4SMike Snitzer bool queue_io; 57676e33fe4SMike Snitzer 57776e33fe4SMike Snitzer /* Do we need to select a new pgpath? */ 578506458efSWill Deacon pgpath = READ_ONCE(m->current_pgpath); 57976e33fe4SMike Snitzer queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags); 58076e33fe4SMike Snitzer if (!pgpath || !queue_io) 5810001ec56SMike Snitzer pgpath = choose_pgpath(m, bio->bi_iter.bi_size); 58276e33fe4SMike Snitzer 58376e33fe4SMike Snitzer if ((pgpath && queue_io) || 58476e33fe4SMike Snitzer (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) { 58576e33fe4SMike Snitzer /* Queue for the daemon to resubmit */ 58676e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 58776e33fe4SMike Snitzer bio_list_add(&m->queued_bios, bio); 58876e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 589848b8aefSMike Snitzer 59076e33fe4SMike Snitzer /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */ 59176e33fe4SMike Snitzer if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 59276e33fe4SMike Snitzer pg_init_all_paths(m); 59376e33fe4SMike Snitzer else if (!queue_io) 59476e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 5950001ec56SMike Snitzer 5960001ec56SMike Snitzer return ERR_PTR(-EAGAIN); 59776e33fe4SMike Snitzer } 59876e33fe4SMike Snitzer 5990001ec56SMike Snitzer return pgpath; 60076e33fe4SMike Snitzer } 60176e33fe4SMike Snitzer 6020001ec56SMike Snitzer static int __multipath_map_bio(struct multipath *m, struct bio *bio, 6030001ec56SMike Snitzer struct dm_mpath_io *mpio) 6040001ec56SMike Snitzer { 605*dbaf971cSMike Snitzer struct pgpath *pgpath = __map_bio(m, bio); 6060001ec56SMike Snitzer 6070001ec56SMike Snitzer if (IS_ERR(pgpath)) 6080001ec56SMike Snitzer return DM_MAPIO_SUBMITTED; 6090001ec56SMike Snitzer 61076e33fe4SMike Snitzer if (!pgpath) { 611c1fd0abeSMike Snitzer if (must_push_back_bio(m)) 61276e33fe4SMike Snitzer return DM_MAPIO_REQUEUE; 61318a482f5SChristoph Hellwig dm_report_EIO(m); 614846785e6SChristoph Hellwig return DM_MAPIO_KILL; 61576e33fe4SMike Snitzer } 61676e33fe4SMike Snitzer 61776e33fe4SMike Snitzer mpio->pgpath = pgpath; 61876e33fe4SMike Snitzer 6194e4cbee9SChristoph Hellwig bio->bi_status = 0; 62074d46992SChristoph Hellwig bio_set_dev(bio, pgpath->path.dev->bdev); 6211eff9d32SJens Axboe bio->bi_opf |= REQ_FAILFAST_TRANSPORT; 62276e33fe4SMike Snitzer 62376e33fe4SMike Snitzer if (pgpath->pg->ps.type->start_io) 62476e33fe4SMike Snitzer pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 62576e33fe4SMike Snitzer &pgpath->path, 626d0442f80SMike Snitzer mpio->nr_bytes); 62776e33fe4SMike Snitzer return DM_MAPIO_REMAPPED; 62876e33fe4SMike Snitzer } 62976e33fe4SMike Snitzer 63076e33fe4SMike Snitzer static int multipath_map_bio(struct dm_target *ti, struct bio *bio) 63176e33fe4SMike Snitzer { 63276e33fe4SMike Snitzer struct multipath *m = ti->private; 633bf661be1SMike Snitzer struct dm_mpath_io *mpio = NULL; 634bf661be1SMike Snitzer 63563f6e6fdSMike Snitzer multipath_init_per_bio_data(bio, &mpio); 63676e33fe4SMike Snitzer return __multipath_map_bio(m, bio, mpio); 63776e33fe4SMike Snitzer } 63876e33fe4SMike Snitzer 6397e48c768SMike Snitzer static void process_queued_io_list(struct multipath *m) 64076e33fe4SMike Snitzer { 641953923c0SMike Snitzer if (m->queue_mode == DM_TYPE_REQUEST_BASED) 6427e48c768SMike Snitzer dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table)); 6438d47e659SMike Snitzer else if (m->queue_mode == DM_TYPE_BIO_BASED) 64476e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 64576e33fe4SMike Snitzer } 64676e33fe4SMike Snitzer 64776e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work) 64876e33fe4SMike Snitzer { 64976e33fe4SMike Snitzer int r; 65076e33fe4SMike Snitzer unsigned long flags; 65176e33fe4SMike Snitzer struct bio *bio; 65276e33fe4SMike Snitzer struct bio_list bios; 65376e33fe4SMike Snitzer struct blk_plug plug; 65476e33fe4SMike Snitzer struct multipath *m = 65576e33fe4SMike Snitzer container_of(work, struct multipath, process_queued_bios); 65676e33fe4SMike Snitzer 65776e33fe4SMike Snitzer bio_list_init(&bios); 65876e33fe4SMike Snitzer 65976e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 66076e33fe4SMike Snitzer 66176e33fe4SMike Snitzer if (bio_list_empty(&m->queued_bios)) { 66276e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 66376e33fe4SMike Snitzer return; 66476e33fe4SMike Snitzer } 66576e33fe4SMike Snitzer 66676e33fe4SMike Snitzer bio_list_merge(&bios, &m->queued_bios); 66776e33fe4SMike Snitzer bio_list_init(&m->queued_bios); 66876e33fe4SMike Snitzer 66976e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 67076e33fe4SMike Snitzer 67176e33fe4SMike Snitzer blk_start_plug(&plug); 67276e33fe4SMike Snitzer while ((bio = bio_list_pop(&bios))) { 6731836df08SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 6741836df08SMike Snitzer dm_bio_restore(get_bio_details_from_mpio(mpio), bio); 6751836df08SMike Snitzer r = __multipath_map_bio(m, bio, mpio); 676846785e6SChristoph Hellwig switch (r) { 677846785e6SChristoph Hellwig case DM_MAPIO_KILL: 6784e4cbee9SChristoph Hellwig bio->bi_status = BLK_STS_IOERR; 6794e4cbee9SChristoph Hellwig bio_endio(bio); 680047385b3SDan Carpenter break; 681846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE: 6824e4cbee9SChristoph Hellwig bio->bi_status = BLK_STS_DM_REQUEUE; 68376e33fe4SMike Snitzer bio_endio(bio); 684846785e6SChristoph Hellwig break; 685846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED: 68676e33fe4SMike Snitzer generic_make_request(bio); 687846785e6SChristoph Hellwig break; 6888192a0cdSWang Sheng-Hui case DM_MAPIO_SUBMITTED: 6899157c8d3SBart Van Assche break; 6909157c8d3SBart Van Assche default: 6919157c8d3SBart Van Assche WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r); 692846785e6SChristoph Hellwig } 69376e33fe4SMike Snitzer } 69476e33fe4SMike Snitzer blk_finish_plug(&plug); 69576e33fe4SMike Snitzer } 69676e33fe4SMike Snitzer 69776e33fe4SMike Snitzer /* 6981da177e4SLinus Torvalds * If we run out of usable paths, should we queue I/O or error it? 6991da177e4SLinus Torvalds */ 700be7d31ccSMike Snitzer static int queue_if_no_path(struct multipath *m, bool queue_if_no_path, 701be7d31ccSMike Snitzer bool save_old_value) 7021da177e4SLinus Torvalds { 7031da177e4SLinus Torvalds unsigned long flags; 7041da177e4SLinus Torvalds 7051da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 7065307e2adSLukas Wunner assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags, 7075307e2adSLukas Wunner (save_old_value && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) || 7085307e2adSLukas Wunner (!save_old_value && queue_if_no_path)); 709c1fd0abeSMike Snitzer assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path); 7101da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 7111da177e4SLinus Torvalds 71276e33fe4SMike Snitzer if (!queue_if_no_path) { 71363d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 7147e48c768SMike Snitzer process_queued_io_list(m); 71576e33fe4SMike Snitzer } 71663d832c3SHannes Reinecke 7171da177e4SLinus Torvalds return 0; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 7201da177e4SLinus Torvalds /* 7211da177e4SLinus Torvalds * An event is triggered whenever a path is taken out of use. 7221da177e4SLinus Torvalds * Includes path failure and PG bypass. 7231da177e4SLinus Torvalds */ 724c4028958SDavid Howells static void trigger_event(struct work_struct *work) 7251da177e4SLinus Torvalds { 726c4028958SDavid Howells struct multipath *m = 727c4028958SDavid Howells container_of(work, struct multipath, trigger_event); 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds dm_table_event(m->ti->table); 7301da177e4SLinus Torvalds } 7311da177e4SLinus Torvalds 7321da177e4SLinus Torvalds /*----------------------------------------------------------------- 7331da177e4SLinus Torvalds * Constructor/argument parsing: 7341da177e4SLinus Torvalds * <#multipath feature args> [<arg>]* 7351da177e4SLinus Torvalds * <#hw_handler args> [hw_handler [<arg>]*] 7361da177e4SLinus Torvalds * <#priority groups> 7371da177e4SLinus Torvalds * <initial priority group> 7381da177e4SLinus Torvalds * [<selector> <#selector args> [<arg>]* 7391da177e4SLinus Torvalds * <#paths> <#per-path selector args> 7401da177e4SLinus Torvalds * [<path> [<arg>]* ]+ ]+ 7411da177e4SLinus Torvalds *---------------------------------------------------------------*/ 742498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg, 7431da177e4SLinus Torvalds struct dm_target *ti) 7441da177e4SLinus Torvalds { 7451da177e4SLinus Torvalds int r; 7461da177e4SLinus Torvalds struct path_selector_type *pst; 7471da177e4SLinus Torvalds unsigned ps_argc; 7481da177e4SLinus Torvalds 7495916a22bSEric Biggers static const struct dm_arg _args[] = { 75072d94861SAlasdair G Kergon {0, 1024, "invalid number of path selector args"}, 7511da177e4SLinus Torvalds }; 7521da177e4SLinus Torvalds 753498f0103SMike Snitzer pst = dm_get_path_selector(dm_shift_arg(as)); 7541da177e4SLinus Torvalds if (!pst) { 75572d94861SAlasdair G Kergon ti->error = "unknown path selector type"; 7561da177e4SLinus Torvalds return -EINVAL; 7571da177e4SLinus Torvalds } 7581da177e4SLinus Torvalds 759498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &ps_argc, &ti->error); 760371b2e34SMikulas Patocka if (r) { 761371b2e34SMikulas Patocka dm_put_path_selector(pst); 7621da177e4SLinus Torvalds return -EINVAL; 763371b2e34SMikulas Patocka } 7641da177e4SLinus Torvalds 7651da177e4SLinus Torvalds r = pst->create(&pg->ps, ps_argc, as->argv); 7661da177e4SLinus Torvalds if (r) { 7671da177e4SLinus Torvalds dm_put_path_selector(pst); 76872d94861SAlasdair G Kergon ti->error = "path selector constructor failed"; 7691da177e4SLinus Torvalds return r; 7701da177e4SLinus Torvalds } 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds pg->ps.type = pst; 773498f0103SMike Snitzer dm_consume_args(as, ps_argc); 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds return 0; 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds 778e8f74a0fSMike Snitzer static int setup_scsi_dh(struct block_device *bdev, struct multipath *m, 779b592211cSMike Snitzer const char **attached_handler_name, char **error) 7801da177e4SLinus Torvalds { 781848b8aefSMike Snitzer struct request_queue *q = bdev_get_queue(bdev); 782848b8aefSMike Snitzer int r; 783a0cf7ea9SHannes Reinecke 784518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) { 7851bab0de0SChristoph Hellwig retain: 786b592211cSMike Snitzer if (*attached_handler_name) { 787a58a935dSMike Snitzer /* 78854cd640dStang.junhui * Clear any hw_handler_params associated with a 78954cd640dStang.junhui * handler that isn't already attached. 79054cd640dStang.junhui */ 791b592211cSMike Snitzer if (m->hw_handler_name && strcmp(*attached_handler_name, m->hw_handler_name)) { 79254cd640dStang.junhui kfree(m->hw_handler_params); 79354cd640dStang.junhui m->hw_handler_params = NULL; 79454cd640dStang.junhui } 79554cd640dStang.junhui 79654cd640dStang.junhui /* 797a58a935dSMike Snitzer * Reset hw_handler_name to match the attached handler 798a58a935dSMike Snitzer * 799a58a935dSMike Snitzer * NB. This modifies the table line to show the actual 800a58a935dSMike Snitzer * handler instead of the original table passed in. 801a58a935dSMike Snitzer */ 802a58a935dSMike Snitzer kfree(m->hw_handler_name); 803b592211cSMike Snitzer m->hw_handler_name = *attached_handler_name; 804b592211cSMike Snitzer *attached_handler_name = NULL; 805a58a935dSMike Snitzer } 806a58a935dSMike Snitzer } 807a58a935dSMike Snitzer 808a58a935dSMike Snitzer if (m->hw_handler_name) { 809a0cf7ea9SHannes Reinecke r = scsi_dh_attach(q, m->hw_handler_name); 810a0cf7ea9SHannes Reinecke if (r == -EBUSY) { 8111bab0de0SChristoph Hellwig char b[BDEVNAME_SIZE]; 812a0cf7ea9SHannes Reinecke 8131bab0de0SChristoph Hellwig printk(KERN_INFO "dm-mpath: retaining handler on device %s\n", 814848b8aefSMike Snitzer bdevname(bdev, b)); 8151bab0de0SChristoph Hellwig goto retain; 8161bab0de0SChristoph Hellwig } 817ae11b1b3SHannes Reinecke if (r < 0) { 818848b8aefSMike Snitzer *error = "error attaching hardware handler"; 819848b8aefSMike Snitzer return r; 820ae11b1b3SHannes Reinecke } 8212bfd2e13SChandra Seetharaman 8222bfd2e13SChandra Seetharaman if (m->hw_handler_params) { 8232bfd2e13SChandra Seetharaman r = scsi_dh_set_params(q, m->hw_handler_params); 8242bfd2e13SChandra Seetharaman if (r < 0) { 825848b8aefSMike Snitzer *error = "unable to set hardware handler parameters"; 826848b8aefSMike Snitzer return r; 827848b8aefSMike Snitzer } 828848b8aefSMike Snitzer } 829848b8aefSMike Snitzer } 830848b8aefSMike Snitzer 831848b8aefSMike Snitzer return 0; 832848b8aefSMike Snitzer } 833848b8aefSMike Snitzer 834848b8aefSMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps, 835848b8aefSMike Snitzer struct dm_target *ti) 836848b8aefSMike Snitzer { 837848b8aefSMike Snitzer int r; 838848b8aefSMike Snitzer struct pgpath *p; 839848b8aefSMike Snitzer struct multipath *m = ti->private; 840e8f74a0fSMike Snitzer struct request_queue *q; 841b592211cSMike Snitzer const char *attached_handler_name = NULL; 842848b8aefSMike Snitzer 843848b8aefSMike Snitzer /* we need at least a path arg */ 844848b8aefSMike Snitzer if (as->argc < 1) { 845848b8aefSMike Snitzer ti->error = "no device given"; 846848b8aefSMike Snitzer return ERR_PTR(-EINVAL); 847848b8aefSMike Snitzer } 848848b8aefSMike Snitzer 849848b8aefSMike Snitzer p = alloc_pgpath(); 850848b8aefSMike Snitzer if (!p) 851848b8aefSMike Snitzer return ERR_PTR(-ENOMEM); 852848b8aefSMike Snitzer 853848b8aefSMike Snitzer r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table), 854848b8aefSMike Snitzer &p->path.dev); 855848b8aefSMike Snitzer if (r) { 856848b8aefSMike Snitzer ti->error = "error getting device"; 8572bfd2e13SChandra Seetharaman goto bad; 8582bfd2e13SChandra Seetharaman } 859848b8aefSMike Snitzer 860e8f74a0fSMike Snitzer q = bdev_get_queue(p->path.dev->bdev); 861e8f74a0fSMike Snitzer attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL); 862e457edf0SMike Snitzer if (attached_handler_name || m->hw_handler_name) { 863848b8aefSMike Snitzer INIT_DELAYED_WORK(&p->activate_path, activate_path_work); 864b592211cSMike Snitzer r = setup_scsi_dh(p->path.dev->bdev, m, &attached_handler_name, &ti->error); 865940bc471SMartin Wilck kfree(attached_handler_name); 866848b8aefSMike Snitzer if (r) { 867848b8aefSMike Snitzer dm_put_device(ti, p->path.dev); 868848b8aefSMike Snitzer goto bad; 8692bfd2e13SChandra Seetharaman } 870ae11b1b3SHannes Reinecke } 871ae11b1b3SHannes Reinecke 8721da177e4SLinus Torvalds r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); 8731da177e4SLinus Torvalds if (r) { 8741da177e4SLinus Torvalds dm_put_device(ti, p->path.dev); 8751da177e4SLinus Torvalds goto bad; 8761da177e4SLinus Torvalds } 8771da177e4SLinus Torvalds 8781da177e4SLinus Torvalds return p; 8791da177e4SLinus Torvalds bad: 8801da177e4SLinus Torvalds free_pgpath(p); 88101460f35SBenjamin Marzinski return ERR_PTR(r); 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 884498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as, 88528f16c20SMicha³ Miros³aw struct multipath *m) 8861da177e4SLinus Torvalds { 8875916a22bSEric Biggers static const struct dm_arg _args[] = { 88872d94861SAlasdair G Kergon {1, 1024, "invalid number of paths"}, 88972d94861SAlasdair G Kergon {0, 1024, "invalid number of selector args"} 8901da177e4SLinus Torvalds }; 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds int r; 893498f0103SMike Snitzer unsigned i, nr_selector_args, nr_args; 8941da177e4SLinus Torvalds struct priority_group *pg; 89528f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds if (as->argc < 2) { 8981da177e4SLinus Torvalds as->argc = 0; 89901460f35SBenjamin Marzinski ti->error = "not enough priority group arguments"; 90001460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds pg = alloc_priority_group(); 9041da177e4SLinus Torvalds if (!pg) { 90572d94861SAlasdair G Kergon ti->error = "couldn't allocate priority group"; 90601460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds pg->m = m; 9091da177e4SLinus Torvalds 9101da177e4SLinus Torvalds r = parse_path_selector(as, pg, ti); 9111da177e4SLinus Torvalds if (r) 9121da177e4SLinus Torvalds goto bad; 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds /* 9151da177e4SLinus Torvalds * read the paths 9161da177e4SLinus Torvalds */ 917498f0103SMike Snitzer r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error); 9181da177e4SLinus Torvalds if (r) 9191da177e4SLinus Torvalds goto bad; 9201da177e4SLinus Torvalds 921498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error); 9221da177e4SLinus Torvalds if (r) 9231da177e4SLinus Torvalds goto bad; 9241da177e4SLinus Torvalds 925498f0103SMike Snitzer nr_args = 1 + nr_selector_args; 9261da177e4SLinus Torvalds for (i = 0; i < pg->nr_pgpaths; i++) { 9271da177e4SLinus Torvalds struct pgpath *pgpath; 928498f0103SMike Snitzer struct dm_arg_set path_args; 9291da177e4SLinus Torvalds 930498f0103SMike Snitzer if (as->argc < nr_args) { 931148acff6SMikulas Patocka ti->error = "not enough path parameters"; 9326bbf79a1SAlasdair G Kergon r = -EINVAL; 9331da177e4SLinus Torvalds goto bad; 934148acff6SMikulas Patocka } 9351da177e4SLinus Torvalds 936498f0103SMike Snitzer path_args.argc = nr_args; 9371da177e4SLinus Torvalds path_args.argv = as->argv; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds pgpath = parse_path(&path_args, &pg->ps, ti); 94001460f35SBenjamin Marzinski if (IS_ERR(pgpath)) { 94101460f35SBenjamin Marzinski r = PTR_ERR(pgpath); 9421da177e4SLinus Torvalds goto bad; 94301460f35SBenjamin Marzinski } 9441da177e4SLinus Torvalds 9451da177e4SLinus Torvalds pgpath->pg = pg; 9461da177e4SLinus Torvalds list_add_tail(&pgpath->list, &pg->pgpaths); 947498f0103SMike Snitzer dm_consume_args(as, nr_args); 9481da177e4SLinus Torvalds } 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds return pg; 9511da177e4SLinus Torvalds 9521da177e4SLinus Torvalds bad: 9531da177e4SLinus Torvalds free_priority_group(pg, ti); 95401460f35SBenjamin Marzinski return ERR_PTR(r); 9551da177e4SLinus Torvalds } 9561da177e4SLinus Torvalds 957498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m) 9581da177e4SLinus Torvalds { 9591da177e4SLinus Torvalds unsigned hw_argc; 9602bfd2e13SChandra Seetharaman int ret; 96128f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 9621da177e4SLinus Torvalds 9635916a22bSEric Biggers static const struct dm_arg _args[] = { 96472d94861SAlasdair G Kergon {0, 1024, "invalid number of hardware handler args"}, 9651da177e4SLinus Torvalds }; 9661da177e4SLinus Torvalds 967498f0103SMike Snitzer if (dm_read_arg_group(_args, as, &hw_argc, &ti->error)) 9681da177e4SLinus Torvalds return -EINVAL; 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds if (!hw_argc) 9711da177e4SLinus Torvalds return 0; 9721da177e4SLinus Torvalds 9738d47e659SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) { 97476e33fe4SMike Snitzer dm_consume_args(as, hw_argc); 97576e33fe4SMike Snitzer DMERR("bio-based multipath doesn't allow hardware handler args"); 97676e33fe4SMike Snitzer return 0; 97776e33fe4SMike Snitzer } 97876e33fe4SMike Snitzer 979498f0103SMike Snitzer m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL); 980f97dc421Stang.junhui if (!m->hw_handler_name) 981f97dc421Stang.junhui return -EINVAL; 98214e98c5cSChandra Seetharaman 9832bfd2e13SChandra Seetharaman if (hw_argc > 1) { 9842bfd2e13SChandra Seetharaman char *p; 9852bfd2e13SChandra Seetharaman int i, j, len = 4; 9862bfd2e13SChandra Seetharaman 9872bfd2e13SChandra Seetharaman for (i = 0; i <= hw_argc - 2; i++) 9882bfd2e13SChandra Seetharaman len += strlen(as->argv[i]) + 1; 9892bfd2e13SChandra Seetharaman p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); 9902bfd2e13SChandra Seetharaman if (!p) { 9912bfd2e13SChandra Seetharaman ti->error = "memory allocation failed"; 9922bfd2e13SChandra Seetharaman ret = -ENOMEM; 9932bfd2e13SChandra Seetharaman goto fail; 9942bfd2e13SChandra Seetharaman } 9952bfd2e13SChandra Seetharaman j = sprintf(p, "%d", hw_argc - 1); 9962bfd2e13SChandra Seetharaman for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1) 9972bfd2e13SChandra Seetharaman j = sprintf(p, "%s", as->argv[i]); 9982bfd2e13SChandra Seetharaman } 999498f0103SMike Snitzer dm_consume_args(as, hw_argc - 1); 10001da177e4SLinus Torvalds 10011da177e4SLinus Torvalds return 0; 10022bfd2e13SChandra Seetharaman fail: 10032bfd2e13SChandra Seetharaman kfree(m->hw_handler_name); 10042bfd2e13SChandra Seetharaman m->hw_handler_name = NULL; 10052bfd2e13SChandra Seetharaman return ret; 10061da177e4SLinus Torvalds } 10071da177e4SLinus Torvalds 1008498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m) 10091da177e4SLinus Torvalds { 10101da177e4SLinus Torvalds int r; 10111da177e4SLinus Torvalds unsigned argc; 101228f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 1013498f0103SMike Snitzer const char *arg_name; 10141da177e4SLinus Torvalds 10155916a22bSEric Biggers static const struct dm_arg _args[] = { 1016e83068a5SMike Snitzer {0, 8, "invalid number of feature args"}, 1017c9e45581SDave Wysochanski {1, 50, "pg_init_retries must be between 1 and 50"}, 10184e2d19e4SChandra Seetharaman {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"}, 10191da177e4SLinus Torvalds }; 10201da177e4SLinus Torvalds 1021498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &argc, &ti->error); 10221da177e4SLinus Torvalds if (r) 10231da177e4SLinus Torvalds return -EINVAL; 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds if (!argc) 10261da177e4SLinus Torvalds return 0; 10271da177e4SLinus Torvalds 1028c9e45581SDave Wysochanski do { 1029498f0103SMike Snitzer arg_name = dm_shift_arg(as); 1030c9e45581SDave Wysochanski argc--; 1031c9e45581SDave Wysochanski 1032498f0103SMike Snitzer if (!strcasecmp(arg_name, "queue_if_no_path")) { 1033be7d31ccSMike Snitzer r = queue_if_no_path(m, true, false); 1034c9e45581SDave Wysochanski continue; 10351da177e4SLinus Torvalds } 1036c9e45581SDave Wysochanski 1037a58a935dSMike Snitzer if (!strcasecmp(arg_name, "retain_attached_hw_handler")) { 1038518257b1SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 1039a58a935dSMike Snitzer continue; 1040a58a935dSMike Snitzer } 1041a58a935dSMike Snitzer 1042498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_retries") && 1043c9e45581SDave Wysochanski (argc >= 1)) { 1044498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error); 1045c9e45581SDave Wysochanski argc--; 1046c9e45581SDave Wysochanski continue; 1047c9e45581SDave Wysochanski } 1048c9e45581SDave Wysochanski 1049498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_delay_msecs") && 10504e2d19e4SChandra Seetharaman (argc >= 1)) { 1051498f0103SMike Snitzer r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error); 10524e2d19e4SChandra Seetharaman argc--; 10534e2d19e4SChandra Seetharaman continue; 10544e2d19e4SChandra Seetharaman } 10554e2d19e4SChandra Seetharaman 1056e83068a5SMike Snitzer if (!strcasecmp(arg_name, "queue_mode") && 1057e83068a5SMike Snitzer (argc >= 1)) { 1058e83068a5SMike Snitzer const char *queue_mode_name = dm_shift_arg(as); 1059e83068a5SMike Snitzer 1060e83068a5SMike Snitzer if (!strcasecmp(queue_mode_name, "bio")) 1061e83068a5SMike Snitzer m->queue_mode = DM_TYPE_BIO_BASED; 1062953923c0SMike Snitzer else if (!strcasecmp(queue_mode_name, "rq") || 1063953923c0SMike Snitzer !strcasecmp(queue_mode_name, "mq")) 1064e83068a5SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 1065e83068a5SMike Snitzer else { 1066e83068a5SMike Snitzer ti->error = "Unknown 'queue_mode' requested"; 1067e83068a5SMike Snitzer r = -EINVAL; 1068e83068a5SMike Snitzer } 1069e83068a5SMike Snitzer argc--; 1070e83068a5SMike Snitzer continue; 1071e83068a5SMike Snitzer } 1072e83068a5SMike Snitzer 1073c9e45581SDave Wysochanski ti->error = "Unrecognised multipath feature request"; 1074c9e45581SDave Wysochanski r = -EINVAL; 1075c9e45581SDave Wysochanski } while (argc && !r); 1076c9e45581SDave Wysochanski 1077c9e45581SDave Wysochanski return r; 10781da177e4SLinus Torvalds } 10791da177e4SLinus Torvalds 1080e83068a5SMike Snitzer static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv) 10811da177e4SLinus Torvalds { 1082498f0103SMike Snitzer /* target arguments */ 10835916a22bSEric Biggers static const struct dm_arg _args[] = { 1084a490a07aSMike Snitzer {0, 1024, "invalid number of priority groups"}, 1085a490a07aSMike Snitzer {0, 1024, "invalid initial priority group number"}, 10861da177e4SLinus Torvalds }; 10871da177e4SLinus Torvalds 10881da177e4SLinus Torvalds int r; 10891da177e4SLinus Torvalds struct multipath *m; 1090498f0103SMike Snitzer struct dm_arg_set as; 10911da177e4SLinus Torvalds unsigned pg_count = 0; 10921da177e4SLinus Torvalds unsigned next_pg_num; 10931da177e4SLinus Torvalds 10941da177e4SLinus Torvalds as.argc = argc; 10951da177e4SLinus Torvalds as.argv = argv; 10961da177e4SLinus Torvalds 1097e83068a5SMike Snitzer m = alloc_multipath(ti); 10981da177e4SLinus Torvalds if (!m) { 109972d94861SAlasdair G Kergon ti->error = "can't allocate multipath"; 11001da177e4SLinus Torvalds return -EINVAL; 11011da177e4SLinus Torvalds } 11021da177e4SLinus Torvalds 110328f16c20SMicha³ Miros³aw r = parse_features(&as, m); 11041da177e4SLinus Torvalds if (r) 11051da177e4SLinus Torvalds goto bad; 11061da177e4SLinus Torvalds 1107e83068a5SMike Snitzer r = alloc_multipath_stage2(ti, m); 1108e83068a5SMike Snitzer if (r) 1109e83068a5SMike Snitzer goto bad; 1110e83068a5SMike Snitzer 111128f16c20SMicha³ Miros³aw r = parse_hw_handler(&as, m); 11121da177e4SLinus Torvalds if (r) 11131da177e4SLinus Torvalds goto bad; 11141da177e4SLinus Torvalds 1115498f0103SMike Snitzer r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error); 11161da177e4SLinus Torvalds if (r) 11171da177e4SLinus Torvalds goto bad; 11181da177e4SLinus Torvalds 1119498f0103SMike Snitzer r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error); 11201da177e4SLinus Torvalds if (r) 11211da177e4SLinus Torvalds goto bad; 11221da177e4SLinus Torvalds 1123a490a07aSMike Snitzer if ((!m->nr_priority_groups && next_pg_num) || 1124a490a07aSMike Snitzer (m->nr_priority_groups && !next_pg_num)) { 1125a490a07aSMike Snitzer ti->error = "invalid initial priority group"; 1126a490a07aSMike Snitzer r = -EINVAL; 1127a490a07aSMike Snitzer goto bad; 1128a490a07aSMike Snitzer } 1129a490a07aSMike Snitzer 11301da177e4SLinus Torvalds /* parse the priority groups */ 11311da177e4SLinus Torvalds while (as.argc) { 11321da177e4SLinus Torvalds struct priority_group *pg; 113391e968aaSMike Snitzer unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths); 11341da177e4SLinus Torvalds 113528f16c20SMicha³ Miros³aw pg = parse_priority_group(&as, m); 113601460f35SBenjamin Marzinski if (IS_ERR(pg)) { 113701460f35SBenjamin Marzinski r = PTR_ERR(pg); 11381da177e4SLinus Torvalds goto bad; 11391da177e4SLinus Torvalds } 11401da177e4SLinus Torvalds 114191e968aaSMike Snitzer nr_valid_paths += pg->nr_pgpaths; 114291e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, nr_valid_paths); 114391e968aaSMike Snitzer 11441da177e4SLinus Torvalds list_add_tail(&pg->list, &m->priority_groups); 11451da177e4SLinus Torvalds pg_count++; 11461da177e4SLinus Torvalds pg->pg_num = pg_count; 11471da177e4SLinus Torvalds if (!--next_pg_num) 11481da177e4SLinus Torvalds m->next_pg = pg; 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds 11511da177e4SLinus Torvalds if (pg_count != m->nr_priority_groups) { 115272d94861SAlasdair G Kergon ti->error = "priority group count mismatch"; 11531da177e4SLinus Torvalds r = -EINVAL; 11541da177e4SLinus Torvalds goto bad; 11551da177e4SLinus Torvalds } 11561da177e4SLinus Torvalds 115755a62eefSAlasdair G Kergon ti->num_flush_bios = 1; 115855a62eefSAlasdair G Kergon ti->num_discard_bios = 1; 1159042bcef8SMike Snitzer ti->num_write_same_bios = 1; 1160ac62d620SChristoph Hellwig ti->num_write_zeroes_bios = 1; 11618d47e659SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) 1162bf661be1SMike Snitzer ti->per_io_data_size = multipath_per_bio_data_size(); 1163eb8db831SChristoph Hellwig else 11648637a6bfSMike Snitzer ti->per_io_data_size = sizeof(struct dm_mpath_io); 11658627921fSMikulas Patocka 11661da177e4SLinus Torvalds return 0; 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds bad: 11691da177e4SLinus Torvalds free_multipath(m); 11701da177e4SLinus Torvalds return r; 11711da177e4SLinus Torvalds } 11721da177e4SLinus Torvalds 11732bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m) 11742bded7bdSKiyoshi Ueda { 11759f4c3f87SBart Van Assche DEFINE_WAIT(wait); 11762bded7bdSKiyoshi Ueda 11772bded7bdSKiyoshi Ueda while (1) { 11789f4c3f87SBart Van Assche prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE); 11792bded7bdSKiyoshi Ueda 118091e968aaSMike Snitzer if (!atomic_read(&m->pg_init_in_progress)) 11812bded7bdSKiyoshi Ueda break; 11822bded7bdSKiyoshi Ueda 11832bded7bdSKiyoshi Ueda io_schedule(); 11842bded7bdSKiyoshi Ueda } 11859f4c3f87SBart Van Assche finish_wait(&m->pg_init_wait, &wait); 11862bded7bdSKiyoshi Ueda } 11872bded7bdSKiyoshi Ueda 11882bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m) 11891da177e4SLinus Torvalds { 1190848b8aefSMike Snitzer if (m->hw_handler_name) { 1191518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1192518257b1SMike Snitzer smp_mb__after_atomic(); 1193954a73d5SShiva Krishna Merla 1194935fcc56Swuzhouhui if (atomic_read(&m->pg_init_in_progress)) 1195bab7cfc7SChandra Seetharaman flush_workqueue(kmpath_handlerd); 11962bded7bdSKiyoshi Ueda multipath_wait_for_pg_init_completion(m); 1197954a73d5SShiva Krishna Merla 1198518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1199518257b1SMike Snitzer smp_mb__after_atomic(); 12006df400abSKiyoshi Ueda } 12016df400abSKiyoshi Ueda 1202935fcc56Swuzhouhui if (m->queue_mode == DM_TYPE_BIO_BASED) 1203935fcc56Swuzhouhui flush_work(&m->process_queued_bios); 1204848b8aefSMike Snitzer flush_work(&m->trigger_event); 1205848b8aefSMike Snitzer } 1206848b8aefSMike Snitzer 12076df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti) 12086df400abSKiyoshi Ueda { 12096df400abSKiyoshi Ueda struct multipath *m = ti->private; 12106df400abSKiyoshi Ueda 12112bded7bdSKiyoshi Ueda flush_multipath_work(m); 12121da177e4SLinus Torvalds free_multipath(m); 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds 12151da177e4SLinus Torvalds /* 12161da177e4SLinus Torvalds * Take a path out of use. 12171da177e4SLinus Torvalds */ 12181da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath) 12191da177e4SLinus Torvalds { 12201da177e4SLinus Torvalds unsigned long flags; 12211da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 12221da177e4SLinus Torvalds 12231da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 12241da177e4SLinus Torvalds 12256680073dSKiyoshi Ueda if (!pgpath->is_active) 12261da177e4SLinus Torvalds goto out; 12271da177e4SLinus Torvalds 122872d94861SAlasdair G Kergon DMWARN("Failing path %s.", pgpath->path.dev->name); 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); 1231be7d31ccSMike Snitzer pgpath->is_active = false; 12321da177e4SLinus Torvalds pgpath->fail_count++; 12331da177e4SLinus Torvalds 123491e968aaSMike Snitzer atomic_dec(&m->nr_valid_paths); 12351da177e4SLinus Torvalds 12361da177e4SLinus Torvalds if (pgpath == m->current_pgpath) 12371da177e4SLinus Torvalds m->current_pgpath = NULL; 12381da177e4SLinus Torvalds 1239b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti, 124091e968aaSMike Snitzer pgpath->path.dev->name, atomic_read(&m->nr_valid_paths)); 1241b15546f9SMike Anderson 1242fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 12431da177e4SLinus Torvalds 12441da177e4SLinus Torvalds out: 12451da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 12461da177e4SLinus Torvalds 12471da177e4SLinus Torvalds return 0; 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds /* 12511da177e4SLinus Torvalds * Reinstate a previously-failed path 12521da177e4SLinus Torvalds */ 12531da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath) 12541da177e4SLinus Torvalds { 125563d832c3SHannes Reinecke int r = 0, run_queue = 0; 12561da177e4SLinus Torvalds unsigned long flags; 12571da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 125891e968aaSMike Snitzer unsigned nr_valid_paths; 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 12611da177e4SLinus Torvalds 12626680073dSKiyoshi Ueda if (pgpath->is_active) 12631da177e4SLinus Torvalds goto out; 12641da177e4SLinus Torvalds 1265ec31f3f7SMike Snitzer DMWARN("Reinstating path %s.", pgpath->path.dev->name); 12661da177e4SLinus Torvalds 12671da177e4SLinus Torvalds r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); 12681da177e4SLinus Torvalds if (r) 12691da177e4SLinus Torvalds goto out; 12701da177e4SLinus Torvalds 1271be7d31ccSMike Snitzer pgpath->is_active = true; 12721da177e4SLinus Torvalds 127391e968aaSMike Snitzer nr_valid_paths = atomic_inc_return(&m->nr_valid_paths); 127491e968aaSMike Snitzer if (nr_valid_paths == 1) { 12751da177e4SLinus Torvalds m->current_pgpath = NULL; 127663d832c3SHannes Reinecke run_queue = 1; 1277e54f77ddSChandra Seetharaman } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) { 12784e2d19e4SChandra Seetharaman if (queue_work(kmpath_handlerd, &pgpath->activate_path.work)) 127991e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 1280e54f77ddSChandra Seetharaman } 12811da177e4SLinus Torvalds 1282b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti, 128391e968aaSMike Snitzer pgpath->path.dev->name, nr_valid_paths); 1284b15546f9SMike Anderson 1285fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 12861da177e4SLinus Torvalds 12871da177e4SLinus Torvalds out: 12881da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 128976e33fe4SMike Snitzer if (run_queue) { 129063d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 12917e48c768SMike Snitzer process_queued_io_list(m); 129276e33fe4SMike Snitzer } 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds return r; 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds /* 12981da177e4SLinus Torvalds * Fail or reinstate all paths that match the provided struct dm_dev. 12991da177e4SLinus Torvalds */ 13001da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev, 13011da177e4SLinus Torvalds action_fn action) 13021da177e4SLinus Torvalds { 130319040c0bSMike Snitzer int r = -EINVAL; 13041da177e4SLinus Torvalds struct pgpath *pgpath; 13051da177e4SLinus Torvalds struct priority_group *pg; 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 13081da177e4SLinus Torvalds list_for_each_entry(pgpath, &pg->pgpaths, list) { 13091da177e4SLinus Torvalds if (pgpath->path.dev == dev) 13101da177e4SLinus Torvalds r = action(pgpath); 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds } 13131da177e4SLinus Torvalds 13141da177e4SLinus Torvalds return r; 13151da177e4SLinus Torvalds } 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds /* 13181da177e4SLinus Torvalds * Temporarily try to avoid having to use the specified PG 13191da177e4SLinus Torvalds */ 13201da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg, 1321be7d31ccSMike Snitzer bool bypassed) 13221da177e4SLinus Torvalds { 13231da177e4SLinus Torvalds unsigned long flags; 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds pg->bypassed = bypassed; 13281da177e4SLinus Torvalds m->current_pgpath = NULL; 13291da177e4SLinus Torvalds m->current_pg = NULL; 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13321da177e4SLinus Torvalds 1333fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13341da177e4SLinus Torvalds } 13351da177e4SLinus Torvalds 13361da177e4SLinus Torvalds /* 13371da177e4SLinus Torvalds * Switch to using the specified PG from the next I/O that gets mapped 13381da177e4SLinus Torvalds */ 13391da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr) 13401da177e4SLinus Torvalds { 13411da177e4SLinus Torvalds struct priority_group *pg; 13421da177e4SLinus Torvalds unsigned pgnum; 13431da177e4SLinus Torvalds unsigned long flags; 134431998ef1SMikulas Patocka char dummy; 13451da177e4SLinus Torvalds 134631998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 1347cc5bd925Stang.junhui !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) { 13481da177e4SLinus Torvalds DMWARN("invalid PG number supplied to switch_pg_num"); 13491da177e4SLinus Torvalds return -EINVAL; 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds 13521da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13531da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 1354be7d31ccSMike Snitzer pg->bypassed = false; 13551da177e4SLinus Torvalds if (--pgnum) 13561da177e4SLinus Torvalds continue; 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds m->current_pgpath = NULL; 13591da177e4SLinus Torvalds m->current_pg = NULL; 13601da177e4SLinus Torvalds m->next_pg = pg; 13611da177e4SLinus Torvalds } 13621da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13631da177e4SLinus Torvalds 1364fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13651da177e4SLinus Torvalds return 0; 13661da177e4SLinus Torvalds } 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds /* 13691da177e4SLinus Torvalds * Set/clear bypassed status of a PG. 13701da177e4SLinus Torvalds * PGs are numbered upwards from 1 in the order they were declared. 13711da177e4SLinus Torvalds */ 1372be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed) 13731da177e4SLinus Torvalds { 13741da177e4SLinus Torvalds struct priority_group *pg; 13751da177e4SLinus Torvalds unsigned pgnum; 137631998ef1SMikulas Patocka char dummy; 13771da177e4SLinus Torvalds 137831998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 1379cc5bd925Stang.junhui !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) { 13801da177e4SLinus Torvalds DMWARN("invalid PG number supplied to bypass_pg"); 13811da177e4SLinus Torvalds return -EINVAL; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 13851da177e4SLinus Torvalds if (!--pgnum) 13861da177e4SLinus Torvalds break; 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds bypass_pg(m, pg, bypassed); 13901da177e4SLinus Torvalds return 0; 13911da177e4SLinus Torvalds } 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds /* 1394c9e45581SDave Wysochanski * Should we retry pg_init immediately? 1395c9e45581SDave Wysochanski */ 1396be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath) 1397c9e45581SDave Wysochanski { 1398c9e45581SDave Wysochanski unsigned long flags; 1399be7d31ccSMike Snitzer bool limit_reached = false; 1400c9e45581SDave Wysochanski 1401c9e45581SDave Wysochanski spin_lock_irqsave(&m->lock, flags); 1402c9e45581SDave Wysochanski 140391e968aaSMike Snitzer if (atomic_read(&m->pg_init_count) <= m->pg_init_retries && 140491e968aaSMike Snitzer !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 1405518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 1406c9e45581SDave Wysochanski else 1407be7d31ccSMike Snitzer limit_reached = true; 1408c9e45581SDave Wysochanski 1409c9e45581SDave Wysochanski spin_unlock_irqrestore(&m->lock, flags); 1410c9e45581SDave Wysochanski 1411c9e45581SDave Wysochanski return limit_reached; 1412c9e45581SDave Wysochanski } 1413c9e45581SDave Wysochanski 14143ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors) 1415cfae5c9bSChandra Seetharaman { 141683c0d5d5SMoger, Babu struct pgpath *pgpath = data; 1417cfae5c9bSChandra Seetharaman struct priority_group *pg = pgpath->pg; 1418cfae5c9bSChandra Seetharaman struct multipath *m = pg->m; 1419cfae5c9bSChandra Seetharaman unsigned long flags; 1420be7d31ccSMike Snitzer bool delay_retry = false; 1421cfae5c9bSChandra Seetharaman 1422cfae5c9bSChandra Seetharaman /* device or driver problems */ 1423cfae5c9bSChandra Seetharaman switch (errors) { 1424cfae5c9bSChandra Seetharaman case SCSI_DH_OK: 1425cfae5c9bSChandra Seetharaman break; 1426cfae5c9bSChandra Seetharaman case SCSI_DH_NOSYS: 1427cfae5c9bSChandra Seetharaman if (!m->hw_handler_name) { 1428cfae5c9bSChandra Seetharaman errors = 0; 1429cfae5c9bSChandra Seetharaman break; 1430cfae5c9bSChandra Seetharaman } 1431f7b934c8SMoger, Babu DMERR("Could not failover the device: Handler scsi_dh_%s " 1432f7b934c8SMoger, Babu "Error %d.", m->hw_handler_name, errors); 1433cfae5c9bSChandra Seetharaman /* 1434cfae5c9bSChandra Seetharaman * Fail path for now, so we do not ping pong 1435cfae5c9bSChandra Seetharaman */ 1436cfae5c9bSChandra Seetharaman fail_path(pgpath); 1437cfae5c9bSChandra Seetharaman break; 1438cfae5c9bSChandra Seetharaman case SCSI_DH_DEV_TEMP_BUSY: 1439cfae5c9bSChandra Seetharaman /* 1440cfae5c9bSChandra Seetharaman * Probably doing something like FW upgrade on the 1441cfae5c9bSChandra Seetharaman * controller so try the other pg. 1442cfae5c9bSChandra Seetharaman */ 1443be7d31ccSMike Snitzer bypass_pg(m, pg, true); 1444cfae5c9bSChandra Seetharaman break; 1445cfae5c9bSChandra Seetharaman case SCSI_DH_RETRY: 14464e2d19e4SChandra Seetharaman /* Wait before retrying. */ 14474e2d19e4SChandra Seetharaman delay_retry = 1; 14487b06e09aSBart Van Assche /* fall through */ 1449cfae5c9bSChandra Seetharaman case SCSI_DH_IMM_RETRY: 1450cfae5c9bSChandra Seetharaman case SCSI_DH_RES_TEMP_UNAVAIL: 1451cfae5c9bSChandra Seetharaman if (pg_init_limit_reached(m, pgpath)) 1452cfae5c9bSChandra Seetharaman fail_path(pgpath); 1453cfae5c9bSChandra Seetharaman errors = 0; 1454cfae5c9bSChandra Seetharaman break; 1455ec31f3f7SMike Snitzer case SCSI_DH_DEV_OFFLINED: 1456cfae5c9bSChandra Seetharaman default: 1457cfae5c9bSChandra Seetharaman /* 1458cfae5c9bSChandra Seetharaman * We probably do not want to fail the path for a device 1459cfae5c9bSChandra Seetharaman * error, but this is what the old dm did. In future 1460cfae5c9bSChandra Seetharaman * patches we can do more advanced handling. 1461cfae5c9bSChandra Seetharaman */ 1462cfae5c9bSChandra Seetharaman fail_path(pgpath); 1463cfae5c9bSChandra Seetharaman } 1464cfae5c9bSChandra Seetharaman 1465cfae5c9bSChandra Seetharaman spin_lock_irqsave(&m->lock, flags); 1466cfae5c9bSChandra Seetharaman if (errors) { 1467e54f77ddSChandra Seetharaman if (pgpath == m->current_pgpath) { 1468cfae5c9bSChandra Seetharaman DMERR("Could not failover device. Error %d.", errors); 1469cfae5c9bSChandra Seetharaman m->current_pgpath = NULL; 1470cfae5c9bSChandra Seetharaman m->current_pg = NULL; 1471e54f77ddSChandra Seetharaman } 1472518257b1SMike Snitzer } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 1473be7d31ccSMike Snitzer pg->bypassed = false; 1474cfae5c9bSChandra Seetharaman 147591e968aaSMike Snitzer if (atomic_dec_return(&m->pg_init_in_progress) > 0) 1476d0259bf0SKiyoshi Ueda /* Activations of other paths are still on going */ 1477d0259bf0SKiyoshi Ueda goto out; 1478d0259bf0SKiyoshi Ueda 1479518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 1480518257b1SMike Snitzer if (delay_retry) 1481518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1482518257b1SMike Snitzer else 1483518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1484518257b1SMike Snitzer 14853e9f1be1SHannes Reinecke if (__pg_init_all_paths(m)) 14863e9f1be1SHannes Reinecke goto out; 14873e9f1be1SHannes Reinecke } 1488518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 1489d0259bf0SKiyoshi Ueda 14907e48c768SMike Snitzer process_queued_io_list(m); 149176e33fe4SMike Snitzer 14922bded7bdSKiyoshi Ueda /* 14932bded7bdSKiyoshi Ueda * Wake up any thread waiting to suspend. 14942bded7bdSKiyoshi Ueda */ 14952bded7bdSKiyoshi Ueda wake_up(&m->pg_init_wait); 14962bded7bdSKiyoshi Ueda 1497d0259bf0SKiyoshi Ueda out: 1498cfae5c9bSChandra Seetharaman spin_unlock_irqrestore(&m->lock, flags); 1499cfae5c9bSChandra Seetharaman } 1500cfae5c9bSChandra Seetharaman 150189bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath) 1502bab7cfc7SChandra Seetharaman { 1503f10e06b7SMike Snitzer struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1504bab7cfc7SChandra Seetharaman 1505f10e06b7SMike Snitzer if (pgpath->is_active && !blk_queue_dying(q)) 1506f10e06b7SMike Snitzer scsi_dh_activate(q, pg_init_done, pgpath); 15073a017509SHannes Reinecke else 15083a017509SHannes Reinecke pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED); 1509bab7cfc7SChandra Seetharaman } 1510bab7cfc7SChandra Seetharaman 151189bfce76SBart Van Assche static void activate_path_work(struct work_struct *work) 151289bfce76SBart Van Assche { 151389bfce76SBart Van Assche struct pgpath *pgpath = 151489bfce76SBart Van Assche container_of(work, struct pgpath, activate_path.work); 151589bfce76SBart Van Assche 151689bfce76SBart Van Assche activate_or_offline_path(pgpath); 151789bfce76SBart Van Assche } 151889bfce76SBart Van Assche 1519b79f10eeSChristoph Hellwig static int multipath_end_io(struct dm_target *ti, struct request *clone, 15202a842acaSChristoph Hellwig blk_status_t error, union map_info *map_context) 15211da177e4SLinus Torvalds { 1522b79f10eeSChristoph Hellwig struct dm_mpath_io *mpio = get_mpio(map_context); 1523b79f10eeSChristoph Hellwig struct pgpath *pgpath = mpio->pgpath; 15247ed8578aSChristoph Hellwig int r = DM_ENDIO_DONE; 1525b79f10eeSChristoph Hellwig 1526f40c67f0SKiyoshi Ueda /* 1527f40c67f0SKiyoshi Ueda * We don't queue any clone request inside the multipath target 1528f40c67f0SKiyoshi Ueda * during end I/O handling, since those clone requests don't have 1529f40c67f0SKiyoshi Ueda * bio clones. If we queue them inside the multipath target, 1530f40c67f0SKiyoshi Ueda * we need to make bio clones, that requires memory allocation. 15314cc96131SMike Snitzer * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests 1532f40c67f0SKiyoshi Ueda * don't have bio clones.) 1533f40c67f0SKiyoshi Ueda * Instead of queueing the clone request here, we queue the original 1534f40c67f0SKiyoshi Ueda * request into dm core, which will remake a clone request and 1535f40c67f0SKiyoshi Ueda * clone bios for it and resubmit it later. 1536f40c67f0SKiyoshi Ueda */ 1537a1275677SKeith Busch if (error && blk_path_error(error)) { 1538b79f10eeSChristoph Hellwig struct multipath *m = ti->private; 15391da177e4SLinus Torvalds 1540ac514ffcSMike Snitzer if (error == BLK_STS_RESOURCE) 1541ac514ffcSMike Snitzer r = DM_ENDIO_DELAY_REQUEUE; 1542ac514ffcSMike Snitzer else 15437ed8578aSChristoph Hellwig r = DM_ENDIO_REQUEUE; 15441da177e4SLinus Torvalds 1545b79f10eeSChristoph Hellwig if (pgpath) 1546b79f10eeSChristoph Hellwig fail_path(pgpath); 15471da177e4SLinus Torvalds 1548ca5beb76SBart Van Assche if (atomic_read(&m->nr_valid_paths) == 0 && 1549c1fd0abeSMike Snitzer !must_push_back_rq(m)) { 15502a842acaSChristoph Hellwig if (error == BLK_STS_IOERR) 155118a482f5SChristoph Hellwig dm_report_EIO(m); 15527ed8578aSChristoph Hellwig /* complete with the original error */ 15537ed8578aSChristoph Hellwig r = DM_ENDIO_DONE; 15547ed8578aSChristoph Hellwig } 15551da177e4SLinus Torvalds } 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds if (pgpath) { 1558b79f10eeSChristoph Hellwig struct path_selector *ps = &pgpath->pg->ps; 1559b79f10eeSChristoph Hellwig 15601da177e4SLinus Torvalds if (ps->type->end_io) 156102ab823fSKiyoshi Ueda ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); 15621da177e4SLinus Torvalds } 15631da177e4SLinus Torvalds 15647ed8578aSChristoph Hellwig return r; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 15674e4cbee9SChristoph Hellwig static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, 15684e4cbee9SChristoph Hellwig blk_status_t *error) 156976e33fe4SMike Snitzer { 157014ef1e48SChristoph Hellwig struct multipath *m = ti->private; 157114ef1e48SChristoph Hellwig struct dm_mpath_io *mpio = get_mpio_from_bio(clone); 157214ef1e48SChristoph Hellwig struct pgpath *pgpath = mpio->pgpath; 157376e33fe4SMike Snitzer unsigned long flags; 15741be56909SChristoph Hellwig int r = DM_ENDIO_DONE; 157576e33fe4SMike Snitzer 1576a1275677SKeith Busch if (!*error || !blk_path_error(*error)) 157714ef1e48SChristoph Hellwig goto done; 157876e33fe4SMike Snitzer 157914ef1e48SChristoph Hellwig if (pgpath) 158014ef1e48SChristoph Hellwig fail_path(pgpath); 158176e33fe4SMike Snitzer 1582ca5beb76SBart Van Assche if (atomic_read(&m->nr_valid_paths) == 0 && 158318a482f5SChristoph Hellwig !test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 1584c1fd0abeSMike Snitzer if (must_push_back_bio(m)) { 1585c1fd0abeSMike Snitzer r = DM_ENDIO_REQUEUE; 1586c1fd0abeSMike Snitzer } else { 158718a482f5SChristoph Hellwig dm_report_EIO(m); 15884e4cbee9SChristoph Hellwig *error = BLK_STS_IOERR; 1589c1fd0abeSMike Snitzer } 159014ef1e48SChristoph Hellwig goto done; 159118a482f5SChristoph Hellwig } 159276e33fe4SMike Snitzer 159376e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 159476e33fe4SMike Snitzer bio_list_add(&m->queued_bios, clone); 159576e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 159676e33fe4SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) 159776e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 159876e33fe4SMike Snitzer 15991be56909SChristoph Hellwig r = DM_ENDIO_INCOMPLETE; 160014ef1e48SChristoph Hellwig done: 160176e33fe4SMike Snitzer if (pgpath) { 160214ef1e48SChristoph Hellwig struct path_selector *ps = &pgpath->pg->ps; 160314ef1e48SChristoph Hellwig 160476e33fe4SMike Snitzer if (ps->type->end_io) 160576e33fe4SMike Snitzer ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); 160676e33fe4SMike Snitzer } 160776e33fe4SMike Snitzer 16081be56909SChristoph Hellwig return r; 160976e33fe4SMike Snitzer } 161076e33fe4SMike Snitzer 16111da177e4SLinus Torvalds /* 16121da177e4SLinus Torvalds * Suspend can't complete until all the I/O is processed so if 1613436d4108SAlasdair G Kergon * the last path fails we must error any remaining I/O. 1614436d4108SAlasdair G Kergon * Note that if the freeze_bdev fails while suspending, the 1615436d4108SAlasdair G Kergon * queue_if_no_path state is lost - userspace should reset it. 16161da177e4SLinus Torvalds */ 16171da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti) 16181da177e4SLinus Torvalds { 16197943bd6dSMike Snitzer struct multipath *m = ti->private; 16201da177e4SLinus Torvalds 1621be7d31ccSMike Snitzer queue_if_no_path(m, false, true); 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds 16246df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti) 16256df400abSKiyoshi Ueda { 16266380f26fSMike Anderson struct multipath *m = ti->private; 16276380f26fSMike Anderson 16286380f26fSMike Anderson mutex_lock(&m->work_mutex); 16292bded7bdSKiyoshi Ueda flush_multipath_work(m); 16306380f26fSMike Anderson mutex_unlock(&m->work_mutex); 16316df400abSKiyoshi Ueda } 16326df400abSKiyoshi Ueda 1633436d4108SAlasdair G Kergon /* 1634436d4108SAlasdair G Kergon * Restore the queue_if_no_path setting. 1635436d4108SAlasdair G Kergon */ 16361da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti) 16371da177e4SLinus Torvalds { 16387943bd6dSMike Snitzer struct multipath *m = ti->private; 16391814f2e3SMike Snitzer unsigned long flags; 16401da177e4SLinus Torvalds 16411814f2e3SMike Snitzer spin_lock_irqsave(&m->lock, flags); 16425307e2adSLukas Wunner assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, 16435307e2adSLukas Wunner test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)); 16441814f2e3SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 16451da177e4SLinus Torvalds } 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds /* 16481da177e4SLinus Torvalds * Info output has the following format: 16491da177e4SLinus Torvalds * num_multipath_feature_args [multipath_feature_args]* 16501da177e4SLinus Torvalds * num_handler_status_args [handler_status_args]* 16511da177e4SLinus Torvalds * num_groups init_group_number 16521da177e4SLinus Torvalds * [A|D|E num_ps_status_args [ps_status_args]* 16531da177e4SLinus Torvalds * num_paths num_selector_args 16541da177e4SLinus Torvalds * [path_dev A|F fail_count [selector_args]* ]+ ]+ 16551da177e4SLinus Torvalds * 16561da177e4SLinus Torvalds * Table output has the following format (identical to the constructor string): 16571da177e4SLinus Torvalds * num_feature_args [features_args]* 16581da177e4SLinus Torvalds * num_handler_args hw_handler [hw_handler_args]* 16591da177e4SLinus Torvalds * num_groups init_group_number 16601da177e4SLinus Torvalds * [priority selector-name num_ps_args [ps_args]* 16611da177e4SLinus Torvalds * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ 16621da177e4SLinus Torvalds */ 1663fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type, 16641f4e0ff0SAlasdair G Kergon unsigned status_flags, char *result, unsigned maxlen) 16651da177e4SLinus Torvalds { 16661da177e4SLinus Torvalds int sz = 0; 16671da177e4SLinus Torvalds unsigned long flags; 16687943bd6dSMike Snitzer struct multipath *m = ti->private; 16691da177e4SLinus Torvalds struct priority_group *pg; 16701da177e4SLinus Torvalds struct pgpath *p; 16711da177e4SLinus Torvalds unsigned pg_num; 16721da177e4SLinus Torvalds char state; 16731da177e4SLinus Torvalds 16741da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 16751da177e4SLinus Torvalds 16761da177e4SLinus Torvalds /* Features */ 16771da177e4SLinus Torvalds if (type == STATUSTYPE_INFO) 167891e968aaSMike Snitzer DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags), 167991e968aaSMike Snitzer atomic_read(&m->pg_init_count)); 1680c9e45581SDave Wysochanski else { 1681518257b1SMike Snitzer DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) + 16824e2d19e4SChandra Seetharaman (m->pg_init_retries > 0) * 2 + 1683a58a935dSMike Snitzer (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 + 1684e83068a5SMike Snitzer test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) + 1685e83068a5SMike Snitzer (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2); 1686e83068a5SMike Snitzer 1687518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 1688c9e45581SDave Wysochanski DMEMIT("queue_if_no_path "); 1689c9e45581SDave Wysochanski if (m->pg_init_retries) 1690c9e45581SDave Wysochanski DMEMIT("pg_init_retries %u ", m->pg_init_retries); 16914e2d19e4SChandra Seetharaman if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) 16924e2d19e4SChandra Seetharaman DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs); 1693518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) 1694a58a935dSMike Snitzer DMEMIT("retain_attached_hw_handler "); 1695e83068a5SMike Snitzer if (m->queue_mode != DM_TYPE_REQUEST_BASED) { 1696e83068a5SMike Snitzer switch(m->queue_mode) { 1697e83068a5SMike Snitzer case DM_TYPE_BIO_BASED: 1698e83068a5SMike Snitzer DMEMIT("queue_mode bio "); 1699e83068a5SMike Snitzer break; 17007e0d574fSBart Van Assche default: 17017e0d574fSBart Van Assche WARN_ON_ONCE(true); 17027e0d574fSBart Van Assche break; 1703e83068a5SMike Snitzer } 1704e83068a5SMike Snitzer } 1705c9e45581SDave Wysochanski } 17061da177e4SLinus Torvalds 1707cfae5c9bSChandra Seetharaman if (!m->hw_handler_name || type == STATUSTYPE_INFO) 17081da177e4SLinus Torvalds DMEMIT("0 "); 17091da177e4SLinus Torvalds else 1710cfae5c9bSChandra Seetharaman DMEMIT("1 %s ", m->hw_handler_name); 17111da177e4SLinus Torvalds 17121da177e4SLinus Torvalds DMEMIT("%u ", m->nr_priority_groups); 17131da177e4SLinus Torvalds 17141da177e4SLinus Torvalds if (m->next_pg) 17151da177e4SLinus Torvalds pg_num = m->next_pg->pg_num; 17161da177e4SLinus Torvalds else if (m->current_pg) 17171da177e4SLinus Torvalds pg_num = m->current_pg->pg_num; 17181da177e4SLinus Torvalds else 1719a490a07aSMike Snitzer pg_num = (m->nr_priority_groups ? 1 : 0); 17201da177e4SLinus Torvalds 17211da177e4SLinus Torvalds DMEMIT("%u ", pg_num); 17221da177e4SLinus Torvalds 17231da177e4SLinus Torvalds switch (type) { 17241da177e4SLinus Torvalds case STATUSTYPE_INFO: 17251da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 17261da177e4SLinus Torvalds if (pg->bypassed) 17271da177e4SLinus Torvalds state = 'D'; /* Disabled */ 17281da177e4SLinus Torvalds else if (pg == m->current_pg) 17291da177e4SLinus Torvalds state = 'A'; /* Currently Active */ 17301da177e4SLinus Torvalds else 17311da177e4SLinus Torvalds state = 'E'; /* Enabled */ 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds DMEMIT("%c ", state); 17341da177e4SLinus Torvalds 17351da177e4SLinus Torvalds if (pg->ps.type->status) 17361da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 17371da177e4SLinus Torvalds result + sz, 17381da177e4SLinus Torvalds maxlen - sz); 17391da177e4SLinus Torvalds else 17401da177e4SLinus Torvalds DMEMIT("0 "); 17411da177e4SLinus Torvalds 17421da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 17431da177e4SLinus Torvalds pg->ps.type->info_args); 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 17461da177e4SLinus Torvalds DMEMIT("%s %s %u ", p->path.dev->name, 17476680073dSKiyoshi Ueda p->is_active ? "A" : "F", 17481da177e4SLinus Torvalds p->fail_count); 17491da177e4SLinus Torvalds if (pg->ps.type->status) 17501da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 17511da177e4SLinus Torvalds &p->path, type, result + sz, 17521da177e4SLinus Torvalds maxlen - sz); 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds } 17551da177e4SLinus Torvalds break; 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds case STATUSTYPE_TABLE: 17581da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 17591da177e4SLinus Torvalds DMEMIT("%s ", pg->ps.type->name); 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds if (pg->ps.type->status) 17621da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 17631da177e4SLinus Torvalds result + sz, 17641da177e4SLinus Torvalds maxlen - sz); 17651da177e4SLinus Torvalds else 17661da177e4SLinus Torvalds DMEMIT("0 "); 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 17691da177e4SLinus Torvalds pg->ps.type->table_args); 17701da177e4SLinus Torvalds 17711da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 17721da177e4SLinus Torvalds DMEMIT("%s ", p->path.dev->name); 17731da177e4SLinus Torvalds if (pg->ps.type->status) 17741da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 17751da177e4SLinus Torvalds &p->path, type, result + sz, 17761da177e4SLinus Torvalds maxlen - sz); 17771da177e4SLinus Torvalds } 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds break; 17801da177e4SLinus Torvalds } 17811da177e4SLinus Torvalds 17821da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 17831da177e4SLinus Torvalds } 17841da177e4SLinus Torvalds 17851eb5fa84SMike Snitzer static int multipath_message(struct dm_target *ti, unsigned argc, char **argv, 17861eb5fa84SMike Snitzer char *result, unsigned maxlen) 17871da177e4SLinus Torvalds { 17886380f26fSMike Anderson int r = -EINVAL; 17891da177e4SLinus Torvalds struct dm_dev *dev; 17907943bd6dSMike Snitzer struct multipath *m = ti->private; 17911da177e4SLinus Torvalds action_fn action; 17921da177e4SLinus Torvalds 17936380f26fSMike Anderson mutex_lock(&m->work_mutex); 17946380f26fSMike Anderson 1795c2f3d24bSKiyoshi Ueda if (dm_suspended(ti)) { 1796c2f3d24bSKiyoshi Ueda r = -EBUSY; 1797c2f3d24bSKiyoshi Ueda goto out; 1798c2f3d24bSKiyoshi Ueda } 1799c2f3d24bSKiyoshi Ueda 18001da177e4SLinus Torvalds if (argc == 1) { 1801498f0103SMike Snitzer if (!strcasecmp(argv[0], "queue_if_no_path")) { 1802be7d31ccSMike Snitzer r = queue_if_no_path(m, true, false); 18036380f26fSMike Anderson goto out; 1804498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "fail_if_no_path")) { 1805be7d31ccSMike Snitzer r = queue_if_no_path(m, false, false); 18066380f26fSMike Anderson goto out; 18076380f26fSMike Anderson } 18081da177e4SLinus Torvalds } 18091da177e4SLinus Torvalds 18106380f26fSMike Anderson if (argc != 2) { 1811a356e426SJose Castillo DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc); 18126380f26fSMike Anderson goto out; 18136380f26fSMike Anderson } 18141da177e4SLinus Torvalds 1815498f0103SMike Snitzer if (!strcasecmp(argv[0], "disable_group")) { 1816be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], true); 18176380f26fSMike Anderson goto out; 1818498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "enable_group")) { 1819be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], false); 18206380f26fSMike Anderson goto out; 1821498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "switch_group")) { 18226380f26fSMike Anderson r = switch_pg_num(m, argv[1]); 18236380f26fSMike Anderson goto out; 1824498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "reinstate_path")) 18251da177e4SLinus Torvalds action = reinstate_path; 1826498f0103SMike Snitzer else if (!strcasecmp(argv[0], "fail_path")) 18271da177e4SLinus Torvalds action = fail_path; 18286380f26fSMike Anderson else { 1829a356e426SJose Castillo DMWARN("Unrecognised multipath message received: %s", argv[0]); 18306380f26fSMike Anderson goto out; 18316380f26fSMike Anderson } 18321da177e4SLinus Torvalds 18338215d6ecSNikanth Karthikesan r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev); 18341da177e4SLinus Torvalds if (r) { 183572d94861SAlasdair G Kergon DMWARN("message: error getting device %s", 18361da177e4SLinus Torvalds argv[1]); 18376380f26fSMike Anderson goto out; 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds r = action_dev(m, dev, action); 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds dm_put_device(ti, dev); 18431da177e4SLinus Torvalds 18446380f26fSMike Anderson out: 18456380f26fSMike Anderson mutex_unlock(&m->work_mutex); 18461da177e4SLinus Torvalds return r; 18471da177e4SLinus Torvalds } 18481da177e4SLinus Torvalds 1849e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti, 18505bd5e8d8SMike Snitzer struct block_device **bdev) 18519af4aa30SMilan Broz { 185235991652SMikulas Patocka struct multipath *m = ti->private; 18532da1610aSMike Snitzer struct pgpath *current_pgpath; 185435991652SMikulas Patocka int r; 185535991652SMikulas Patocka 1856506458efSWill Deacon current_pgpath = READ_ONCE(m->current_pgpath); 18572da1610aSMike Snitzer if (!current_pgpath) 18582da1610aSMike Snitzer current_pgpath = choose_pgpath(m, 0); 18599af4aa30SMilan Broz 18602da1610aSMike Snitzer if (current_pgpath) { 1861518257b1SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) { 18622da1610aSMike Snitzer *bdev = current_pgpath->path.dev->bdev; 186343e43c9eSJunichi Nomura r = 0; 186443e43c9eSJunichi Nomura } else { 186543e43c9eSJunichi Nomura /* pg_init has not started or completed */ 18666c182cd8SHannes Reinecke r = -ENOTCONN; 186743e43c9eSJunichi Nomura } 186843e43c9eSJunichi Nomura } else { 186943e43c9eSJunichi Nomura /* No path is available */ 1870518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 187143e43c9eSJunichi Nomura r = -ENOTCONN; 187243e43c9eSJunichi Nomura else 18739af4aa30SMilan Broz r = -EIO; 187443e43c9eSJunichi Nomura } 18759af4aa30SMilan Broz 18765bbbfdf6SJunichi Nomura if (r == -ENOTCONN) { 1877506458efSWill Deacon if (!READ_ONCE(m->current_pg)) { 18783e9f1be1SHannes Reinecke /* Path status changed, redo selection */ 18792da1610aSMike Snitzer (void) choose_pgpath(m, 0); 18803e9f1be1SHannes Reinecke } 1881518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 18822da1610aSMike Snitzer pg_init_all_paths(m); 188363d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 18847e48c768SMike Snitzer process_queued_io_list(m); 18853e9f1be1SHannes Reinecke } 188635991652SMikulas Patocka 1887e56f81e0SChristoph Hellwig /* 1888e56f81e0SChristoph Hellwig * Only pass ioctls through if the device sizes match exactly. 1889e56f81e0SChristoph Hellwig */ 1890e56f81e0SChristoph Hellwig if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT) 1891e56f81e0SChristoph Hellwig return 1; 1892e56f81e0SChristoph Hellwig return r; 18939af4aa30SMilan Broz } 18949af4aa30SMilan Broz 1895af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti, 1896af4874e0SMike Snitzer iterate_devices_callout_fn fn, void *data) 1897af4874e0SMike Snitzer { 1898af4874e0SMike Snitzer struct multipath *m = ti->private; 1899af4874e0SMike Snitzer struct priority_group *pg; 1900af4874e0SMike Snitzer struct pgpath *p; 1901af4874e0SMike Snitzer int ret = 0; 1902af4874e0SMike Snitzer 1903af4874e0SMike Snitzer list_for_each_entry(pg, &m->priority_groups, list) { 1904af4874e0SMike Snitzer list_for_each_entry(p, &pg->pgpaths, list) { 19055dea271bSMike Snitzer ret = fn(ti, p->path.dev, ti->begin, ti->len, data); 1906af4874e0SMike Snitzer if (ret) 1907af4874e0SMike Snitzer goto out; 1908af4874e0SMike Snitzer } 1909af4874e0SMike Snitzer } 1910af4874e0SMike Snitzer 1911af4874e0SMike Snitzer out: 1912af4874e0SMike Snitzer return ret; 1913af4874e0SMike Snitzer } 1914af4874e0SMike Snitzer 19159f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath) 1916f40c67f0SKiyoshi Ueda { 1917f40c67f0SKiyoshi Ueda struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1918f40c67f0SKiyoshi Ueda 191952b09914SMike Snitzer return blk_lld_busy(q); 1920f40c67f0SKiyoshi Ueda } 1921f40c67f0SKiyoshi Ueda 1922f40c67f0SKiyoshi Ueda /* 1923f40c67f0SKiyoshi Ueda * We return "busy", only when we can map I/Os but underlying devices 1924f40c67f0SKiyoshi Ueda * are busy (so even if we map I/Os now, the I/Os will wait on 1925f40c67f0SKiyoshi Ueda * the underlying queue). 1926f40c67f0SKiyoshi Ueda * In other words, if we want to kill I/Os or queue them inside us 1927f40c67f0SKiyoshi Ueda * due to map unavailability, we don't return "busy". Otherwise, 1928f40c67f0SKiyoshi Ueda * dm core won't give us the I/Os and we can't do what we want. 1929f40c67f0SKiyoshi Ueda */ 1930f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti) 1931f40c67f0SKiyoshi Ueda { 1932be7d31ccSMike Snitzer bool busy = false, has_active = false; 1933f40c67f0SKiyoshi Ueda struct multipath *m = ti->private; 19342da1610aSMike Snitzer struct priority_group *pg, *next_pg; 1935f40c67f0SKiyoshi Ueda struct pgpath *pgpath; 1936f40c67f0SKiyoshi Ueda 1937b88efd43SMike Snitzer /* pg_init in progress */ 1938b88efd43SMike Snitzer if (atomic_read(&m->pg_init_in_progress)) 19392da1610aSMike Snitzer return true; 19402da1610aSMike Snitzer 1941b88efd43SMike Snitzer /* no paths available, for blk-mq: rely on IO mapping to delay requeue */ 1942b88efd43SMike Snitzer if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 1943953923c0SMike Snitzer return (m->queue_mode != DM_TYPE_REQUEST_BASED); 1944b88efd43SMike Snitzer 1945f40c67f0SKiyoshi Ueda /* Guess which priority_group will be used at next mapping time */ 1946506458efSWill Deacon pg = READ_ONCE(m->current_pg); 1947506458efSWill Deacon next_pg = READ_ONCE(m->next_pg); 1948506458efSWill Deacon if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg)) 19492da1610aSMike Snitzer pg = next_pg; 19502da1610aSMike Snitzer 19512da1610aSMike Snitzer if (!pg) { 1952f40c67f0SKiyoshi Ueda /* 1953f40c67f0SKiyoshi Ueda * We don't know which pg will be used at next mapping time. 19542da1610aSMike Snitzer * We don't call choose_pgpath() here to avoid to trigger 1955f40c67f0SKiyoshi Ueda * pg_init just by busy checking. 1956f40c67f0SKiyoshi Ueda * So we don't know whether underlying devices we will be using 1957f40c67f0SKiyoshi Ueda * at next mapping time are busy or not. Just try mapping. 1958f40c67f0SKiyoshi Ueda */ 19592da1610aSMike Snitzer return busy; 19602da1610aSMike Snitzer } 1961f40c67f0SKiyoshi Ueda 1962f40c67f0SKiyoshi Ueda /* 1963f40c67f0SKiyoshi Ueda * If there is one non-busy active path at least, the path selector 1964f40c67f0SKiyoshi Ueda * will be able to select it. So we consider such a pg as not busy. 1965f40c67f0SKiyoshi Ueda */ 1966be7d31ccSMike Snitzer busy = true; 19672da1610aSMike Snitzer list_for_each_entry(pgpath, &pg->pgpaths, list) { 1968f40c67f0SKiyoshi Ueda if (pgpath->is_active) { 1969be7d31ccSMike Snitzer has_active = true; 19709f54cec5SMike Snitzer if (!pgpath_busy(pgpath)) { 1971be7d31ccSMike Snitzer busy = false; 1972f40c67f0SKiyoshi Ueda break; 1973f40c67f0SKiyoshi Ueda } 1974f40c67f0SKiyoshi Ueda } 19752da1610aSMike Snitzer } 1976f40c67f0SKiyoshi Ueda 19772da1610aSMike Snitzer if (!has_active) { 1978f40c67f0SKiyoshi Ueda /* 1979f40c67f0SKiyoshi Ueda * No active path in this pg, so this pg won't be used and 1980f40c67f0SKiyoshi Ueda * the current_pg will be changed at next mapping time. 1981f40c67f0SKiyoshi Ueda * We need to try mapping to determine it. 1982f40c67f0SKiyoshi Ueda */ 1983be7d31ccSMike Snitzer busy = false; 19842da1610aSMike Snitzer } 1985f40c67f0SKiyoshi Ueda 1986f40c67f0SKiyoshi Ueda return busy; 1987f40c67f0SKiyoshi Ueda } 1988f40c67f0SKiyoshi Ueda 19891da177e4SLinus Torvalds /*----------------------------------------------------------------- 19901da177e4SLinus Torvalds * Module setup 19911da177e4SLinus Torvalds *---------------------------------------------------------------*/ 19921da177e4SLinus Torvalds static struct target_type multipath_target = { 19931da177e4SLinus Torvalds .name = "multipath", 19948c5c1473SSteffen Maier .version = {1, 13, 0}, 19958c5c1473SSteffen Maier .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE | 19968c5c1473SSteffen Maier DM_TARGET_PASSES_INTEGRITY, 19971da177e4SLinus Torvalds .module = THIS_MODULE, 19981da177e4SLinus Torvalds .ctr = multipath_ctr, 19991da177e4SLinus Torvalds .dtr = multipath_dtr, 2000e5863d9aSMike Snitzer .clone_and_map_rq = multipath_clone_and_map, 2001e5863d9aSMike Snitzer .release_clone_rq = multipath_release_clone, 2002f40c67f0SKiyoshi Ueda .rq_end_io = multipath_end_io, 200376e33fe4SMike Snitzer .map = multipath_map_bio, 200476e33fe4SMike Snitzer .end_io = multipath_end_io_bio, 200576e33fe4SMike Snitzer .presuspend = multipath_presuspend, 200676e33fe4SMike Snitzer .postsuspend = multipath_postsuspend, 200776e33fe4SMike Snitzer .resume = multipath_resume, 200876e33fe4SMike Snitzer .status = multipath_status, 200976e33fe4SMike Snitzer .message = multipath_message, 201076e33fe4SMike Snitzer .prepare_ioctl = multipath_prepare_ioctl, 201176e33fe4SMike Snitzer .iterate_devices = multipath_iterate_devices, 201276e33fe4SMike Snitzer .busy = multipath_busy, 201376e33fe4SMike Snitzer }; 201476e33fe4SMike Snitzer 20151da177e4SLinus Torvalds static int __init dm_multipath_init(void) 20161da177e4SLinus Torvalds { 20171da177e4SLinus Torvalds int r; 20181da177e4SLinus Torvalds 20194d4d66abSTejun Heo kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0); 2020c557308eSAlasdair G Kergon if (!kmultipathd) { 20210cd33124SAlasdair G Kergon DMERR("failed to create workqueue kmpathd"); 2022ff658e9cSJohannes Thumshirn r = -ENOMEM; 2023ff658e9cSJohannes Thumshirn goto bad_alloc_kmultipathd; 2024c557308eSAlasdair G Kergon } 2025c557308eSAlasdair G Kergon 2026bab7cfc7SChandra Seetharaman /* 2027bab7cfc7SChandra Seetharaman * A separate workqueue is used to handle the device handlers 2028bab7cfc7SChandra Seetharaman * to avoid overloading existing workqueue. Overloading the 2029bab7cfc7SChandra Seetharaman * old workqueue would also create a bottleneck in the 2030bab7cfc7SChandra Seetharaman * path of the storage hardware device activation. 2031bab7cfc7SChandra Seetharaman */ 20324d4d66abSTejun Heo kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd", 20334d4d66abSTejun Heo WQ_MEM_RECLAIM); 2034bab7cfc7SChandra Seetharaman if (!kmpath_handlerd) { 2035bab7cfc7SChandra Seetharaman DMERR("failed to create workqueue kmpath_handlerd"); 2036ff658e9cSJohannes Thumshirn r = -ENOMEM; 2037ff658e9cSJohannes Thumshirn goto bad_alloc_kmpath_handlerd; 2038bab7cfc7SChandra Seetharaman } 2039bab7cfc7SChandra Seetharaman 20407e6358d2Smonty_pavel@sina.com r = dm_register_target(&multipath_target); 20417e6358d2Smonty_pavel@sina.com if (r < 0) { 20427e6358d2Smonty_pavel@sina.com DMERR("request-based register failed %d", r); 20437e6358d2Smonty_pavel@sina.com r = -EINVAL; 20447e6358d2Smonty_pavel@sina.com goto bad_register_target; 20457e6358d2Smonty_pavel@sina.com } 20467e6358d2Smonty_pavel@sina.com 2047ff658e9cSJohannes Thumshirn return 0; 2048ff658e9cSJohannes Thumshirn 20497e6358d2Smonty_pavel@sina.com bad_register_target: 20507e6358d2Smonty_pavel@sina.com destroy_workqueue(kmpath_handlerd); 2051ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd: 2052ff658e9cSJohannes Thumshirn destroy_workqueue(kmultipathd); 2053ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd: 20541da177e4SLinus Torvalds return r; 20551da177e4SLinus Torvalds } 20561da177e4SLinus Torvalds 20571da177e4SLinus Torvalds static void __exit dm_multipath_exit(void) 20581da177e4SLinus Torvalds { 2059bab7cfc7SChandra Seetharaman destroy_workqueue(kmpath_handlerd); 2060c557308eSAlasdair G Kergon destroy_workqueue(kmultipathd); 2061c557308eSAlasdair G Kergon 206210d3bd09SMikulas Patocka dm_unregister_target(&multipath_target); 20631da177e4SLinus Torvalds } 20641da177e4SLinus Torvalds 20651da177e4SLinus Torvalds module_init(dm_multipath_init); 20661da177e4SLinus Torvalds module_exit(dm_multipath_exit); 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target"); 20691da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); 20701da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2071