11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (C) 2003 Sistina Software Limited. 31da177e4SLinus Torvalds * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * This file is released under the GPL. 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 8586e80e6SMikulas Patocka #include <linux/device-mapper.h> 9586e80e6SMikulas Patocka 104cc96131SMike Snitzer #include "dm-rq.h" 1176e33fe4SMike Snitzer #include "dm-bio-record.h" 121da177e4SLinus Torvalds #include "dm-path-selector.h" 13b15546f9SMike Anderson #include "dm-uevent.h" 141da177e4SLinus Torvalds 15e5863d9aSMike Snitzer #include <linux/blkdev.h> 161da177e4SLinus Torvalds #include <linux/ctype.h> 171da177e4SLinus Torvalds #include <linux/init.h> 181da177e4SLinus Torvalds #include <linux/mempool.h> 191da177e4SLinus Torvalds #include <linux/module.h> 201da177e4SLinus Torvalds #include <linux/pagemap.h> 211da177e4SLinus Torvalds #include <linux/slab.h> 221da177e4SLinus Torvalds #include <linux/time.h> 231da177e4SLinus Torvalds #include <linux/workqueue.h> 2435991652SMikulas Patocka #include <linux/delay.h> 25cfae5c9bSChandra Seetharaman #include <scsi/scsi_dh.h> 2660063497SArun Sharma #include <linux/atomic.h> 2778ce23b5SMike Snitzer #include <linux/blk-mq.h> 281da177e4SLinus Torvalds 2972d94861SAlasdair G Kergon #define DM_MSG_PREFIX "multipath" 304e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_MSECS 2000 314e2d19e4SChandra Seetharaman #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1) 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds /* Path properties */ 341da177e4SLinus Torvalds struct pgpath { 351da177e4SLinus Torvalds struct list_head list; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds struct priority_group *pg; /* Owning PG */ 381da177e4SLinus Torvalds unsigned fail_count; /* Cumulative failure count */ 391da177e4SLinus Torvalds 40c922d5f7SJosef "Jeff" Sipek struct dm_path path; 414e2d19e4SChandra Seetharaman struct delayed_work activate_path; 42be7d31ccSMike Snitzer 43be7d31ccSMike Snitzer bool is_active:1; /* Path status */ 441da177e4SLinus Torvalds }; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path) 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds /* 491da177e4SLinus Torvalds * Paths are grouped into Priority Groups and numbered from 1 upwards. 501da177e4SLinus Torvalds * Each has a path selector which controls which path gets used. 511da177e4SLinus Torvalds */ 521da177e4SLinus Torvalds struct priority_group { 531da177e4SLinus Torvalds struct list_head list; 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds struct multipath *m; /* Owning multipath instance */ 561da177e4SLinus Torvalds struct path_selector ps; 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds unsigned pg_num; /* Reference number */ 591da177e4SLinus Torvalds unsigned nr_pgpaths; /* Number of paths in PG */ 601da177e4SLinus Torvalds struct list_head pgpaths; 61be7d31ccSMike Snitzer 62be7d31ccSMike Snitzer bool bypassed:1; /* Temporarily bypass this PG? */ 631da177e4SLinus Torvalds }; 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds /* Multipath context */ 661da177e4SLinus Torvalds struct multipath { 671da177e4SLinus Torvalds struct list_head list; 681da177e4SLinus Torvalds struct dm_target *ti; 691da177e4SLinus Torvalds 70cfae5c9bSChandra Seetharaman const char *hw_handler_name; 712bfd2e13SChandra Seetharaman char *hw_handler_params; 724e2d19e4SChandra Seetharaman 731fbdd2b3SMike Snitzer spinlock_t lock; 741fbdd2b3SMike Snitzer 751da177e4SLinus Torvalds unsigned nr_priority_groups; 761da177e4SLinus Torvalds struct list_head priority_groups; 774e2d19e4SChandra Seetharaman 784e2d19e4SChandra Seetharaman wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */ 794e2d19e4SChandra Seetharaman 801da177e4SLinus Torvalds struct pgpath *current_pgpath; 811da177e4SLinus Torvalds struct priority_group *current_pg; 821da177e4SLinus Torvalds struct priority_group *next_pg; /* Switch to this PG if set */ 831da177e4SLinus Torvalds 84518257b1SMike Snitzer unsigned long flags; /* Multipath state flags */ 851fbdd2b3SMike Snitzer 86c9e45581SDave Wysochanski unsigned pg_init_retries; /* Number of times to retry pg_init */ 874e2d19e4SChandra Seetharaman unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */ 881da177e4SLinus Torvalds 8991e968aaSMike Snitzer atomic_t nr_valid_paths; /* Total number of usable paths */ 9091e968aaSMike Snitzer atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */ 9191e968aaSMike Snitzer atomic_t pg_init_count; /* Number of times pg_init called */ 9291e968aaSMike Snitzer 93e83068a5SMike Snitzer unsigned queue_mode; 94e83068a5SMike Snitzer 951da177e4SLinus Torvalds /* 96028867acSAlasdair G Kergon * We must use a mempool of dm_mpath_io structs so that we 971da177e4SLinus Torvalds * can resubmit bios on error. 981da177e4SLinus Torvalds */ 991da177e4SLinus Torvalds mempool_t *mpio_pool; 1006380f26fSMike Anderson 1016380f26fSMike Anderson struct mutex work_mutex; 10220800cb3SMike Snitzer struct work_struct trigger_event; 10376e33fe4SMike Snitzer 10476e33fe4SMike Snitzer struct work_struct process_queued_bios; 10576e33fe4SMike Snitzer struct bio_list queued_bios; 1061da177e4SLinus Torvalds }; 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds /* 10976e33fe4SMike Snitzer * Context information attached to each io we process. 1101da177e4SLinus Torvalds */ 111028867acSAlasdair G Kergon struct dm_mpath_io { 1121da177e4SLinus Torvalds struct pgpath *pgpath; 11302ab823fSKiyoshi Ueda size_t nr_bytes; 1141da177e4SLinus Torvalds }; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds typedef int (*action_fn) (struct pgpath *pgpath); 1171da177e4SLinus Torvalds 118e18b890bSChristoph Lameter static struct kmem_cache *_mpio_cache; 1191da177e4SLinus Torvalds 120bab7cfc7SChandra Seetharaman static struct workqueue_struct *kmultipathd, *kmpath_handlerd; 121c4028958SDavid Howells static void trigger_event(struct work_struct *work); 122bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work); 12376e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work); 1241da177e4SLinus Torvalds 125518257b1SMike Snitzer /*----------------------------------------------- 126518257b1SMike Snitzer * Multipath state flags. 127518257b1SMike Snitzer *-----------------------------------------------*/ 128518257b1SMike Snitzer 129518257b1SMike Snitzer #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */ 130518257b1SMike Snitzer #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */ 131518257b1SMike Snitzer #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */ 132518257b1SMike Snitzer #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */ 133518257b1SMike Snitzer #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */ 134518257b1SMike Snitzer #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */ 135518257b1SMike Snitzer #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */ 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds /*----------------------------------------------- 1381da177e4SLinus Torvalds * Allocation routines 1391da177e4SLinus Torvalds *-----------------------------------------------*/ 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds static struct pgpath *alloc_pgpath(void) 1421da177e4SLinus Torvalds { 143e69fae56SMicha³ Miros³aw struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL); 1441da177e4SLinus Torvalds 145224cb3e9SMike Anderson if (pgpath) { 146be7d31ccSMike Snitzer pgpath->is_active = true; 1474e2d19e4SChandra Seetharaman INIT_DELAYED_WORK(&pgpath->activate_path, activate_path); 148224cb3e9SMike Anderson } 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds return pgpath; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds 153028867acSAlasdair G Kergon static void free_pgpath(struct pgpath *pgpath) 1541da177e4SLinus Torvalds { 1551da177e4SLinus Torvalds kfree(pgpath); 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds static struct priority_group *alloc_priority_group(void) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds struct priority_group *pg; 1611da177e4SLinus Torvalds 162e69fae56SMicha³ Miros³aw pg = kzalloc(sizeof(*pg), GFP_KERNEL); 1631da177e4SLinus Torvalds 164e69fae56SMicha³ Miros³aw if (pg) 1651da177e4SLinus Torvalds INIT_LIST_HEAD(&pg->pgpaths); 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds return pg; 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti) 1711da177e4SLinus Torvalds { 1721da177e4SLinus Torvalds struct pgpath *pgpath, *tmp; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds list_for_each_entry_safe(pgpath, tmp, pgpaths, list) { 1751da177e4SLinus Torvalds list_del(&pgpath->list); 1761da177e4SLinus Torvalds dm_put_device(ti, pgpath->path.dev); 1771da177e4SLinus Torvalds free_pgpath(pgpath); 1781da177e4SLinus Torvalds } 1791da177e4SLinus Torvalds } 1801da177e4SLinus Torvalds 1811da177e4SLinus Torvalds static void free_priority_group(struct priority_group *pg, 1821da177e4SLinus Torvalds struct dm_target *ti) 1831da177e4SLinus Torvalds { 1841da177e4SLinus Torvalds struct path_selector *ps = &pg->ps; 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds if (ps->type) { 1871da177e4SLinus Torvalds ps->type->destroy(ps); 1881da177e4SLinus Torvalds dm_put_path_selector(ps->type); 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds free_pgpaths(&pg->pgpaths, ti); 1921da177e4SLinus Torvalds kfree(pg); 1931da177e4SLinus Torvalds } 1941da177e4SLinus Torvalds 195e83068a5SMike Snitzer static struct multipath *alloc_multipath(struct dm_target *ti) 1961da177e4SLinus Torvalds { 1971da177e4SLinus Torvalds struct multipath *m; 1981da177e4SLinus Torvalds 199e69fae56SMicha³ Miros³aw m = kzalloc(sizeof(*m), GFP_KERNEL); 2001da177e4SLinus Torvalds if (m) { 2011da177e4SLinus Torvalds INIT_LIST_HEAD(&m->priority_groups); 2021da177e4SLinus Torvalds spin_lock_init(&m->lock); 203518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 20491e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, 0); 20591e968aaSMike Snitzer atomic_set(&m->pg_init_in_progress, 0); 20691e968aaSMike Snitzer atomic_set(&m->pg_init_count, 0); 2074e2d19e4SChandra Seetharaman m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT; 208c4028958SDavid Howells INIT_WORK(&m->trigger_event, trigger_event); 2092bded7bdSKiyoshi Ueda init_waitqueue_head(&m->pg_init_wait); 2106380f26fSMike Anderson mutex_init(&m->work_mutex); 2118637a6bfSMike Snitzer 2128637a6bfSMike Snitzer m->mpio_pool = NULL; 213e83068a5SMike Snitzer m->queue_mode = DM_TYPE_NONE; 214e83068a5SMike Snitzer 215e83068a5SMike Snitzer m->ti = ti; 216e83068a5SMike Snitzer ti->private = m; 217e83068a5SMike Snitzer } 218e83068a5SMike Snitzer 219e83068a5SMike Snitzer return m; 220e83068a5SMike Snitzer } 221e83068a5SMike Snitzer 222e83068a5SMike Snitzer static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m) 223e83068a5SMike Snitzer { 224e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_NONE) { 225e83068a5SMike Snitzer /* 226e83068a5SMike Snitzer * Default to request-based. 227e83068a5SMike Snitzer */ 228e83068a5SMike Snitzer if (dm_use_blk_mq(dm_table_get_md(ti->table))) 229e83068a5SMike Snitzer m->queue_mode = DM_TYPE_MQ_REQUEST_BASED; 230e83068a5SMike Snitzer else 231e83068a5SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 232e83068a5SMike Snitzer } 233e83068a5SMike Snitzer 234e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_REQUEST_BASED) { 2358637a6bfSMike Snitzer unsigned min_ios = dm_get_reserved_rq_based_ios(); 2368637a6bfSMike Snitzer 237f4790826SMike Snitzer m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache); 238e83068a5SMike Snitzer if (!m->mpio_pool) 239e83068a5SMike Snitzer return -ENOMEM; 2401da177e4SLinus Torvalds } 241e83068a5SMike Snitzer else if (m->queue_mode == DM_TYPE_BIO_BASED) { 24276e33fe4SMike Snitzer INIT_WORK(&m->process_queued_bios, process_queued_bios); 24376e33fe4SMike Snitzer /* 24476e33fe4SMike Snitzer * bio-based doesn't support any direct scsi_dh management; 24576e33fe4SMike Snitzer * it just discovers if a scsi_dh is attached. 24676e33fe4SMike Snitzer */ 24776e33fe4SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 24876e33fe4SMike Snitzer } 24976e33fe4SMike Snitzer 250e83068a5SMike Snitzer dm_table_set_type(ti->table, m->queue_mode); 2511da177e4SLinus Torvalds 252e83068a5SMike Snitzer return 0; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds static void free_multipath(struct multipath *m) 2561da177e4SLinus Torvalds { 2571da177e4SLinus Torvalds struct priority_group *pg, *tmp; 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) { 2601da177e4SLinus Torvalds list_del(&pg->list); 2611da177e4SLinus Torvalds free_priority_group(pg, m->ti); 2621da177e4SLinus Torvalds } 2631da177e4SLinus Torvalds 264cfae5c9bSChandra Seetharaman kfree(m->hw_handler_name); 2652bfd2e13SChandra Seetharaman kfree(m->hw_handler_params); 2661da177e4SLinus Torvalds mempool_destroy(m->mpio_pool); 2671da177e4SLinus Torvalds kfree(m); 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds 2702eff1924SMike Snitzer static struct dm_mpath_io *get_mpio(union map_info *info) 2712eff1924SMike Snitzer { 2722eff1924SMike Snitzer return info->ptr; 2732eff1924SMike Snitzer } 2742eff1924SMike Snitzer 2752eff1924SMike Snitzer static struct dm_mpath_io *set_mpio(struct multipath *m, union map_info *info) 276466891f9SJun'ichi Nomura { 277466891f9SJun'ichi Nomura struct dm_mpath_io *mpio; 278466891f9SJun'ichi Nomura 2798637a6bfSMike Snitzer if (!m->mpio_pool) { 2808637a6bfSMike Snitzer /* Use blk-mq pdu memory requested via per_io_data_size */ 2812eff1924SMike Snitzer mpio = get_mpio(info); 2828637a6bfSMike Snitzer memset(mpio, 0, sizeof(*mpio)); 2838637a6bfSMike Snitzer return mpio; 2848637a6bfSMike Snitzer } 2858637a6bfSMike Snitzer 286466891f9SJun'ichi Nomura mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC); 287466891f9SJun'ichi Nomura if (!mpio) 2882eff1924SMike Snitzer return NULL; 289466891f9SJun'ichi Nomura 290466891f9SJun'ichi Nomura memset(mpio, 0, sizeof(*mpio)); 291466891f9SJun'ichi Nomura info->ptr = mpio; 292466891f9SJun'ichi Nomura 2932eff1924SMike Snitzer return mpio; 294466891f9SJun'ichi Nomura } 295466891f9SJun'ichi Nomura 2962eff1924SMike Snitzer static void clear_request_fn_mpio(struct multipath *m, union map_info *info) 297466891f9SJun'ichi Nomura { 2982eff1924SMike Snitzer /* Only needed for non blk-mq (.request_fn) multipath */ 2998637a6bfSMike Snitzer if (m->mpio_pool) { 300466891f9SJun'ichi Nomura struct dm_mpath_io *mpio = info->ptr; 301466891f9SJun'ichi Nomura 302466891f9SJun'ichi Nomura info->ptr = NULL; 303466891f9SJun'ichi Nomura mempool_free(mpio, m->mpio_pool); 304466891f9SJun'ichi Nomura } 3058637a6bfSMike Snitzer } 3061da177e4SLinus Torvalds 307bf661be1SMike Snitzer static size_t multipath_per_bio_data_size(void) 30876e33fe4SMike Snitzer { 309bf661be1SMike Snitzer return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details); 31076e33fe4SMike Snitzer } 31176e33fe4SMike Snitzer 312bf661be1SMike Snitzer static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio) 313bf661be1SMike Snitzer { 314bf661be1SMike Snitzer return dm_per_bio_data(bio, multipath_per_bio_data_size()); 315bf661be1SMike Snitzer } 316bf661be1SMike Snitzer 317bf661be1SMike Snitzer static struct dm_bio_details *get_bio_details_from_bio(struct bio *bio) 318bf661be1SMike Snitzer { 319bf661be1SMike Snitzer /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */ 320bf661be1SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 321bf661be1SMike Snitzer void *bio_details = mpio + 1; 322bf661be1SMike Snitzer 323bf661be1SMike Snitzer return bio_details; 324bf661be1SMike Snitzer } 325bf661be1SMike Snitzer 326bf661be1SMike Snitzer static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p, 327bf661be1SMike Snitzer struct dm_bio_details **bio_details_p) 32876e33fe4SMike Snitzer { 32976e33fe4SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(bio); 330bf661be1SMike Snitzer struct dm_bio_details *bio_details = get_bio_details_from_bio(bio); 33176e33fe4SMike Snitzer 33276e33fe4SMike Snitzer memset(mpio, 0, sizeof(*mpio)); 333bf661be1SMike Snitzer memset(bio_details, 0, sizeof(*bio_details)); 334bf661be1SMike Snitzer dm_bio_record(bio_details, bio); 33576e33fe4SMike Snitzer 336bf661be1SMike Snitzer if (mpio_p) 337bf661be1SMike Snitzer *mpio_p = mpio; 338bf661be1SMike Snitzer if (bio_details_p) 339bf661be1SMike Snitzer *bio_details_p = bio_details; 34076e33fe4SMike Snitzer } 34176e33fe4SMike Snitzer 3421da177e4SLinus Torvalds /*----------------------------------------------- 3431da177e4SLinus Torvalds * Path selection 3441da177e4SLinus Torvalds *-----------------------------------------------*/ 3451da177e4SLinus Torvalds 3463e9f1be1SHannes Reinecke static int __pg_init_all_paths(struct multipath *m) 347fb612642SKiyoshi Ueda { 348fb612642SKiyoshi Ueda struct pgpath *pgpath; 3494e2d19e4SChandra Seetharaman unsigned long pg_init_delay = 0; 350fb612642SKiyoshi Ueda 35191e968aaSMike Snitzer if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 3523e9f1be1SHannes Reinecke return 0; 35317f4ff45SHannes Reinecke 35491e968aaSMike Snitzer atomic_inc(&m->pg_init_count); 355518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 3563e9f1be1SHannes Reinecke 3573e9f1be1SHannes Reinecke /* Check here to reset pg_init_required */ 3583e9f1be1SHannes Reinecke if (!m->current_pg) 3593e9f1be1SHannes Reinecke return 0; 3603e9f1be1SHannes Reinecke 361518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags)) 3624e2d19e4SChandra Seetharaman pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ? 3634e2d19e4SChandra Seetharaman m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS); 364fb612642SKiyoshi Ueda list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) { 365fb612642SKiyoshi Ueda /* Skip failed paths */ 366fb612642SKiyoshi Ueda if (!pgpath->is_active) 367fb612642SKiyoshi Ueda continue; 3684e2d19e4SChandra Seetharaman if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path, 3694e2d19e4SChandra Seetharaman pg_init_delay)) 37091e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 371fb612642SKiyoshi Ueda } 37291e968aaSMike Snitzer return atomic_read(&m->pg_init_in_progress); 373fb612642SKiyoshi Ueda } 374fb612642SKiyoshi Ueda 3752da1610aSMike Snitzer static int pg_init_all_paths(struct multipath *m) 3761da177e4SLinus Torvalds { 3772da1610aSMike Snitzer int r; 3782da1610aSMike Snitzer unsigned long flags; 3792da1610aSMike Snitzer 3802da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 3812da1610aSMike Snitzer r = __pg_init_all_paths(m); 3822da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 3832da1610aSMike Snitzer 3842da1610aSMike Snitzer return r; 3852da1610aSMike Snitzer } 3862da1610aSMike Snitzer 3872da1610aSMike Snitzer static void __switch_pg(struct multipath *m, struct priority_group *pg) 3882da1610aSMike Snitzer { 3892da1610aSMike Snitzer m->current_pg = pg; 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds /* Must we initialise the PG first, and queue I/O till it's ready? */ 392cfae5c9bSChandra Seetharaman if (m->hw_handler_name) { 393518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 394518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IO, &m->flags); 3951da177e4SLinus Torvalds } else { 396518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 397518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 3981da177e4SLinus Torvalds } 399c9e45581SDave Wysochanski 40091e968aaSMike Snitzer atomic_set(&m->pg_init_count, 0); 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4032da1610aSMike Snitzer static struct pgpath *choose_path_in_pg(struct multipath *m, 4042da1610aSMike Snitzer struct priority_group *pg, 40502ab823fSKiyoshi Ueda size_t nr_bytes) 4061da177e4SLinus Torvalds { 4072da1610aSMike Snitzer unsigned long flags; 408c922d5f7SJosef "Jeff" Sipek struct dm_path *path; 4092da1610aSMike Snitzer struct pgpath *pgpath; 4101da177e4SLinus Torvalds 41190a4323cSMike Snitzer path = pg->ps.type->select_path(&pg->ps, nr_bytes); 4121da177e4SLinus Torvalds if (!path) 4132da1610aSMike Snitzer return ERR_PTR(-ENXIO); 4141da177e4SLinus Torvalds 4152da1610aSMike Snitzer pgpath = path_to_pgpath(path); 4161da177e4SLinus Torvalds 4172da1610aSMike Snitzer if (unlikely(lockless_dereference(m->current_pg) != pg)) { 4182da1610aSMike Snitzer /* Only update current_pgpath if pg changed */ 4192da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 4202da1610aSMike Snitzer m->current_pgpath = pgpath; 4212da1610aSMike Snitzer __switch_pg(m, pg); 4222da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4231da177e4SLinus Torvalds } 4241da177e4SLinus Torvalds 4252da1610aSMike Snitzer return pgpath; 4262da1610aSMike Snitzer } 4272da1610aSMike Snitzer 4282da1610aSMike Snitzer static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes) 4291da177e4SLinus Torvalds { 4302da1610aSMike Snitzer unsigned long flags; 4311da177e4SLinus Torvalds struct priority_group *pg; 4322da1610aSMike Snitzer struct pgpath *pgpath; 433be7d31ccSMike Snitzer bool bypassed = true; 4341da177e4SLinus Torvalds 43591e968aaSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 436518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 4371da177e4SLinus Torvalds goto failed; 4381f271972SBenjamin Marzinski } 4391da177e4SLinus Torvalds 4401da177e4SLinus Torvalds /* Were we instructed to switch PG? */ 4412da1610aSMike Snitzer if (lockless_dereference(m->next_pg)) { 4422da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 4431da177e4SLinus Torvalds pg = m->next_pg; 4442da1610aSMike Snitzer if (!pg) { 4452da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4462da1610aSMike Snitzer goto check_current_pg; 4472da1610aSMike Snitzer } 4481da177e4SLinus Torvalds m->next_pg = NULL; 4492da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4502da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4512da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 4522da1610aSMike Snitzer return pgpath; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds /* Don't change PG until it has no remaining paths */ 4562da1610aSMike Snitzer check_current_pg: 4572da1610aSMike Snitzer pg = lockless_dereference(m->current_pg); 4582da1610aSMike Snitzer if (pg) { 4592da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4602da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) 4612da1610aSMike Snitzer return pgpath; 4622da1610aSMike Snitzer } 4631da177e4SLinus Torvalds 4641da177e4SLinus Torvalds /* 4651da177e4SLinus Torvalds * Loop through priority groups until we find a valid path. 4661da177e4SLinus Torvalds * First time we skip PGs marked 'bypassed'. 467f220fd4eSMike Christie * Second time we only try the ones we skipped, but set 468f220fd4eSMike Christie * pg_init_delay_retry so we do not hammer controllers. 4691da177e4SLinus Torvalds */ 4701da177e4SLinus Torvalds do { 4711da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 4721da177e4SLinus Torvalds if (pg->bypassed == bypassed) 4731da177e4SLinus Torvalds continue; 4742da1610aSMike Snitzer pgpath = choose_path_in_pg(m, pg, nr_bytes); 4752da1610aSMike Snitzer if (!IS_ERR_OR_NULL(pgpath)) { 476f220fd4eSMike Christie if (!bypassed) 477518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 4782da1610aSMike Snitzer return pgpath; 4791da177e4SLinus Torvalds } 480f220fd4eSMike Christie } 4811da177e4SLinus Torvalds } while (bypassed--); 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds failed: 4842da1610aSMike Snitzer spin_lock_irqsave(&m->lock, flags); 4851da177e4SLinus Torvalds m->current_pgpath = NULL; 4861da177e4SLinus Torvalds m->current_pg = NULL; 4872da1610aSMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 4882da1610aSMike Snitzer 4892da1610aSMike Snitzer return NULL; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds 49245e15720SKiyoshi Ueda /* 49345e15720SKiyoshi Ueda * Check whether bios must be queued in the device-mapper core rather 49445e15720SKiyoshi Ueda * than here in the target. 49545e15720SKiyoshi Ueda * 49645e15720SKiyoshi Ueda * If m->queue_if_no_path and m->saved_queue_if_no_path hold the 49745e15720SKiyoshi Ueda * same value then we are not between multipath_presuspend() 49845e15720SKiyoshi Ueda * and multipath_resume() calls and we have no need to check 49945e15720SKiyoshi Ueda * for the DMF_NOFLUSH_SUSPENDING flag. 50045e15720SKiyoshi Ueda */ 50176e33fe4SMike Snitzer static bool __must_push_back(struct multipath *m) 50276e33fe4SMike Snitzer { 50376e33fe4SMike Snitzer return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) != 50476e33fe4SMike Snitzer test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) && 50576e33fe4SMike Snitzer dm_noflush_suspending(m->ti)); 50676e33fe4SMike Snitzer } 50776e33fe4SMike Snitzer 50876e33fe4SMike Snitzer static bool must_push_back_rq(struct multipath *m) 50945e15720SKiyoshi Ueda { 5101814f2e3SMike Snitzer bool r; 5111814f2e3SMike Snitzer unsigned long flags; 5121814f2e3SMike Snitzer 5131814f2e3SMike Snitzer spin_lock_irqsave(&m->lock, flags); 5141814f2e3SMike Snitzer r = (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) || 51576e33fe4SMike Snitzer __must_push_back(m)); 5161814f2e3SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 5171814f2e3SMike Snitzer 5181814f2e3SMike Snitzer return r; 51976e33fe4SMike Snitzer } 52076e33fe4SMike Snitzer 52176e33fe4SMike Snitzer static bool must_push_back_bio(struct multipath *m) 52276e33fe4SMike Snitzer { 5231814f2e3SMike Snitzer bool r; 5241814f2e3SMike Snitzer unsigned long flags; 5251814f2e3SMike Snitzer 5261814f2e3SMike Snitzer spin_lock_irqsave(&m->lock, flags); 5271814f2e3SMike Snitzer r = __must_push_back(m); 5281814f2e3SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 5291814f2e3SMike Snitzer 5301814f2e3SMike Snitzer return r; 53145e15720SKiyoshi Ueda } 53245e15720SKiyoshi Ueda 53336fcffccSHannes Reinecke /* 53476e33fe4SMike Snitzer * Map cloned requests (request-based multipath) 53536fcffccSHannes Reinecke */ 536e5863d9aSMike Snitzer static int __multipath_map(struct dm_target *ti, struct request *clone, 537e5863d9aSMike Snitzer union map_info *map_context, 538e5863d9aSMike Snitzer struct request *rq, struct request **__clone) 5391da177e4SLinus Torvalds { 5407943bd6dSMike Snitzer struct multipath *m = ti->private; 541e3bde04fSHannes Reinecke int r = DM_MAPIO_REQUEUE; 542e5863d9aSMike Snitzer size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq); 5431da177e4SLinus Torvalds struct pgpath *pgpath; 544f40c67f0SKiyoshi Ueda struct block_device *bdev; 545e3bde04fSHannes Reinecke struct dm_mpath_io *mpio; 5461da177e4SLinus Torvalds 5471da177e4SLinus Torvalds /* Do we need to select a new pgpath? */ 5482da1610aSMike Snitzer pgpath = lockless_dereference(m->current_pgpath); 5492da1610aSMike Snitzer if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags)) 5502da1610aSMike Snitzer pgpath = choose_pgpath(m, nr_bytes); 5511da177e4SLinus Torvalds 5529bf59a61SMike Snitzer if (!pgpath) { 55376e33fe4SMike Snitzer if (!must_push_back_rq(m)) 5549bf59a61SMike Snitzer r = -EIO; /* Failed */ 5552da1610aSMike Snitzer return r; 556518257b1SMike Snitzer } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) || 557518257b1SMike Snitzer test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 5582da1610aSMike Snitzer pg_init_all_paths(m); 5592da1610aSMike Snitzer return r; 5609bf59a61SMike Snitzer } 5616afbc01dSMike Snitzer 5622eff1924SMike Snitzer mpio = set_mpio(m, map_context); 5632eff1924SMike Snitzer if (!mpio) 564e3bde04fSHannes Reinecke /* ENOMEM, requeue */ 5652da1610aSMike Snitzer return r; 566e3bde04fSHannes Reinecke 567e8099177SHannes Reinecke mpio->pgpath = pgpath; 568e8099177SHannes Reinecke mpio->nr_bytes = nr_bytes; 5692eb6e1e3SKeith Busch 5702eb6e1e3SKeith Busch bdev = pgpath->path.dev->bdev; 5712eb6e1e3SKeith Busch 572e5863d9aSMike Snitzer if (clone) { 573c5248f79SMike Snitzer /* 574c5248f79SMike Snitzer * Old request-based interface: allocated clone is passed in. 575c5248f79SMike Snitzer * Used by: .request_fn stacked on .request_fn path(s). 576c5248f79SMike Snitzer */ 5772eb6e1e3SKeith Busch clone->q = bdev_get_queue(bdev); 5782eb6e1e3SKeith Busch clone->rq_disk = bdev->bd_disk; 5792eb6e1e3SKeith Busch clone->cmd_flags |= REQ_FAILFAST_TRANSPORT; 580e5863d9aSMike Snitzer } else { 581eca7ee6dSMike Snitzer /* 582eca7ee6dSMike Snitzer * blk-mq request-based interface; used by both: 583eca7ee6dSMike Snitzer * .request_fn stacked on blk-mq path(s) and 584eca7ee6dSMike Snitzer * blk-mq stacked on blk-mq path(s). 585eca7ee6dSMike Snitzer */ 58678ce23b5SMike Snitzer *__clone = blk_mq_alloc_request(bdev_get_queue(bdev), 58778ce23b5SMike Snitzer rq_data_dir(rq), BLK_MQ_REQ_NOWAIT); 5884c6dd53dSMike Snitzer if (IS_ERR(*__clone)) { 589e5863d9aSMike Snitzer /* ENOMEM, requeue */ 5902eff1924SMike Snitzer clear_request_fn_mpio(m, map_context); 591e5863d9aSMike Snitzer return r; 5924c6dd53dSMike Snitzer } 593e5863d9aSMike Snitzer (*__clone)->bio = (*__clone)->biotail = NULL; 594e5863d9aSMike Snitzer (*__clone)->rq_disk = bdev->bd_disk; 595e5863d9aSMike Snitzer (*__clone)->cmd_flags |= REQ_FAILFAST_TRANSPORT; 596e5863d9aSMike Snitzer } 5972eb6e1e3SKeith Busch 598e8099177SHannes Reinecke if (pgpath->pg->ps.type->start_io) 599e8099177SHannes Reinecke pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 600e8099177SHannes Reinecke &pgpath->path, 601e8099177SHannes Reinecke nr_bytes); 6022eb6e1e3SKeith Busch return DM_MAPIO_REMAPPED; 6031da177e4SLinus Torvalds } 6041da177e4SLinus Torvalds 605e5863d9aSMike Snitzer static int multipath_map(struct dm_target *ti, struct request *clone, 606e5863d9aSMike Snitzer union map_info *map_context) 607e5863d9aSMike Snitzer { 608e5863d9aSMike Snitzer return __multipath_map(ti, clone, map_context, NULL, NULL); 609e5863d9aSMike Snitzer } 610e5863d9aSMike Snitzer 611e5863d9aSMike Snitzer static int multipath_clone_and_map(struct dm_target *ti, struct request *rq, 612e5863d9aSMike Snitzer union map_info *map_context, 613e5863d9aSMike Snitzer struct request **clone) 614e5863d9aSMike Snitzer { 615e5863d9aSMike Snitzer return __multipath_map(ti, NULL, map_context, rq, clone); 616e5863d9aSMike Snitzer } 617e5863d9aSMike Snitzer 618e5863d9aSMike Snitzer static void multipath_release_clone(struct request *clone) 619e5863d9aSMike Snitzer { 62078ce23b5SMike Snitzer blk_mq_free_request(clone); 621e5863d9aSMike Snitzer } 622e5863d9aSMike Snitzer 6231da177e4SLinus Torvalds /* 62476e33fe4SMike Snitzer * Map cloned bios (bio-based multipath) 62576e33fe4SMike Snitzer */ 62676e33fe4SMike Snitzer static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio) 62776e33fe4SMike Snitzer { 62876e33fe4SMike Snitzer size_t nr_bytes = bio->bi_iter.bi_size; 62976e33fe4SMike Snitzer struct pgpath *pgpath; 63076e33fe4SMike Snitzer unsigned long flags; 63176e33fe4SMike Snitzer bool queue_io; 63276e33fe4SMike Snitzer 63376e33fe4SMike Snitzer /* Do we need to select a new pgpath? */ 63476e33fe4SMike Snitzer pgpath = lockless_dereference(m->current_pgpath); 63576e33fe4SMike Snitzer queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags); 63676e33fe4SMike Snitzer if (!pgpath || !queue_io) 63776e33fe4SMike Snitzer pgpath = choose_pgpath(m, nr_bytes); 63876e33fe4SMike Snitzer 63976e33fe4SMike Snitzer if ((pgpath && queue_io) || 64076e33fe4SMike Snitzer (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) { 64176e33fe4SMike Snitzer /* Queue for the daemon to resubmit */ 64276e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 64376e33fe4SMike Snitzer bio_list_add(&m->queued_bios, bio); 64476e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 64576e33fe4SMike Snitzer /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */ 64676e33fe4SMike Snitzer if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 64776e33fe4SMike Snitzer pg_init_all_paths(m); 64876e33fe4SMike Snitzer else if (!queue_io) 64976e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 65076e33fe4SMike Snitzer return DM_MAPIO_SUBMITTED; 65176e33fe4SMike Snitzer } 65276e33fe4SMike Snitzer 65376e33fe4SMike Snitzer if (!pgpath) { 65476e33fe4SMike Snitzer if (!must_push_back_bio(m)) 65576e33fe4SMike Snitzer return -EIO; 65676e33fe4SMike Snitzer return DM_MAPIO_REQUEUE; 65776e33fe4SMike Snitzer } 65876e33fe4SMike Snitzer 65976e33fe4SMike Snitzer mpio->pgpath = pgpath; 66076e33fe4SMike Snitzer mpio->nr_bytes = nr_bytes; 66176e33fe4SMike Snitzer 66276e33fe4SMike Snitzer bio->bi_error = 0; 66376e33fe4SMike Snitzer bio->bi_bdev = pgpath->path.dev->bdev; 6641eff9d32SJens Axboe bio->bi_opf |= REQ_FAILFAST_TRANSPORT; 66576e33fe4SMike Snitzer 66676e33fe4SMike Snitzer if (pgpath->pg->ps.type->start_io) 66776e33fe4SMike Snitzer pgpath->pg->ps.type->start_io(&pgpath->pg->ps, 66876e33fe4SMike Snitzer &pgpath->path, 66976e33fe4SMike Snitzer nr_bytes); 67076e33fe4SMike Snitzer return DM_MAPIO_REMAPPED; 67176e33fe4SMike Snitzer } 67276e33fe4SMike Snitzer 67376e33fe4SMike Snitzer static int multipath_map_bio(struct dm_target *ti, struct bio *bio) 67476e33fe4SMike Snitzer { 67576e33fe4SMike Snitzer struct multipath *m = ti->private; 676bf661be1SMike Snitzer struct dm_mpath_io *mpio = NULL; 677bf661be1SMike Snitzer 678bf661be1SMike Snitzer multipath_init_per_bio_data(bio, &mpio, NULL); 67976e33fe4SMike Snitzer 68076e33fe4SMike Snitzer return __multipath_map_bio(m, bio, mpio); 68176e33fe4SMike Snitzer } 68276e33fe4SMike Snitzer 683*7e48c768SMike Snitzer static void process_queued_io_list(struct multipath *m) 68476e33fe4SMike Snitzer { 685*7e48c768SMike Snitzer if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED) 686*7e48c768SMike Snitzer dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table)); 687*7e48c768SMike Snitzer else if (m->queue_mode == DM_TYPE_BIO_BASED) 68876e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 68976e33fe4SMike Snitzer } 69076e33fe4SMike Snitzer 69176e33fe4SMike Snitzer static void process_queued_bios(struct work_struct *work) 69276e33fe4SMike Snitzer { 69376e33fe4SMike Snitzer int r; 69476e33fe4SMike Snitzer unsigned long flags; 69576e33fe4SMike Snitzer struct bio *bio; 69676e33fe4SMike Snitzer struct bio_list bios; 69776e33fe4SMike Snitzer struct blk_plug plug; 69876e33fe4SMike Snitzer struct multipath *m = 69976e33fe4SMike Snitzer container_of(work, struct multipath, process_queued_bios); 70076e33fe4SMike Snitzer 70176e33fe4SMike Snitzer bio_list_init(&bios); 70276e33fe4SMike Snitzer 70376e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 70476e33fe4SMike Snitzer 70576e33fe4SMike Snitzer if (bio_list_empty(&m->queued_bios)) { 70676e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 70776e33fe4SMike Snitzer return; 70876e33fe4SMike Snitzer } 70976e33fe4SMike Snitzer 71076e33fe4SMike Snitzer bio_list_merge(&bios, &m->queued_bios); 71176e33fe4SMike Snitzer bio_list_init(&m->queued_bios); 71276e33fe4SMike Snitzer 71376e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 71476e33fe4SMike Snitzer 71576e33fe4SMike Snitzer blk_start_plug(&plug); 71676e33fe4SMike Snitzer while ((bio = bio_list_pop(&bios))) { 71776e33fe4SMike Snitzer r = __multipath_map_bio(m, bio, get_mpio_from_bio(bio)); 71876e33fe4SMike Snitzer if (r < 0 || r == DM_MAPIO_REQUEUE) { 71976e33fe4SMike Snitzer bio->bi_error = r; 72076e33fe4SMike Snitzer bio_endio(bio); 72176e33fe4SMike Snitzer } else if (r == DM_MAPIO_REMAPPED) 72276e33fe4SMike Snitzer generic_make_request(bio); 72376e33fe4SMike Snitzer } 72476e33fe4SMike Snitzer blk_finish_plug(&plug); 72576e33fe4SMike Snitzer } 72676e33fe4SMike Snitzer 72776e33fe4SMike Snitzer /* 7281da177e4SLinus Torvalds * If we run out of usable paths, should we queue I/O or error it? 7291da177e4SLinus Torvalds */ 730be7d31ccSMike Snitzer static int queue_if_no_path(struct multipath *m, bool queue_if_no_path, 731be7d31ccSMike Snitzer bool save_old_value) 7321da177e4SLinus Torvalds { 7331da177e4SLinus Torvalds unsigned long flags; 7341da177e4SLinus Torvalds 7351da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 7361da177e4SLinus Torvalds 737518257b1SMike Snitzer if (save_old_value) { 738518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 739518257b1SMike Snitzer set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 740485ef69eSAlasdair G Kergon else 741518257b1SMike Snitzer clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 742518257b1SMike Snitzer } else { 743518257b1SMike Snitzer if (queue_if_no_path) 744518257b1SMike Snitzer set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 745518257b1SMike Snitzer else 746518257b1SMike Snitzer clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags); 747518257b1SMike Snitzer } 748518257b1SMike Snitzer if (queue_if_no_path) 749518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 750518257b1SMike Snitzer else 751518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 752518257b1SMike Snitzer 7531da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 7541da177e4SLinus Torvalds 75576e33fe4SMike Snitzer if (!queue_if_no_path) { 75663d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 757*7e48c768SMike Snitzer process_queued_io_list(m); 75876e33fe4SMike Snitzer } 75963d832c3SHannes Reinecke 7601da177e4SLinus Torvalds return 0; 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds /* 7641da177e4SLinus Torvalds * An event is triggered whenever a path is taken out of use. 7651da177e4SLinus Torvalds * Includes path failure and PG bypass. 7661da177e4SLinus Torvalds */ 767c4028958SDavid Howells static void trigger_event(struct work_struct *work) 7681da177e4SLinus Torvalds { 769c4028958SDavid Howells struct multipath *m = 770c4028958SDavid Howells container_of(work, struct multipath, trigger_event); 7711da177e4SLinus Torvalds 7721da177e4SLinus Torvalds dm_table_event(m->ti->table); 7731da177e4SLinus Torvalds } 7741da177e4SLinus Torvalds 7751da177e4SLinus Torvalds /*----------------------------------------------------------------- 7761da177e4SLinus Torvalds * Constructor/argument parsing: 7771da177e4SLinus Torvalds * <#multipath feature args> [<arg>]* 7781da177e4SLinus Torvalds * <#hw_handler args> [hw_handler [<arg>]*] 7791da177e4SLinus Torvalds * <#priority groups> 7801da177e4SLinus Torvalds * <initial priority group> 7811da177e4SLinus Torvalds * [<selector> <#selector args> [<arg>]* 7821da177e4SLinus Torvalds * <#paths> <#per-path selector args> 7831da177e4SLinus Torvalds * [<path> [<arg>]* ]+ ]+ 7841da177e4SLinus Torvalds *---------------------------------------------------------------*/ 785498f0103SMike Snitzer static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg, 7861da177e4SLinus Torvalds struct dm_target *ti) 7871da177e4SLinus Torvalds { 7881da177e4SLinus Torvalds int r; 7891da177e4SLinus Torvalds struct path_selector_type *pst; 7901da177e4SLinus Torvalds unsigned ps_argc; 7911da177e4SLinus Torvalds 792498f0103SMike Snitzer static struct dm_arg _args[] = { 79372d94861SAlasdair G Kergon {0, 1024, "invalid number of path selector args"}, 7941da177e4SLinus Torvalds }; 7951da177e4SLinus Torvalds 796498f0103SMike Snitzer pst = dm_get_path_selector(dm_shift_arg(as)); 7971da177e4SLinus Torvalds if (!pst) { 79872d94861SAlasdair G Kergon ti->error = "unknown path selector type"; 7991da177e4SLinus Torvalds return -EINVAL; 8001da177e4SLinus Torvalds } 8011da177e4SLinus Torvalds 802498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &ps_argc, &ti->error); 803371b2e34SMikulas Patocka if (r) { 804371b2e34SMikulas Patocka dm_put_path_selector(pst); 8051da177e4SLinus Torvalds return -EINVAL; 806371b2e34SMikulas Patocka } 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds r = pst->create(&pg->ps, ps_argc, as->argv); 8091da177e4SLinus Torvalds if (r) { 8101da177e4SLinus Torvalds dm_put_path_selector(pst); 81172d94861SAlasdair G Kergon ti->error = "path selector constructor failed"; 8121da177e4SLinus Torvalds return r; 8131da177e4SLinus Torvalds } 8141da177e4SLinus Torvalds 8151da177e4SLinus Torvalds pg->ps.type = pst; 816498f0103SMike Snitzer dm_consume_args(as, ps_argc); 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds return 0; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds 821498f0103SMike Snitzer static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps, 8221da177e4SLinus Torvalds struct dm_target *ti) 8231da177e4SLinus Torvalds { 8241da177e4SLinus Torvalds int r; 8251da177e4SLinus Torvalds struct pgpath *p; 826ae11b1b3SHannes Reinecke struct multipath *m = ti->private; 827a58a935dSMike Snitzer struct request_queue *q = NULL; 828a58a935dSMike Snitzer const char *attached_handler_name; 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds /* we need at least a path arg */ 8311da177e4SLinus Torvalds if (as->argc < 1) { 83272d94861SAlasdair G Kergon ti->error = "no device given"; 83301460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 8341da177e4SLinus Torvalds } 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds p = alloc_pgpath(); 8371da177e4SLinus Torvalds if (!p) 83801460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 8391da177e4SLinus Torvalds 840498f0103SMike Snitzer r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table), 8418215d6ecSNikanth Karthikesan &p->path.dev); 8421da177e4SLinus Torvalds if (r) { 84372d94861SAlasdair G Kergon ti->error = "error getting device"; 8441da177e4SLinus Torvalds goto bad; 8451da177e4SLinus Torvalds } 8461da177e4SLinus Torvalds 847518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name) 848a58a935dSMike Snitzer q = bdev_get_queue(p->path.dev->bdev); 849a0cf7ea9SHannes Reinecke 850518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) { 8511bab0de0SChristoph Hellwig retain: 852a58a935dSMike Snitzer attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL); 853a58a935dSMike Snitzer if (attached_handler_name) { 854a58a935dSMike Snitzer /* 855a58a935dSMike Snitzer * Reset hw_handler_name to match the attached handler 856a58a935dSMike Snitzer * and clear any hw_handler_params associated with the 857a58a935dSMike Snitzer * ignored handler. 858a58a935dSMike Snitzer * 859a58a935dSMike Snitzer * NB. This modifies the table line to show the actual 860a58a935dSMike Snitzer * handler instead of the original table passed in. 861a58a935dSMike Snitzer */ 862a58a935dSMike Snitzer kfree(m->hw_handler_name); 863a58a935dSMike Snitzer m->hw_handler_name = attached_handler_name; 864a58a935dSMike Snitzer 865a58a935dSMike Snitzer kfree(m->hw_handler_params); 866a58a935dSMike Snitzer m->hw_handler_params = NULL; 867a58a935dSMike Snitzer } 868a58a935dSMike Snitzer } 869a58a935dSMike Snitzer 870a58a935dSMike Snitzer if (m->hw_handler_name) { 871a0cf7ea9SHannes Reinecke r = scsi_dh_attach(q, m->hw_handler_name); 872a0cf7ea9SHannes Reinecke if (r == -EBUSY) { 8731bab0de0SChristoph Hellwig char b[BDEVNAME_SIZE]; 874a0cf7ea9SHannes Reinecke 8751bab0de0SChristoph Hellwig printk(KERN_INFO "dm-mpath: retaining handler on device %s\n", 8761bab0de0SChristoph Hellwig bdevname(p->path.dev->bdev, b)); 8771bab0de0SChristoph Hellwig goto retain; 8781bab0de0SChristoph Hellwig } 879ae11b1b3SHannes Reinecke if (r < 0) { 880a0cf7ea9SHannes Reinecke ti->error = "error attaching hardware handler"; 881ae11b1b3SHannes Reinecke dm_put_device(ti, p->path.dev); 882ae11b1b3SHannes Reinecke goto bad; 883ae11b1b3SHannes Reinecke } 8842bfd2e13SChandra Seetharaman 8852bfd2e13SChandra Seetharaman if (m->hw_handler_params) { 8862bfd2e13SChandra Seetharaman r = scsi_dh_set_params(q, m->hw_handler_params); 8872bfd2e13SChandra Seetharaman if (r < 0) { 8882bfd2e13SChandra Seetharaman ti->error = "unable to set hardware " 8892bfd2e13SChandra Seetharaman "handler parameters"; 8902bfd2e13SChandra Seetharaman dm_put_device(ti, p->path.dev); 8912bfd2e13SChandra Seetharaman goto bad; 8922bfd2e13SChandra Seetharaman } 8932bfd2e13SChandra Seetharaman } 894ae11b1b3SHannes Reinecke } 895ae11b1b3SHannes Reinecke 8961da177e4SLinus Torvalds r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error); 8971da177e4SLinus Torvalds if (r) { 8981da177e4SLinus Torvalds dm_put_device(ti, p->path.dev); 8991da177e4SLinus Torvalds goto bad; 9001da177e4SLinus Torvalds } 9011da177e4SLinus Torvalds 9021da177e4SLinus Torvalds return p; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds bad: 9051da177e4SLinus Torvalds free_pgpath(p); 90601460f35SBenjamin Marzinski return ERR_PTR(r); 9071da177e4SLinus Torvalds } 9081da177e4SLinus Torvalds 909498f0103SMike Snitzer static struct priority_group *parse_priority_group(struct dm_arg_set *as, 91028f16c20SMicha³ Miros³aw struct multipath *m) 9111da177e4SLinus Torvalds { 912498f0103SMike Snitzer static struct dm_arg _args[] = { 91372d94861SAlasdair G Kergon {1, 1024, "invalid number of paths"}, 91472d94861SAlasdair G Kergon {0, 1024, "invalid number of selector args"} 9151da177e4SLinus Torvalds }; 9161da177e4SLinus Torvalds 9171da177e4SLinus Torvalds int r; 918498f0103SMike Snitzer unsigned i, nr_selector_args, nr_args; 9191da177e4SLinus Torvalds struct priority_group *pg; 92028f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 9211da177e4SLinus Torvalds 9221da177e4SLinus Torvalds if (as->argc < 2) { 9231da177e4SLinus Torvalds as->argc = 0; 92401460f35SBenjamin Marzinski ti->error = "not enough priority group arguments"; 92501460f35SBenjamin Marzinski return ERR_PTR(-EINVAL); 9261da177e4SLinus Torvalds } 9271da177e4SLinus Torvalds 9281da177e4SLinus Torvalds pg = alloc_priority_group(); 9291da177e4SLinus Torvalds if (!pg) { 93072d94861SAlasdair G Kergon ti->error = "couldn't allocate priority group"; 93101460f35SBenjamin Marzinski return ERR_PTR(-ENOMEM); 9321da177e4SLinus Torvalds } 9331da177e4SLinus Torvalds pg->m = m; 9341da177e4SLinus Torvalds 9351da177e4SLinus Torvalds r = parse_path_selector(as, pg, ti); 9361da177e4SLinus Torvalds if (r) 9371da177e4SLinus Torvalds goto bad; 9381da177e4SLinus Torvalds 9391da177e4SLinus Torvalds /* 9401da177e4SLinus Torvalds * read the paths 9411da177e4SLinus Torvalds */ 942498f0103SMike Snitzer r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error); 9431da177e4SLinus Torvalds if (r) 9441da177e4SLinus Torvalds goto bad; 9451da177e4SLinus Torvalds 946498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error); 9471da177e4SLinus Torvalds if (r) 9481da177e4SLinus Torvalds goto bad; 9491da177e4SLinus Torvalds 950498f0103SMike Snitzer nr_args = 1 + nr_selector_args; 9511da177e4SLinus Torvalds for (i = 0; i < pg->nr_pgpaths; i++) { 9521da177e4SLinus Torvalds struct pgpath *pgpath; 953498f0103SMike Snitzer struct dm_arg_set path_args; 9541da177e4SLinus Torvalds 955498f0103SMike Snitzer if (as->argc < nr_args) { 956148acff6SMikulas Patocka ti->error = "not enough path parameters"; 9576bbf79a1SAlasdair G Kergon r = -EINVAL; 9581da177e4SLinus Torvalds goto bad; 959148acff6SMikulas Patocka } 9601da177e4SLinus Torvalds 961498f0103SMike Snitzer path_args.argc = nr_args; 9621da177e4SLinus Torvalds path_args.argv = as->argv; 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds pgpath = parse_path(&path_args, &pg->ps, ti); 96501460f35SBenjamin Marzinski if (IS_ERR(pgpath)) { 96601460f35SBenjamin Marzinski r = PTR_ERR(pgpath); 9671da177e4SLinus Torvalds goto bad; 96801460f35SBenjamin Marzinski } 9691da177e4SLinus Torvalds 9701da177e4SLinus Torvalds pgpath->pg = pg; 9711da177e4SLinus Torvalds list_add_tail(&pgpath->list, &pg->pgpaths); 972498f0103SMike Snitzer dm_consume_args(as, nr_args); 9731da177e4SLinus Torvalds } 9741da177e4SLinus Torvalds 9751da177e4SLinus Torvalds return pg; 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds bad: 9781da177e4SLinus Torvalds free_priority_group(pg, ti); 97901460f35SBenjamin Marzinski return ERR_PTR(r); 9801da177e4SLinus Torvalds } 9811da177e4SLinus Torvalds 982498f0103SMike Snitzer static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m) 9831da177e4SLinus Torvalds { 9841da177e4SLinus Torvalds unsigned hw_argc; 9852bfd2e13SChandra Seetharaman int ret; 98628f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 9871da177e4SLinus Torvalds 988498f0103SMike Snitzer static struct dm_arg _args[] = { 98972d94861SAlasdair G Kergon {0, 1024, "invalid number of hardware handler args"}, 9901da177e4SLinus Torvalds }; 9911da177e4SLinus Torvalds 992498f0103SMike Snitzer if (dm_read_arg_group(_args, as, &hw_argc, &ti->error)) 9931da177e4SLinus Torvalds return -EINVAL; 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds if (!hw_argc) 9961da177e4SLinus Torvalds return 0; 9971da177e4SLinus Torvalds 998e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) { 99976e33fe4SMike Snitzer dm_consume_args(as, hw_argc); 100076e33fe4SMike Snitzer DMERR("bio-based multipath doesn't allow hardware handler args"); 100176e33fe4SMike Snitzer return 0; 100276e33fe4SMike Snitzer } 100376e33fe4SMike Snitzer 1004498f0103SMike Snitzer m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL); 100514e98c5cSChandra Seetharaman 10062bfd2e13SChandra Seetharaman if (hw_argc > 1) { 10072bfd2e13SChandra Seetharaman char *p; 10082bfd2e13SChandra Seetharaman int i, j, len = 4; 10092bfd2e13SChandra Seetharaman 10102bfd2e13SChandra Seetharaman for (i = 0; i <= hw_argc - 2; i++) 10112bfd2e13SChandra Seetharaman len += strlen(as->argv[i]) + 1; 10122bfd2e13SChandra Seetharaman p = m->hw_handler_params = kzalloc(len, GFP_KERNEL); 10132bfd2e13SChandra Seetharaman if (!p) { 10142bfd2e13SChandra Seetharaman ti->error = "memory allocation failed"; 10152bfd2e13SChandra Seetharaman ret = -ENOMEM; 10162bfd2e13SChandra Seetharaman goto fail; 10172bfd2e13SChandra Seetharaman } 10182bfd2e13SChandra Seetharaman j = sprintf(p, "%d", hw_argc - 1); 10192bfd2e13SChandra Seetharaman for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1) 10202bfd2e13SChandra Seetharaman j = sprintf(p, "%s", as->argv[i]); 10212bfd2e13SChandra Seetharaman } 1022498f0103SMike Snitzer dm_consume_args(as, hw_argc - 1); 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds return 0; 10252bfd2e13SChandra Seetharaman fail: 10262bfd2e13SChandra Seetharaman kfree(m->hw_handler_name); 10272bfd2e13SChandra Seetharaman m->hw_handler_name = NULL; 10282bfd2e13SChandra Seetharaman return ret; 10291da177e4SLinus Torvalds } 10301da177e4SLinus Torvalds 1031498f0103SMike Snitzer static int parse_features(struct dm_arg_set *as, struct multipath *m) 10321da177e4SLinus Torvalds { 10331da177e4SLinus Torvalds int r; 10341da177e4SLinus Torvalds unsigned argc; 103528f16c20SMicha³ Miros³aw struct dm_target *ti = m->ti; 1036498f0103SMike Snitzer const char *arg_name; 10371da177e4SLinus Torvalds 1038498f0103SMike Snitzer static struct dm_arg _args[] = { 1039e83068a5SMike Snitzer {0, 8, "invalid number of feature args"}, 1040c9e45581SDave Wysochanski {1, 50, "pg_init_retries must be between 1 and 50"}, 10414e2d19e4SChandra Seetharaman {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"}, 10421da177e4SLinus Torvalds }; 10431da177e4SLinus Torvalds 1044498f0103SMike Snitzer r = dm_read_arg_group(_args, as, &argc, &ti->error); 10451da177e4SLinus Torvalds if (r) 10461da177e4SLinus Torvalds return -EINVAL; 10471da177e4SLinus Torvalds 10481da177e4SLinus Torvalds if (!argc) 10491da177e4SLinus Torvalds return 0; 10501da177e4SLinus Torvalds 1051c9e45581SDave Wysochanski do { 1052498f0103SMike Snitzer arg_name = dm_shift_arg(as); 1053c9e45581SDave Wysochanski argc--; 1054c9e45581SDave Wysochanski 1055498f0103SMike Snitzer if (!strcasecmp(arg_name, "queue_if_no_path")) { 1056be7d31ccSMike Snitzer r = queue_if_no_path(m, true, false); 1057c9e45581SDave Wysochanski continue; 10581da177e4SLinus Torvalds } 1059c9e45581SDave Wysochanski 1060a58a935dSMike Snitzer if (!strcasecmp(arg_name, "retain_attached_hw_handler")) { 1061518257b1SMike Snitzer set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags); 1062a58a935dSMike Snitzer continue; 1063a58a935dSMike Snitzer } 1064a58a935dSMike Snitzer 1065498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_retries") && 1066c9e45581SDave Wysochanski (argc >= 1)) { 1067498f0103SMike Snitzer r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error); 1068c9e45581SDave Wysochanski argc--; 1069c9e45581SDave Wysochanski continue; 1070c9e45581SDave Wysochanski } 1071c9e45581SDave Wysochanski 1072498f0103SMike Snitzer if (!strcasecmp(arg_name, "pg_init_delay_msecs") && 10734e2d19e4SChandra Seetharaman (argc >= 1)) { 1074498f0103SMike Snitzer r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error); 10754e2d19e4SChandra Seetharaman argc--; 10764e2d19e4SChandra Seetharaman continue; 10774e2d19e4SChandra Seetharaman } 10784e2d19e4SChandra Seetharaman 1079e83068a5SMike Snitzer if (!strcasecmp(arg_name, "queue_mode") && 1080e83068a5SMike Snitzer (argc >= 1)) { 1081e83068a5SMike Snitzer const char *queue_mode_name = dm_shift_arg(as); 1082e83068a5SMike Snitzer 1083e83068a5SMike Snitzer if (!strcasecmp(queue_mode_name, "bio")) 1084e83068a5SMike Snitzer m->queue_mode = DM_TYPE_BIO_BASED; 1085e83068a5SMike Snitzer else if (!strcasecmp(queue_mode_name, "rq")) 1086e83068a5SMike Snitzer m->queue_mode = DM_TYPE_REQUEST_BASED; 1087e83068a5SMike Snitzer else if (!strcasecmp(queue_mode_name, "mq")) 1088e83068a5SMike Snitzer m->queue_mode = DM_TYPE_MQ_REQUEST_BASED; 1089e83068a5SMike Snitzer else { 1090e83068a5SMike Snitzer ti->error = "Unknown 'queue_mode' requested"; 1091e83068a5SMike Snitzer r = -EINVAL; 1092e83068a5SMike Snitzer } 1093e83068a5SMike Snitzer argc--; 1094e83068a5SMike Snitzer continue; 1095e83068a5SMike Snitzer } 1096e83068a5SMike Snitzer 1097c9e45581SDave Wysochanski ti->error = "Unrecognised multipath feature request"; 1098c9e45581SDave Wysochanski r = -EINVAL; 1099c9e45581SDave Wysochanski } while (argc && !r); 1100c9e45581SDave Wysochanski 1101c9e45581SDave Wysochanski return r; 11021da177e4SLinus Torvalds } 11031da177e4SLinus Torvalds 1104e83068a5SMike Snitzer static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv) 11051da177e4SLinus Torvalds { 1106498f0103SMike Snitzer /* target arguments */ 1107498f0103SMike Snitzer static struct dm_arg _args[] = { 1108a490a07aSMike Snitzer {0, 1024, "invalid number of priority groups"}, 1109a490a07aSMike Snitzer {0, 1024, "invalid initial priority group number"}, 11101da177e4SLinus Torvalds }; 11111da177e4SLinus Torvalds 11121da177e4SLinus Torvalds int r; 11131da177e4SLinus Torvalds struct multipath *m; 1114498f0103SMike Snitzer struct dm_arg_set as; 11151da177e4SLinus Torvalds unsigned pg_count = 0; 11161da177e4SLinus Torvalds unsigned next_pg_num; 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds as.argc = argc; 11191da177e4SLinus Torvalds as.argv = argv; 11201da177e4SLinus Torvalds 1121e83068a5SMike Snitzer m = alloc_multipath(ti); 11221da177e4SLinus Torvalds if (!m) { 112372d94861SAlasdair G Kergon ti->error = "can't allocate multipath"; 11241da177e4SLinus Torvalds return -EINVAL; 11251da177e4SLinus Torvalds } 11261da177e4SLinus Torvalds 112728f16c20SMicha³ Miros³aw r = parse_features(&as, m); 11281da177e4SLinus Torvalds if (r) 11291da177e4SLinus Torvalds goto bad; 11301da177e4SLinus Torvalds 1131e83068a5SMike Snitzer r = alloc_multipath_stage2(ti, m); 1132e83068a5SMike Snitzer if (r) 1133e83068a5SMike Snitzer goto bad; 1134e83068a5SMike Snitzer 113528f16c20SMicha³ Miros³aw r = parse_hw_handler(&as, m); 11361da177e4SLinus Torvalds if (r) 11371da177e4SLinus Torvalds goto bad; 11381da177e4SLinus Torvalds 1139498f0103SMike Snitzer r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error); 11401da177e4SLinus Torvalds if (r) 11411da177e4SLinus Torvalds goto bad; 11421da177e4SLinus Torvalds 1143498f0103SMike Snitzer r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error); 11441da177e4SLinus Torvalds if (r) 11451da177e4SLinus Torvalds goto bad; 11461da177e4SLinus Torvalds 1147a490a07aSMike Snitzer if ((!m->nr_priority_groups && next_pg_num) || 1148a490a07aSMike Snitzer (m->nr_priority_groups && !next_pg_num)) { 1149a490a07aSMike Snitzer ti->error = "invalid initial priority group"; 1150a490a07aSMike Snitzer r = -EINVAL; 1151a490a07aSMike Snitzer goto bad; 1152a490a07aSMike Snitzer } 1153a490a07aSMike Snitzer 11541da177e4SLinus Torvalds /* parse the priority groups */ 11551da177e4SLinus Torvalds while (as.argc) { 11561da177e4SLinus Torvalds struct priority_group *pg; 115791e968aaSMike Snitzer unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths); 11581da177e4SLinus Torvalds 115928f16c20SMicha³ Miros³aw pg = parse_priority_group(&as, m); 116001460f35SBenjamin Marzinski if (IS_ERR(pg)) { 116101460f35SBenjamin Marzinski r = PTR_ERR(pg); 11621da177e4SLinus Torvalds goto bad; 11631da177e4SLinus Torvalds } 11641da177e4SLinus Torvalds 116591e968aaSMike Snitzer nr_valid_paths += pg->nr_pgpaths; 116691e968aaSMike Snitzer atomic_set(&m->nr_valid_paths, nr_valid_paths); 116791e968aaSMike Snitzer 11681da177e4SLinus Torvalds list_add_tail(&pg->list, &m->priority_groups); 11691da177e4SLinus Torvalds pg_count++; 11701da177e4SLinus Torvalds pg->pg_num = pg_count; 11711da177e4SLinus Torvalds if (!--next_pg_num) 11721da177e4SLinus Torvalds m->next_pg = pg; 11731da177e4SLinus Torvalds } 11741da177e4SLinus Torvalds 11751da177e4SLinus Torvalds if (pg_count != m->nr_priority_groups) { 117672d94861SAlasdair G Kergon ti->error = "priority group count mismatch"; 11771da177e4SLinus Torvalds r = -EINVAL; 11781da177e4SLinus Torvalds goto bad; 11791da177e4SLinus Torvalds } 11801da177e4SLinus Torvalds 118155a62eefSAlasdair G Kergon ti->num_flush_bios = 1; 118255a62eefSAlasdair G Kergon ti->num_discard_bios = 1; 1183042bcef8SMike Snitzer ti->num_write_same_bios = 1; 1184e83068a5SMike Snitzer if (m->queue_mode == DM_TYPE_BIO_BASED) 1185bf661be1SMike Snitzer ti->per_io_data_size = multipath_per_bio_data_size(); 1186e83068a5SMike Snitzer else if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED) 11878637a6bfSMike Snitzer ti->per_io_data_size = sizeof(struct dm_mpath_io); 11888627921fSMikulas Patocka 11891da177e4SLinus Torvalds return 0; 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds bad: 11921da177e4SLinus Torvalds free_multipath(m); 11931da177e4SLinus Torvalds return r; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds 11962bded7bdSKiyoshi Ueda static void multipath_wait_for_pg_init_completion(struct multipath *m) 11972bded7bdSKiyoshi Ueda { 11989f4c3f87SBart Van Assche DEFINE_WAIT(wait); 11992bded7bdSKiyoshi Ueda 12002bded7bdSKiyoshi Ueda while (1) { 12019f4c3f87SBart Van Assche prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE); 12022bded7bdSKiyoshi Ueda 120391e968aaSMike Snitzer if (!atomic_read(&m->pg_init_in_progress)) 12042bded7bdSKiyoshi Ueda break; 12052bded7bdSKiyoshi Ueda 12062bded7bdSKiyoshi Ueda io_schedule(); 12072bded7bdSKiyoshi Ueda } 12089f4c3f87SBart Van Assche finish_wait(&m->pg_init_wait, &wait); 12092bded7bdSKiyoshi Ueda } 12102bded7bdSKiyoshi Ueda 12112bded7bdSKiyoshi Ueda static void flush_multipath_work(struct multipath *m) 12121da177e4SLinus Torvalds { 1213518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1214518257b1SMike Snitzer smp_mb__after_atomic(); 1215954a73d5SShiva Krishna Merla 1216bab7cfc7SChandra Seetharaman flush_workqueue(kmpath_handlerd); 12172bded7bdSKiyoshi Ueda multipath_wait_for_pg_init_completion(m); 1218a044d016SAlasdair G Kergon flush_workqueue(kmultipathd); 121943829731STejun Heo flush_work(&m->trigger_event); 1220954a73d5SShiva Krishna Merla 1221518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags); 1222518257b1SMike Snitzer smp_mb__after_atomic(); 12236df400abSKiyoshi Ueda } 12246df400abSKiyoshi Ueda 12256df400abSKiyoshi Ueda static void multipath_dtr(struct dm_target *ti) 12266df400abSKiyoshi Ueda { 12276df400abSKiyoshi Ueda struct multipath *m = ti->private; 12286df400abSKiyoshi Ueda 12292bded7bdSKiyoshi Ueda flush_multipath_work(m); 12301da177e4SLinus Torvalds free_multipath(m); 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds /* 12341da177e4SLinus Torvalds * Take a path out of use. 12351da177e4SLinus Torvalds */ 12361da177e4SLinus Torvalds static int fail_path(struct pgpath *pgpath) 12371da177e4SLinus Torvalds { 12381da177e4SLinus Torvalds unsigned long flags; 12391da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 12401da177e4SLinus Torvalds 12411da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 12421da177e4SLinus Torvalds 12436680073dSKiyoshi Ueda if (!pgpath->is_active) 12441da177e4SLinus Torvalds goto out; 12451da177e4SLinus Torvalds 124672d94861SAlasdair G Kergon DMWARN("Failing path %s.", pgpath->path.dev->name); 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path); 1249be7d31ccSMike Snitzer pgpath->is_active = false; 12501da177e4SLinus Torvalds pgpath->fail_count++; 12511da177e4SLinus Torvalds 125291e968aaSMike Snitzer atomic_dec(&m->nr_valid_paths); 12531da177e4SLinus Torvalds 12541da177e4SLinus Torvalds if (pgpath == m->current_pgpath) 12551da177e4SLinus Torvalds m->current_pgpath = NULL; 12561da177e4SLinus Torvalds 1257b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti, 125891e968aaSMike Snitzer pgpath->path.dev->name, atomic_read(&m->nr_valid_paths)); 1259b15546f9SMike Anderson 1260fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds out: 12631da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 12641da177e4SLinus Torvalds 12651da177e4SLinus Torvalds return 0; 12661da177e4SLinus Torvalds } 12671da177e4SLinus Torvalds 12681da177e4SLinus Torvalds /* 12691da177e4SLinus Torvalds * Reinstate a previously-failed path 12701da177e4SLinus Torvalds */ 12711da177e4SLinus Torvalds static int reinstate_path(struct pgpath *pgpath) 12721da177e4SLinus Torvalds { 127363d832c3SHannes Reinecke int r = 0, run_queue = 0; 12741da177e4SLinus Torvalds unsigned long flags; 12751da177e4SLinus Torvalds struct multipath *m = pgpath->pg->m; 127691e968aaSMike Snitzer unsigned nr_valid_paths; 12771da177e4SLinus Torvalds 12781da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 12791da177e4SLinus Torvalds 12806680073dSKiyoshi Ueda if (pgpath->is_active) 12811da177e4SLinus Torvalds goto out; 12821da177e4SLinus Torvalds 1283ec31f3f7SMike Snitzer DMWARN("Reinstating path %s.", pgpath->path.dev->name); 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path); 12861da177e4SLinus Torvalds if (r) 12871da177e4SLinus Torvalds goto out; 12881da177e4SLinus Torvalds 1289be7d31ccSMike Snitzer pgpath->is_active = true; 12901da177e4SLinus Torvalds 129191e968aaSMike Snitzer nr_valid_paths = atomic_inc_return(&m->nr_valid_paths); 129291e968aaSMike Snitzer if (nr_valid_paths == 1) { 12931da177e4SLinus Torvalds m->current_pgpath = NULL; 129463d832c3SHannes Reinecke run_queue = 1; 1295e54f77ddSChandra Seetharaman } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) { 12964e2d19e4SChandra Seetharaman if (queue_work(kmpath_handlerd, &pgpath->activate_path.work)) 129791e968aaSMike Snitzer atomic_inc(&m->pg_init_in_progress); 1298e54f77ddSChandra Seetharaman } 12991da177e4SLinus Torvalds 1300b15546f9SMike Anderson dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti, 130191e968aaSMike Snitzer pgpath->path.dev->name, nr_valid_paths); 1302b15546f9SMike Anderson 1303fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13041da177e4SLinus Torvalds 13051da177e4SLinus Torvalds out: 13061da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 130776e33fe4SMike Snitzer if (run_queue) { 130863d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 1309*7e48c768SMike Snitzer process_queued_io_list(m); 131076e33fe4SMike Snitzer } 13111da177e4SLinus Torvalds 13121da177e4SLinus Torvalds return r; 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds 13151da177e4SLinus Torvalds /* 13161da177e4SLinus Torvalds * Fail or reinstate all paths that match the provided struct dm_dev. 13171da177e4SLinus Torvalds */ 13181da177e4SLinus Torvalds static int action_dev(struct multipath *m, struct dm_dev *dev, 13191da177e4SLinus Torvalds action_fn action) 13201da177e4SLinus Torvalds { 132119040c0bSMike Snitzer int r = -EINVAL; 13221da177e4SLinus Torvalds struct pgpath *pgpath; 13231da177e4SLinus Torvalds struct priority_group *pg; 13241da177e4SLinus Torvalds 13251da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 13261da177e4SLinus Torvalds list_for_each_entry(pgpath, &pg->pgpaths, list) { 13271da177e4SLinus Torvalds if (pgpath->path.dev == dev) 13281da177e4SLinus Torvalds r = action(pgpath); 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds } 13311da177e4SLinus Torvalds 13321da177e4SLinus Torvalds return r; 13331da177e4SLinus Torvalds } 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds /* 13361da177e4SLinus Torvalds * Temporarily try to avoid having to use the specified PG 13371da177e4SLinus Torvalds */ 13381da177e4SLinus Torvalds static void bypass_pg(struct multipath *m, struct priority_group *pg, 1339be7d31ccSMike Snitzer bool bypassed) 13401da177e4SLinus Torvalds { 13411da177e4SLinus Torvalds unsigned long flags; 13421da177e4SLinus Torvalds 13431da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13441da177e4SLinus Torvalds 13451da177e4SLinus Torvalds pg->bypassed = bypassed; 13461da177e4SLinus Torvalds m->current_pgpath = NULL; 13471da177e4SLinus Torvalds m->current_pg = NULL; 13481da177e4SLinus Torvalds 13491da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13501da177e4SLinus Torvalds 1351fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds /* 13551da177e4SLinus Torvalds * Switch to using the specified PG from the next I/O that gets mapped 13561da177e4SLinus Torvalds */ 13571da177e4SLinus Torvalds static int switch_pg_num(struct multipath *m, const char *pgstr) 13581da177e4SLinus Torvalds { 13591da177e4SLinus Torvalds struct priority_group *pg; 13601da177e4SLinus Torvalds unsigned pgnum; 13611da177e4SLinus Torvalds unsigned long flags; 136231998ef1SMikulas Patocka char dummy; 13631da177e4SLinus Torvalds 136431998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 13651da177e4SLinus Torvalds (pgnum > m->nr_priority_groups)) { 13661da177e4SLinus Torvalds DMWARN("invalid PG number supplied to switch_pg_num"); 13671da177e4SLinus Torvalds return -EINVAL; 13681da177e4SLinus Torvalds } 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 13711da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 1372be7d31ccSMike Snitzer pg->bypassed = false; 13731da177e4SLinus Torvalds if (--pgnum) 13741da177e4SLinus Torvalds continue; 13751da177e4SLinus Torvalds 13761da177e4SLinus Torvalds m->current_pgpath = NULL; 13771da177e4SLinus Torvalds m->current_pg = NULL; 13781da177e4SLinus Torvalds m->next_pg = pg; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 13811da177e4SLinus Torvalds 1382fe9cf30eSAlasdair G Kergon schedule_work(&m->trigger_event); 13831da177e4SLinus Torvalds return 0; 13841da177e4SLinus Torvalds } 13851da177e4SLinus Torvalds 13861da177e4SLinus Torvalds /* 13871da177e4SLinus Torvalds * Set/clear bypassed status of a PG. 13881da177e4SLinus Torvalds * PGs are numbered upwards from 1 in the order they were declared. 13891da177e4SLinus Torvalds */ 1390be7d31ccSMike Snitzer static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed) 13911da177e4SLinus Torvalds { 13921da177e4SLinus Torvalds struct priority_group *pg; 13931da177e4SLinus Torvalds unsigned pgnum; 139431998ef1SMikulas Patocka char dummy; 13951da177e4SLinus Torvalds 139631998ef1SMikulas Patocka if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum || 13971da177e4SLinus Torvalds (pgnum > m->nr_priority_groups)) { 13981da177e4SLinus Torvalds DMWARN("invalid PG number supplied to bypass_pg"); 13991da177e4SLinus Torvalds return -EINVAL; 14001da177e4SLinus Torvalds } 14011da177e4SLinus Torvalds 14021da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 14031da177e4SLinus Torvalds if (!--pgnum) 14041da177e4SLinus Torvalds break; 14051da177e4SLinus Torvalds } 14061da177e4SLinus Torvalds 14071da177e4SLinus Torvalds bypass_pg(m, pg, bypassed); 14081da177e4SLinus Torvalds return 0; 14091da177e4SLinus Torvalds } 14101da177e4SLinus Torvalds 14111da177e4SLinus Torvalds /* 1412c9e45581SDave Wysochanski * Should we retry pg_init immediately? 1413c9e45581SDave Wysochanski */ 1414be7d31ccSMike Snitzer static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath) 1415c9e45581SDave Wysochanski { 1416c9e45581SDave Wysochanski unsigned long flags; 1417be7d31ccSMike Snitzer bool limit_reached = false; 1418c9e45581SDave Wysochanski 1419c9e45581SDave Wysochanski spin_lock_irqsave(&m->lock, flags); 1420c9e45581SDave Wysochanski 142191e968aaSMike Snitzer if (atomic_read(&m->pg_init_count) <= m->pg_init_retries && 142291e968aaSMike Snitzer !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags)) 1423518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags); 1424c9e45581SDave Wysochanski else 1425be7d31ccSMike Snitzer limit_reached = true; 1426c9e45581SDave Wysochanski 1427c9e45581SDave Wysochanski spin_unlock_irqrestore(&m->lock, flags); 1428c9e45581SDave Wysochanski 1429c9e45581SDave Wysochanski return limit_reached; 1430c9e45581SDave Wysochanski } 1431c9e45581SDave Wysochanski 14323ae31f6aSChandra Seetharaman static void pg_init_done(void *data, int errors) 1433cfae5c9bSChandra Seetharaman { 143483c0d5d5SMoger, Babu struct pgpath *pgpath = data; 1435cfae5c9bSChandra Seetharaman struct priority_group *pg = pgpath->pg; 1436cfae5c9bSChandra Seetharaman struct multipath *m = pg->m; 1437cfae5c9bSChandra Seetharaman unsigned long flags; 1438be7d31ccSMike Snitzer bool delay_retry = false; 1439cfae5c9bSChandra Seetharaman 1440cfae5c9bSChandra Seetharaman /* device or driver problems */ 1441cfae5c9bSChandra Seetharaman switch (errors) { 1442cfae5c9bSChandra Seetharaman case SCSI_DH_OK: 1443cfae5c9bSChandra Seetharaman break; 1444cfae5c9bSChandra Seetharaman case SCSI_DH_NOSYS: 1445cfae5c9bSChandra Seetharaman if (!m->hw_handler_name) { 1446cfae5c9bSChandra Seetharaman errors = 0; 1447cfae5c9bSChandra Seetharaman break; 1448cfae5c9bSChandra Seetharaman } 1449f7b934c8SMoger, Babu DMERR("Could not failover the device: Handler scsi_dh_%s " 1450f7b934c8SMoger, Babu "Error %d.", m->hw_handler_name, errors); 1451cfae5c9bSChandra Seetharaman /* 1452cfae5c9bSChandra Seetharaman * Fail path for now, so we do not ping pong 1453cfae5c9bSChandra Seetharaman */ 1454cfae5c9bSChandra Seetharaman fail_path(pgpath); 1455cfae5c9bSChandra Seetharaman break; 1456cfae5c9bSChandra Seetharaman case SCSI_DH_DEV_TEMP_BUSY: 1457cfae5c9bSChandra Seetharaman /* 1458cfae5c9bSChandra Seetharaman * Probably doing something like FW upgrade on the 1459cfae5c9bSChandra Seetharaman * controller so try the other pg. 1460cfae5c9bSChandra Seetharaman */ 1461be7d31ccSMike Snitzer bypass_pg(m, pg, true); 1462cfae5c9bSChandra Seetharaman break; 1463cfae5c9bSChandra Seetharaman case SCSI_DH_RETRY: 14644e2d19e4SChandra Seetharaman /* Wait before retrying. */ 14654e2d19e4SChandra Seetharaman delay_retry = 1; 1466cfae5c9bSChandra Seetharaman case SCSI_DH_IMM_RETRY: 1467cfae5c9bSChandra Seetharaman case SCSI_DH_RES_TEMP_UNAVAIL: 1468cfae5c9bSChandra Seetharaman if (pg_init_limit_reached(m, pgpath)) 1469cfae5c9bSChandra Seetharaman fail_path(pgpath); 1470cfae5c9bSChandra Seetharaman errors = 0; 1471cfae5c9bSChandra Seetharaman break; 1472ec31f3f7SMike Snitzer case SCSI_DH_DEV_OFFLINED: 1473cfae5c9bSChandra Seetharaman default: 1474cfae5c9bSChandra Seetharaman /* 1475cfae5c9bSChandra Seetharaman * We probably do not want to fail the path for a device 1476cfae5c9bSChandra Seetharaman * error, but this is what the old dm did. In future 1477cfae5c9bSChandra Seetharaman * patches we can do more advanced handling. 1478cfae5c9bSChandra Seetharaman */ 1479cfae5c9bSChandra Seetharaman fail_path(pgpath); 1480cfae5c9bSChandra Seetharaman } 1481cfae5c9bSChandra Seetharaman 1482cfae5c9bSChandra Seetharaman spin_lock_irqsave(&m->lock, flags); 1483cfae5c9bSChandra Seetharaman if (errors) { 1484e54f77ddSChandra Seetharaman if (pgpath == m->current_pgpath) { 1485cfae5c9bSChandra Seetharaman DMERR("Could not failover device. Error %d.", errors); 1486cfae5c9bSChandra Seetharaman m->current_pgpath = NULL; 1487cfae5c9bSChandra Seetharaman m->current_pg = NULL; 1488e54f77ddSChandra Seetharaman } 1489518257b1SMike Snitzer } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 1490be7d31ccSMike Snitzer pg->bypassed = false; 1491cfae5c9bSChandra Seetharaman 149291e968aaSMike Snitzer if (atomic_dec_return(&m->pg_init_in_progress) > 0) 1493d0259bf0SKiyoshi Ueda /* Activations of other paths are still on going */ 1494d0259bf0SKiyoshi Ueda goto out; 1495d0259bf0SKiyoshi Ueda 1496518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) { 1497518257b1SMike Snitzer if (delay_retry) 1498518257b1SMike Snitzer set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1499518257b1SMike Snitzer else 1500518257b1SMike Snitzer clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags); 1501518257b1SMike Snitzer 15023e9f1be1SHannes Reinecke if (__pg_init_all_paths(m)) 15033e9f1be1SHannes Reinecke goto out; 15043e9f1be1SHannes Reinecke } 1505518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IO, &m->flags); 1506d0259bf0SKiyoshi Ueda 1507*7e48c768SMike Snitzer process_queued_io_list(m); 150876e33fe4SMike Snitzer 15092bded7bdSKiyoshi Ueda /* 15102bded7bdSKiyoshi Ueda * Wake up any thread waiting to suspend. 15112bded7bdSKiyoshi Ueda */ 15122bded7bdSKiyoshi Ueda wake_up(&m->pg_init_wait); 15132bded7bdSKiyoshi Ueda 1514d0259bf0SKiyoshi Ueda out: 1515cfae5c9bSChandra Seetharaman spin_unlock_irqrestore(&m->lock, flags); 1516cfae5c9bSChandra Seetharaman } 1517cfae5c9bSChandra Seetharaman 1518bab7cfc7SChandra Seetharaman static void activate_path(struct work_struct *work) 1519bab7cfc7SChandra Seetharaman { 1520e54f77ddSChandra Seetharaman struct pgpath *pgpath = 15214e2d19e4SChandra Seetharaman container_of(work, struct pgpath, activate_path.work); 1522f10e06b7SMike Snitzer struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1523bab7cfc7SChandra Seetharaman 1524f10e06b7SMike Snitzer if (pgpath->is_active && !blk_queue_dying(q)) 1525f10e06b7SMike Snitzer scsi_dh_activate(q, pg_init_done, pgpath); 15263a017509SHannes Reinecke else 15273a017509SHannes Reinecke pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED); 1528bab7cfc7SChandra Seetharaman } 1529bab7cfc7SChandra Seetharaman 15307e782af5SHannes Reinecke static int noretry_error(int error) 15317e782af5SHannes Reinecke { 15327e782af5SHannes Reinecke switch (error) { 15337e782af5SHannes Reinecke case -EOPNOTSUPP: 15347e782af5SHannes Reinecke case -EREMOTEIO: 15357e782af5SHannes Reinecke case -EILSEQ: 15367e782af5SHannes Reinecke case -ENODATA: 1537cc9d3c38SJun'ichi Nomura case -ENOSPC: 15387e782af5SHannes Reinecke return 1; 15397e782af5SHannes Reinecke } 15407e782af5SHannes Reinecke 15417e782af5SHannes Reinecke /* Anything else could be a path failure, so should be retried */ 15427e782af5SHannes Reinecke return 0; 15437e782af5SHannes Reinecke } 15447e782af5SHannes Reinecke 15451da177e4SLinus Torvalds /* 15461da177e4SLinus Torvalds * end_io handling 15471da177e4SLinus Torvalds */ 1548f40c67f0SKiyoshi Ueda static int do_end_io(struct multipath *m, struct request *clone, 1549028867acSAlasdair G Kergon int error, struct dm_mpath_io *mpio) 15501da177e4SLinus Torvalds { 1551f40c67f0SKiyoshi Ueda /* 1552f40c67f0SKiyoshi Ueda * We don't queue any clone request inside the multipath target 1553f40c67f0SKiyoshi Ueda * during end I/O handling, since those clone requests don't have 1554f40c67f0SKiyoshi Ueda * bio clones. If we queue them inside the multipath target, 1555f40c67f0SKiyoshi Ueda * we need to make bio clones, that requires memory allocation. 15564cc96131SMike Snitzer * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests 1557f40c67f0SKiyoshi Ueda * don't have bio clones.) 1558f40c67f0SKiyoshi Ueda * Instead of queueing the clone request here, we queue the original 1559f40c67f0SKiyoshi Ueda * request into dm core, which will remake a clone request and 1560f40c67f0SKiyoshi Ueda * clone bios for it and resubmit it later. 1561f40c67f0SKiyoshi Ueda */ 1562f40c67f0SKiyoshi Ueda int r = DM_ENDIO_REQUEUE; 15631da177e4SLinus Torvalds 1564f40c67f0SKiyoshi Ueda if (!error && !clone->errors) 15651da177e4SLinus Torvalds return 0; /* I/O complete */ 15661da177e4SLinus Torvalds 15677eee4ae2SMike Snitzer if (noretry_error(error)) 1568959eb4e5SMike Snitzer return error; 1569959eb4e5SMike Snitzer 1570cfae5c9bSChandra Seetharaman if (mpio->pgpath) 15711da177e4SLinus Torvalds fail_path(mpio->pgpath); 15721da177e4SLinus Torvalds 157391e968aaSMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 1574518257b1SMike Snitzer if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 157576e33fe4SMike Snitzer if (!must_push_back_rq(m)) 1576f40c67f0SKiyoshi Ueda r = -EIO; 1577751b2a7dSHannes Reinecke } else { 1578751b2a7dSHannes Reinecke if (error == -EBADE) 1579751b2a7dSHannes Reinecke r = error; 1580751b2a7dSHannes Reinecke } 1581751b2a7dSHannes Reinecke } 15821da177e4SLinus Torvalds 1583f40c67f0SKiyoshi Ueda return r; 15841da177e4SLinus Torvalds } 15851da177e4SLinus Torvalds 1586f40c67f0SKiyoshi Ueda static int multipath_end_io(struct dm_target *ti, struct request *clone, 15871da177e4SLinus Torvalds int error, union map_info *map_context) 15881da177e4SLinus Torvalds { 1589028867acSAlasdair G Kergon struct multipath *m = ti->private; 15902eff1924SMike Snitzer struct dm_mpath_io *mpio = get_mpio(map_context); 1591a71a261fSWei Yongjun struct pgpath *pgpath; 15921da177e4SLinus Torvalds struct path_selector *ps; 15931da177e4SLinus Torvalds int r; 15941da177e4SLinus Torvalds 1595466891f9SJun'ichi Nomura BUG_ON(!mpio); 1596466891f9SJun'ichi Nomura 1597f40c67f0SKiyoshi Ueda r = do_end_io(m, clone, error, mpio); 1598a71a261fSWei Yongjun pgpath = mpio->pgpath; 15991da177e4SLinus Torvalds if (pgpath) { 16001da177e4SLinus Torvalds ps = &pgpath->pg->ps; 16011da177e4SLinus Torvalds if (ps->type->end_io) 160202ab823fSKiyoshi Ueda ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); 16031da177e4SLinus Torvalds } 16042eff1924SMike Snitzer clear_request_fn_mpio(m, map_context); 16051da177e4SLinus Torvalds 16061da177e4SLinus Torvalds return r; 16071da177e4SLinus Torvalds } 16081da177e4SLinus Torvalds 160976e33fe4SMike Snitzer static int do_end_io_bio(struct multipath *m, struct bio *clone, 161076e33fe4SMike Snitzer int error, struct dm_mpath_io *mpio) 161176e33fe4SMike Snitzer { 161276e33fe4SMike Snitzer unsigned long flags; 161376e33fe4SMike Snitzer 161476e33fe4SMike Snitzer if (!error) 161576e33fe4SMike Snitzer return 0; /* I/O complete */ 161676e33fe4SMike Snitzer 161776e33fe4SMike Snitzer if (noretry_error(error)) 161876e33fe4SMike Snitzer return error; 161976e33fe4SMike Snitzer 162076e33fe4SMike Snitzer if (mpio->pgpath) 162176e33fe4SMike Snitzer fail_path(mpio->pgpath); 162276e33fe4SMike Snitzer 162376e33fe4SMike Snitzer if (!atomic_read(&m->nr_valid_paths)) { 162476e33fe4SMike Snitzer if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) { 162576e33fe4SMike Snitzer if (!must_push_back_bio(m)) 162676e33fe4SMike Snitzer return -EIO; 162776e33fe4SMike Snitzer return DM_ENDIO_REQUEUE; 162876e33fe4SMike Snitzer } else { 162976e33fe4SMike Snitzer if (error == -EBADE) 163076e33fe4SMike Snitzer return error; 163176e33fe4SMike Snitzer } 163276e33fe4SMike Snitzer } 163376e33fe4SMike Snitzer 163476e33fe4SMike Snitzer /* Queue for the daemon to resubmit */ 1635bf661be1SMike Snitzer dm_bio_restore(get_bio_details_from_bio(clone), clone); 163676e33fe4SMike Snitzer 163776e33fe4SMike Snitzer spin_lock_irqsave(&m->lock, flags); 163876e33fe4SMike Snitzer bio_list_add(&m->queued_bios, clone); 163976e33fe4SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 164076e33fe4SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) 164176e33fe4SMike Snitzer queue_work(kmultipathd, &m->process_queued_bios); 164276e33fe4SMike Snitzer 164376e33fe4SMike Snitzer return DM_ENDIO_INCOMPLETE; 164476e33fe4SMike Snitzer } 164576e33fe4SMike Snitzer 164676e33fe4SMike Snitzer static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, int error) 164776e33fe4SMike Snitzer { 164876e33fe4SMike Snitzer struct multipath *m = ti->private; 164976e33fe4SMike Snitzer struct dm_mpath_io *mpio = get_mpio_from_bio(clone); 165076e33fe4SMike Snitzer struct pgpath *pgpath; 165176e33fe4SMike Snitzer struct path_selector *ps; 165276e33fe4SMike Snitzer int r; 165376e33fe4SMike Snitzer 165476e33fe4SMike Snitzer BUG_ON(!mpio); 165576e33fe4SMike Snitzer 165676e33fe4SMike Snitzer r = do_end_io_bio(m, clone, error, mpio); 165776e33fe4SMike Snitzer pgpath = mpio->pgpath; 165876e33fe4SMike Snitzer if (pgpath) { 165976e33fe4SMike Snitzer ps = &pgpath->pg->ps; 166076e33fe4SMike Snitzer if (ps->type->end_io) 166176e33fe4SMike Snitzer ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes); 166276e33fe4SMike Snitzer } 166376e33fe4SMike Snitzer 166476e33fe4SMike Snitzer return r; 166576e33fe4SMike Snitzer } 166676e33fe4SMike Snitzer 16671da177e4SLinus Torvalds /* 16681da177e4SLinus Torvalds * Suspend can't complete until all the I/O is processed so if 1669436d4108SAlasdair G Kergon * the last path fails we must error any remaining I/O. 1670436d4108SAlasdair G Kergon * Note that if the freeze_bdev fails while suspending, the 1671436d4108SAlasdair G Kergon * queue_if_no_path state is lost - userspace should reset it. 16721da177e4SLinus Torvalds */ 16731da177e4SLinus Torvalds static void multipath_presuspend(struct dm_target *ti) 16741da177e4SLinus Torvalds { 16757943bd6dSMike Snitzer struct multipath *m = ti->private; 16761da177e4SLinus Torvalds 1677be7d31ccSMike Snitzer queue_if_no_path(m, false, true); 16781da177e4SLinus Torvalds } 16791da177e4SLinus Torvalds 16806df400abSKiyoshi Ueda static void multipath_postsuspend(struct dm_target *ti) 16816df400abSKiyoshi Ueda { 16826380f26fSMike Anderson struct multipath *m = ti->private; 16836380f26fSMike Anderson 16846380f26fSMike Anderson mutex_lock(&m->work_mutex); 16852bded7bdSKiyoshi Ueda flush_multipath_work(m); 16866380f26fSMike Anderson mutex_unlock(&m->work_mutex); 16876df400abSKiyoshi Ueda } 16886df400abSKiyoshi Ueda 1689436d4108SAlasdair G Kergon /* 1690436d4108SAlasdair G Kergon * Restore the queue_if_no_path setting. 1691436d4108SAlasdair G Kergon */ 16921da177e4SLinus Torvalds static void multipath_resume(struct dm_target *ti) 16931da177e4SLinus Torvalds { 16947943bd6dSMike Snitzer struct multipath *m = ti->private; 16951814f2e3SMike Snitzer unsigned long flags; 16961da177e4SLinus Torvalds 16971814f2e3SMike Snitzer spin_lock_irqsave(&m->lock, flags); 1698518257b1SMike Snitzer if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) 1699518257b1SMike Snitzer set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 1700518257b1SMike Snitzer else 1701518257b1SMike Snitzer clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags); 17021814f2e3SMike Snitzer spin_unlock_irqrestore(&m->lock, flags); 17031da177e4SLinus Torvalds } 17041da177e4SLinus Torvalds 17051da177e4SLinus Torvalds /* 17061da177e4SLinus Torvalds * Info output has the following format: 17071da177e4SLinus Torvalds * num_multipath_feature_args [multipath_feature_args]* 17081da177e4SLinus Torvalds * num_handler_status_args [handler_status_args]* 17091da177e4SLinus Torvalds * num_groups init_group_number 17101da177e4SLinus Torvalds * [A|D|E num_ps_status_args [ps_status_args]* 17111da177e4SLinus Torvalds * num_paths num_selector_args 17121da177e4SLinus Torvalds * [path_dev A|F fail_count [selector_args]* ]+ ]+ 17131da177e4SLinus Torvalds * 17141da177e4SLinus Torvalds * Table output has the following format (identical to the constructor string): 17151da177e4SLinus Torvalds * num_feature_args [features_args]* 17161da177e4SLinus Torvalds * num_handler_args hw_handler [hw_handler_args]* 17171da177e4SLinus Torvalds * num_groups init_group_number 17181da177e4SLinus Torvalds * [priority selector-name num_ps_args [ps_args]* 17191da177e4SLinus Torvalds * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+ 17201da177e4SLinus Torvalds */ 1721fd7c092eSMikulas Patocka static void multipath_status(struct dm_target *ti, status_type_t type, 17221f4e0ff0SAlasdair G Kergon unsigned status_flags, char *result, unsigned maxlen) 17231da177e4SLinus Torvalds { 17241da177e4SLinus Torvalds int sz = 0; 17251da177e4SLinus Torvalds unsigned long flags; 17267943bd6dSMike Snitzer struct multipath *m = ti->private; 17271da177e4SLinus Torvalds struct priority_group *pg; 17281da177e4SLinus Torvalds struct pgpath *p; 17291da177e4SLinus Torvalds unsigned pg_num; 17301da177e4SLinus Torvalds char state; 17311da177e4SLinus Torvalds 17321da177e4SLinus Torvalds spin_lock_irqsave(&m->lock, flags); 17331da177e4SLinus Torvalds 17341da177e4SLinus Torvalds /* Features */ 17351da177e4SLinus Torvalds if (type == STATUSTYPE_INFO) 173691e968aaSMike Snitzer DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags), 173791e968aaSMike Snitzer atomic_read(&m->pg_init_count)); 1738c9e45581SDave Wysochanski else { 1739518257b1SMike Snitzer DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) + 17404e2d19e4SChandra Seetharaman (m->pg_init_retries > 0) * 2 + 1741a58a935dSMike Snitzer (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 + 1742e83068a5SMike Snitzer test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) + 1743e83068a5SMike Snitzer (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2); 1744e83068a5SMike Snitzer 1745518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 1746c9e45581SDave Wysochanski DMEMIT("queue_if_no_path "); 1747c9e45581SDave Wysochanski if (m->pg_init_retries) 1748c9e45581SDave Wysochanski DMEMIT("pg_init_retries %u ", m->pg_init_retries); 17494e2d19e4SChandra Seetharaman if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) 17504e2d19e4SChandra Seetharaman DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs); 1751518257b1SMike Snitzer if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) 1752a58a935dSMike Snitzer DMEMIT("retain_attached_hw_handler "); 1753e83068a5SMike Snitzer if (m->queue_mode != DM_TYPE_REQUEST_BASED) { 1754e83068a5SMike Snitzer switch(m->queue_mode) { 1755e83068a5SMike Snitzer case DM_TYPE_BIO_BASED: 1756e83068a5SMike Snitzer DMEMIT("queue_mode bio "); 1757e83068a5SMike Snitzer break; 1758e83068a5SMike Snitzer case DM_TYPE_MQ_REQUEST_BASED: 1759e83068a5SMike Snitzer DMEMIT("queue_mode mq "); 1760e83068a5SMike Snitzer break; 1761e83068a5SMike Snitzer } 1762e83068a5SMike Snitzer } 1763c9e45581SDave Wysochanski } 17641da177e4SLinus Torvalds 1765cfae5c9bSChandra Seetharaman if (!m->hw_handler_name || type == STATUSTYPE_INFO) 17661da177e4SLinus Torvalds DMEMIT("0 "); 17671da177e4SLinus Torvalds else 1768cfae5c9bSChandra Seetharaman DMEMIT("1 %s ", m->hw_handler_name); 17691da177e4SLinus Torvalds 17701da177e4SLinus Torvalds DMEMIT("%u ", m->nr_priority_groups); 17711da177e4SLinus Torvalds 17721da177e4SLinus Torvalds if (m->next_pg) 17731da177e4SLinus Torvalds pg_num = m->next_pg->pg_num; 17741da177e4SLinus Torvalds else if (m->current_pg) 17751da177e4SLinus Torvalds pg_num = m->current_pg->pg_num; 17761da177e4SLinus Torvalds else 1777a490a07aSMike Snitzer pg_num = (m->nr_priority_groups ? 1 : 0); 17781da177e4SLinus Torvalds 17791da177e4SLinus Torvalds DMEMIT("%u ", pg_num); 17801da177e4SLinus Torvalds 17811da177e4SLinus Torvalds switch (type) { 17821da177e4SLinus Torvalds case STATUSTYPE_INFO: 17831da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 17841da177e4SLinus Torvalds if (pg->bypassed) 17851da177e4SLinus Torvalds state = 'D'; /* Disabled */ 17861da177e4SLinus Torvalds else if (pg == m->current_pg) 17871da177e4SLinus Torvalds state = 'A'; /* Currently Active */ 17881da177e4SLinus Torvalds else 17891da177e4SLinus Torvalds state = 'E'; /* Enabled */ 17901da177e4SLinus Torvalds 17911da177e4SLinus Torvalds DMEMIT("%c ", state); 17921da177e4SLinus Torvalds 17931da177e4SLinus Torvalds if (pg->ps.type->status) 17941da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 17951da177e4SLinus Torvalds result + sz, 17961da177e4SLinus Torvalds maxlen - sz); 17971da177e4SLinus Torvalds else 17981da177e4SLinus Torvalds DMEMIT("0 "); 17991da177e4SLinus Torvalds 18001da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 18011da177e4SLinus Torvalds pg->ps.type->info_args); 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 18041da177e4SLinus Torvalds DMEMIT("%s %s %u ", p->path.dev->name, 18056680073dSKiyoshi Ueda p->is_active ? "A" : "F", 18061da177e4SLinus Torvalds p->fail_count); 18071da177e4SLinus Torvalds if (pg->ps.type->status) 18081da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 18091da177e4SLinus Torvalds &p->path, type, result + sz, 18101da177e4SLinus Torvalds maxlen - sz); 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds } 18131da177e4SLinus Torvalds break; 18141da177e4SLinus Torvalds 18151da177e4SLinus Torvalds case STATUSTYPE_TABLE: 18161da177e4SLinus Torvalds list_for_each_entry(pg, &m->priority_groups, list) { 18171da177e4SLinus Torvalds DMEMIT("%s ", pg->ps.type->name); 18181da177e4SLinus Torvalds 18191da177e4SLinus Torvalds if (pg->ps.type->status) 18201da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, NULL, type, 18211da177e4SLinus Torvalds result + sz, 18221da177e4SLinus Torvalds maxlen - sz); 18231da177e4SLinus Torvalds else 18241da177e4SLinus Torvalds DMEMIT("0 "); 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds DMEMIT("%u %u ", pg->nr_pgpaths, 18271da177e4SLinus Torvalds pg->ps.type->table_args); 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds list_for_each_entry(p, &pg->pgpaths, list) { 18301da177e4SLinus Torvalds DMEMIT("%s ", p->path.dev->name); 18311da177e4SLinus Torvalds if (pg->ps.type->status) 18321da177e4SLinus Torvalds sz += pg->ps.type->status(&pg->ps, 18331da177e4SLinus Torvalds &p->path, type, result + sz, 18341da177e4SLinus Torvalds maxlen - sz); 18351da177e4SLinus Torvalds } 18361da177e4SLinus Torvalds } 18371da177e4SLinus Torvalds break; 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds 18401da177e4SLinus Torvalds spin_unlock_irqrestore(&m->lock, flags); 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds 18431da177e4SLinus Torvalds static int multipath_message(struct dm_target *ti, unsigned argc, char **argv) 18441da177e4SLinus Torvalds { 18456380f26fSMike Anderson int r = -EINVAL; 18461da177e4SLinus Torvalds struct dm_dev *dev; 18477943bd6dSMike Snitzer struct multipath *m = ti->private; 18481da177e4SLinus Torvalds action_fn action; 18491da177e4SLinus Torvalds 18506380f26fSMike Anderson mutex_lock(&m->work_mutex); 18516380f26fSMike Anderson 1852c2f3d24bSKiyoshi Ueda if (dm_suspended(ti)) { 1853c2f3d24bSKiyoshi Ueda r = -EBUSY; 1854c2f3d24bSKiyoshi Ueda goto out; 1855c2f3d24bSKiyoshi Ueda } 1856c2f3d24bSKiyoshi Ueda 18571da177e4SLinus Torvalds if (argc == 1) { 1858498f0103SMike Snitzer if (!strcasecmp(argv[0], "queue_if_no_path")) { 1859be7d31ccSMike Snitzer r = queue_if_no_path(m, true, false); 18606380f26fSMike Anderson goto out; 1861498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "fail_if_no_path")) { 1862be7d31ccSMike Snitzer r = queue_if_no_path(m, false, false); 18636380f26fSMike Anderson goto out; 18646380f26fSMike Anderson } 18651da177e4SLinus Torvalds } 18661da177e4SLinus Torvalds 18676380f26fSMike Anderson if (argc != 2) { 1868a356e426SJose Castillo DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc); 18696380f26fSMike Anderson goto out; 18706380f26fSMike Anderson } 18711da177e4SLinus Torvalds 1872498f0103SMike Snitzer if (!strcasecmp(argv[0], "disable_group")) { 1873be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], true); 18746380f26fSMike Anderson goto out; 1875498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "enable_group")) { 1876be7d31ccSMike Snitzer r = bypass_pg_num(m, argv[1], false); 18776380f26fSMike Anderson goto out; 1878498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "switch_group")) { 18796380f26fSMike Anderson r = switch_pg_num(m, argv[1]); 18806380f26fSMike Anderson goto out; 1881498f0103SMike Snitzer } else if (!strcasecmp(argv[0], "reinstate_path")) 18821da177e4SLinus Torvalds action = reinstate_path; 1883498f0103SMike Snitzer else if (!strcasecmp(argv[0], "fail_path")) 18841da177e4SLinus Torvalds action = fail_path; 18856380f26fSMike Anderson else { 1886a356e426SJose Castillo DMWARN("Unrecognised multipath message received: %s", argv[0]); 18876380f26fSMike Anderson goto out; 18886380f26fSMike Anderson } 18891da177e4SLinus Torvalds 18908215d6ecSNikanth Karthikesan r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev); 18911da177e4SLinus Torvalds if (r) { 189272d94861SAlasdair G Kergon DMWARN("message: error getting device %s", 18931da177e4SLinus Torvalds argv[1]); 18946380f26fSMike Anderson goto out; 18951da177e4SLinus Torvalds } 18961da177e4SLinus Torvalds 18971da177e4SLinus Torvalds r = action_dev(m, dev, action); 18981da177e4SLinus Torvalds 18991da177e4SLinus Torvalds dm_put_device(ti, dev); 19001da177e4SLinus Torvalds 19016380f26fSMike Anderson out: 19026380f26fSMike Anderson mutex_unlock(&m->work_mutex); 19031da177e4SLinus Torvalds return r; 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds 1906e56f81e0SChristoph Hellwig static int multipath_prepare_ioctl(struct dm_target *ti, 1907e56f81e0SChristoph Hellwig struct block_device **bdev, fmode_t *mode) 19089af4aa30SMilan Broz { 190935991652SMikulas Patocka struct multipath *m = ti->private; 19102da1610aSMike Snitzer struct pgpath *current_pgpath; 191135991652SMikulas Patocka int r; 191235991652SMikulas Patocka 19132da1610aSMike Snitzer current_pgpath = lockless_dereference(m->current_pgpath); 19142da1610aSMike Snitzer if (!current_pgpath) 19152da1610aSMike Snitzer current_pgpath = choose_pgpath(m, 0); 19169af4aa30SMilan Broz 19172da1610aSMike Snitzer if (current_pgpath) { 1918518257b1SMike Snitzer if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) { 19192da1610aSMike Snitzer *bdev = current_pgpath->path.dev->bdev; 19202da1610aSMike Snitzer *mode = current_pgpath->path.dev->mode; 192143e43c9eSJunichi Nomura r = 0; 192243e43c9eSJunichi Nomura } else { 192343e43c9eSJunichi Nomura /* pg_init has not started or completed */ 19246c182cd8SHannes Reinecke r = -ENOTCONN; 192543e43c9eSJunichi Nomura } 192643e43c9eSJunichi Nomura } else { 192743e43c9eSJunichi Nomura /* No path is available */ 1928518257b1SMike Snitzer if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) 192943e43c9eSJunichi Nomura r = -ENOTCONN; 193043e43c9eSJunichi Nomura else 19319af4aa30SMilan Broz r = -EIO; 193243e43c9eSJunichi Nomura } 19339af4aa30SMilan Broz 19345bbbfdf6SJunichi Nomura if (r == -ENOTCONN) { 19352da1610aSMike Snitzer if (!lockless_dereference(m->current_pg)) { 19363e9f1be1SHannes Reinecke /* Path status changed, redo selection */ 19372da1610aSMike Snitzer (void) choose_pgpath(m, 0); 19383e9f1be1SHannes Reinecke } 1939518257b1SMike Snitzer if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) 19402da1610aSMike Snitzer pg_init_all_paths(m); 194163d832c3SHannes Reinecke dm_table_run_md_queue_async(m->ti->table); 1942*7e48c768SMike Snitzer process_queued_io_list(m); 19433e9f1be1SHannes Reinecke } 194435991652SMikulas Patocka 1945e56f81e0SChristoph Hellwig /* 1946e56f81e0SChristoph Hellwig * Only pass ioctls through if the device sizes match exactly. 1947e56f81e0SChristoph Hellwig */ 1948e56f81e0SChristoph Hellwig if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT) 1949e56f81e0SChristoph Hellwig return 1; 1950e56f81e0SChristoph Hellwig return r; 19519af4aa30SMilan Broz } 19529af4aa30SMilan Broz 1953af4874e0SMike Snitzer static int multipath_iterate_devices(struct dm_target *ti, 1954af4874e0SMike Snitzer iterate_devices_callout_fn fn, void *data) 1955af4874e0SMike Snitzer { 1956af4874e0SMike Snitzer struct multipath *m = ti->private; 1957af4874e0SMike Snitzer struct priority_group *pg; 1958af4874e0SMike Snitzer struct pgpath *p; 1959af4874e0SMike Snitzer int ret = 0; 1960af4874e0SMike Snitzer 1961af4874e0SMike Snitzer list_for_each_entry(pg, &m->priority_groups, list) { 1962af4874e0SMike Snitzer list_for_each_entry(p, &pg->pgpaths, list) { 19635dea271bSMike Snitzer ret = fn(ti, p->path.dev, ti->begin, ti->len, data); 1964af4874e0SMike Snitzer if (ret) 1965af4874e0SMike Snitzer goto out; 1966af4874e0SMike Snitzer } 1967af4874e0SMike Snitzer } 1968af4874e0SMike Snitzer 1969af4874e0SMike Snitzer out: 1970af4874e0SMike Snitzer return ret; 1971af4874e0SMike Snitzer } 1972af4874e0SMike Snitzer 19739f54cec5SMike Snitzer static int pgpath_busy(struct pgpath *pgpath) 1974f40c67f0SKiyoshi Ueda { 1975f40c67f0SKiyoshi Ueda struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev); 1976f40c67f0SKiyoshi Ueda 197752b09914SMike Snitzer return blk_lld_busy(q); 1978f40c67f0SKiyoshi Ueda } 1979f40c67f0SKiyoshi Ueda 1980f40c67f0SKiyoshi Ueda /* 1981f40c67f0SKiyoshi Ueda * We return "busy", only when we can map I/Os but underlying devices 1982f40c67f0SKiyoshi Ueda * are busy (so even if we map I/Os now, the I/Os will wait on 1983f40c67f0SKiyoshi Ueda * the underlying queue). 1984f40c67f0SKiyoshi Ueda * In other words, if we want to kill I/Os or queue them inside us 1985f40c67f0SKiyoshi Ueda * due to map unavailability, we don't return "busy". Otherwise, 1986f40c67f0SKiyoshi Ueda * dm core won't give us the I/Os and we can't do what we want. 1987f40c67f0SKiyoshi Ueda */ 1988f40c67f0SKiyoshi Ueda static int multipath_busy(struct dm_target *ti) 1989f40c67f0SKiyoshi Ueda { 1990be7d31ccSMike Snitzer bool busy = false, has_active = false; 1991f40c67f0SKiyoshi Ueda struct multipath *m = ti->private; 19922da1610aSMike Snitzer struct priority_group *pg, *next_pg; 1993f40c67f0SKiyoshi Ueda struct pgpath *pgpath; 1994f40c67f0SKiyoshi Ueda 19957a7a3b45SJun'ichi Nomura /* pg_init in progress or no paths available */ 199691e968aaSMike Snitzer if (atomic_read(&m->pg_init_in_progress) || 19972da1610aSMike Snitzer (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) 19982da1610aSMike Snitzer return true; 19992da1610aSMike Snitzer 2000f40c67f0SKiyoshi Ueda /* Guess which priority_group will be used at next mapping time */ 20012da1610aSMike Snitzer pg = lockless_dereference(m->current_pg); 20022da1610aSMike Snitzer next_pg = lockless_dereference(m->next_pg); 20032da1610aSMike Snitzer if (unlikely(!lockless_dereference(m->current_pgpath) && next_pg)) 20042da1610aSMike Snitzer pg = next_pg; 20052da1610aSMike Snitzer 20062da1610aSMike Snitzer if (!pg) { 2007f40c67f0SKiyoshi Ueda /* 2008f40c67f0SKiyoshi Ueda * We don't know which pg will be used at next mapping time. 20092da1610aSMike Snitzer * We don't call choose_pgpath() here to avoid to trigger 2010f40c67f0SKiyoshi Ueda * pg_init just by busy checking. 2011f40c67f0SKiyoshi Ueda * So we don't know whether underlying devices we will be using 2012f40c67f0SKiyoshi Ueda * at next mapping time are busy or not. Just try mapping. 2013f40c67f0SKiyoshi Ueda */ 20142da1610aSMike Snitzer return busy; 20152da1610aSMike Snitzer } 2016f40c67f0SKiyoshi Ueda 2017f40c67f0SKiyoshi Ueda /* 2018f40c67f0SKiyoshi Ueda * If there is one non-busy active path at least, the path selector 2019f40c67f0SKiyoshi Ueda * will be able to select it. So we consider such a pg as not busy. 2020f40c67f0SKiyoshi Ueda */ 2021be7d31ccSMike Snitzer busy = true; 20222da1610aSMike Snitzer list_for_each_entry(pgpath, &pg->pgpaths, list) { 2023f40c67f0SKiyoshi Ueda if (pgpath->is_active) { 2024be7d31ccSMike Snitzer has_active = true; 20259f54cec5SMike Snitzer if (!pgpath_busy(pgpath)) { 2026be7d31ccSMike Snitzer busy = false; 2027f40c67f0SKiyoshi Ueda break; 2028f40c67f0SKiyoshi Ueda } 2029f40c67f0SKiyoshi Ueda } 20302da1610aSMike Snitzer } 2031f40c67f0SKiyoshi Ueda 20322da1610aSMike Snitzer if (!has_active) { 2033f40c67f0SKiyoshi Ueda /* 2034f40c67f0SKiyoshi Ueda * No active path in this pg, so this pg won't be used and 2035f40c67f0SKiyoshi Ueda * the current_pg will be changed at next mapping time. 2036f40c67f0SKiyoshi Ueda * We need to try mapping to determine it. 2037f40c67f0SKiyoshi Ueda */ 2038be7d31ccSMike Snitzer busy = false; 20392da1610aSMike Snitzer } 2040f40c67f0SKiyoshi Ueda 2041f40c67f0SKiyoshi Ueda return busy; 2042f40c67f0SKiyoshi Ueda } 2043f40c67f0SKiyoshi Ueda 20441da177e4SLinus Torvalds /*----------------------------------------------------------------- 20451da177e4SLinus Torvalds * Module setup 20461da177e4SLinus Torvalds *---------------------------------------------------------------*/ 20471da177e4SLinus Torvalds static struct target_type multipath_target = { 20481da177e4SLinus Torvalds .name = "multipath", 2049e83068a5SMike Snitzer .version = {1, 12, 0}, 205016f12266SMike Snitzer .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE, 20511da177e4SLinus Torvalds .module = THIS_MODULE, 20521da177e4SLinus Torvalds .ctr = multipath_ctr, 20531da177e4SLinus Torvalds .dtr = multipath_dtr, 2054f40c67f0SKiyoshi Ueda .map_rq = multipath_map, 2055e5863d9aSMike Snitzer .clone_and_map_rq = multipath_clone_and_map, 2056e5863d9aSMike Snitzer .release_clone_rq = multipath_release_clone, 2057f40c67f0SKiyoshi Ueda .rq_end_io = multipath_end_io, 205876e33fe4SMike Snitzer .map = multipath_map_bio, 205976e33fe4SMike Snitzer .end_io = multipath_end_io_bio, 206076e33fe4SMike Snitzer .presuspend = multipath_presuspend, 206176e33fe4SMike Snitzer .postsuspend = multipath_postsuspend, 206276e33fe4SMike Snitzer .resume = multipath_resume, 206376e33fe4SMike Snitzer .status = multipath_status, 206476e33fe4SMike Snitzer .message = multipath_message, 206576e33fe4SMike Snitzer .prepare_ioctl = multipath_prepare_ioctl, 206676e33fe4SMike Snitzer .iterate_devices = multipath_iterate_devices, 206776e33fe4SMike Snitzer .busy = multipath_busy, 206876e33fe4SMike Snitzer }; 206976e33fe4SMike Snitzer 20701da177e4SLinus Torvalds static int __init dm_multipath_init(void) 20711da177e4SLinus Torvalds { 20721da177e4SLinus Torvalds int r; 20731da177e4SLinus Torvalds 207476e33fe4SMike Snitzer /* allocate a slab for the dm_mpath_ios */ 2075028867acSAlasdair G Kergon _mpio_cache = KMEM_CACHE(dm_mpath_io, 0); 20761da177e4SLinus Torvalds if (!_mpio_cache) 20771da177e4SLinus Torvalds return -ENOMEM; 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds r = dm_register_target(&multipath_target); 20801da177e4SLinus Torvalds if (r < 0) { 208176e33fe4SMike Snitzer DMERR("request-based register failed %d", r); 2082ff658e9cSJohannes Thumshirn r = -EINVAL; 2083ff658e9cSJohannes Thumshirn goto bad_register_target; 20841da177e4SLinus Torvalds } 20851da177e4SLinus Torvalds 20864d4d66abSTejun Heo kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0); 2087c557308eSAlasdair G Kergon if (!kmultipathd) { 20880cd33124SAlasdair G Kergon DMERR("failed to create workqueue kmpathd"); 2089ff658e9cSJohannes Thumshirn r = -ENOMEM; 2090ff658e9cSJohannes Thumshirn goto bad_alloc_kmultipathd; 2091c557308eSAlasdair G Kergon } 2092c557308eSAlasdair G Kergon 2093bab7cfc7SChandra Seetharaman /* 2094bab7cfc7SChandra Seetharaman * A separate workqueue is used to handle the device handlers 2095bab7cfc7SChandra Seetharaman * to avoid overloading existing workqueue. Overloading the 2096bab7cfc7SChandra Seetharaman * old workqueue would also create a bottleneck in the 2097bab7cfc7SChandra Seetharaman * path of the storage hardware device activation. 2098bab7cfc7SChandra Seetharaman */ 20994d4d66abSTejun Heo kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd", 21004d4d66abSTejun Heo WQ_MEM_RECLAIM); 2101bab7cfc7SChandra Seetharaman if (!kmpath_handlerd) { 2102bab7cfc7SChandra Seetharaman DMERR("failed to create workqueue kmpath_handlerd"); 2103ff658e9cSJohannes Thumshirn r = -ENOMEM; 2104ff658e9cSJohannes Thumshirn goto bad_alloc_kmpath_handlerd; 2105bab7cfc7SChandra Seetharaman } 2106bab7cfc7SChandra Seetharaman 2107ff658e9cSJohannes Thumshirn return 0; 2108ff658e9cSJohannes Thumshirn 2109ff658e9cSJohannes Thumshirn bad_alloc_kmpath_handlerd: 2110ff658e9cSJohannes Thumshirn destroy_workqueue(kmultipathd); 2111ff658e9cSJohannes Thumshirn bad_alloc_kmultipathd: 2112ff658e9cSJohannes Thumshirn dm_unregister_target(&multipath_target); 2113ff658e9cSJohannes Thumshirn bad_register_target: 2114ff658e9cSJohannes Thumshirn kmem_cache_destroy(_mpio_cache); 2115ff658e9cSJohannes Thumshirn 21161da177e4SLinus Torvalds return r; 21171da177e4SLinus Torvalds } 21181da177e4SLinus Torvalds 21191da177e4SLinus Torvalds static void __exit dm_multipath_exit(void) 21201da177e4SLinus Torvalds { 2121bab7cfc7SChandra Seetharaman destroy_workqueue(kmpath_handlerd); 2122c557308eSAlasdair G Kergon destroy_workqueue(kmultipathd); 2123c557308eSAlasdair G Kergon 212410d3bd09SMikulas Patocka dm_unregister_target(&multipath_target); 21251da177e4SLinus Torvalds kmem_cache_destroy(_mpio_cache); 21261da177e4SLinus Torvalds } 21271da177e4SLinus Torvalds 21281da177e4SLinus Torvalds module_init(dm_multipath_init); 21291da177e4SLinus Torvalds module_exit(dm_multipath_exit); 21301da177e4SLinus Torvalds 21311da177e4SLinus Torvalds MODULE_DESCRIPTION(DM_NAME " multipath target"); 21321da177e4SLinus Torvalds MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>"); 21331da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 2134