13bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Copyright (C) 2003 Sistina Software Limited. 41da177e4SLinus Torvalds * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * This file is released under the GPL. 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 9586e80e6SMikulas Patocka #include <linux/device-mapper.h> 10586e80e6SMikulas Patocka 114cc96131SMike Snitzer #include "dm-rq.h" 1276e33fe4SMike Snitzer #include "dm-bio-record.h" 131da177e4SLinus Torvalds #include "dm-path-selector.h" 14b15546f9SMike Anderson #include "dm-uevent.h" 151da177e4SLinus Torvalds 16e5863d9aSMike Snitzer #include <linux/blkdev.h> 171da177e4SLinus Torvalds #include <linux/ctype.h> 181da177e4SLinus Torvalds #include <linux/init.h> 191da177e4SLinus Torvalds #include <linux/mempool.h> 201da177e4SLinus Torvalds #include <linux/module.h> 211da177e4SLinus Torvalds #include <linux/pagemap.h> 221da177e4SLinus Torvalds #include <linux/slab.h> 231da177e4SLinus Torvalds #include <linux/time.h> 24be240ff5SAnatol Pomazau #include <linux/timer.h> 251da177e4SLinus Torvalds #include <linux/workqueue.h> 2635991652SMikulas Patocka #include <linux/delay.h> 27cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h> 2860063497SArun Sharma #include <linux/atomic.h> 2978ce23b5SMike Snitzer #include <linux/blk-mq.h> 301da177e4SLinus Torvalds 3172d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath" 324e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000 33*86a3238cSHeinz Mauelshagen #define DM_PG_INIT_DELAY_DEFAULT ((unsigned int) -1) 34be240ff5SAnatol Pomazau #define QUEUE_IF_NO_PATH_TIMEOUT_DEFAULT 0 35be240ff5SAnatol Pomazau 36be240ff5SAnatol Pomazau static unsigned long queue_if_no_path_timeout_secs = QUEUE_IF_NO_PATH_TIMEOUT_DEFAULT; 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds /* Path properties */ 391da177e4SLinus Torvalds struct pgpath { 401da177e4SLinus Torvalds struct list_head list; 411da177e4SLinus Torvalds 421da177e4SLinus Torvalds struct priority_group *pg; /* Owning PG */ 43*86a3238cSHeinz Mauelshagen unsigned int fail_count; /* Cumulative failure count */ 441da177e4SLinus Torvalds 45c922d5f7SJosef "Jeff" Sipek struct dm_path path; 464e2d19e4SChandra Seetharaman struct delayed_work activate_path; 47be7d31ccSMike Snitzer 48be7d31ccSMike Snitzer bool is_active:1; /* Path status */ 491da177e4SLinus Torvalds }; 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path) 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* 541da177e4SLinus Torvalds * Paths are grouped into Priority Groups and numbered from 1 upwards. 551da177e4SLinus Torvalds * Each has a path selector which controls which path gets used. 561da177e4SLinus Torvalds */ 571da177e4SLinus Torvalds struct priority_group { 581da177e4SLinus Torvalds struct list_head list; 591da177e4SLinus Torvalds 601da177e4SLinus Torvalds struct multipath *m; /* Owning multipath instance */ 611da177e4SLinus Torvalds struct path_selector ps; 621da177e4SLinus Torvalds 63*86a3238cSHeinz Mauelshagen unsigned int pg_num; /* Reference number */ 64*86a3238cSHeinz Mauelshagen unsigned int nr_pgpaths; /* Number of paths in PG */ 651da177e4SLinus Torvalds struct list_head pgpaths; 66be7d31ccSMike Snitzer 67be7d31ccSMike Snitzer bool bypassed:1; /* Temporarily bypass this PG? */ 681da177e4SLinus Torvalds }; 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds /* Multipath context */ 711da177e4SLinus Torvalds struct multipath { 72848b8aefSMike Snitzer unsigned long flags; /* Multipath state flags */ 734e2d19e4SChandra Seetharaman 741fbdd2b3SMike Snitzer spinlock_t lock; 75848b8aefSMike Snitzer enum dm_queue_mode queue_mode; 764e2d19e4SChandra Seetharaman 771da177e4SLinus Torvalds struct pgpath *current_pgpath; 781da177e4SLinus Torvalds struct priority_group *current_pg; 791da177e4SLinus Torvalds struct priority_group *next_pg; /* Switch to this PG if set */ 801da177e4SLinus Torvalds 81848b8aefSMike Snitzer atomic_t nr_valid_paths; /* Total number of usable paths */ 82*86a3238cSHeinz Mauelshagen unsigned int nr_priority_groups; 83848b8aefSMike Snitzer struct list_head priority_groups; 841fbdd2b3SMike Snitzer 85848b8aefSMike Snitzer const char *hw_handler_name; 86848b8aefSMike Snitzer char *hw_handler_params; 87848b8aefSMike Snitzer wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */ 88*86a3238cSHeinz Mauelshagen unsigned int pg_init_retries; /* Number of times to retry pg_init */ 89*86a3238cSHeinz Mauelshagen unsigned int pg_init_delay_msecs; /* Number of msecs before pg_init retry */ 9091e968aaSMike Snitzer atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */ 9191e968aaSMike Snitzer atomic_t pg_init_count; /* Number of times pg_init called */ 9291e968aaSMike Snitzer 936380f26fSMike Anderson struct mutex work_mutex; 9420800cb3SMike Snitzer struct work_struct trigger_event; 95848b8aefSMike Snitzer struct dm_target *ti; 9676e33fe4SMike Snitzer 9776e33fe4SMike Snitzer struct work_struct process_queued_bios; 9876e33fe4SMike Snitzer struct bio_list queued_bios; 99be240ff5SAnatol Pomazau 100be240ff5SAnatol Pomazau struct timer_list nopath_timer; /* Timeout for queue_if_no_path */ 1011da177e4SLinus Torvalds }; 1021da177e4SLinus Torvalds 1031da177e4SLinus Torvalds /* 10476e33fe4SMike Snitzer * Context information attached to each io we process. 1051da177e4SLinus Torvalds */ 106028867acSAlasdair G Kergon struct dm_mpath_io { 1071da177e4SLinus Torvalds struct pgpath *pgpath; 10802ab823fSKiyoshi Ueda size_t nr_bytes; 109c06dfd12SGabriel Krisman Bertazi u64 start_time_ns; 1101da177e4SLinus Torvalds }; 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath); 1131da177e4SLinus Torvalds 114bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd; 115c4028958SDavid Howells static void trigger_event(struct work_struct *work); 11689bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath); 11789bfce76SBart Van Assche static void activate_path_work(struct work_struct *work); 11876e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work); 119be240ff5SAnatol Pomazau static void queue_if_no_path_timeout_work(struct timer_list *t); 1201da177e4SLinus Torvalds 121518257b1SMike Snitzer /*----------------------------------------------- 122518257b1SMike Snitzer * Multipath state flags. 123518257b1SMike Snitzer *-----------------------------------------------*/ 124518257b1SMike Snitzer 125518257b1SMike Snitzer #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */ 126518257b1SMike Snitzer #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */ 127518257b1SMike Snitzer #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */ 128518257b1SMike Snitzer #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */ 129518257b1SMike Snitzer #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */ 130518257b1SMike Snitzer #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */ 131518257b1SMike Snitzer #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */ 1321da177e4SLinus Torvalds 133374117adSMike Snitzer static bool mpath_double_check_test_bit(int MPATHF_bit, struct multipath *m) 134374117adSMike Snitzer { 135374117adSMike Snitzer bool r = test_bit(MPATHF_bit, &m->flags); 136374117adSMike Snitzer 137374117adSMike Snitzer if (r) { 138374117adSMike Snitzer unsigned long flags; 139374117adSMike Snitzer spin_lock_irqsave(&m->lock, flags); 140374117adSMike Snitzer r = test_bit(MPATHF_bit, &m->flags); 141374117adSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 142374117adSMike Snitzer } 143374117adSMike Snitzer 144374117adSMike Snitzer return r; 145374117adSMike Snitzer } 146374117adSMike Snitzer 1471da177e4SLinus Torvalds /*----------------------------------------------- 1481da177e4SLinus Torvalds * Allocation routines 1491da177e4SLinus Torvalds *-----------------------------------------------*/ 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void) 1521da177e4SLinus Torvalds { 153e69fae56SMicha³ Miros³aw struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL); 1541da177e4SLinus Torvalds 155848b8aefSMike Snitzer if (!pgpath) 156848b8aefSMike Snitzer return NULL; 157848b8aefSMike Snitzer 158be7d31ccSMike Snitzer pgpath->is_active = true; 1591da177e4SLinus Torvalds 1601da177e4SLinus Torvalds return pgpath; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 163028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath) 1641da177e4SLinus Torvalds { 1651da177e4SLinus Torvalds kfree(pgpath); 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void) 1691da177e4SLinus Torvalds { 1701da177e4SLinus Torvalds struct priority_group *pg; 1711da177e4SLinus Torvalds 172e69fae56SMicha³ Miros³aw pg = kzalloc(sizeof(*pg), GFP_KERNEL); 1731da177e4SLinus Torvalds 174e69fae56SMicha³ Miros³aw if (pg) 1751da177e4SLinus Torvalds INIT_LIST_HEAD(&pg->pgpaths); 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds return pg; 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti) 1811da177e4SLinus Torvalds { 1821da177e4SLinus Torvalds struct pgpath *pgpath, *tmp; 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds list_for_each_entry_safe(pgpath, tmp, pgpaths, list) { 1851da177e4SLinus Torvalds list_del(&pgpath->list); 1861da177e4SLinus Torvalds dm_put_device(ti, pgpath->path.dev); 1871da177e4SLinus Torvalds free_pgpath(pgpath); 1881da177e4SLinus Torvalds } 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg, 1921da177e4SLinus Torvalds struct dm_target *ti) 1931da177e4SLinus Torvalds { 1941da177e4SLinus Torvalds struct path_selector *ps = &pg->ps; 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds if (ps->type) { 1971da177e4SLinus Torvalds ps->type->destroy(ps); 1981da177e4SLinus Torvalds dm_put_path_selector(ps->type); 1991da177e4SLinus Torvalds } 2001da177e4SLinus Torvalds 2011da177e4SLinus Torvalds free_pgpaths(&pg->pgpaths, ti); 2021da177e4SLinus Torvalds kfree(pg); 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 205e83068a5SMike Snitzer static struct multipath *alloc_multipath(struct dm_target *ti) 2061da177e4SLinus Torvalds { 2071da177e4SLinus Torvalds struct multipath *m; 2081da177e4SLinus Torvalds 209e69fae56SMicha³ Miros³aw m = kzalloc(sizeof(*m), GFP_KERNEL); 2101da177e4SLinus Torvalds if (m) { 2111da177e4SLinus Torvalds INIT_LIST_HEAD(&m->priority_groups); 2121da177e4SLinus Torvalds spin_lock_init(&m->lock); 21391e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, 0); 214c4028958SDavid Howells INIT_WORK(&m->trigger_event, trigger_event); 2156380f26fSMike Anderson mutex_init(&m->work_mutex); 2168637a6bfSMike Snitzer 217e83068a5SMike Snitzer m->queue_mode = DM_TYPE_NONE; 218e83068a5SMike Snitzer 219e83068a5SMike Snitzer m->ti = ti; 220e83068a5SMike Snitzer ti->private = m; 221be240ff5SAnatol Pomazau 222be240ff5SAnatol Pomazau timer_setup(&m->nopath_timer, queue_if_no_path_timeout_work, 0); 223e83068a5SMike Snitzer } 224e83068a5SMike Snitzer 225e83068a5SMike Snitzer return m; 226e83068a5SMike Snitzer } 227e83068a5SMike Snitzer 228e83068a5SMike Snitzer static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m) 229e83068a5SMike Snitzer { 230e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_NONE) { 231953923c0SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 2328d47e659SMike Snitzer } else if (m->queue_mode == DM_TYPE_BIO_BASED) { 23376e33fe4SMike Snitzer INIT_WORK(&m->process_queued_bios, process_queued_bios); 23476e33fe4SMike Snitzer /* 23576e33fe4SMike Snitzer * bio-based doesn't support any direct scsi_dh management; 23676e33fe4SMike Snitzer * it just discovers if a scsi_dh is attached. 23776e33fe4SMike Snitzer */ 23876e33fe4SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 23976e33fe4SMike Snitzer } 24076e33fe4SMike Snitzer 241e83068a5SMike Snitzer dm_table_set_type(ti->table, m->queue_mode); 2421da177e4SLinus Torvalds 243c3736674SMike Snitzer /* 244c3736674SMike Snitzer * Init fields that are only used when a scsi_dh is attached 245c3736674SMike Snitzer * - must do this unconditionally (really doesn't hurt non-SCSI uses) 246c3736674SMike Snitzer */ 247c3736674SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 248c3736674SMike Snitzer atomic_set(&m->pg_init_in_progress, 0); 249c3736674SMike Snitzer atomic_set(&m->pg_init_count, 0); 250c3736674SMike Snitzer m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT; 251c3736674SMike Snitzer init_waitqueue_head(&m->pg_init_wait); 252c3736674SMike Snitzer 253e83068a5SMike Snitzer return 0; 2541da177e4SLinus Torvalds } 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds static void free_multipath(struct multipath *m) 2571da177e4SLinus Torvalds { 2581da177e4SLinus Torvalds struct priority_group *pg, *tmp; 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { 2611da177e4SLinus Torvalds list_del(&pg->list); 2621da177e4SLinus Torvalds free_priority_group(pg, m->ti); 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 265cfae5c9bSChandra Seetharaman kfree(m->hw_handler_name); 2662bfd2e13SChandra Seetharaman kfree(m->hw_handler_params); 267d5ffebddSMike Snitzer mutex_destroy(&m->work_mutex); 2681da177e4SLinus Torvalds kfree(m); 2691da177e4SLinus Torvalds } 2701da177e4SLinus Torvalds 2712eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info) 2722eff1924SMike Snitzer { 2732eff1924SMike Snitzer return info->ptr; 2742eff1924SMike Snitzer } 2752eff1924SMike Snitzer 276bf661be1SMike Snitzer static size_t multipath_per_bio_data_size(void) 27776e33fe4SMike Snitzer { 278bf661be1SMike Snitzer return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details); 27976e33fe4SMike Snitzer } 28076e33fe4SMike Snitzer 281bf661be1SMike Snitzer static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio) 282bf661be1SMike Snitzer { 283bf661be1SMike Snitzer return dm_per_bio_data(bio, multipath_per_bio_data_size()); 284bf661be1SMike Snitzer } 285bf661be1SMike Snitzer 286d07a241dSMike Snitzer static struct dm_bio_details *get_bio_details_from_mpio(struct dm_mpath_io *mpio) 287bf661be1SMike Snitzer { 288bf661be1SMike Snitzer /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */ 289bf661be1SMike Snitzer void *bio_details = mpio + 1; 290bf661be1SMike Snitzer return bio_details; 291bf661be1SMike Snitzer } 292bf661be1SMike Snitzer 29363f6e6fdSMike Snitzer static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p) 29476e33fe4SMike Snitzer { 29576e33fe4SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 296d07a241dSMike Snitzer struct dm_bio_details *bio_details = get_bio_details_from_mpio(mpio); 29776e33fe4SMike Snitzer 298d0442f80SMike Snitzer mpio->nr_bytes = bio->bi_iter.bi_size; 299d0442f80SMike Snitzer mpio->pgpath = NULL; 300c06dfd12SGabriel Krisman Bertazi mpio->start_time_ns = 0; 301bf661be1SMike Snitzer *mpio_p = mpio; 302d0442f80SMike Snitzer 303d0442f80SMike Snitzer dm_bio_record(bio_details, bio); 30476e33fe4SMike Snitzer } 30576e33fe4SMike Snitzer 3061da177e4SLinus Torvalds /*----------------------------------------------- 3071da177e4SLinus Torvalds * Path selection 3081da177e4SLinus Torvalds *-----------------------------------------------*/ 3091da177e4SLinus Torvalds 3103e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m) 311fb612642SKiyoshi Ueda { 312fb612642SKiyoshi Ueda struct pgpath *pgpath; 3134e2d19e4SChandra Seetharaman unsigned long pg_init_delay = 0; 314fb612642SKiyoshi Ueda 315b194679fSBart Van Assche lockdep_assert_held(&m->lock); 316b194679fSBart Van Assche 31791e968aaSMike Snitzer if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 3183e9f1be1SHannes Reinecke return 0; 31917f4ff45SHannes Reinecke 32091e968aaSMike Snitzer atomic_inc(&m->pg_init_count); 321518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 3223e9f1be1SHannes Reinecke 3233e9f1be1SHannes Reinecke /* Check here to reset pg_init_required */ 3243e9f1be1SHannes Reinecke if (!m->current_pg) 3253e9f1be1SHannes Reinecke return 0; 3263e9f1be1SHannes Reinecke 327518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags)) 3284e2d19e4SChandra Seetharaman pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ? 3294e2d19e4SChandra Seetharaman m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS); 330fb612642SKiyoshi Ueda list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) { 331fb612642SKiyoshi Ueda /* Skip failed paths */ 332fb612642SKiyoshi Ueda if (!pgpath->is_active) 333fb612642SKiyoshi Ueda continue; 3344e2d19e4SChandra Seetharaman if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path, 3354e2d19e4SChandra Seetharaman pg_init_delay)) 33691e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 337fb612642SKiyoshi Ueda } 33891e968aaSMike Snitzer return atomic_read(&m->pg_init_in_progress); 339fb612642SKiyoshi Ueda } 340fb612642SKiyoshi Ueda 341c1d7ecf7SBart Van Assche static int pg_init_all_paths(struct multipath *m) 3421da177e4SLinus Torvalds { 343c1d7ecf7SBart Van Assche int ret; 3442da1610aSMike Snitzer unsigned long flags; 3452da1610aSMike Snitzer 3462da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 347c1d7ecf7SBart Van Assche ret = __pg_init_all_paths(m); 3482da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 349c1d7ecf7SBart Van Assche 350c1d7ecf7SBart Van Assche return ret; 3512da1610aSMike Snitzer } 3522da1610aSMike Snitzer 3532da1610aSMike Snitzer static void __switch_pg(struct multipath *m, struct priority_group *pg) 3542da1610aSMike Snitzer { 35569cea0d4SMike Snitzer lockdep_assert_held(&m->lock); 35669cea0d4SMike Snitzer 3572da1610aSMike Snitzer m->current_pg = pg; 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds /* Must we initialise the PG first, and queue I/O till it's ready? */ 360cfae5c9bSChandra Seetharaman if (m->hw_handler_name) { 361518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 362518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 3631da177e4SLinus Torvalds } else { 364518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 365518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 3661da177e4SLinus Torvalds } 367c9e45581SDave Wysochanski 36891e968aaSMike Snitzer atomic_set(&m->pg_init_count, 0); 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 3712da1610aSMike Snitzer static struct pgpath *choose_path_in_pg(struct multipath *m, 3722da1610aSMike Snitzer struct priority_group *pg, 37302ab823fSKiyoshi Ueda size_t nr_bytes) 3741da177e4SLinus Torvalds { 3752da1610aSMike Snitzer unsigned long flags; 376c922d5f7SJosef "Jeff" Sipek struct dm_path *path; 3772da1610aSMike Snitzer struct pgpath *pgpath; 3781da177e4SLinus Torvalds 37990a4323cSMike Snitzer path = pg->ps.type->select_path(&pg->ps, nr_bytes); 3801da177e4SLinus Torvalds if (!path) 3812da1610aSMike Snitzer return ERR_PTR(-ENXIO); 3821da177e4SLinus Torvalds 3832da1610aSMike Snitzer pgpath = path_to_pgpath(path); 3841da177e4SLinus Torvalds 385506458efSWill Deacon if (unlikely(READ_ONCE(m->current_pg) != pg)) { 3862da1610aSMike Snitzer /* Only update current_pgpath if pg changed */ 3872da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 3882da1610aSMike Snitzer m->current_pgpath = pgpath; 3892da1610aSMike Snitzer __switch_pg(m, pg); 3902da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3911da177e4SLinus Torvalds } 3921da177e4SLinus Torvalds 3932da1610aSMike Snitzer return pgpath; 3942da1610aSMike Snitzer } 3952da1610aSMike Snitzer 3962da1610aSMike Snitzer static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes) 3971da177e4SLinus Torvalds { 3982da1610aSMike Snitzer unsigned long flags; 3991da177e4SLinus Torvalds struct priority_group *pg; 4002da1610aSMike Snitzer struct pgpath *pgpath; 401*86a3238cSHeinz Mauelshagen unsigned int bypassed = 1; 4021da177e4SLinus Torvalds 40391e968aaSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 40469cea0d4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 405518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 40669cea0d4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4071da177e4SLinus Torvalds goto failed; 4081f271972SBenjamin Marzinski } 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds /* Were we instructed to switch PG? */ 411506458efSWill Deacon if (READ_ONCE(m->next_pg)) { 4122da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 4131da177e4SLinus Torvalds pg = m->next_pg; 4142da1610aSMike Snitzer if (!pg) { 4152da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4162da1610aSMike Snitzer goto check_current_pg; 4172da1610aSMike Snitzer } 4181da177e4SLinus Torvalds m->next_pg = NULL; 4192da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4202da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4212da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 4222da1610aSMike Snitzer return pgpath; 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds /* Don't change PG until it has no remaining paths */ 4262da1610aSMike Snitzer check_current_pg: 427506458efSWill Deacon pg = READ_ONCE(m->current_pg); 4282da1610aSMike Snitzer if (pg) { 4292da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4302da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 4312da1610aSMike Snitzer return pgpath; 4322da1610aSMike Snitzer } 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds /* 4351da177e4SLinus Torvalds * Loop through priority groups until we find a valid path. 4361da177e4SLinus Torvalds * First time we skip PGs marked 'bypassed'. 437f220fd4eSMike Christie * Second time we only try the ones we skipped, but set 438f220fd4eSMike Christie * pg_init_delay_retry so we do not hammer controllers. 4391da177e4SLinus Torvalds */ 4401da177e4SLinus Torvalds do { 4411da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 442d19a55ccSMike Snitzer if (pg->bypassed == !!bypassed) 4431da177e4SLinus Torvalds continue; 4442da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4452da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) { 44669cea0d4SMike Snitzer if (!bypassed) { 44769cea0d4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 448518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 44969cea0d4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 45069cea0d4SMike Snitzer } 4512da1610aSMike Snitzer return pgpath; 4521da177e4SLinus Torvalds } 453f220fd4eSMike Christie } 4541da177e4SLinus Torvalds } while (bypassed--); 4551da177e4SLinus Torvalds 4561da177e4SLinus Torvalds failed: 4572da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 4581da177e4SLinus Torvalds m->current_pgpath = NULL; 4591da177e4SLinus Torvalds m->current_pg = NULL; 4602da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4612da1610aSMike Snitzer 4622da1610aSMike Snitzer return NULL; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 46545e15720SKiyoshi Ueda /* 466ac75b09fSMike Snitzer * dm_report_EIO() is a macro instead of a function to make pr_debug_ratelimited() 46786331f39SBart Van Assche * report the function name and line number of the function from which 46886331f39SBart Van Assche * it has been invoked. 46945e15720SKiyoshi Ueda */ 47086331f39SBart Van Assche #define dm_report_EIO(m) \ 47118a482f5SChristoph Hellwig do { \ 472ac75b09fSMike Snitzer DMDEBUG_LIMIT("%s: returning EIO; QIFNP = %d; SQIFNP = %d; DNFS = %d", \ 473d4a512edSMike Snitzer dm_table_device_name((m)->ti->table), \ 47486331f39SBart Van Assche test_bit(MPATHF_QUEUE_IF_NO_PATH, &(m)->flags), \ 47586331f39SBart Van Assche test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &(m)->flags), \ 47686331f39SBart Van Assche dm_noflush_suspending((m)->ti)); \ 47718a482f5SChristoph Hellwig } while (0) 47845e15720SKiyoshi Ueda 47936fcffccSHannes Reinecke /* 480c1fd0abeSMike Snitzer * Check whether bios must be queued in the device-mapper core rather 481c1fd0abeSMike Snitzer * than here in the target. 482c1fd0abeSMike Snitzer */ 483a862e4e2SMike Snitzer static bool __must_push_back(struct multipath *m) 484c1fd0abeSMike Snitzer { 485a862e4e2SMike Snitzer return dm_noflush_suspending(m->ti); 486c1fd0abeSMike Snitzer } 487c1fd0abeSMike Snitzer 488c1fd0abeSMike Snitzer static bool must_push_back_rq(struct multipath *m) 489c1fd0abeSMike Snitzer { 49073265f3fSMike Snitzer unsigned long flags; 49173265f3fSMike Snitzer bool ret; 49273265f3fSMike Snitzer 49373265f3fSMike Snitzer spin_lock_irqsave(&m->lock, flags); 49473265f3fSMike Snitzer ret = (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) || __must_push_back(m)); 49573265f3fSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 49673265f3fSMike Snitzer 49773265f3fSMike Snitzer return ret; 498c1fd0abeSMike Snitzer } 499c1fd0abeSMike Snitzer 500c1fd0abeSMike Snitzer /* 50176e33fe4SMike Snitzer * Map cloned requests (request-based multipath) 50236fcffccSHannes Reinecke */ 503eb8db831SChristoph Hellwig static int multipath_clone_and_map(struct dm_target *ti, struct request *rq, 504e5863d9aSMike Snitzer union map_info *map_context, 505eb8db831SChristoph Hellwig struct request **__clone) 5061da177e4SLinus Torvalds { 5077943bd6dSMike Snitzer struct multipath *m = ti->private; 508eb8db831SChristoph Hellwig size_t nr_bytes = blk_rq_bytes(rq); 5091da177e4SLinus Torvalds struct pgpath *pgpath; 510f40c67f0SKiyoshi Ueda struct block_device *bdev; 511eb8db831SChristoph Hellwig struct dm_mpath_io *mpio = get_mpio(map_context); 5127083abbbSBart Van Assche struct request_queue *q; 513eb8db831SChristoph Hellwig struct request *clone; 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds /* Do we need to select a new pgpath? */ 516506458efSWill Deacon pgpath = READ_ONCE(m->current_pgpath); 517374117adSMike Snitzer if (!pgpath || !mpath_double_check_test_bit(MPATHF_QUEUE_IO, m)) 5182da1610aSMike Snitzer pgpath = choose_pgpath(m, nr_bytes); 5191da177e4SLinus Torvalds 5209bf59a61SMike Snitzer if (!pgpath) { 521c1fd0abeSMike Snitzer if (must_push_back_rq(m)) 522b88efd43SMike Snitzer return DM_MAPIO_DELAY_REQUEUE; 52318a482f5SChristoph Hellwig dm_report_EIO(m); /* Failed */ 524f98e0eb6SChristoph Hellwig return DM_MAPIO_KILL; 525374117adSMike Snitzer } else if (mpath_double_check_test_bit(MPATHF_QUEUE_IO, m) || 526374117adSMike Snitzer mpath_double_check_test_bit(MPATHF_PG_INIT_REQUIRED, m)) { 527459b5401SMing Lei pg_init_all_paths(m); 528c1d7ecf7SBart Van Assche return DM_MAPIO_DELAY_REQUEUE; 5299bf59a61SMike Snitzer } 5306afbc01dSMike Snitzer 531e8099177SHannes Reinecke mpio->pgpath = pgpath; 532e8099177SHannes Reinecke mpio->nr_bytes = nr_bytes; 5332eb6e1e3SKeith Busch 5342eb6e1e3SKeith Busch bdev = pgpath->path.dev->bdev; 5357083abbbSBart Van Assche q = bdev_get_queue(bdev); 5360bf6d96cSChristoph Hellwig clone = blk_mq_alloc_request(q, rq->cmd_flags | REQ_NOMERGE, 537ff005a06SChristoph Hellwig BLK_MQ_REQ_NOWAIT); 5386599c84eSBart Van Assche if (IS_ERR(clone)) { 5396599c84eSBart Van Assche /* EBUSY, ENODEV or EWOULDBLOCK: requeue */ 540848b8aefSMike Snitzer if (blk_queue_dying(q)) { 5417083abbbSBart Van Assche atomic_inc(&m->pg_init_in_progress); 5427083abbbSBart Van Assche activate_or_offline_path(pgpath); 543050af08fSMing Lei return DM_MAPIO_DELAY_REQUEUE; 5447083abbbSBart Van Assche } 545050af08fSMing Lei 546050af08fSMing Lei /* 547050af08fSMing Lei * blk-mq's SCHED_RESTART can cover this requeue, so we 548050af08fSMing Lei * needn't deal with it by DELAY_REQUEUE. More importantly, 549050af08fSMing Lei * we have to return DM_MAPIO_REQUEUE so that blk-mq can 550050af08fSMing Lei * get the queue busy feedback (via BLK_STS_RESOURCE), 551050af08fSMing Lei * otherwise I/O merging can suffer. 552050af08fSMing Lei */ 553050af08fSMing Lei return DM_MAPIO_REQUEUE; 5544c6dd53dSMike Snitzer } 5556599c84eSBart Van Assche clone->bio = clone->biotail = NULL; 5566599c84eSBart Van Assche clone->cmd_flags |= REQ_FAILFAST_TRANSPORT; 5576599c84eSBart Van Assche *__clone = clone; 5582eb6e1e3SKeith Busch 559e8099177SHannes Reinecke if (pgpath->pg->ps.type->start_io) 560e8099177SHannes Reinecke pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 561e8099177SHannes Reinecke &pgpath->path, 562e8099177SHannes Reinecke nr_bytes); 5632eb6e1e3SKeith Busch return DM_MAPIO_REMAPPED; 5641da177e4SLinus Torvalds } 5651da177e4SLinus Torvalds 5665de719e3SYufen Yu static void multipath_release_clone(struct request *clone, 5675de719e3SYufen Yu union map_info *map_context) 568e5863d9aSMike Snitzer { 5695de719e3SYufen Yu if (unlikely(map_context)) { 5705de719e3SYufen Yu /* 5715de719e3SYufen Yu * non-NULL map_context means caller is still map 5725de719e3SYufen Yu * method; must undo multipath_clone_and_map() 5735de719e3SYufen Yu */ 5745de719e3SYufen Yu struct dm_mpath_io *mpio = get_mpio(map_context); 5755de719e3SYufen Yu struct pgpath *pgpath = mpio->pgpath; 5765de719e3SYufen Yu 5775de719e3SYufen Yu if (pgpath && pgpath->pg->ps.type->end_io) 5785de719e3SYufen Yu pgpath->pg->ps.type->end_io(&pgpath->pg->ps, 5795de719e3SYufen Yu &pgpath->path, 580087615bfSGabriel Krisman Bertazi mpio->nr_bytes, 581087615bfSGabriel Krisman Bertazi clone->io_start_time_ns); 5825de719e3SYufen Yu } 5835de719e3SYufen Yu 5840bf6d96cSChristoph Hellwig blk_mq_free_request(clone); 585e5863d9aSMike Snitzer } 586e5863d9aSMike Snitzer 5871da177e4SLinus Torvalds /* 58876e33fe4SMike Snitzer * Map cloned bios (bio-based multipath) 58976e33fe4SMike Snitzer */ 5900001ec56SMike Snitzer 59117213ec1SMike Snitzer static void __multipath_queue_bio(struct multipath *m, struct bio *bio) 59217213ec1SMike Snitzer { 59317213ec1SMike Snitzer /* Queue for the daemon to resubmit */ 59417213ec1SMike Snitzer bio_list_add(&m->queued_bios, bio); 59517213ec1SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) 59617213ec1SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 59717213ec1SMike Snitzer } 59817213ec1SMike Snitzer 599f45f1186SMike Snitzer static void multipath_queue_bio(struct multipath *m, struct bio *bio) 600f45f1186SMike Snitzer { 601f45f1186SMike Snitzer unsigned long flags; 602f45f1186SMike Snitzer 603f45f1186SMike Snitzer spin_lock_irqsave(&m->lock, flags); 60417213ec1SMike Snitzer __multipath_queue_bio(m, bio); 605f45f1186SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 606f45f1186SMike Snitzer } 607f45f1186SMike Snitzer 6080001ec56SMike Snitzer static struct pgpath *__map_bio(struct multipath *m, struct bio *bio) 60976e33fe4SMike Snitzer { 61076e33fe4SMike Snitzer struct pgpath *pgpath; 61176e33fe4SMike Snitzer unsigned long flags; 61276e33fe4SMike Snitzer 61376e33fe4SMike Snitzer /* Do we need to select a new pgpath? */ 614506458efSWill Deacon pgpath = READ_ONCE(m->current_pgpath); 615374117adSMike Snitzer if (!pgpath || !mpath_double_check_test_bit(MPATHF_QUEUE_IO, m)) 6160001ec56SMike Snitzer pgpath = choose_pgpath(m, bio->bi_iter.bi_size); 61776e33fe4SMike Snitzer 61817213ec1SMike Snitzer if (!pgpath) { 61976e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 62017213ec1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 62117213ec1SMike Snitzer __multipath_queue_bio(m, bio); 62217213ec1SMike Snitzer pgpath = ERR_PTR(-EAGAIN); 62317213ec1SMike Snitzer } 62476e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 625848b8aefSMike Snitzer 626374117adSMike Snitzer } else if (mpath_double_check_test_bit(MPATHF_QUEUE_IO, m) || 627374117adSMike Snitzer mpath_double_check_test_bit(MPATHF_PG_INIT_REQUIRED, m)) { 628f45f1186SMike Snitzer multipath_queue_bio(m, bio); 62976e33fe4SMike Snitzer pg_init_all_paths(m); 6300001ec56SMike Snitzer return ERR_PTR(-EAGAIN); 63176e33fe4SMike Snitzer } 63276e33fe4SMike Snitzer 6330001ec56SMike Snitzer return pgpath; 63476e33fe4SMike Snitzer } 63576e33fe4SMike Snitzer 6360001ec56SMike Snitzer static int __multipath_map_bio(struct multipath *m, struct bio *bio, 6370001ec56SMike Snitzer struct dm_mpath_io *mpio) 6380001ec56SMike Snitzer { 639dbaf971cSMike Snitzer struct pgpath *pgpath = __map_bio(m, bio); 6400001ec56SMike Snitzer 6410001ec56SMike Snitzer if (IS_ERR(pgpath)) 6420001ec56SMike Snitzer return DM_MAPIO_SUBMITTED; 6430001ec56SMike Snitzer 64476e33fe4SMike Snitzer if (!pgpath) { 645a862e4e2SMike Snitzer if (__must_push_back(m)) 64676e33fe4SMike Snitzer return DM_MAPIO_REQUEUE; 64718a482f5SChristoph Hellwig dm_report_EIO(m); 648846785e6SChristoph Hellwig return DM_MAPIO_KILL; 64976e33fe4SMike Snitzer } 65076e33fe4SMike Snitzer 65176e33fe4SMike Snitzer mpio->pgpath = pgpath; 65276e33fe4SMike Snitzer 653c06dfd12SGabriel Krisman Bertazi if (dm_ps_use_hr_timer(pgpath->pg->ps.type)) 654c06dfd12SGabriel Krisman Bertazi mpio->start_time_ns = ktime_get_ns(); 655c06dfd12SGabriel Krisman Bertazi 6564e4cbee9SChristoph Hellwig bio->bi_status = 0; 65774d46992SChristoph Hellwig bio_set_dev(bio, pgpath->path.dev->bdev); 6581eff9d32SJens Axboe bio->bi_opf |= REQ_FAILFAST_TRANSPORT; 65976e33fe4SMike Snitzer 66076e33fe4SMike Snitzer if (pgpath->pg->ps.type->start_io) 66176e33fe4SMike Snitzer pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 66276e33fe4SMike Snitzer &pgpath->path, 663d0442f80SMike Snitzer mpio->nr_bytes); 66476e33fe4SMike Snitzer return DM_MAPIO_REMAPPED; 66576e33fe4SMike Snitzer } 66676e33fe4SMike Snitzer 66776e33fe4SMike Snitzer static int multipath_map_bio(struct dm_target *ti, struct bio *bio) 66876e33fe4SMike Snitzer { 66976e33fe4SMike Snitzer struct multipath *m = ti->private; 670bf661be1SMike Snitzer struct dm_mpath_io *mpio = NULL; 671bf661be1SMike Snitzer 67263f6e6fdSMike Snitzer multipath_init_per_bio_data(bio, &mpio); 67376e33fe4SMike Snitzer return __multipath_map_bio(m, bio, mpio); 67476e33fe4SMike Snitzer } 67576e33fe4SMike Snitzer 6767e48c768SMike Snitzer static void process_queued_io_list(struct multipath *m) 67776e33fe4SMike Snitzer { 678953923c0SMike Snitzer if (m->queue_mode == DM_TYPE_REQUEST_BASED) 6797e48c768SMike Snitzer dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table)); 6808d47e659SMike Snitzer else if (m->queue_mode == DM_TYPE_BIO_BASED) 68176e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 68276e33fe4SMike Snitzer } 68376e33fe4SMike Snitzer 68476e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work) 68576e33fe4SMike Snitzer { 68676e33fe4SMike Snitzer int r; 68776e33fe4SMike Snitzer unsigned long flags; 68876e33fe4SMike Snitzer struct bio *bio; 68976e33fe4SMike Snitzer struct bio_list bios; 69076e33fe4SMike Snitzer struct blk_plug plug; 69176e33fe4SMike Snitzer struct multipath *m = 69276e33fe4SMike Snitzer container_of(work, struct multipath, process_queued_bios); 69376e33fe4SMike Snitzer 69476e33fe4SMike Snitzer bio_list_init(&bios); 69576e33fe4SMike Snitzer 69676e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 69776e33fe4SMike Snitzer 69876e33fe4SMike Snitzer if (bio_list_empty(&m->queued_bios)) { 69976e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 70076e33fe4SMike Snitzer return; 70176e33fe4SMike Snitzer } 70276e33fe4SMike Snitzer 70376e33fe4SMike Snitzer bio_list_merge(&bios, &m->queued_bios); 70476e33fe4SMike Snitzer bio_list_init(&m->queued_bios); 70576e33fe4SMike Snitzer 70676e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 70776e33fe4SMike Snitzer 70876e33fe4SMike Snitzer blk_start_plug(&plug); 70976e33fe4SMike Snitzer while ((bio = bio_list_pop(&bios))) { 7101836df08SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 7111836df08SMike Snitzer dm_bio_restore(get_bio_details_from_mpio(mpio), bio); 7121836df08SMike Snitzer r = __multipath_map_bio(m, bio, mpio); 713846785e6SChristoph Hellwig switch (r) { 714846785e6SChristoph Hellwig case DM_MAPIO_KILL: 7154e4cbee9SChristoph Hellwig bio->bi_status = BLK_STS_IOERR; 7164e4cbee9SChristoph Hellwig bio_endio(bio); 717047385b3SDan Carpenter break; 718846785e6SChristoph Hellwig case DM_MAPIO_REQUEUE: 7194e4cbee9SChristoph Hellwig bio->bi_status = BLK_STS_DM_REQUEUE; 72076e33fe4SMike Snitzer bio_endio(bio); 721846785e6SChristoph Hellwig break; 722846785e6SChristoph Hellwig case DM_MAPIO_REMAPPED: 723ed00aabdSChristoph Hellwig submit_bio_noacct(bio); 724846785e6SChristoph Hellwig break; 7258192a0cdSWang Sheng-Hui case DM_MAPIO_SUBMITTED: 7269157c8d3SBart Van Assche break; 7279157c8d3SBart Van Assche default: 7289157c8d3SBart Van Assche WARN_ONCE(true, "__multipath_map_bio() returned %d\n", r); 729846785e6SChristoph Hellwig } 73076e33fe4SMike Snitzer } 73176e33fe4SMike Snitzer blk_finish_plug(&plug); 73276e33fe4SMike Snitzer } 73376e33fe4SMike Snitzer 73476e33fe4SMike Snitzer /* 7351da177e4SLinus Torvalds * If we run out of usable paths, should we queue I/O or error it? 7361da177e4SLinus Torvalds */ 737be7d31ccSMike Snitzer static int queue_if_no_path(struct multipath *m, bool queue_if_no_path, 7384c3f4838SMike Snitzer bool save_old_value, const char *caller) 7391da177e4SLinus Torvalds { 7401da177e4SLinus Torvalds unsigned long flags; 741553ec94cSMike Snitzer bool queue_if_no_path_bit, saved_queue_if_no_path_bit; 742d4a512edSMike Snitzer const char *dm_dev_name = dm_table_device_name(m->ti->table); 7434c3f4838SMike Snitzer 7444c3f4838SMike Snitzer DMDEBUG("%s: %s caller=%s queue_if_no_path=%d save_old_value=%d", 7454c3f4838SMike Snitzer dm_dev_name, __func__, caller, queue_if_no_path, save_old_value); 7461da177e4SLinus Torvalds 7471da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 748553ec94cSMike Snitzer 749553ec94cSMike Snitzer queue_if_no_path_bit = test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 750553ec94cSMike Snitzer saved_queue_if_no_path_bit = test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 751553ec94cSMike Snitzer 752553ec94cSMike Snitzer if (save_old_value) { 753553ec94cSMike Snitzer if (unlikely(!queue_if_no_path_bit && saved_queue_if_no_path_bit)) { 754553ec94cSMike Snitzer DMERR("%s: QIFNP disabled but saved as enabled, saving again loses state, not saving!", 7554c3f4838SMike Snitzer dm_dev_name); 756553ec94cSMike Snitzer } else 757553ec94cSMike Snitzer assign_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path_bit); 758553ec94cSMike Snitzer } else if (!queue_if_no_path && saved_queue_if_no_path_bit) { 759553ec94cSMike Snitzer /* due to "fail_if_no_path" message, need to honor it. */ 760553ec94cSMike Snitzer clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 761553ec94cSMike Snitzer } 762c1fd0abeSMike Snitzer assign_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags, queue_if_no_path); 763553ec94cSMike Snitzer 7644c3f4838SMike Snitzer DMDEBUG("%s: after %s changes; QIFNP = %d; SQIFNP = %d; DNFS = %d", 7654c3f4838SMike Snitzer dm_dev_name, __func__, 7664c3f4838SMike Snitzer test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags), 7674c3f4838SMike Snitzer test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags), 7684c3f4838SMike Snitzer dm_noflush_suspending(m->ti)); 7694c3f4838SMike Snitzer 7701da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 7711da177e4SLinus Torvalds 77276e33fe4SMike Snitzer if (!queue_if_no_path) { 77363d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 7747e48c768SMike Snitzer process_queued_io_list(m); 77576e33fe4SMike Snitzer } 77663d832c3SHannes Reinecke 7771da177e4SLinus Torvalds return 0; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds 7801da177e4SLinus Torvalds /* 781be240ff5SAnatol Pomazau * If the queue_if_no_path timeout fires, turn off queue_if_no_path and 782be240ff5SAnatol Pomazau * process any queued I/O. 783be240ff5SAnatol Pomazau */ 784be240ff5SAnatol Pomazau static void queue_if_no_path_timeout_work(struct timer_list *t) 785be240ff5SAnatol Pomazau { 786be240ff5SAnatol Pomazau struct multipath *m = from_timer(m, t, nopath_timer); 787be240ff5SAnatol Pomazau 788d4a512edSMike Snitzer DMWARN("queue_if_no_path timeout on %s, failing queued IO", 789d4a512edSMike Snitzer dm_table_device_name(m->ti->table)); 7904c3f4838SMike Snitzer queue_if_no_path(m, false, false, __func__); 791be240ff5SAnatol Pomazau } 792be240ff5SAnatol Pomazau 793be240ff5SAnatol Pomazau /* 794be240ff5SAnatol Pomazau * Enable the queue_if_no_path timeout if necessary. 795be240ff5SAnatol Pomazau * Called with m->lock held. 796be240ff5SAnatol Pomazau */ 797be240ff5SAnatol Pomazau static void enable_nopath_timeout(struct multipath *m) 798be240ff5SAnatol Pomazau { 799be240ff5SAnatol Pomazau unsigned long queue_if_no_path_timeout = 800be240ff5SAnatol Pomazau READ_ONCE(queue_if_no_path_timeout_secs) * HZ; 801be240ff5SAnatol Pomazau 802be240ff5SAnatol Pomazau lockdep_assert_held(&m->lock); 803be240ff5SAnatol Pomazau 804be240ff5SAnatol Pomazau if (queue_if_no_path_timeout > 0 && 805be240ff5SAnatol Pomazau atomic_read(&m->nr_valid_paths) == 0 && 806be240ff5SAnatol Pomazau test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 807be240ff5SAnatol Pomazau mod_timer(&m->nopath_timer, 808be240ff5SAnatol Pomazau jiffies + queue_if_no_path_timeout); 809be240ff5SAnatol Pomazau } 810be240ff5SAnatol Pomazau } 811be240ff5SAnatol Pomazau 812be240ff5SAnatol Pomazau static void disable_nopath_timeout(struct multipath *m) 813be240ff5SAnatol Pomazau { 814be240ff5SAnatol Pomazau del_timer_sync(&m->nopath_timer); 815be240ff5SAnatol Pomazau } 816be240ff5SAnatol Pomazau 817be240ff5SAnatol Pomazau /* 8181da177e4SLinus Torvalds * An event is triggered whenever a path is taken out of use. 8191da177e4SLinus Torvalds * Includes path failure and PG bypass. 8201da177e4SLinus Torvalds */ 821c4028958SDavid Howells static void trigger_event(struct work_struct *work) 8221da177e4SLinus Torvalds { 823c4028958SDavid Howells struct multipath *m = 824c4028958SDavid Howells container_of(work, struct multipath, trigger_event); 8251da177e4SLinus Torvalds 8261da177e4SLinus Torvalds dm_table_event(m->ti->table); 8271da177e4SLinus Torvalds } 8281da177e4SLinus Torvalds 8291da177e4SLinus Torvalds /*----------------------------------------------------------------- 8301da177e4SLinus Torvalds * Constructor/argument parsing: 8311da177e4SLinus Torvalds * <#multipath feature args> [<arg>]* 8321da177e4SLinus Torvalds * <#hw_handler args> [hw_handler [<arg>]*] 8331da177e4SLinus Torvalds * <#priority groups> 8341da177e4SLinus Torvalds * <initial priority group> 8351da177e4SLinus Torvalds * [<selector> <#selector args> [<arg>]* 8361da177e4SLinus Torvalds * <#paths> <#per-path selector args> 8371da177e4SLinus Torvalds * [<path> [<arg>]* ]+ ]+ 8381da177e4SLinus Torvalds *---------------------------------------------------------------*/ 839498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg, 8401da177e4SLinus Torvalds struct dm_target *ti) 8411da177e4SLinus Torvalds { 8421da177e4SLinus Torvalds int r; 8431da177e4SLinus Torvalds struct path_selector_type *pst; 844*86a3238cSHeinz Mauelshagen unsigned int ps_argc; 8451da177e4SLinus Torvalds 8465916a22bSEric Biggers static const struct dm_arg _args[] = { 84772d94861SAlasdair G Kergon {0, 1024, "invalid number of path selector args"}, 8481da177e4SLinus Torvalds }; 8491da177e4SLinus Torvalds 850498f0103SMike Snitzer pst = dm_get_path_selector(dm_shift_arg(as)); 8511da177e4SLinus Torvalds if (!pst) { 85272d94861SAlasdair G Kergon ti->error = "unknown path selector type"; 8531da177e4SLinus Torvalds return -EINVAL; 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds 856498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &ps_argc, &ti->error); 857371b2e34SMikulas Patocka if (r) { 858371b2e34SMikulas Patocka dm_put_path_selector(pst); 8591da177e4SLinus Torvalds return -EINVAL; 860371b2e34SMikulas Patocka } 8611da177e4SLinus Torvalds 8621da177e4SLinus Torvalds r = pst->create(&pg->ps, ps_argc, as->argv); 8631da177e4SLinus Torvalds if (r) { 8641da177e4SLinus Torvalds dm_put_path_selector(pst); 86572d94861SAlasdair G Kergon ti->error = "path selector constructor failed"; 8661da177e4SLinus Torvalds return r; 8671da177e4SLinus Torvalds } 8681da177e4SLinus Torvalds 8691da177e4SLinus Torvalds pg->ps.type = pst; 870498f0103SMike Snitzer dm_consume_args(as, ps_argc); 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds return 0; 8731da177e4SLinus Torvalds } 8741da177e4SLinus Torvalds 875e8f74a0fSMike Snitzer static int setup_scsi_dh(struct block_device *bdev, struct multipath *m, 876b592211cSMike Snitzer const char **attached_handler_name, char **error) 8771da177e4SLinus Torvalds { 878848b8aefSMike Snitzer struct request_queue *q = bdev_get_queue(bdev); 879848b8aefSMike Snitzer int r; 880a0cf7ea9SHannes Reinecke 881374117adSMike Snitzer if (mpath_double_check_test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, m)) { 8821bab0de0SChristoph Hellwig retain: 883b592211cSMike Snitzer if (*attached_handler_name) { 884a58a935dSMike Snitzer /* 88554cd640dStang.junhui * Clear any hw_handler_params associated with a 88654cd640dStang.junhui * handler that isn't already attached. 88754cd640dStang.junhui */ 888b592211cSMike Snitzer if (m->hw_handler_name && strcmp(*attached_handler_name, m->hw_handler_name)) { 88954cd640dStang.junhui kfree(m->hw_handler_params); 89054cd640dStang.junhui m->hw_handler_params = NULL; 89154cd640dStang.junhui } 89254cd640dStang.junhui 89354cd640dStang.junhui /* 894a58a935dSMike Snitzer * Reset hw_handler_name to match the attached handler 895a58a935dSMike Snitzer * 896a58a935dSMike Snitzer * NB. This modifies the table line to show the actual 897a58a935dSMike Snitzer * handler instead of the original table passed in. 898a58a935dSMike Snitzer */ 899a58a935dSMike Snitzer kfree(m->hw_handler_name); 900b592211cSMike Snitzer m->hw_handler_name = *attached_handler_name; 901b592211cSMike Snitzer *attached_handler_name = NULL; 902a58a935dSMike Snitzer } 903a58a935dSMike Snitzer } 904a58a935dSMike Snitzer 905a58a935dSMike Snitzer if (m->hw_handler_name) { 906a0cf7ea9SHannes Reinecke r = scsi_dh_attach(q, m->hw_handler_name); 907a0cf7ea9SHannes Reinecke if (r == -EBUSY) { 908168678d7SMike Snitzer DMINFO("retaining handler on device %pg", bdev); 9091bab0de0SChristoph Hellwig goto retain; 9101bab0de0SChristoph Hellwig } 911ae11b1b3SHannes Reinecke if (r < 0) { 912848b8aefSMike Snitzer *error = "error attaching hardware handler"; 913848b8aefSMike Snitzer return r; 914ae11b1b3SHannes Reinecke } 9152bfd2e13SChandra Seetharaman 9162bfd2e13SChandra Seetharaman if (m->hw_handler_params) { 9172bfd2e13SChandra Seetharaman r = scsi_dh_set_params(q, m->hw_handler_params); 9182bfd2e13SChandra Seetharaman if (r < 0) { 919848b8aefSMike Snitzer *error = "unable to set hardware handler parameters"; 920848b8aefSMike Snitzer return r; 921848b8aefSMike Snitzer } 922848b8aefSMike Snitzer } 923848b8aefSMike Snitzer } 924848b8aefSMike Snitzer 925848b8aefSMike Snitzer return 0; 926848b8aefSMike Snitzer } 927848b8aefSMike Snitzer 928848b8aefSMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps, 929848b8aefSMike Snitzer struct dm_target *ti) 930848b8aefSMike Snitzer { 931848b8aefSMike Snitzer int r; 932848b8aefSMike Snitzer struct pgpath *p; 933848b8aefSMike Snitzer struct multipath *m = ti->private; 934e8f74a0fSMike Snitzer struct request_queue *q; 935b592211cSMike Snitzer const char *attached_handler_name = NULL; 936848b8aefSMike Snitzer 937848b8aefSMike Snitzer /* we need at least a path arg */ 938848b8aefSMike Snitzer if (as->argc < 1) { 939848b8aefSMike Snitzer ti->error = "no device given"; 940848b8aefSMike Snitzer return ERR_PTR(-EINVAL); 941848b8aefSMike Snitzer } 942848b8aefSMike Snitzer 943848b8aefSMike Snitzer p = alloc_pgpath(); 944848b8aefSMike Snitzer if (!p) 945848b8aefSMike Snitzer return ERR_PTR(-ENOMEM); 946848b8aefSMike Snitzer 947848b8aefSMike Snitzer r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table), 948848b8aefSMike Snitzer &p->path.dev); 949848b8aefSMike Snitzer if (r) { 950848b8aefSMike Snitzer ti->error = "error getting device"; 9512bfd2e13SChandra Seetharaman goto bad; 9522bfd2e13SChandra Seetharaman } 953848b8aefSMike Snitzer 954e8f74a0fSMike Snitzer q = bdev_get_queue(p->path.dev->bdev); 955e8f74a0fSMike Snitzer attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL); 956e457edf0SMike Snitzer if (attached_handler_name || m->hw_handler_name) { 957848b8aefSMike Snitzer INIT_DELAYED_WORK(&p->activate_path, activate_path_work); 958b592211cSMike Snitzer r = setup_scsi_dh(p->path.dev->bdev, m, &attached_handler_name, &ti->error); 959940bc471SMartin Wilck kfree(attached_handler_name); 960848b8aefSMike Snitzer if (r) { 961848b8aefSMike Snitzer dm_put_device(ti, p->path.dev); 962848b8aefSMike Snitzer goto bad; 9632bfd2e13SChandra Seetharaman } 964ae11b1b3SHannes Reinecke } 965ae11b1b3SHannes Reinecke 9661da177e4SLinus Torvalds r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); 9671da177e4SLinus Torvalds if (r) { 9681da177e4SLinus Torvalds dm_put_device(ti, p->path.dev); 9691da177e4SLinus Torvalds goto bad; 9701da177e4SLinus Torvalds } 9711da177e4SLinus Torvalds 9721da177e4SLinus Torvalds return p; 9731da177e4SLinus Torvalds bad: 9741da177e4SLinus Torvalds free_pgpath(p); 97501460f35SBenjamin Marzinski return ERR_PTR(r); 9761da177e4SLinus Torvalds } 9771da177e4SLinus Torvalds 978498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as, 97928f16c20SMicha³ Miros³aw struct multipath *m) 9801da177e4SLinus Torvalds { 9815916a22bSEric Biggers static const struct dm_arg _args[] = { 98272d94861SAlasdair G Kergon {1, 1024, "invalid number of paths"}, 98372d94861SAlasdair G Kergon {0, 1024, "invalid number of selector args"} 9841da177e4SLinus Torvalds }; 9851da177e4SLinus Torvalds 9861da177e4SLinus Torvalds int r; 987*86a3238cSHeinz Mauelshagen unsigned int i, nr_selector_args, nr_args; 9881da177e4SLinus Torvalds struct priority_group *pg; 98928f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 9901da177e4SLinus Torvalds 9911da177e4SLinus Torvalds if (as->argc < 2) { 9921da177e4SLinus Torvalds as->argc = 0; 99301460f35SBenjamin Marzinski ti->error = "not enough priority group arguments"; 99401460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 9951da177e4SLinus Torvalds } 9961da177e4SLinus Torvalds 9971da177e4SLinus Torvalds pg = alloc_priority_group(); 9981da177e4SLinus Torvalds if (!pg) { 99972d94861SAlasdair G Kergon ti->error = "couldn't allocate priority group"; 100001460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 10011da177e4SLinus Torvalds } 10021da177e4SLinus Torvalds pg->m = m; 10031da177e4SLinus Torvalds 10041da177e4SLinus Torvalds r = parse_path_selector(as, pg, ti); 10051da177e4SLinus Torvalds if (r) 10061da177e4SLinus Torvalds goto bad; 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds /* 10091da177e4SLinus Torvalds * read the paths 10101da177e4SLinus Torvalds */ 1011498f0103SMike Snitzer r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error); 10121da177e4SLinus Torvalds if (r) 10131da177e4SLinus Torvalds goto bad; 10141da177e4SLinus Torvalds 1015498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error); 10161da177e4SLinus Torvalds if (r) 10171da177e4SLinus Torvalds goto bad; 10181da177e4SLinus Torvalds 1019498f0103SMike Snitzer nr_args = 1 + nr_selector_args; 10201da177e4SLinus Torvalds for (i = 0; i < pg->nr_pgpaths; i++) { 10211da177e4SLinus Torvalds struct pgpath *pgpath; 1022498f0103SMike Snitzer struct dm_arg_set path_args; 10231da177e4SLinus Torvalds 1024498f0103SMike Snitzer if (as->argc < nr_args) { 1025148acff6SMikulas Patocka ti->error = "not enough path parameters"; 10266bbf79a1SAlasdair G Kergon r = -EINVAL; 10271da177e4SLinus Torvalds goto bad; 1028148acff6SMikulas Patocka } 10291da177e4SLinus Torvalds 1030498f0103SMike Snitzer path_args.argc = nr_args; 10311da177e4SLinus Torvalds path_args.argv = as->argv; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds pgpath = parse_path(&path_args, &pg->ps, ti); 103401460f35SBenjamin Marzinski if (IS_ERR(pgpath)) { 103501460f35SBenjamin Marzinski r = PTR_ERR(pgpath); 10361da177e4SLinus Torvalds goto bad; 103701460f35SBenjamin Marzinski } 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds pgpath->pg = pg; 10401da177e4SLinus Torvalds list_add_tail(&pgpath->list, &pg->pgpaths); 1041498f0103SMike Snitzer dm_consume_args(as, nr_args); 10421da177e4SLinus Torvalds } 10431da177e4SLinus Torvalds 10441da177e4SLinus Torvalds return pg; 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds bad: 10471da177e4SLinus Torvalds free_priority_group(pg, ti); 104801460f35SBenjamin Marzinski return ERR_PTR(r); 10491da177e4SLinus Torvalds } 10501da177e4SLinus Torvalds 1051498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m) 10521da177e4SLinus Torvalds { 1053*86a3238cSHeinz Mauelshagen unsigned int hw_argc; 10542bfd2e13SChandra Seetharaman int ret; 105528f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 10561da177e4SLinus Torvalds 10575916a22bSEric Biggers static const struct dm_arg _args[] = { 105872d94861SAlasdair G Kergon {0, 1024, "invalid number of hardware handler args"}, 10591da177e4SLinus Torvalds }; 10601da177e4SLinus Torvalds 1061498f0103SMike Snitzer if (dm_read_arg_group(_args, as, &hw_argc, &ti->error)) 10621da177e4SLinus Torvalds return -EINVAL; 10631da177e4SLinus Torvalds 10641da177e4SLinus Torvalds if (!hw_argc) 10651da177e4SLinus Torvalds return 0; 10661da177e4SLinus Torvalds 10678d47e659SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) { 106876e33fe4SMike Snitzer dm_consume_args(as, hw_argc); 106976e33fe4SMike Snitzer DMERR("bio-based multipath doesn't allow hardware handler args"); 107076e33fe4SMike Snitzer return 0; 107176e33fe4SMike Snitzer } 107276e33fe4SMike Snitzer 1073498f0103SMike Snitzer m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL); 1074f97dc421Stang.junhui if (!m->hw_handler_name) 1075f97dc421Stang.junhui return -EINVAL; 107614e98c5cSChandra Seetharaman 10772bfd2e13SChandra Seetharaman if (hw_argc > 1) { 10782bfd2e13SChandra Seetharaman char *p; 10792bfd2e13SChandra Seetharaman int i, j, len = 4; 10802bfd2e13SChandra Seetharaman 10812bfd2e13SChandra Seetharaman for (i = 0; i <= hw_argc - 2; i++) 10822bfd2e13SChandra Seetharaman len += strlen(as->argv[i]) + 1; 10832bfd2e13SChandra Seetharaman p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); 10842bfd2e13SChandra Seetharaman if (!p) { 10852bfd2e13SChandra Seetharaman ti->error = "memory allocation failed"; 10862bfd2e13SChandra Seetharaman ret = -ENOMEM; 10872bfd2e13SChandra Seetharaman goto fail; 10882bfd2e13SChandra Seetharaman } 10892bfd2e13SChandra Seetharaman j = sprintf(p, "%d", hw_argc - 1); 10902bfd2e13SChandra Seetharaman for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1) 10912bfd2e13SChandra Seetharaman j = sprintf(p, "%s", as->argv[i]); 10922bfd2e13SChandra Seetharaman } 1093498f0103SMike Snitzer dm_consume_args(as, hw_argc - 1); 10941da177e4SLinus Torvalds 10951da177e4SLinus Torvalds return 0; 10962bfd2e13SChandra Seetharaman fail: 10972bfd2e13SChandra Seetharaman kfree(m->hw_handler_name); 10982bfd2e13SChandra Seetharaman m->hw_handler_name = NULL; 10992bfd2e13SChandra Seetharaman return ret; 11001da177e4SLinus Torvalds } 11011da177e4SLinus Torvalds 1102498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m) 11031da177e4SLinus Torvalds { 11041da177e4SLinus Torvalds int r; 1105*86a3238cSHeinz Mauelshagen unsigned int argc; 110628f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 1107498f0103SMike Snitzer const char *arg_name; 11081da177e4SLinus Torvalds 11095916a22bSEric Biggers static const struct dm_arg _args[] = { 1110e83068a5SMike Snitzer {0, 8, "invalid number of feature args"}, 1111c9e45581SDave Wysochanski {1, 50, "pg_init_retries must be between 1 and 50"}, 11124e2d19e4SChandra Seetharaman {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"}, 11131da177e4SLinus Torvalds }; 11141da177e4SLinus Torvalds 1115498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &argc, &ti->error); 11161da177e4SLinus Torvalds if (r) 11171da177e4SLinus Torvalds return -EINVAL; 11181da177e4SLinus Torvalds 11191da177e4SLinus Torvalds if (!argc) 11201da177e4SLinus Torvalds return 0; 11211da177e4SLinus Torvalds 1122c9e45581SDave Wysochanski do { 1123498f0103SMike Snitzer arg_name = dm_shift_arg(as); 1124c9e45581SDave Wysochanski argc--; 1125c9e45581SDave Wysochanski 1126498f0103SMike Snitzer if (!strcasecmp(arg_name, "queue_if_no_path")) { 11274c3f4838SMike Snitzer r = queue_if_no_path(m, true, false, __func__); 1128c9e45581SDave Wysochanski continue; 11291da177e4SLinus Torvalds } 1130c9e45581SDave Wysochanski 1131a58a935dSMike Snitzer if (!strcasecmp(arg_name, "retain_attached_hw_handler")) { 1132518257b1SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 1133a58a935dSMike Snitzer continue; 1134a58a935dSMike Snitzer } 1135a58a935dSMike Snitzer 1136498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_retries") && 1137c9e45581SDave Wysochanski (argc >= 1)) { 1138498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error); 1139c9e45581SDave Wysochanski argc--; 1140c9e45581SDave Wysochanski continue; 1141c9e45581SDave Wysochanski } 1142c9e45581SDave Wysochanski 1143498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_delay_msecs") && 11444e2d19e4SChandra Seetharaman (argc >= 1)) { 1145498f0103SMike Snitzer r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error); 11464e2d19e4SChandra Seetharaman argc--; 11474e2d19e4SChandra Seetharaman continue; 11484e2d19e4SChandra Seetharaman } 11494e2d19e4SChandra Seetharaman 1150e83068a5SMike Snitzer if (!strcasecmp(arg_name, "queue_mode") && 1151e83068a5SMike Snitzer (argc >= 1)) { 1152e83068a5SMike Snitzer const char *queue_mode_name = dm_shift_arg(as); 1153e83068a5SMike Snitzer 1154e83068a5SMike Snitzer if (!strcasecmp(queue_mode_name, "bio")) 1155e83068a5SMike Snitzer m->queue_mode = DM_TYPE_BIO_BASED; 1156953923c0SMike Snitzer else if (!strcasecmp(queue_mode_name, "rq") || 1157953923c0SMike Snitzer !strcasecmp(queue_mode_name, "mq")) 1158e83068a5SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 1159e83068a5SMike Snitzer else { 1160e83068a5SMike Snitzer ti->error = "Unknown 'queue_mode' requested"; 1161e83068a5SMike Snitzer r = -EINVAL; 1162e83068a5SMike Snitzer } 1163e83068a5SMike Snitzer argc--; 1164e83068a5SMike Snitzer continue; 1165e83068a5SMike Snitzer } 1166e83068a5SMike Snitzer 1167c9e45581SDave Wysochanski ti->error = "Unrecognised multipath feature request"; 1168c9e45581SDave Wysochanski r = -EINVAL; 1169c9e45581SDave Wysochanski } while (argc && !r); 1170c9e45581SDave Wysochanski 1171c9e45581SDave Wysochanski return r; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 1174*86a3238cSHeinz Mauelshagen static int multipath_ctr(struct dm_target *ti, unsigned int argc, char **argv) 11751da177e4SLinus Torvalds { 1176498f0103SMike Snitzer /* target arguments */ 11775916a22bSEric Biggers static const struct dm_arg _args[] = { 1178a490a07aSMike Snitzer {0, 1024, "invalid number of priority groups"}, 1179a490a07aSMike Snitzer {0, 1024, "invalid initial priority group number"}, 11801da177e4SLinus Torvalds }; 11811da177e4SLinus Torvalds 11821da177e4SLinus Torvalds int r; 11831da177e4SLinus Torvalds struct multipath *m; 1184498f0103SMike Snitzer struct dm_arg_set as; 1185*86a3238cSHeinz Mauelshagen unsigned int pg_count = 0; 1186*86a3238cSHeinz Mauelshagen unsigned int next_pg_num; 1187be240ff5SAnatol Pomazau unsigned long flags; 11881da177e4SLinus Torvalds 11891da177e4SLinus Torvalds as.argc = argc; 11901da177e4SLinus Torvalds as.argv = argv; 11911da177e4SLinus Torvalds 1192e83068a5SMike Snitzer m = alloc_multipath(ti); 11931da177e4SLinus Torvalds if (!m) { 119472d94861SAlasdair G Kergon ti->error = "can't allocate multipath"; 11951da177e4SLinus Torvalds return -EINVAL; 11961da177e4SLinus Torvalds } 11971da177e4SLinus Torvalds 119828f16c20SMicha³ Miros³aw r = parse_features(&as, m); 11991da177e4SLinus Torvalds if (r) 12001da177e4SLinus Torvalds goto bad; 12011da177e4SLinus Torvalds 1202e83068a5SMike Snitzer r = alloc_multipath_stage2(ti, m); 1203e83068a5SMike Snitzer if (r) 1204e83068a5SMike Snitzer goto bad; 1205e83068a5SMike Snitzer 120628f16c20SMicha³ Miros³aw r = parse_hw_handler(&as, m); 12071da177e4SLinus Torvalds if (r) 12081da177e4SLinus Torvalds goto bad; 12091da177e4SLinus Torvalds 1210498f0103SMike Snitzer r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error); 12111da177e4SLinus Torvalds if (r) 12121da177e4SLinus Torvalds goto bad; 12131da177e4SLinus Torvalds 1214498f0103SMike Snitzer r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error); 12151da177e4SLinus Torvalds if (r) 12161da177e4SLinus Torvalds goto bad; 12171da177e4SLinus Torvalds 1218a490a07aSMike Snitzer if ((!m->nr_priority_groups && next_pg_num) || 1219a490a07aSMike Snitzer (m->nr_priority_groups && !next_pg_num)) { 1220a490a07aSMike Snitzer ti->error = "invalid initial priority group"; 1221a490a07aSMike Snitzer r = -EINVAL; 1222a490a07aSMike Snitzer goto bad; 1223a490a07aSMike Snitzer } 1224a490a07aSMike Snitzer 12251da177e4SLinus Torvalds /* parse the priority groups */ 12261da177e4SLinus Torvalds while (as.argc) { 12271da177e4SLinus Torvalds struct priority_group *pg; 1228*86a3238cSHeinz Mauelshagen unsigned int nr_valid_paths = atomic_read(&m->nr_valid_paths); 12291da177e4SLinus Torvalds 123028f16c20SMicha³ Miros³aw pg = parse_priority_group(&as, m); 123101460f35SBenjamin Marzinski if (IS_ERR(pg)) { 123201460f35SBenjamin Marzinski r = PTR_ERR(pg); 12331da177e4SLinus Torvalds goto bad; 12341da177e4SLinus Torvalds } 12351da177e4SLinus Torvalds 123691e968aaSMike Snitzer nr_valid_paths += pg->nr_pgpaths; 123791e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, nr_valid_paths); 123891e968aaSMike Snitzer 12391da177e4SLinus Torvalds list_add_tail(&pg->list, &m->priority_groups); 12401da177e4SLinus Torvalds pg_count++; 12411da177e4SLinus Torvalds pg->pg_num = pg_count; 12421da177e4SLinus Torvalds if (!--next_pg_num) 12431da177e4SLinus Torvalds m->next_pg = pg; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12461da177e4SLinus Torvalds if (pg_count != m->nr_priority_groups) { 124772d94861SAlasdair G Kergon ti->error = "priority group count mismatch"; 12481da177e4SLinus Torvalds r = -EINVAL; 12491da177e4SLinus Torvalds goto bad; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds 1252be240ff5SAnatol Pomazau spin_lock_irqsave(&m->lock, flags); 1253be240ff5SAnatol Pomazau enable_nopath_timeout(m); 1254be240ff5SAnatol Pomazau spin_unlock_irqrestore(&m->lock, flags); 1255be240ff5SAnatol Pomazau 125655a62eefSAlasdair G Kergon ti->num_flush_bios = 1; 125755a62eefSAlasdair G Kergon ti->num_discard_bios = 1; 1258ac62d620SChristoph Hellwig ti->num_write_zeroes_bios = 1; 12598d47e659SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) 1260bf661be1SMike Snitzer ti->per_io_data_size = multipath_per_bio_data_size(); 1261eb8db831SChristoph Hellwig else 12628637a6bfSMike Snitzer ti->per_io_data_size = sizeof(struct dm_mpath_io); 12638627921fSMikulas Patocka 12641da177e4SLinus Torvalds return 0; 12651da177e4SLinus Torvalds 12661da177e4SLinus Torvalds bad: 12671da177e4SLinus Torvalds free_multipath(m); 12681da177e4SLinus Torvalds return r; 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds 12712bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m) 12722bded7bdSKiyoshi Ueda { 12739f4c3f87SBart Van Assche DEFINE_WAIT(wait); 12742bded7bdSKiyoshi Ueda 12752bded7bdSKiyoshi Ueda while (1) { 12769f4c3f87SBart Van Assche prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE); 12772bded7bdSKiyoshi Ueda 127891e968aaSMike Snitzer if (!atomic_read(&m->pg_init_in_progress)) 12792bded7bdSKiyoshi Ueda break; 12802bded7bdSKiyoshi Ueda 12812bded7bdSKiyoshi Ueda io_schedule(); 12822bded7bdSKiyoshi Ueda } 12839f4c3f87SBart Van Assche finish_wait(&m->pg_init_wait, &wait); 12842bded7bdSKiyoshi Ueda } 12852bded7bdSKiyoshi Ueda 12862bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m) 12871da177e4SLinus Torvalds { 1288848b8aefSMike Snitzer if (m->hw_handler_name) { 1289c322ee93SMike Snitzer unsigned long flags; 1290954a73d5SShiva Krishna Merla 1291c322ee93SMike Snitzer if (!atomic_read(&m->pg_init_in_progress)) 1292c322ee93SMike Snitzer goto skip; 1293c322ee93SMike Snitzer 1294c322ee93SMike Snitzer spin_lock_irqsave(&m->lock, flags); 1295c322ee93SMike Snitzer if (atomic_read(&m->pg_init_in_progress) && 1296c322ee93SMike Snitzer !test_and_set_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) { 1297c322ee93SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 1298c322ee93SMike Snitzer 1299bab7cfc7SChandra Seetharaman flush_workqueue(kmpath_handlerd); 13002bded7bdSKiyoshi Ueda multipath_wait_for_pg_init_completion(m); 1301954a73d5SShiva Krishna Merla 1302c322ee93SMike Snitzer spin_lock_irqsave(&m->lock, flags); 1303518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 13046df400abSKiyoshi Ueda } 1305c322ee93SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 1306c322ee93SMike Snitzer } 1307c322ee93SMike Snitzer skip: 1308935fcc56Swuzhouhui if (m->queue_mode == DM_TYPE_BIO_BASED) 1309935fcc56Swuzhouhui flush_work(&m->process_queued_bios); 1310848b8aefSMike Snitzer flush_work(&m->trigger_event); 1311848b8aefSMike Snitzer } 1312848b8aefSMike Snitzer 13136df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti) 13146df400abSKiyoshi Ueda { 13156df400abSKiyoshi Ueda struct multipath *m = ti->private; 13166df400abSKiyoshi Ueda 1317be240ff5SAnatol Pomazau disable_nopath_timeout(m); 13182bded7bdSKiyoshi Ueda flush_multipath_work(m); 13191da177e4SLinus Torvalds free_multipath(m); 13201da177e4SLinus Torvalds } 13211da177e4SLinus Torvalds 13221da177e4SLinus Torvalds /* 13231da177e4SLinus Torvalds * Take a path out of use. 13241da177e4SLinus Torvalds */ 13251da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath) 13261da177e4SLinus Torvalds { 13271da177e4SLinus Torvalds unsigned long flags; 13281da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 13291da177e4SLinus Torvalds 13301da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13311da177e4SLinus Torvalds 13326680073dSKiyoshi Ueda if (!pgpath->is_active) 13331da177e4SLinus Torvalds goto out; 13341da177e4SLinus Torvalds 133504867370SMike Snitzer DMWARN("%s: Failing path %s.", 1336d4a512edSMike Snitzer dm_table_device_name(m->ti->table), 133704867370SMike Snitzer pgpath->path.dev->name); 13381da177e4SLinus Torvalds 13391da177e4SLinus Torvalds pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); 1340be7d31ccSMike Snitzer pgpath->is_active = false; 13411da177e4SLinus Torvalds pgpath->fail_count++; 13421da177e4SLinus Torvalds 134391e968aaSMike Snitzer atomic_dec(&m->nr_valid_paths); 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds if (pgpath == m->current_pgpath) 13461da177e4SLinus Torvalds m->current_pgpath = NULL; 13471da177e4SLinus Torvalds 1348b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti, 134991e968aaSMike Snitzer pgpath->path.dev->name, atomic_read(&m->nr_valid_paths)); 1350b15546f9SMike Anderson 1351fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13521da177e4SLinus Torvalds 1353be240ff5SAnatol Pomazau enable_nopath_timeout(m); 1354be240ff5SAnatol Pomazau 13551da177e4SLinus Torvalds out: 13561da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13571da177e4SLinus Torvalds 13581da177e4SLinus Torvalds return 0; 13591da177e4SLinus Torvalds } 13601da177e4SLinus Torvalds 13611da177e4SLinus Torvalds /* 13621da177e4SLinus Torvalds * Reinstate a previously-failed path 13631da177e4SLinus Torvalds */ 13641da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath) 13651da177e4SLinus Torvalds { 136663d832c3SHannes Reinecke int r = 0, run_queue = 0; 13671da177e4SLinus Torvalds unsigned long flags; 13681da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 1369*86a3238cSHeinz Mauelshagen unsigned int nr_valid_paths; 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13721da177e4SLinus Torvalds 13736680073dSKiyoshi Ueda if (pgpath->is_active) 13741da177e4SLinus Torvalds goto out; 13751da177e4SLinus Torvalds 137604867370SMike Snitzer DMWARN("%s: Reinstating path %s.", 1377d4a512edSMike Snitzer dm_table_device_name(m->ti->table), 137804867370SMike Snitzer pgpath->path.dev->name); 13791da177e4SLinus Torvalds 13801da177e4SLinus Torvalds r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); 13811da177e4SLinus Torvalds if (r) 13821da177e4SLinus Torvalds goto out; 13831da177e4SLinus Torvalds 1384be7d31ccSMike Snitzer pgpath->is_active = true; 13851da177e4SLinus Torvalds 138691e968aaSMike Snitzer nr_valid_paths = atomic_inc_return(&m->nr_valid_paths); 138791e968aaSMike Snitzer if (nr_valid_paths == 1) { 13881da177e4SLinus Torvalds m->current_pgpath = NULL; 138963d832c3SHannes Reinecke run_queue = 1; 1390e54f77ddSChandra Seetharaman } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) { 13914e2d19e4SChandra Seetharaman if (queue_work(kmpath_handlerd, &pgpath->activate_path.work)) 139291e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 1393e54f77ddSChandra Seetharaman } 13941da177e4SLinus Torvalds 1395b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti, 139691e968aaSMike Snitzer pgpath->path.dev->name, nr_valid_paths); 1397b15546f9SMike Anderson 1398fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds out: 14011da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 140276e33fe4SMike Snitzer if (run_queue) { 140363d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 14047e48c768SMike Snitzer process_queued_io_list(m); 140576e33fe4SMike Snitzer } 14061da177e4SLinus Torvalds 1407be240ff5SAnatol Pomazau if (pgpath->is_active) 1408be240ff5SAnatol Pomazau disable_nopath_timeout(m); 1409be240ff5SAnatol Pomazau 14101da177e4SLinus Torvalds return r; 14111da177e4SLinus Torvalds } 14121da177e4SLinus Torvalds 14131da177e4SLinus Torvalds /* 14141da177e4SLinus Torvalds * Fail or reinstate all paths that match the provided struct dm_dev. 14151da177e4SLinus Torvalds */ 14161da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev, 14171da177e4SLinus Torvalds action_fn action) 14181da177e4SLinus Torvalds { 141919040c0bSMike Snitzer int r = -EINVAL; 14201da177e4SLinus Torvalds struct pgpath *pgpath; 14211da177e4SLinus Torvalds struct priority_group *pg; 14221da177e4SLinus Torvalds 14231da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 14241da177e4SLinus Torvalds list_for_each_entry(pgpath, &pg->pgpaths, list) { 14251da177e4SLinus Torvalds if (pgpath->path.dev == dev) 14261da177e4SLinus Torvalds r = action(pgpath); 14271da177e4SLinus Torvalds } 14281da177e4SLinus Torvalds } 14291da177e4SLinus Torvalds 14301da177e4SLinus Torvalds return r; 14311da177e4SLinus Torvalds } 14321da177e4SLinus Torvalds 14331da177e4SLinus Torvalds /* 14341da177e4SLinus Torvalds * Temporarily try to avoid having to use the specified PG 14351da177e4SLinus Torvalds */ 14361da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg, 1437be7d31ccSMike Snitzer bool bypassed) 14381da177e4SLinus Torvalds { 14391da177e4SLinus Torvalds unsigned long flags; 14401da177e4SLinus Torvalds 14411da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 14421da177e4SLinus Torvalds 14431da177e4SLinus Torvalds pg->bypassed = bypassed; 14441da177e4SLinus Torvalds m->current_pgpath = NULL; 14451da177e4SLinus Torvalds m->current_pg = NULL; 14461da177e4SLinus Torvalds 14471da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 14481da177e4SLinus Torvalds 1449fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 14501da177e4SLinus Torvalds } 14511da177e4SLinus Torvalds 14521da177e4SLinus Torvalds /* 14531da177e4SLinus Torvalds * Switch to using the specified PG from the next I/O that gets mapped 14541da177e4SLinus Torvalds */ 14551da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr) 14561da177e4SLinus Torvalds { 14571da177e4SLinus Torvalds struct priority_group *pg; 1458*86a3238cSHeinz Mauelshagen unsigned int pgnum; 14591da177e4SLinus Torvalds unsigned long flags; 146031998ef1SMikulas Patocka char dummy; 14611da177e4SLinus Torvalds 146231998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 1463cc5bd925Stang.junhui !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) { 14641da177e4SLinus Torvalds DMWARN("invalid PG number supplied to switch_pg_num"); 14651da177e4SLinus Torvalds return -EINVAL; 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds 14681da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 14691da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 1470be7d31ccSMike Snitzer pg->bypassed = false; 14711da177e4SLinus Torvalds if (--pgnum) 14721da177e4SLinus Torvalds continue; 14731da177e4SLinus Torvalds 14741da177e4SLinus Torvalds m->current_pgpath = NULL; 14751da177e4SLinus Torvalds m->current_pg = NULL; 14761da177e4SLinus Torvalds m->next_pg = pg; 14771da177e4SLinus Torvalds } 14781da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 14791da177e4SLinus Torvalds 1480fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 14811da177e4SLinus Torvalds return 0; 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds 14841da177e4SLinus Torvalds /* 14851da177e4SLinus Torvalds * Set/clear bypassed status of a PG. 14861da177e4SLinus Torvalds * PGs are numbered upwards from 1 in the order they were declared. 14871da177e4SLinus Torvalds */ 1488be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed) 14891da177e4SLinus Torvalds { 14901da177e4SLinus Torvalds struct priority_group *pg; 1491*86a3238cSHeinz Mauelshagen unsigned int pgnum; 149231998ef1SMikulas Patocka char dummy; 14931da177e4SLinus Torvalds 149431998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 1495cc5bd925Stang.junhui !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) { 14961da177e4SLinus Torvalds DMWARN("invalid PG number supplied to bypass_pg"); 14971da177e4SLinus Torvalds return -EINVAL; 14981da177e4SLinus Torvalds } 14991da177e4SLinus Torvalds 15001da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 15011da177e4SLinus Torvalds if (!--pgnum) 15021da177e4SLinus Torvalds break; 15031da177e4SLinus Torvalds } 15041da177e4SLinus Torvalds 15051da177e4SLinus Torvalds bypass_pg(m, pg, bypassed); 15061da177e4SLinus Torvalds return 0; 15071da177e4SLinus Torvalds } 15081da177e4SLinus Torvalds 15091da177e4SLinus Torvalds /* 1510c9e45581SDave Wysochanski * Should we retry pg_init immediately? 1511c9e45581SDave Wysochanski */ 1512be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath) 1513c9e45581SDave Wysochanski { 1514c9e45581SDave Wysochanski unsigned long flags; 1515be7d31ccSMike Snitzer bool limit_reached = false; 1516c9e45581SDave Wysochanski 1517c9e45581SDave Wysochanski spin_lock_irqsave(&m->lock, flags); 1518c9e45581SDave Wysochanski 151991e968aaSMike Snitzer if (atomic_read(&m->pg_init_count) <= m->pg_init_retries && 152091e968aaSMike Snitzer !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 1521518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 1522c9e45581SDave Wysochanski else 1523be7d31ccSMike Snitzer limit_reached = true; 1524c9e45581SDave Wysochanski 1525c9e45581SDave Wysochanski spin_unlock_irqrestore(&m->lock, flags); 1526c9e45581SDave Wysochanski 1527c9e45581SDave Wysochanski return limit_reached; 1528c9e45581SDave Wysochanski } 1529c9e45581SDave Wysochanski 15303ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors) 1531cfae5c9bSChandra Seetharaman { 153283c0d5d5SMoger, Babu struct pgpath *pgpath = data; 1533cfae5c9bSChandra Seetharaman struct priority_group *pg = pgpath->pg; 1534cfae5c9bSChandra Seetharaman struct multipath *m = pg->m; 1535cfae5c9bSChandra Seetharaman unsigned long flags; 1536be7d31ccSMike Snitzer bool delay_retry = false; 1537cfae5c9bSChandra Seetharaman 1538cfae5c9bSChandra Seetharaman /* device or driver problems */ 1539cfae5c9bSChandra Seetharaman switch (errors) { 1540cfae5c9bSChandra Seetharaman case SCSI_DH_OK: 1541cfae5c9bSChandra Seetharaman break; 1542cfae5c9bSChandra Seetharaman case SCSI_DH_NOSYS: 1543cfae5c9bSChandra Seetharaman if (!m->hw_handler_name) { 1544cfae5c9bSChandra Seetharaman errors = 0; 1545cfae5c9bSChandra Seetharaman break; 1546cfae5c9bSChandra Seetharaman } 1547f7b934c8SMoger, Babu DMERR("Could not failover the device: Handler scsi_dh_%s " 1548f7b934c8SMoger, Babu "Error %d.", m->hw_handler_name, errors); 1549cfae5c9bSChandra Seetharaman /* 1550cfae5c9bSChandra Seetharaman * Fail path for now, so we do not ping pong 1551cfae5c9bSChandra Seetharaman */ 1552cfae5c9bSChandra Seetharaman fail_path(pgpath); 1553cfae5c9bSChandra Seetharaman break; 1554cfae5c9bSChandra Seetharaman case SCSI_DH_DEV_TEMP_BUSY: 1555cfae5c9bSChandra Seetharaman /* 1556cfae5c9bSChandra Seetharaman * Probably doing something like FW upgrade on the 1557cfae5c9bSChandra Seetharaman * controller so try the other pg. 1558cfae5c9bSChandra Seetharaman */ 1559be7d31ccSMike Snitzer bypass_pg(m, pg, true); 1560cfae5c9bSChandra Seetharaman break; 1561cfae5c9bSChandra Seetharaman case SCSI_DH_RETRY: 15624e2d19e4SChandra Seetharaman /* Wait before retrying. */ 15634ecc5081Szhengbin delay_retry = true; 1564df561f66SGustavo A. R. Silva fallthrough; 1565cfae5c9bSChandra Seetharaman case SCSI_DH_IMM_RETRY: 1566cfae5c9bSChandra Seetharaman case SCSI_DH_RES_TEMP_UNAVAIL: 1567cfae5c9bSChandra Seetharaman if (pg_init_limit_reached(m, pgpath)) 1568cfae5c9bSChandra Seetharaman fail_path(pgpath); 1569cfae5c9bSChandra Seetharaman errors = 0; 1570cfae5c9bSChandra Seetharaman break; 1571ec31f3f7SMike Snitzer case SCSI_DH_DEV_OFFLINED: 1572cfae5c9bSChandra Seetharaman default: 1573cfae5c9bSChandra Seetharaman /* 1574cfae5c9bSChandra Seetharaman * We probably do not want to fail the path for a device 1575cfae5c9bSChandra Seetharaman * error, but this is what the old dm did. In future 1576cfae5c9bSChandra Seetharaman * patches we can do more advanced handling. 1577cfae5c9bSChandra Seetharaman */ 1578cfae5c9bSChandra Seetharaman fail_path(pgpath); 1579cfae5c9bSChandra Seetharaman } 1580cfae5c9bSChandra Seetharaman 1581cfae5c9bSChandra Seetharaman spin_lock_irqsave(&m->lock, flags); 1582cfae5c9bSChandra Seetharaman if (errors) { 1583e54f77ddSChandra Seetharaman if (pgpath == m->current_pgpath) { 1584cfae5c9bSChandra Seetharaman DMERR("Could not failover device. Error %d.", errors); 1585cfae5c9bSChandra Seetharaman m->current_pgpath = NULL; 1586cfae5c9bSChandra Seetharaman m->current_pg = NULL; 1587e54f77ddSChandra Seetharaman } 1588518257b1SMike Snitzer } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 1589be7d31ccSMike Snitzer pg->bypassed = false; 1590cfae5c9bSChandra Seetharaman 159191e968aaSMike Snitzer if (atomic_dec_return(&m->pg_init_in_progress) > 0) 1592d0259bf0SKiyoshi Ueda /* Activations of other paths are still on going */ 1593d0259bf0SKiyoshi Ueda goto out; 1594d0259bf0SKiyoshi Ueda 1595518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 1596518257b1SMike Snitzer if (delay_retry) 1597518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1598518257b1SMike Snitzer else 1599518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1600518257b1SMike Snitzer 16013e9f1be1SHannes Reinecke if (__pg_init_all_paths(m)) 16023e9f1be1SHannes Reinecke goto out; 16033e9f1be1SHannes Reinecke } 1604518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 1605d0259bf0SKiyoshi Ueda 16067e48c768SMike Snitzer process_queued_io_list(m); 160776e33fe4SMike Snitzer 16082bded7bdSKiyoshi Ueda /* 16092bded7bdSKiyoshi Ueda * Wake up any thread waiting to suspend. 16102bded7bdSKiyoshi Ueda */ 16112bded7bdSKiyoshi Ueda wake_up(&m->pg_init_wait); 16122bded7bdSKiyoshi Ueda 1613d0259bf0SKiyoshi Ueda out: 1614cfae5c9bSChandra Seetharaman spin_unlock_irqrestore(&m->lock, flags); 1615cfae5c9bSChandra Seetharaman } 1616cfae5c9bSChandra Seetharaman 161789bfce76SBart Van Assche static void activate_or_offline_path(struct pgpath *pgpath) 1618bab7cfc7SChandra Seetharaman { 1619f10e06b7SMike Snitzer struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1620bab7cfc7SChandra Seetharaman 1621f10e06b7SMike Snitzer if (pgpath->is_active && !blk_queue_dying(q)) 1622f10e06b7SMike Snitzer scsi_dh_activate(q, pg_init_done, pgpath); 16233a017509SHannes Reinecke else 16243a017509SHannes Reinecke pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED); 1625bab7cfc7SChandra Seetharaman } 1626bab7cfc7SChandra Seetharaman 162789bfce76SBart Van Assche static void activate_path_work(struct work_struct *work) 162889bfce76SBart Van Assche { 162989bfce76SBart Van Assche struct pgpath *pgpath = 163089bfce76SBart Van Assche container_of(work, struct pgpath, activate_path.work); 163189bfce76SBart Van Assche 163289bfce76SBart Van Assche activate_or_offline_path(pgpath); 163389bfce76SBart Van Assche } 163489bfce76SBart Van Assche 1635b79f10eeSChristoph Hellwig static int multipath_end_io(struct dm_target *ti, struct request *clone, 16362a842acaSChristoph Hellwig blk_status_t error, union map_info *map_context) 16371da177e4SLinus Torvalds { 1638b79f10eeSChristoph Hellwig struct dm_mpath_io *mpio = get_mpio(map_context); 1639b79f10eeSChristoph Hellwig struct pgpath *pgpath = mpio->pgpath; 16407ed8578aSChristoph Hellwig int r = DM_ENDIO_DONE; 1641b79f10eeSChristoph Hellwig 1642f40c67f0SKiyoshi Ueda /* 1643f40c67f0SKiyoshi Ueda * We don't queue any clone request inside the multipath target 1644f40c67f0SKiyoshi Ueda * during end I/O handling, since those clone requests don't have 1645f40c67f0SKiyoshi Ueda * bio clones. If we queue them inside the multipath target, 1646f40c67f0SKiyoshi Ueda * we need to make bio clones, that requires memory allocation. 16474cc96131SMike Snitzer * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests 1648f40c67f0SKiyoshi Ueda * don't have bio clones.) 1649f40c67f0SKiyoshi Ueda * Instead of queueing the clone request here, we queue the original 1650f40c67f0SKiyoshi Ueda * request into dm core, which will remake a clone request and 1651f40c67f0SKiyoshi Ueda * clone bios for it and resubmit it later. 1652f40c67f0SKiyoshi Ueda */ 1653a1275677SKeith Busch if (error && blk_path_error(error)) { 1654b79f10eeSChristoph Hellwig struct multipath *m = ti->private; 16551da177e4SLinus Torvalds 1656ac514ffcSMike Snitzer if (error == BLK_STS_RESOURCE) 1657ac514ffcSMike Snitzer r = DM_ENDIO_DELAY_REQUEUE; 1658ac514ffcSMike Snitzer else 16597ed8578aSChristoph Hellwig r = DM_ENDIO_REQUEUE; 16601da177e4SLinus Torvalds 1661b79f10eeSChristoph Hellwig if (pgpath) 1662b79f10eeSChristoph Hellwig fail_path(pgpath); 16631da177e4SLinus Torvalds 166473265f3fSMike Snitzer if (!atomic_read(&m->nr_valid_paths) && 1665c1fd0abeSMike Snitzer !must_push_back_rq(m)) { 16662a842acaSChristoph Hellwig if (error == BLK_STS_IOERR) 166718a482f5SChristoph Hellwig dm_report_EIO(m); 16687ed8578aSChristoph Hellwig /* complete with the original error */ 16697ed8578aSChristoph Hellwig r = DM_ENDIO_DONE; 16707ed8578aSChristoph Hellwig } 16711da177e4SLinus Torvalds } 16721da177e4SLinus Torvalds 16731da177e4SLinus Torvalds if (pgpath) { 1674b79f10eeSChristoph Hellwig struct path_selector *ps = &pgpath->pg->ps; 1675b79f10eeSChristoph Hellwig 16761da177e4SLinus Torvalds if (ps->type->end_io) 1677087615bfSGabriel Krisman Bertazi ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes, 1678087615bfSGabriel Krisman Bertazi clone->io_start_time_ns); 16791da177e4SLinus Torvalds } 16801da177e4SLinus Torvalds 16817ed8578aSChristoph Hellwig return r; 16821da177e4SLinus Torvalds } 16831da177e4SLinus Torvalds 16844e4cbee9SChristoph Hellwig static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, 16854e4cbee9SChristoph Hellwig blk_status_t *error) 168676e33fe4SMike Snitzer { 168714ef1e48SChristoph Hellwig struct multipath *m = ti->private; 168814ef1e48SChristoph Hellwig struct dm_mpath_io *mpio = get_mpio_from_bio(clone); 168914ef1e48SChristoph Hellwig struct pgpath *pgpath = mpio->pgpath; 169076e33fe4SMike Snitzer unsigned long flags; 16911be56909SChristoph Hellwig int r = DM_ENDIO_DONE; 169276e33fe4SMike Snitzer 1693a1275677SKeith Busch if (!*error || !blk_path_error(*error)) 169414ef1e48SChristoph Hellwig goto done; 169576e33fe4SMike Snitzer 169614ef1e48SChristoph Hellwig if (pgpath) 169714ef1e48SChristoph Hellwig fail_path(pgpath); 169876e33fe4SMike Snitzer 1699a271a89cSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 1700a271a89cSMike Snitzer spin_lock_irqsave(&m->lock, flags); 1701a271a89cSMike Snitzer if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 1702a862e4e2SMike Snitzer if (__must_push_back(m)) { 1703c1fd0abeSMike Snitzer r = DM_ENDIO_REQUEUE; 1704c1fd0abeSMike Snitzer } else { 170518a482f5SChristoph Hellwig dm_report_EIO(m); 17064e4cbee9SChristoph Hellwig *error = BLK_STS_IOERR; 1707c1fd0abeSMike Snitzer } 1708a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 170914ef1e48SChristoph Hellwig goto done; 171018a482f5SChristoph Hellwig } 171176e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 1712a271a89cSMike Snitzer } 171376e33fe4SMike Snitzer 1714f45f1186SMike Snitzer multipath_queue_bio(m, clone); 17151be56909SChristoph Hellwig r = DM_ENDIO_INCOMPLETE; 171614ef1e48SChristoph Hellwig done: 171776e33fe4SMike Snitzer if (pgpath) { 171814ef1e48SChristoph Hellwig struct path_selector *ps = &pgpath->pg->ps; 171914ef1e48SChristoph Hellwig 172076e33fe4SMike Snitzer if (ps->type->end_io) 1721087615bfSGabriel Krisman Bertazi ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes, 1722c06dfd12SGabriel Krisman Bertazi (mpio->start_time_ns ?: 1723c06dfd12SGabriel Krisman Bertazi dm_start_time_ns_from_clone(clone))); 172476e33fe4SMike Snitzer } 172576e33fe4SMike Snitzer 17261be56909SChristoph Hellwig return r; 172776e33fe4SMike Snitzer } 172876e33fe4SMike Snitzer 17291da177e4SLinus Torvalds /* 1730553ec94cSMike Snitzer * Suspend with flush can't complete until all the I/O is processed 1731553ec94cSMike Snitzer * so if the last path fails we must error any remaining I/O. 1732553ec94cSMike Snitzer * - Note that if the freeze_bdev fails while suspending, the 1733436d4108SAlasdair G Kergon * queue_if_no_path state is lost - userspace should reset it. 1734553ec94cSMike Snitzer * Otherwise, during noflush suspend, queue_if_no_path will not change. 17351da177e4SLinus Torvalds */ 17361da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti) 17371da177e4SLinus Torvalds { 17387943bd6dSMike Snitzer struct multipath *m = ti->private; 17391da177e4SLinus Torvalds 1740553ec94cSMike Snitzer /* FIXME: bio-based shouldn't need to always disable queue_if_no_path */ 1741553ec94cSMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED || !dm_noflush_suspending(m->ti)) 17424c3f4838SMike Snitzer queue_if_no_path(m, false, true, __func__); 17431da177e4SLinus Torvalds } 17441da177e4SLinus Torvalds 17456df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti) 17466df400abSKiyoshi Ueda { 17476380f26fSMike Anderson struct multipath *m = ti->private; 17486380f26fSMike Anderson 17496380f26fSMike Anderson mutex_lock(&m->work_mutex); 17502bded7bdSKiyoshi Ueda flush_multipath_work(m); 17516380f26fSMike Anderson mutex_unlock(&m->work_mutex); 17526df400abSKiyoshi Ueda } 17536df400abSKiyoshi Ueda 1754436d4108SAlasdair G Kergon /* 1755436d4108SAlasdair G Kergon * Restore the queue_if_no_path setting. 1756436d4108SAlasdair G Kergon */ 17571da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti) 17581da177e4SLinus Torvalds { 17597943bd6dSMike Snitzer struct multipath *m = ti->private; 17601814f2e3SMike Snitzer unsigned long flags; 17611da177e4SLinus Torvalds 17621814f2e3SMike Snitzer spin_lock_irqsave(&m->lock, flags); 1763553ec94cSMike Snitzer if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) { 1764553ec94cSMike Snitzer set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 1765553ec94cSMike Snitzer clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 1766553ec94cSMike Snitzer } 17674c3f4838SMike Snitzer 17684c3f4838SMike Snitzer DMDEBUG("%s: %s finished; QIFNP = %d; SQIFNP = %d", 1769d4a512edSMike Snitzer dm_table_device_name(m->ti->table), __func__, 17704c3f4838SMike Snitzer test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags), 17714c3f4838SMike Snitzer test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)); 17724c3f4838SMike Snitzer 17731814f2e3SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 17741da177e4SLinus Torvalds } 17751da177e4SLinus Torvalds 17761da177e4SLinus Torvalds /* 17771da177e4SLinus Torvalds * Info output has the following format: 17781da177e4SLinus Torvalds * num_multipath_feature_args [multipath_feature_args]* 17791da177e4SLinus Torvalds * num_handler_status_args [handler_status_args]* 17801da177e4SLinus Torvalds * num_groups init_group_number 17811da177e4SLinus Torvalds * [A|D|E num_ps_status_args [ps_status_args]* 17821da177e4SLinus Torvalds * num_paths num_selector_args 17831da177e4SLinus Torvalds * [path_dev A|F fail_count [selector_args]* ]+ ]+ 17841da177e4SLinus Torvalds * 17851da177e4SLinus Torvalds * Table output has the following format (identical to the constructor string): 17861da177e4SLinus Torvalds * num_feature_args [features_args]* 17871da177e4SLinus Torvalds * num_handler_args hw_handler [hw_handler_args]* 17881da177e4SLinus Torvalds * num_groups init_group_number 17891da177e4SLinus Torvalds * [priority selector-name num_ps_args [ps_args]* 17901da177e4SLinus Torvalds * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ 17911da177e4SLinus Torvalds */ 1792fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type, 1793*86a3238cSHeinz Mauelshagen unsigned int status_flags, char *result, unsigned int maxlen) 17941da177e4SLinus Torvalds { 179533ace4caSTushar Sugandhi int sz = 0, pg_counter, pgpath_counter; 17961da177e4SLinus Torvalds unsigned long flags; 17977943bd6dSMike Snitzer struct multipath *m = ti->private; 17981da177e4SLinus Torvalds struct priority_group *pg; 17991da177e4SLinus Torvalds struct pgpath *p; 1800*86a3238cSHeinz Mauelshagen unsigned int pg_num; 18011da177e4SLinus Torvalds char state; 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 18041da177e4SLinus Torvalds 18051da177e4SLinus Torvalds /* Features */ 18061da177e4SLinus Torvalds if (type == STATUSTYPE_INFO) 180791e968aaSMike Snitzer DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags), 180891e968aaSMike Snitzer atomic_read(&m->pg_init_count)); 1809c9e45581SDave Wysochanski else { 1810518257b1SMike Snitzer DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) + 18114e2d19e4SChandra Seetharaman (m->pg_init_retries > 0) * 2 + 1812a58a935dSMike Snitzer (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 + 1813e83068a5SMike Snitzer test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) + 1814e83068a5SMike Snitzer (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2); 1815e83068a5SMike Snitzer 1816518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 1817c9e45581SDave Wysochanski DMEMIT("queue_if_no_path "); 1818c9e45581SDave Wysochanski if (m->pg_init_retries) 1819c9e45581SDave Wysochanski DMEMIT("pg_init_retries %u ", m->pg_init_retries); 18204e2d19e4SChandra Seetharaman if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) 18214e2d19e4SChandra Seetharaman DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs); 1822518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) 1823a58a935dSMike Snitzer DMEMIT("retain_attached_hw_handler "); 1824e83068a5SMike Snitzer if (m->queue_mode != DM_TYPE_REQUEST_BASED) { 1825e83068a5SMike Snitzer switch(m->queue_mode) { 1826e83068a5SMike Snitzer case DM_TYPE_BIO_BASED: 1827e83068a5SMike Snitzer DMEMIT("queue_mode bio "); 1828e83068a5SMike Snitzer break; 18297e0d574fSBart Van Assche default: 18307e0d574fSBart Van Assche WARN_ON_ONCE(true); 18317e0d574fSBart Van Assche break; 1832e83068a5SMike Snitzer } 1833e83068a5SMike Snitzer } 1834c9e45581SDave Wysochanski } 18351da177e4SLinus Torvalds 1836cfae5c9bSChandra Seetharaman if (!m->hw_handler_name || type == STATUSTYPE_INFO) 18371da177e4SLinus Torvalds DMEMIT("0 "); 18381da177e4SLinus Torvalds else 1839cfae5c9bSChandra Seetharaman DMEMIT("1 %s ", m->hw_handler_name); 18401da177e4SLinus Torvalds 18411da177e4SLinus Torvalds DMEMIT("%u ", m->nr_priority_groups); 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds if (m->next_pg) 18441da177e4SLinus Torvalds pg_num = m->next_pg->pg_num; 18451da177e4SLinus Torvalds else if (m->current_pg) 18461da177e4SLinus Torvalds pg_num = m->current_pg->pg_num; 18471da177e4SLinus Torvalds else 1848a490a07aSMike Snitzer pg_num = (m->nr_priority_groups ? 1 : 0); 18491da177e4SLinus Torvalds 18501da177e4SLinus Torvalds DMEMIT("%u ", pg_num); 18511da177e4SLinus Torvalds 18521da177e4SLinus Torvalds switch (type) { 18531da177e4SLinus Torvalds case STATUSTYPE_INFO: 18541da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 18551da177e4SLinus Torvalds if (pg->bypassed) 18561da177e4SLinus Torvalds state = 'D'; /* Disabled */ 18571da177e4SLinus Torvalds else if (pg == m->current_pg) 18581da177e4SLinus Torvalds state = 'A'; /* Currently Active */ 18591da177e4SLinus Torvalds else 18601da177e4SLinus Torvalds state = 'E'; /* Enabled */ 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds DMEMIT("%c ", state); 18631da177e4SLinus Torvalds 18641da177e4SLinus Torvalds if (pg->ps.type->status) 18651da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 18661da177e4SLinus Torvalds result + sz, 18671da177e4SLinus Torvalds maxlen - sz); 18681da177e4SLinus Torvalds else 18691da177e4SLinus Torvalds DMEMIT("0 "); 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 18721da177e4SLinus Torvalds pg->ps.type->info_args); 18731da177e4SLinus Torvalds 18741da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 18751da177e4SLinus Torvalds DMEMIT("%s %s %u ", p->path.dev->name, 18766680073dSKiyoshi Ueda p->is_active ? "A" : "F", 18771da177e4SLinus Torvalds p->fail_count); 18781da177e4SLinus Torvalds if (pg->ps.type->status) 18791da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 18801da177e4SLinus Torvalds &p->path, type, result + sz, 18811da177e4SLinus Torvalds maxlen - sz); 18821da177e4SLinus Torvalds } 18831da177e4SLinus Torvalds } 18841da177e4SLinus Torvalds break; 18851da177e4SLinus Torvalds 18861da177e4SLinus Torvalds case STATUSTYPE_TABLE: 18871da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 18881da177e4SLinus Torvalds DMEMIT("%s ", pg->ps.type->name); 18891da177e4SLinus Torvalds 18901da177e4SLinus Torvalds if (pg->ps.type->status) 18911da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 18921da177e4SLinus Torvalds result + sz, 18931da177e4SLinus Torvalds maxlen - sz); 18941da177e4SLinus Torvalds else 18951da177e4SLinus Torvalds DMEMIT("0 "); 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 18981da177e4SLinus Torvalds pg->ps.type->table_args); 18991da177e4SLinus Torvalds 19001da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 19011da177e4SLinus Torvalds DMEMIT("%s ", p->path.dev->name); 19021da177e4SLinus Torvalds if (pg->ps.type->status) 19031da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 19041da177e4SLinus Torvalds &p->path, type, result + sz, 19051da177e4SLinus Torvalds maxlen - sz); 19061da177e4SLinus Torvalds } 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds break; 19098ec45662STushar Sugandhi 19108ec45662STushar Sugandhi case STATUSTYPE_IMA: 191133ace4caSTushar Sugandhi sz = 0; /*reset the result pointer*/ 191233ace4caSTushar Sugandhi 19138ec45662STushar Sugandhi DMEMIT_TARGET_NAME_VERSION(ti->type); 191433ace4caSTushar Sugandhi DMEMIT(",nr_priority_groups=%u", m->nr_priority_groups); 191533ace4caSTushar Sugandhi 191633ace4caSTushar Sugandhi pg_counter = 0; 19178ec45662STushar Sugandhi list_for_each_entry(pg, &m->priority_groups, list) { 19188ec45662STushar Sugandhi if (pg->bypassed) 19198ec45662STushar Sugandhi state = 'D'; /* Disabled */ 19208ec45662STushar Sugandhi else if (pg == m->current_pg) 19218ec45662STushar Sugandhi state = 'A'; /* Currently Active */ 19228ec45662STushar Sugandhi else 19238ec45662STushar Sugandhi state = 'E'; /* Enabled */ 192433ace4caSTushar Sugandhi DMEMIT(",pg_state_%d=%c", pg_counter, state); 192533ace4caSTushar Sugandhi DMEMIT(",nr_pgpaths_%d=%u", pg_counter, pg->nr_pgpaths); 192633ace4caSTushar Sugandhi DMEMIT(",path_selector_name_%d=%s", pg_counter, pg->ps.type->name); 19278ec45662STushar Sugandhi 192833ace4caSTushar Sugandhi pgpath_counter = 0; 19298ec45662STushar Sugandhi list_for_each_entry(p, &pg->pgpaths, list) { 193033ace4caSTushar Sugandhi DMEMIT(",path_name_%d_%d=%s,is_active_%d_%d=%c,fail_count_%d_%d=%u", 193133ace4caSTushar Sugandhi pg_counter, pgpath_counter, p->path.dev->name, 193233ace4caSTushar Sugandhi pg_counter, pgpath_counter, p->is_active ? 'A' : 'F', 193333ace4caSTushar Sugandhi pg_counter, pgpath_counter, p->fail_count); 19348ec45662STushar Sugandhi if (pg->ps.type->status) { 193533ace4caSTushar Sugandhi DMEMIT(",path_selector_status_%d_%d=", 193633ace4caSTushar Sugandhi pg_counter, pgpath_counter); 19378ec45662STushar Sugandhi sz += pg->ps.type->status(&pg->ps, &p->path, 19388ec45662STushar Sugandhi type, result + sz, 19398ec45662STushar Sugandhi maxlen - sz); 19408ec45662STushar Sugandhi } 194133ace4caSTushar Sugandhi pgpath_counter++; 19428ec45662STushar Sugandhi } 194333ace4caSTushar Sugandhi pg_counter++; 19448ec45662STushar Sugandhi } 19458ec45662STushar Sugandhi DMEMIT(";"); 19468ec45662STushar Sugandhi break; 19471da177e4SLinus Torvalds } 19481da177e4SLinus Torvalds 19491da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 19501da177e4SLinus Torvalds } 19511da177e4SLinus Torvalds 1952*86a3238cSHeinz Mauelshagen static int multipath_message(struct dm_target *ti, unsigned int argc, char **argv, 1953*86a3238cSHeinz Mauelshagen char *result, unsigned int maxlen) 19541da177e4SLinus Torvalds { 19556380f26fSMike Anderson int r = -EINVAL; 19561da177e4SLinus Torvalds struct dm_dev *dev; 19577943bd6dSMike Snitzer struct multipath *m = ti->private; 19581da177e4SLinus Torvalds action_fn action; 1959be240ff5SAnatol Pomazau unsigned long flags; 19601da177e4SLinus Torvalds 19616380f26fSMike Anderson mutex_lock(&m->work_mutex); 19626380f26fSMike Anderson 1963c2f3d24bSKiyoshi Ueda if (dm_suspended(ti)) { 1964c2f3d24bSKiyoshi Ueda r = -EBUSY; 1965c2f3d24bSKiyoshi Ueda goto out; 1966c2f3d24bSKiyoshi Ueda } 1967c2f3d24bSKiyoshi Ueda 19681da177e4SLinus Torvalds if (argc == 1) { 1969498f0103SMike Snitzer if (!strcasecmp(argv[0], "queue_if_no_path")) { 19704c3f4838SMike Snitzer r = queue_if_no_path(m, true, false, __func__); 1971be240ff5SAnatol Pomazau spin_lock_irqsave(&m->lock, flags); 1972be240ff5SAnatol Pomazau enable_nopath_timeout(m); 1973be240ff5SAnatol Pomazau spin_unlock_irqrestore(&m->lock, flags); 19746380f26fSMike Anderson goto out; 1975498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "fail_if_no_path")) { 19764c3f4838SMike Snitzer r = queue_if_no_path(m, false, false, __func__); 1977be240ff5SAnatol Pomazau disable_nopath_timeout(m); 19786380f26fSMike Anderson goto out; 19796380f26fSMike Anderson } 19801da177e4SLinus Torvalds } 19811da177e4SLinus Torvalds 19826380f26fSMike Anderson if (argc != 2) { 1983a356e426SJose Castillo DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc); 19846380f26fSMike Anderson goto out; 19856380f26fSMike Anderson } 19861da177e4SLinus Torvalds 1987498f0103SMike Snitzer if (!strcasecmp(argv[0], "disable_group")) { 1988be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], true); 19896380f26fSMike Anderson goto out; 1990498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "enable_group")) { 1991be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], false); 19926380f26fSMike Anderson goto out; 1993498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "switch_group")) { 19946380f26fSMike Anderson r = switch_pg_num(m, argv[1]); 19956380f26fSMike Anderson goto out; 1996498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "reinstate_path")) 19971da177e4SLinus Torvalds action = reinstate_path; 1998498f0103SMike Snitzer else if (!strcasecmp(argv[0], "fail_path")) 19991da177e4SLinus Torvalds action = fail_path; 20006380f26fSMike Anderson else { 2001a356e426SJose Castillo DMWARN("Unrecognised multipath message received: %s", argv[0]); 20026380f26fSMike Anderson goto out; 20036380f26fSMike Anderson } 20041da177e4SLinus Torvalds 20058215d6ecSNikanth Karthikesan r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev); 20061da177e4SLinus Torvalds if (r) { 200772d94861SAlasdair G Kergon DMWARN("message: error getting device %s", 20081da177e4SLinus Torvalds argv[1]); 20096380f26fSMike Anderson goto out; 20101da177e4SLinus Torvalds } 20111da177e4SLinus Torvalds 20121da177e4SLinus Torvalds r = action_dev(m, dev, action); 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds dm_put_device(ti, dev); 20151da177e4SLinus Torvalds 20166380f26fSMike Anderson out: 20176380f26fSMike Anderson mutex_unlock(&m->work_mutex); 20181da177e4SLinus Torvalds return r; 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds 2021e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti, 20225bd5e8d8SMike Snitzer struct block_device **bdev) 20239af4aa30SMilan Broz { 202435991652SMikulas Patocka struct multipath *m = ti->private; 2025564dbb13SMike Snitzer struct pgpath *pgpath; 202669cea0d4SMike Snitzer unsigned long flags; 202735991652SMikulas Patocka int r; 202835991652SMikulas Patocka 2029564dbb13SMike Snitzer pgpath = READ_ONCE(m->current_pgpath); 2030374117adSMike Snitzer if (!pgpath || !mpath_double_check_test_bit(MPATHF_QUEUE_IO, m)) 2031564dbb13SMike Snitzer pgpath = choose_pgpath(m, 0); 20329af4aa30SMilan Broz 2033564dbb13SMike Snitzer if (pgpath) { 2034374117adSMike Snitzer if (!mpath_double_check_test_bit(MPATHF_QUEUE_IO, m)) { 2035564dbb13SMike Snitzer *bdev = pgpath->path.dev->bdev; 203643e43c9eSJunichi Nomura r = 0; 203743e43c9eSJunichi Nomura } else { 203843e43c9eSJunichi Nomura /* pg_init has not started or completed */ 20396c182cd8SHannes Reinecke r = -ENOTCONN; 204043e43c9eSJunichi Nomura } 204143e43c9eSJunichi Nomura } else { 204243e43c9eSJunichi Nomura /* No path is available */ 2043a271a89cSMike Snitzer r = -EIO; 2044a271a89cSMike Snitzer spin_lock_irqsave(&m->lock, flags); 2045518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 204643e43c9eSJunichi Nomura r = -ENOTCONN; 2047a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 204843e43c9eSJunichi Nomura } 20499af4aa30SMilan Broz 20505bbbfdf6SJunichi Nomura if (r == -ENOTCONN) { 2051506458efSWill Deacon if (!READ_ONCE(m->current_pg)) { 20523e9f1be1SHannes Reinecke /* Path status changed, redo selection */ 20532da1610aSMike Snitzer (void) choose_pgpath(m, 0); 20543e9f1be1SHannes Reinecke } 205569cea0d4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 2056518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 205769cea0d4SMike Snitzer (void) __pg_init_all_paths(m); 205869cea0d4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 205963d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 20607e48c768SMike Snitzer process_queued_io_list(m); 20613e9f1be1SHannes Reinecke } 206235991652SMikulas Patocka 2063e56f81e0SChristoph Hellwig /* 2064e56f81e0SChristoph Hellwig * Only pass ioctls through if the device sizes match exactly. 2065e56f81e0SChristoph Hellwig */ 20666dcbb52cSChristoph Hellwig if (!r && ti->len != bdev_nr_sectors((*bdev))) 2067e56f81e0SChristoph Hellwig return 1; 2068e56f81e0SChristoph Hellwig return r; 20699af4aa30SMilan Broz } 20709af4aa30SMilan Broz 2071af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti, 2072af4874e0SMike Snitzer iterate_devices_callout_fn fn, void *data) 2073af4874e0SMike Snitzer { 2074af4874e0SMike Snitzer struct multipath *m = ti->private; 2075af4874e0SMike Snitzer struct priority_group *pg; 2076af4874e0SMike Snitzer struct pgpath *p; 2077af4874e0SMike Snitzer int ret = 0; 2078af4874e0SMike Snitzer 2079af4874e0SMike Snitzer list_for_each_entry(pg, &m->priority_groups, list) { 2080af4874e0SMike Snitzer list_for_each_entry(p, &pg->pgpaths, list) { 20815dea271bSMike Snitzer ret = fn(ti, p->path.dev, ti->begin, ti->len, data); 2082af4874e0SMike Snitzer if (ret) 2083af4874e0SMike Snitzer goto out; 2084af4874e0SMike Snitzer } 2085af4874e0SMike Snitzer } 2086af4874e0SMike Snitzer 2087af4874e0SMike Snitzer out: 2088af4874e0SMike Snitzer return ret; 2089af4874e0SMike Snitzer } 2090af4874e0SMike Snitzer 20919f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath) 2092f40c67f0SKiyoshi Ueda { 2093f40c67f0SKiyoshi Ueda struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 2094f40c67f0SKiyoshi Ueda 209552b09914SMike Snitzer return blk_lld_busy(q); 2096f40c67f0SKiyoshi Ueda } 2097f40c67f0SKiyoshi Ueda 2098f40c67f0SKiyoshi Ueda /* 2099f40c67f0SKiyoshi Ueda * We return "busy", only when we can map I/Os but underlying devices 2100f40c67f0SKiyoshi Ueda * are busy (so even if we map I/Os now, the I/Os will wait on 2101f40c67f0SKiyoshi Ueda * the underlying queue). 2102f40c67f0SKiyoshi Ueda * In other words, if we want to kill I/Os or queue them inside us 2103f40c67f0SKiyoshi Ueda * due to map unavailability, we don't return "busy". Otherwise, 2104f40c67f0SKiyoshi Ueda * dm core won't give us the I/Os and we can't do what we want. 2105f40c67f0SKiyoshi Ueda */ 2106f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti) 2107f40c67f0SKiyoshi Ueda { 2108be7d31ccSMike Snitzer bool busy = false, has_active = false; 2109f40c67f0SKiyoshi Ueda struct multipath *m = ti->private; 21102da1610aSMike Snitzer struct priority_group *pg, *next_pg; 2111f40c67f0SKiyoshi Ueda struct pgpath *pgpath; 2112f40c67f0SKiyoshi Ueda 2113b88efd43SMike Snitzer /* pg_init in progress */ 2114b88efd43SMike Snitzer if (atomic_read(&m->pg_init_in_progress)) 21152da1610aSMike Snitzer return true; 21162da1610aSMike Snitzer 2117b88efd43SMike Snitzer /* no paths available, for blk-mq: rely on IO mapping to delay requeue */ 2118a271a89cSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 2119a271a89cSMike Snitzer unsigned long flags; 2120a271a89cSMike Snitzer spin_lock_irqsave(&m->lock, flags); 2121a271a89cSMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 2122a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 2123953923c0SMike Snitzer return (m->queue_mode != DM_TYPE_REQUEST_BASED); 2124a271a89cSMike Snitzer } 2125a271a89cSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 2126a271a89cSMike Snitzer } 2127b88efd43SMike Snitzer 2128f40c67f0SKiyoshi Ueda /* Guess which priority_group will be used at next mapping time */ 2129506458efSWill Deacon pg = READ_ONCE(m->current_pg); 2130506458efSWill Deacon next_pg = READ_ONCE(m->next_pg); 2131506458efSWill Deacon if (unlikely(!READ_ONCE(m->current_pgpath) && next_pg)) 21322da1610aSMike Snitzer pg = next_pg; 21332da1610aSMike Snitzer 21342da1610aSMike Snitzer if (!pg) { 2135f40c67f0SKiyoshi Ueda /* 2136f40c67f0SKiyoshi Ueda * We don't know which pg will be used at next mapping time. 21372da1610aSMike Snitzer * We don't call choose_pgpath() here to avoid to trigger 2138f40c67f0SKiyoshi Ueda * pg_init just by busy checking. 2139f40c67f0SKiyoshi Ueda * So we don't know whether underlying devices we will be using 2140f40c67f0SKiyoshi Ueda * at next mapping time are busy or not. Just try mapping. 2141f40c67f0SKiyoshi Ueda */ 21422da1610aSMike Snitzer return busy; 21432da1610aSMike Snitzer } 2144f40c67f0SKiyoshi Ueda 2145f40c67f0SKiyoshi Ueda /* 2146f40c67f0SKiyoshi Ueda * If there is one non-busy active path at least, the path selector 2147f40c67f0SKiyoshi Ueda * will be able to select it. So we consider such a pg as not busy. 2148f40c67f0SKiyoshi Ueda */ 2149be7d31ccSMike Snitzer busy = true; 21502da1610aSMike Snitzer list_for_each_entry(pgpath, &pg->pgpaths, list) { 2151f40c67f0SKiyoshi Ueda if (pgpath->is_active) { 2152be7d31ccSMike Snitzer has_active = true; 21539f54cec5SMike Snitzer if (!pgpath_busy(pgpath)) { 2154be7d31ccSMike Snitzer busy = false; 2155f40c67f0SKiyoshi Ueda break; 2156f40c67f0SKiyoshi Ueda } 2157f40c67f0SKiyoshi Ueda } 21582da1610aSMike Snitzer } 2159f40c67f0SKiyoshi Ueda 21602da1610aSMike Snitzer if (!has_active) { 2161f40c67f0SKiyoshi Ueda /* 2162f40c67f0SKiyoshi Ueda * No active path in this pg, so this pg won't be used and 2163f40c67f0SKiyoshi Ueda * the current_pg will be changed at next mapping time. 2164f40c67f0SKiyoshi Ueda * We need to try mapping to determine it. 2165f40c67f0SKiyoshi Ueda */ 2166be7d31ccSMike Snitzer busy = false; 21672da1610aSMike Snitzer } 2168f40c67f0SKiyoshi Ueda 2169f40c67f0SKiyoshi Ueda return busy; 2170f40c67f0SKiyoshi Ueda } 2171f40c67f0SKiyoshi Ueda 21721da177e4SLinus Torvalds /*----------------------------------------------------------------- 21731da177e4SLinus Torvalds * Module setup 21741da177e4SLinus Torvalds *---------------------------------------------------------------*/ 21751da177e4SLinus Torvalds static struct target_type multipath_target = { 21761da177e4SLinus Torvalds .name = "multipath", 2177636be424SMike Snitzer .version = {1, 14, 0}, 21788c5c1473SSteffen Maier .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE | 21798c5c1473SSteffen Maier DM_TARGET_PASSES_INTEGRITY, 21801da177e4SLinus Torvalds .module = THIS_MODULE, 21811da177e4SLinus Torvalds .ctr = multipath_ctr, 21821da177e4SLinus Torvalds .dtr = multipath_dtr, 2183e5863d9aSMike Snitzer .clone_and_map_rq = multipath_clone_and_map, 2184e5863d9aSMike Snitzer .release_clone_rq = multipath_release_clone, 2185f40c67f0SKiyoshi Ueda .rq_end_io = multipath_end_io, 218676e33fe4SMike Snitzer .map = multipath_map_bio, 218776e33fe4SMike Snitzer .end_io = multipath_end_io_bio, 218876e33fe4SMike Snitzer .presuspend = multipath_presuspend, 218976e33fe4SMike Snitzer .postsuspend = multipath_postsuspend, 219076e33fe4SMike Snitzer .resume = multipath_resume, 219176e33fe4SMike Snitzer .status = multipath_status, 219276e33fe4SMike Snitzer .message = multipath_message, 219376e33fe4SMike Snitzer .prepare_ioctl = multipath_prepare_ioctl, 219476e33fe4SMike Snitzer .iterate_devices = multipath_iterate_devices, 219576e33fe4SMike Snitzer .busy = multipath_busy, 219676e33fe4SMike Snitzer }; 219776e33fe4SMike Snitzer 21981da177e4SLinus Torvalds static int __init dm_multipath_init(void) 21991da177e4SLinus Torvalds { 22001da177e4SLinus Torvalds int r; 22011da177e4SLinus Torvalds 22024d4d66abSTejun Heo kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0); 2203c557308eSAlasdair G Kergon if (!kmultipathd) { 22040cd33124SAlasdair G Kergon DMERR("failed to create workqueue kmpathd"); 2205ff658e9cSJohannes Thumshirn r = -ENOMEM; 2206ff658e9cSJohannes Thumshirn goto bad_alloc_kmultipathd; 2207c557308eSAlasdair G Kergon } 2208c557308eSAlasdair G Kergon 2209bab7cfc7SChandra Seetharaman /* 2210bab7cfc7SChandra Seetharaman * A separate workqueue is used to handle the device handlers 2211bab7cfc7SChandra Seetharaman * to avoid overloading existing workqueue. Overloading the 2212bab7cfc7SChandra Seetharaman * old workqueue would also create a bottleneck in the 2213bab7cfc7SChandra Seetharaman * path of the storage hardware device activation. 2214bab7cfc7SChandra Seetharaman */ 22154d4d66abSTejun Heo kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd", 22164d4d66abSTejun Heo WQ_MEM_RECLAIM); 2217bab7cfc7SChandra Seetharaman if (!kmpath_handlerd) { 2218bab7cfc7SChandra Seetharaman DMERR("failed to create workqueue kmpath_handlerd"); 2219ff658e9cSJohannes Thumshirn r = -ENOMEM; 2220ff658e9cSJohannes Thumshirn goto bad_alloc_kmpath_handlerd; 2221bab7cfc7SChandra Seetharaman } 2222bab7cfc7SChandra Seetharaman 22237e6358d2Smonty_pavel@sina.com r = dm_register_target(&multipath_target); 22247e6358d2Smonty_pavel@sina.com if (r < 0) { 22257e6358d2Smonty_pavel@sina.com DMERR("request-based register failed %d", r); 22267e6358d2Smonty_pavel@sina.com r = -EINVAL; 22277e6358d2Smonty_pavel@sina.com goto bad_register_target; 22287e6358d2Smonty_pavel@sina.com } 22297e6358d2Smonty_pavel@sina.com 2230ff658e9cSJohannes Thumshirn return 0; 2231ff658e9cSJohannes Thumshirn 22327e6358d2Smonty_pavel@sina.com bad_register_target: 22337e6358d2Smonty_pavel@sina.com destroy_workqueue(kmpath_handlerd); 2234ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd: 2235ff658e9cSJohannes Thumshirn destroy_workqueue(kmultipathd); 2236ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd: 22371da177e4SLinus Torvalds return r; 22381da177e4SLinus Torvalds } 22391da177e4SLinus Torvalds 22401da177e4SLinus Torvalds static void __exit dm_multipath_exit(void) 22411da177e4SLinus Torvalds { 2242bab7cfc7SChandra Seetharaman destroy_workqueue(kmpath_handlerd); 2243c557308eSAlasdair G Kergon destroy_workqueue(kmultipathd); 2244c557308eSAlasdair G Kergon 224510d3bd09SMikulas Patocka dm_unregister_target(&multipath_target); 22461da177e4SLinus Torvalds } 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds module_init(dm_multipath_init); 22491da177e4SLinus Torvalds module_exit(dm_multipath_exit); 22501da177e4SLinus Torvalds 2251be240ff5SAnatol Pomazau module_param_named(queue_if_no_path_timeout_secs, 2252be240ff5SAnatol Pomazau queue_if_no_path_timeout_secs, ulong, S_IRUGO | S_IWUSR); 2253be240ff5SAnatol Pomazau MODULE_PARM_DESC(queue_if_no_path_timeout_secs, "No available paths queue IO timeout in seconds"); 2254be240ff5SAnatol Pomazau 22551da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target"); 22561da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); 22571da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2258