dm-mpath.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142
  1. /*
  2. * Copyright (C) 2003 Sistina Software Limited.
  3. * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include <linux/device-mapper.h>
  8. #include "dm-rq.h"
  9. #include "dm-bio-record.h"
  10. #include "dm-path-selector.h"
  11. #include "dm-uevent.h"
  12. #include <linux/blkdev.h>
  13. #include <linux/ctype.h>
  14. #include <linux/init.h>
  15. #include <linux/mempool.h>
  16. #include <linux/module.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/slab.h>
  19. #include <linux/time.h>
  20. #include <linux/workqueue.h>
  21. #include <linux/delay.h>
  22. #include <scsi/scsi_dh.h>
  23. #include <linux/atomic.h>
  24. #include <linux/blk-mq.h>
  25. #define DM_MSG_PREFIX "multipath"
  26. #define DM_PG_INIT_DELAY_MSECS 2000
  27. #define DM_PG_INIT_DELAY_DEFAULT ((unsigned) -1)
  28. /* Path properties */
  29. struct pgpath {
  30. struct list_head list;
  31. struct priority_group *pg; /* Owning PG */
  32. unsigned fail_count; /* Cumulative failure count */
  33. struct dm_path path;
  34. struct delayed_work activate_path;
  35. bool is_active:1; /* Path status */
  36. };
  37. #define path_to_pgpath(__pgp) container_of((__pgp), struct pgpath, path)
  38. /*
  39. * Paths are grouped into Priority Groups and numbered from 1 upwards.
  40. * Each has a path selector which controls which path gets used.
  41. */
  42. struct priority_group {
  43. struct list_head list;
  44. struct multipath *m; /* Owning multipath instance */
  45. struct path_selector ps;
  46. unsigned pg_num; /* Reference number */
  47. unsigned nr_pgpaths; /* Number of paths in PG */
  48. struct list_head pgpaths;
  49. bool bypassed:1; /* Temporarily bypass this PG? */
  50. };
  51. /* Multipath context */
  52. struct multipath {
  53. struct list_head list;
  54. struct dm_target *ti;
  55. const char *hw_handler_name;
  56. char *hw_handler_params;
  57. spinlock_t lock;
  58. unsigned nr_priority_groups;
  59. struct list_head priority_groups;
  60. wait_queue_head_t pg_init_wait; /* Wait for pg_init completion */
  61. struct pgpath *current_pgpath;
  62. struct priority_group *current_pg;
  63. struct priority_group *next_pg; /* Switch to this PG if set */
  64. unsigned long flags; /* Multipath state flags */
  65. unsigned pg_init_retries; /* Number of times to retry pg_init */
  66. unsigned pg_init_delay_msecs; /* Number of msecs before pg_init retry */
  67. atomic_t nr_valid_paths; /* Total number of usable paths */
  68. atomic_t pg_init_in_progress; /* Only one pg_init allowed at once */
  69. atomic_t pg_init_count; /* Number of times pg_init called */
  70. unsigned queue_mode;
  71. /*
  72. * We must use a mempool of dm_mpath_io structs so that we
  73. * can resubmit bios on error.
  74. */
  75. mempool_t *mpio_pool;
  76. struct mutex work_mutex;
  77. struct work_struct trigger_event;
  78. struct work_struct process_queued_bios;
  79. struct bio_list queued_bios;
  80. };
  81. /*
  82. * Context information attached to each io we process.
  83. */
  84. struct dm_mpath_io {
  85. struct pgpath *pgpath;
  86. size_t nr_bytes;
  87. };
  88. typedef int (*action_fn) (struct pgpath *pgpath);
  89. static struct kmem_cache *_mpio_cache;
  90. static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
  91. static void trigger_event(struct work_struct *work);
  92. static void activate_path(struct work_struct *work);
  93. static void process_queued_bios(struct work_struct *work);
  94. /*-----------------------------------------------
  95. * Multipath state flags.
  96. *-----------------------------------------------*/
  97. #define MPATHF_QUEUE_IO 0 /* Must we queue all I/O? */
  98. #define MPATHF_QUEUE_IF_NO_PATH 1 /* Queue I/O if last path fails? */
  99. #define MPATHF_SAVED_QUEUE_IF_NO_PATH 2 /* Saved state during suspension */
  100. #define MPATHF_RETAIN_ATTACHED_HW_HANDLER 3 /* If there's already a hw_handler present, don't change it. */
  101. #define MPATHF_PG_INIT_DISABLED 4 /* pg_init is not currently allowed */
  102. #define MPATHF_PG_INIT_REQUIRED 5 /* pg_init needs calling? */
  103. #define MPATHF_PG_INIT_DELAY_RETRY 6 /* Delay pg_init retry? */
  104. /*-----------------------------------------------
  105. * Allocation routines
  106. *-----------------------------------------------*/
  107. static struct pgpath *alloc_pgpath(void)
  108. {
  109. struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
  110. if (pgpath) {
  111. pgpath->is_active = true;
  112. INIT_DELAYED_WORK(&pgpath->activate_path, activate_path);
  113. }
  114. return pgpath;
  115. }
  116. static void free_pgpath(struct pgpath *pgpath)
  117. {
  118. kfree(pgpath);
  119. }
  120. static struct priority_group *alloc_priority_group(void)
  121. {
  122. struct priority_group *pg;
  123. pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  124. if (pg)
  125. INIT_LIST_HEAD(&pg->pgpaths);
  126. return pg;
  127. }
  128. static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
  129. {
  130. struct pgpath *pgpath, *tmp;
  131. list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
  132. list_del(&pgpath->list);
  133. dm_put_device(ti, pgpath->path.dev);
  134. free_pgpath(pgpath);
  135. }
  136. }
  137. static void free_priority_group(struct priority_group *pg,
  138. struct dm_target *ti)
  139. {
  140. struct path_selector *ps = &pg->ps;
  141. if (ps->type) {
  142. ps->type->destroy(ps);
  143. dm_put_path_selector(ps->type);
  144. }
  145. free_pgpaths(&pg->pgpaths, ti);
  146. kfree(pg);
  147. }
  148. static struct multipath *alloc_multipath(struct dm_target *ti)
  149. {
  150. struct multipath *m;
  151. m = kzalloc(sizeof(*m), GFP_KERNEL);
  152. if (m) {
  153. INIT_LIST_HEAD(&m->priority_groups);
  154. spin_lock_init(&m->lock);
  155. set_bit(MPATHF_QUEUE_IO, &m->flags);
  156. atomic_set(&m->nr_valid_paths, 0);
  157. atomic_set(&m->pg_init_in_progress, 0);
  158. atomic_set(&m->pg_init_count, 0);
  159. m->pg_init_delay_msecs = DM_PG_INIT_DELAY_DEFAULT;
  160. INIT_WORK(&m->trigger_event, trigger_event);
  161. init_waitqueue_head(&m->pg_init_wait);
  162. mutex_init(&m->work_mutex);
  163. m->mpio_pool = NULL;
  164. m->queue_mode = DM_TYPE_NONE;
  165. m->ti = ti;
  166. ti->private = m;
  167. }
  168. return m;
  169. }
  170. static int alloc_multipath_stage2(struct dm_target *ti, struct multipath *m)
  171. {
  172. if (m->queue_mode == DM_TYPE_NONE) {
  173. /*
  174. * Default to request-based.
  175. */
  176. if (dm_use_blk_mq(dm_table_get_md(ti->table)))
  177. m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
  178. else
  179. m->queue_mode = DM_TYPE_REQUEST_BASED;
  180. }
  181. if (m->queue_mode == DM_TYPE_REQUEST_BASED) {
  182. unsigned min_ios = dm_get_reserved_rq_based_ios();
  183. m->mpio_pool = mempool_create_slab_pool(min_ios, _mpio_cache);
  184. if (!m->mpio_pool)
  185. return -ENOMEM;
  186. }
  187. else if (m->queue_mode == DM_TYPE_BIO_BASED) {
  188. INIT_WORK(&m->process_queued_bios, process_queued_bios);
  189. /*
  190. * bio-based doesn't support any direct scsi_dh management;
  191. * it just discovers if a scsi_dh is attached.
  192. */
  193. set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
  194. }
  195. dm_table_set_type(ti->table, m->queue_mode);
  196. return 0;
  197. }
  198. static void free_multipath(struct multipath *m)
  199. {
  200. struct priority_group *pg, *tmp;
  201. list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
  202. list_del(&pg->list);
  203. free_priority_group(pg, m->ti);
  204. }
  205. kfree(m->hw_handler_name);
  206. kfree(m->hw_handler_params);
  207. mempool_destroy(m->mpio_pool);
  208. kfree(m);
  209. }
  210. static struct dm_mpath_io *get_mpio(union map_info *info)
  211. {
  212. return info->ptr;
  213. }
  214. static struct dm_mpath_io *set_mpio(struct multipath *m, union map_info *info)
  215. {
  216. struct dm_mpath_io *mpio;
  217. if (!m->mpio_pool) {
  218. /* Use blk-mq pdu memory requested via per_io_data_size */
  219. mpio = get_mpio(info);
  220. memset(mpio, 0, sizeof(*mpio));
  221. return mpio;
  222. }
  223. mpio = mempool_alloc(m->mpio_pool, GFP_ATOMIC);
  224. if (!mpio)
  225. return NULL;
  226. memset(mpio, 0, sizeof(*mpio));
  227. info->ptr = mpio;
  228. return mpio;
  229. }
  230. static void clear_request_fn_mpio(struct multipath *m, union map_info *info)
  231. {
  232. /* Only needed for non blk-mq (.request_fn) multipath */
  233. if (m->mpio_pool) {
  234. struct dm_mpath_io *mpio = info->ptr;
  235. info->ptr = NULL;
  236. mempool_free(mpio, m->mpio_pool);
  237. }
  238. }
  239. static size_t multipath_per_bio_data_size(void)
  240. {
  241. return sizeof(struct dm_mpath_io) + sizeof(struct dm_bio_details);
  242. }
  243. static struct dm_mpath_io *get_mpio_from_bio(struct bio *bio)
  244. {
  245. return dm_per_bio_data(bio, multipath_per_bio_data_size());
  246. }
  247. static struct dm_bio_details *get_bio_details_from_bio(struct bio *bio)
  248. {
  249. /* dm_bio_details is immediately after the dm_mpath_io in bio's per-bio-data */
  250. struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
  251. void *bio_details = mpio + 1;
  252. return bio_details;
  253. }
  254. static void multipath_init_per_bio_data(struct bio *bio, struct dm_mpath_io **mpio_p,
  255. struct dm_bio_details **bio_details_p)
  256. {
  257. struct dm_mpath_io *mpio = get_mpio_from_bio(bio);
  258. struct dm_bio_details *bio_details = get_bio_details_from_bio(bio);
  259. memset(mpio, 0, sizeof(*mpio));
  260. memset(bio_details, 0, sizeof(*bio_details));
  261. dm_bio_record(bio_details, bio);
  262. if (mpio_p)
  263. *mpio_p = mpio;
  264. if (bio_details_p)
  265. *bio_details_p = bio_details;
  266. }
  267. /*-----------------------------------------------
  268. * Path selection
  269. *-----------------------------------------------*/
  270. static int __pg_init_all_paths(struct multipath *m)
  271. {
  272. struct pgpath *pgpath;
  273. unsigned long pg_init_delay = 0;
  274. if (atomic_read(&m->pg_init_in_progress) || test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
  275. return 0;
  276. atomic_inc(&m->pg_init_count);
  277. clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
  278. /* Check here to reset pg_init_required */
  279. if (!m->current_pg)
  280. return 0;
  281. if (test_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags))
  282. pg_init_delay = msecs_to_jiffies(m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT ?
  283. m->pg_init_delay_msecs : DM_PG_INIT_DELAY_MSECS);
  284. list_for_each_entry(pgpath, &m->current_pg->pgpaths, list) {
  285. /* Skip failed paths */
  286. if (!pgpath->is_active)
  287. continue;
  288. if (queue_delayed_work(kmpath_handlerd, &pgpath->activate_path,
  289. pg_init_delay))
  290. atomic_inc(&m->pg_init_in_progress);
  291. }
  292. return atomic_read(&m->pg_init_in_progress);
  293. }
  294. static void pg_init_all_paths(struct multipath *m)
  295. {
  296. unsigned long flags;
  297. spin_lock_irqsave(&m->lock, flags);
  298. __pg_init_all_paths(m);
  299. spin_unlock_irqrestore(&m->lock, flags);
  300. }
  301. static void __switch_pg(struct multipath *m, struct priority_group *pg)
  302. {
  303. m->current_pg = pg;
  304. /* Must we initialise the PG first, and queue I/O till it's ready? */
  305. if (m->hw_handler_name) {
  306. set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
  307. set_bit(MPATHF_QUEUE_IO, &m->flags);
  308. } else {
  309. clear_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
  310. clear_bit(MPATHF_QUEUE_IO, &m->flags);
  311. }
  312. atomic_set(&m->pg_init_count, 0);
  313. }
  314. static struct pgpath *choose_path_in_pg(struct multipath *m,
  315. struct priority_group *pg,
  316. size_t nr_bytes)
  317. {
  318. unsigned long flags;
  319. struct dm_path *path;
  320. struct pgpath *pgpath;
  321. path = pg->ps.type->select_path(&pg->ps, nr_bytes);
  322. if (!path)
  323. return ERR_PTR(-ENXIO);
  324. pgpath = path_to_pgpath(path);
  325. if (unlikely(lockless_dereference(m->current_pg) != pg)) {
  326. /* Only update current_pgpath if pg changed */
  327. spin_lock_irqsave(&m->lock, flags);
  328. m->current_pgpath = pgpath;
  329. __switch_pg(m, pg);
  330. spin_unlock_irqrestore(&m->lock, flags);
  331. }
  332. return pgpath;
  333. }
  334. static struct pgpath *choose_pgpath(struct multipath *m, size_t nr_bytes)
  335. {
  336. unsigned long flags;
  337. struct priority_group *pg;
  338. struct pgpath *pgpath;
  339. bool bypassed = true;
  340. if (!atomic_read(&m->nr_valid_paths)) {
  341. clear_bit(MPATHF_QUEUE_IO, &m->flags);
  342. goto failed;
  343. }
  344. /* Were we instructed to switch PG? */
  345. if (lockless_dereference(m->next_pg)) {
  346. spin_lock_irqsave(&m->lock, flags);
  347. pg = m->next_pg;
  348. if (!pg) {
  349. spin_unlock_irqrestore(&m->lock, flags);
  350. goto check_current_pg;
  351. }
  352. m->next_pg = NULL;
  353. spin_unlock_irqrestore(&m->lock, flags);
  354. pgpath = choose_path_in_pg(m, pg, nr_bytes);
  355. if (!IS_ERR_OR_NULL(pgpath))
  356. return pgpath;
  357. }
  358. /* Don't change PG until it has no remaining paths */
  359. check_current_pg:
  360. pg = lockless_dereference(m->current_pg);
  361. if (pg) {
  362. pgpath = choose_path_in_pg(m, pg, nr_bytes);
  363. if (!IS_ERR_OR_NULL(pgpath))
  364. return pgpath;
  365. }
  366. /*
  367. * Loop through priority groups until we find a valid path.
  368. * First time we skip PGs marked 'bypassed'.
  369. * Second time we only try the ones we skipped, but set
  370. * pg_init_delay_retry so we do not hammer controllers.
  371. */
  372. do {
  373. list_for_each_entry(pg, &m->priority_groups, list) {
  374. if (pg->bypassed == bypassed)
  375. continue;
  376. pgpath = choose_path_in_pg(m, pg, nr_bytes);
  377. if (!IS_ERR_OR_NULL(pgpath)) {
  378. if (!bypassed)
  379. set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
  380. return pgpath;
  381. }
  382. }
  383. } while (bypassed--);
  384. failed:
  385. spin_lock_irqsave(&m->lock, flags);
  386. m->current_pgpath = NULL;
  387. m->current_pg = NULL;
  388. spin_unlock_irqrestore(&m->lock, flags);
  389. return NULL;
  390. }
  391. /*
  392. * Check whether bios must be queued in the device-mapper core rather
  393. * than here in the target.
  394. *
  395. * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
  396. * same value then we are not between multipath_presuspend()
  397. * and multipath_resume() calls and we have no need to check
  398. * for the DMF_NOFLUSH_SUSPENDING flag.
  399. */
  400. static bool __must_push_back(struct multipath *m)
  401. {
  402. return ((test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) !=
  403. test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags)) &&
  404. dm_noflush_suspending(m->ti));
  405. }
  406. static bool must_push_back_rq(struct multipath *m)
  407. {
  408. bool r;
  409. unsigned long flags;
  410. spin_lock_irqsave(&m->lock, flags);
  411. r = (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) ||
  412. __must_push_back(m));
  413. spin_unlock_irqrestore(&m->lock, flags);
  414. return r;
  415. }
  416. static bool must_push_back_bio(struct multipath *m)
  417. {
  418. bool r;
  419. unsigned long flags;
  420. spin_lock_irqsave(&m->lock, flags);
  421. r = __must_push_back(m);
  422. spin_unlock_irqrestore(&m->lock, flags);
  423. return r;
  424. }
  425. /*
  426. * Map cloned requests (request-based multipath)
  427. */
  428. static int __multipath_map(struct dm_target *ti, struct request *clone,
  429. union map_info *map_context,
  430. struct request *rq, struct request **__clone)
  431. {
  432. struct multipath *m = ti->private;
  433. int r = DM_MAPIO_REQUEUE;
  434. size_t nr_bytes = clone ? blk_rq_bytes(clone) : blk_rq_bytes(rq);
  435. struct pgpath *pgpath;
  436. struct block_device *bdev;
  437. struct dm_mpath_io *mpio;
  438. /* Do we need to select a new pgpath? */
  439. pgpath = lockless_dereference(m->current_pgpath);
  440. if (!pgpath || !test_bit(MPATHF_QUEUE_IO, &m->flags))
  441. pgpath = choose_pgpath(m, nr_bytes);
  442. if (!pgpath) {
  443. if (must_push_back_rq(m))
  444. return DM_MAPIO_DELAY_REQUEUE;
  445. return -EIO; /* Failed */
  446. } else if (test_bit(MPATHF_QUEUE_IO, &m->flags) ||
  447. test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
  448. pg_init_all_paths(m);
  449. return r;
  450. }
  451. mpio = set_mpio(m, map_context);
  452. if (!mpio)
  453. /* ENOMEM, requeue */
  454. return r;
  455. mpio->pgpath = pgpath;
  456. mpio->nr_bytes = nr_bytes;
  457. bdev = pgpath->path.dev->bdev;
  458. if (clone) {
  459. /*
  460. * Old request-based interface: allocated clone is passed in.
  461. * Used by: .request_fn stacked on .request_fn path(s).
  462. */
  463. clone->q = bdev_get_queue(bdev);
  464. clone->rq_disk = bdev->bd_disk;
  465. clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
  466. } else {
  467. /*
  468. * blk-mq request-based interface; used by both:
  469. * .request_fn stacked on blk-mq path(s) and
  470. * blk-mq stacked on blk-mq path(s).
  471. */
  472. clone = blk_mq_alloc_request(bdev_get_queue(bdev),
  473. rq_data_dir(rq), BLK_MQ_REQ_NOWAIT);
  474. if (IS_ERR(clone)) {
  475. /* EBUSY, ENODEV or EWOULDBLOCK: requeue */
  476. clear_request_fn_mpio(m, map_context);
  477. return r;
  478. }
  479. clone->bio = clone->biotail = NULL;
  480. clone->rq_disk = bdev->bd_disk;
  481. clone->cmd_flags |= REQ_FAILFAST_TRANSPORT;
  482. *__clone = clone;
  483. }
  484. if (pgpath->pg->ps.type->start_io)
  485. pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
  486. &pgpath->path,
  487. nr_bytes);
  488. return DM_MAPIO_REMAPPED;
  489. }
  490. static int multipath_map(struct dm_target *ti, struct request *clone,
  491. union map_info *map_context)
  492. {
  493. return __multipath_map(ti, clone, map_context, NULL, NULL);
  494. }
  495. static int multipath_clone_and_map(struct dm_target *ti, struct request *rq,
  496. union map_info *map_context,
  497. struct request **clone)
  498. {
  499. return __multipath_map(ti, NULL, map_context, rq, clone);
  500. }
  501. static void multipath_release_clone(struct request *clone)
  502. {
  503. blk_mq_free_request(clone);
  504. }
  505. /*
  506. * Map cloned bios (bio-based multipath)
  507. */
  508. static int __multipath_map_bio(struct multipath *m, struct bio *bio, struct dm_mpath_io *mpio)
  509. {
  510. size_t nr_bytes = bio->bi_iter.bi_size;
  511. struct pgpath *pgpath;
  512. unsigned long flags;
  513. bool queue_io;
  514. /* Do we need to select a new pgpath? */
  515. pgpath = lockless_dereference(m->current_pgpath);
  516. queue_io = test_bit(MPATHF_QUEUE_IO, &m->flags);
  517. if (!pgpath || !queue_io)
  518. pgpath = choose_pgpath(m, nr_bytes);
  519. if ((pgpath && queue_io) ||
  520. (!pgpath && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))) {
  521. /* Queue for the daemon to resubmit */
  522. spin_lock_irqsave(&m->lock, flags);
  523. bio_list_add(&m->queued_bios, bio);
  524. spin_unlock_irqrestore(&m->lock, flags);
  525. /* PG_INIT_REQUIRED cannot be set without QUEUE_IO */
  526. if (queue_io || test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
  527. pg_init_all_paths(m);
  528. else if (!queue_io)
  529. queue_work(kmultipathd, &m->process_queued_bios);
  530. return DM_MAPIO_SUBMITTED;
  531. }
  532. if (!pgpath) {
  533. if (!must_push_back_bio(m))
  534. return -EIO;
  535. return DM_MAPIO_REQUEUE;
  536. }
  537. mpio->pgpath = pgpath;
  538. mpio->nr_bytes = nr_bytes;
  539. bio->bi_error = 0;
  540. bio->bi_bdev = pgpath->path.dev->bdev;
  541. bio->bi_opf |= REQ_FAILFAST_TRANSPORT;
  542. if (pgpath->pg->ps.type->start_io)
  543. pgpath->pg->ps.type->start_io(&pgpath->pg->ps,
  544. &pgpath->path,
  545. nr_bytes);
  546. return DM_MAPIO_REMAPPED;
  547. }
  548. static int multipath_map_bio(struct dm_target *ti, struct bio *bio)
  549. {
  550. struct multipath *m = ti->private;
  551. struct dm_mpath_io *mpio = NULL;
  552. multipath_init_per_bio_data(bio, &mpio, NULL);
  553. return __multipath_map_bio(m, bio, mpio);
  554. }
  555. static void process_queued_io_list(struct multipath *m)
  556. {
  557. if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED)
  558. dm_mq_kick_requeue_list(dm_table_get_md(m->ti->table));
  559. else if (m->queue_mode == DM_TYPE_BIO_BASED)
  560. queue_work(kmultipathd, &m->process_queued_bios);
  561. }
  562. static void process_queued_bios(struct work_struct *work)
  563. {
  564. int r;
  565. unsigned long flags;
  566. struct bio *bio;
  567. struct bio_list bios;
  568. struct blk_plug plug;
  569. struct multipath *m =
  570. container_of(work, struct multipath, process_queued_bios);
  571. bio_list_init(&bios);
  572. spin_lock_irqsave(&m->lock, flags);
  573. if (bio_list_empty(&m->queued_bios)) {
  574. spin_unlock_irqrestore(&m->lock, flags);
  575. return;
  576. }
  577. bio_list_merge(&bios, &m->queued_bios);
  578. bio_list_init(&m->queued_bios);
  579. spin_unlock_irqrestore(&m->lock, flags);
  580. blk_start_plug(&plug);
  581. while ((bio = bio_list_pop(&bios))) {
  582. r = __multipath_map_bio(m, bio, get_mpio_from_bio(bio));
  583. if (r < 0 || r == DM_MAPIO_REQUEUE) {
  584. bio->bi_error = r;
  585. bio_endio(bio);
  586. } else if (r == DM_MAPIO_REMAPPED)
  587. generic_make_request(bio);
  588. }
  589. blk_finish_plug(&plug);
  590. }
  591. /*
  592. * If we run out of usable paths, should we queue I/O or error it?
  593. */
  594. static int queue_if_no_path(struct multipath *m, bool queue_if_no_path,
  595. bool save_old_value)
  596. {
  597. unsigned long flags;
  598. spin_lock_irqsave(&m->lock, flags);
  599. if (save_old_value) {
  600. if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
  601. set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
  602. else
  603. clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
  604. } else {
  605. if (queue_if_no_path)
  606. set_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
  607. else
  608. clear_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags);
  609. }
  610. if (queue_if_no_path)
  611. set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
  612. else
  613. clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
  614. spin_unlock_irqrestore(&m->lock, flags);
  615. if (!queue_if_no_path) {
  616. dm_table_run_md_queue_async(m->ti->table);
  617. process_queued_io_list(m);
  618. }
  619. return 0;
  620. }
  621. /*
  622. * An event is triggered whenever a path is taken out of use.
  623. * Includes path failure and PG bypass.
  624. */
  625. static void trigger_event(struct work_struct *work)
  626. {
  627. struct multipath *m =
  628. container_of(work, struct multipath, trigger_event);
  629. dm_table_event(m->ti->table);
  630. }
  631. /*-----------------------------------------------------------------
  632. * Constructor/argument parsing:
  633. * <#multipath feature args> [<arg>]*
  634. * <#hw_handler args> [hw_handler [<arg>]*]
  635. * <#priority groups>
  636. * <initial priority group>
  637. * [<selector> <#selector args> [<arg>]*
  638. * <#paths> <#per-path selector args>
  639. * [<path> [<arg>]* ]+ ]+
  640. *---------------------------------------------------------------*/
  641. static int parse_path_selector(struct dm_arg_set *as, struct priority_group *pg,
  642. struct dm_target *ti)
  643. {
  644. int r;
  645. struct path_selector_type *pst;
  646. unsigned ps_argc;
  647. static struct dm_arg _args[] = {
  648. {0, 1024, "invalid number of path selector args"},
  649. };
  650. pst = dm_get_path_selector(dm_shift_arg(as));
  651. if (!pst) {
  652. ti->error = "unknown path selector type";
  653. return -EINVAL;
  654. }
  655. r = dm_read_arg_group(_args, as, &ps_argc, &ti->error);
  656. if (r) {
  657. dm_put_path_selector(pst);
  658. return -EINVAL;
  659. }
  660. r = pst->create(&pg->ps, ps_argc, as->argv);
  661. if (r) {
  662. dm_put_path_selector(pst);
  663. ti->error = "path selector constructor failed";
  664. return r;
  665. }
  666. pg->ps.type = pst;
  667. dm_consume_args(as, ps_argc);
  668. return 0;
  669. }
  670. static struct pgpath *parse_path(struct dm_arg_set *as, struct path_selector *ps,
  671. struct dm_target *ti)
  672. {
  673. int r;
  674. struct pgpath *p;
  675. struct multipath *m = ti->private;
  676. struct request_queue *q = NULL;
  677. const char *attached_handler_name;
  678. /* we need at least a path arg */
  679. if (as->argc < 1) {
  680. ti->error = "no device given";
  681. return ERR_PTR(-EINVAL);
  682. }
  683. p = alloc_pgpath();
  684. if (!p)
  685. return ERR_PTR(-ENOMEM);
  686. r = dm_get_device(ti, dm_shift_arg(as), dm_table_get_mode(ti->table),
  687. &p->path.dev);
  688. if (r) {
  689. ti->error = "error getting device";
  690. goto bad;
  691. }
  692. if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) || m->hw_handler_name)
  693. q = bdev_get_queue(p->path.dev->bdev);
  694. if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags)) {
  695. retain:
  696. attached_handler_name = scsi_dh_attached_handler_name(q, GFP_KERNEL);
  697. if (attached_handler_name) {
  698. /*
  699. * Clear any hw_handler_params associated with a
  700. * handler that isn't already attached.
  701. */
  702. if (m->hw_handler_name && strcmp(attached_handler_name, m->hw_handler_name)) {
  703. kfree(m->hw_handler_params);
  704. m->hw_handler_params = NULL;
  705. }
  706. /*
  707. * Reset hw_handler_name to match the attached handler
  708. *
  709. * NB. This modifies the table line to show the actual
  710. * handler instead of the original table passed in.
  711. */
  712. kfree(m->hw_handler_name);
  713. m->hw_handler_name = attached_handler_name;
  714. }
  715. }
  716. if (m->hw_handler_name) {
  717. r = scsi_dh_attach(q, m->hw_handler_name);
  718. if (r == -EBUSY) {
  719. char b[BDEVNAME_SIZE];
  720. printk(KERN_INFO "dm-mpath: retaining handler on device %s\n",
  721. bdevname(p->path.dev->bdev, b));
  722. goto retain;
  723. }
  724. if (r < 0) {
  725. ti->error = "error attaching hardware handler";
  726. dm_put_device(ti, p->path.dev);
  727. goto bad;
  728. }
  729. if (m->hw_handler_params) {
  730. r = scsi_dh_set_params(q, m->hw_handler_params);
  731. if (r < 0) {
  732. ti->error = "unable to set hardware "
  733. "handler parameters";
  734. dm_put_device(ti, p->path.dev);
  735. goto bad;
  736. }
  737. }
  738. }
  739. r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
  740. if (r) {
  741. dm_put_device(ti, p->path.dev);
  742. goto bad;
  743. }
  744. return p;
  745. bad:
  746. free_pgpath(p);
  747. return ERR_PTR(r);
  748. }
  749. static struct priority_group *parse_priority_group(struct dm_arg_set *as,
  750. struct multipath *m)
  751. {
  752. static struct dm_arg _args[] = {
  753. {1, 1024, "invalid number of paths"},
  754. {0, 1024, "invalid number of selector args"}
  755. };
  756. int r;
  757. unsigned i, nr_selector_args, nr_args;
  758. struct priority_group *pg;
  759. struct dm_target *ti = m->ti;
  760. if (as->argc < 2) {
  761. as->argc = 0;
  762. ti->error = "not enough priority group arguments";
  763. return ERR_PTR(-EINVAL);
  764. }
  765. pg = alloc_priority_group();
  766. if (!pg) {
  767. ti->error = "couldn't allocate priority group";
  768. return ERR_PTR(-ENOMEM);
  769. }
  770. pg->m = m;
  771. r = parse_path_selector(as, pg, ti);
  772. if (r)
  773. goto bad;
  774. /*
  775. * read the paths
  776. */
  777. r = dm_read_arg(_args, as, &pg->nr_pgpaths, &ti->error);
  778. if (r)
  779. goto bad;
  780. r = dm_read_arg(_args + 1, as, &nr_selector_args, &ti->error);
  781. if (r)
  782. goto bad;
  783. nr_args = 1 + nr_selector_args;
  784. for (i = 0; i < pg->nr_pgpaths; i++) {
  785. struct pgpath *pgpath;
  786. struct dm_arg_set path_args;
  787. if (as->argc < nr_args) {
  788. ti->error = "not enough path parameters";
  789. r = -EINVAL;
  790. goto bad;
  791. }
  792. path_args.argc = nr_args;
  793. path_args.argv = as->argv;
  794. pgpath = parse_path(&path_args, &pg->ps, ti);
  795. if (IS_ERR(pgpath)) {
  796. r = PTR_ERR(pgpath);
  797. goto bad;
  798. }
  799. pgpath->pg = pg;
  800. list_add_tail(&pgpath->list, &pg->pgpaths);
  801. dm_consume_args(as, nr_args);
  802. }
  803. return pg;
  804. bad:
  805. free_priority_group(pg, ti);
  806. return ERR_PTR(r);
  807. }
  808. static int parse_hw_handler(struct dm_arg_set *as, struct multipath *m)
  809. {
  810. unsigned hw_argc;
  811. int ret;
  812. struct dm_target *ti = m->ti;
  813. static struct dm_arg _args[] = {
  814. {0, 1024, "invalid number of hardware handler args"},
  815. };
  816. if (dm_read_arg_group(_args, as, &hw_argc, &ti->error))
  817. return -EINVAL;
  818. if (!hw_argc)
  819. return 0;
  820. if (m->queue_mode == DM_TYPE_BIO_BASED) {
  821. dm_consume_args(as, hw_argc);
  822. DMERR("bio-based multipath doesn't allow hardware handler args");
  823. return 0;
  824. }
  825. m->hw_handler_name = kstrdup(dm_shift_arg(as), GFP_KERNEL);
  826. if (!m->hw_handler_name)
  827. return -EINVAL;
  828. if (hw_argc > 1) {
  829. char *p;
  830. int i, j, len = 4;
  831. for (i = 0; i <= hw_argc - 2; i++)
  832. len += strlen(as->argv[i]) + 1;
  833. p = m->hw_handler_params = kzalloc(len, GFP_KERNEL);
  834. if (!p) {
  835. ti->error = "memory allocation failed";
  836. ret = -ENOMEM;
  837. goto fail;
  838. }
  839. j = sprintf(p, "%d", hw_argc - 1);
  840. for (i = 0, p+=j+1; i <= hw_argc - 2; i++, p+=j+1)
  841. j = sprintf(p, "%s", as->argv[i]);
  842. }
  843. dm_consume_args(as, hw_argc - 1);
  844. return 0;
  845. fail:
  846. kfree(m->hw_handler_name);
  847. m->hw_handler_name = NULL;
  848. return ret;
  849. }
  850. static int parse_features(struct dm_arg_set *as, struct multipath *m)
  851. {
  852. int r;
  853. unsigned argc;
  854. struct dm_target *ti = m->ti;
  855. const char *arg_name;
  856. static struct dm_arg _args[] = {
  857. {0, 8, "invalid number of feature args"},
  858. {1, 50, "pg_init_retries must be between 1 and 50"},
  859. {0, 60000, "pg_init_delay_msecs must be between 0 and 60000"},
  860. };
  861. r = dm_read_arg_group(_args, as, &argc, &ti->error);
  862. if (r)
  863. return -EINVAL;
  864. if (!argc)
  865. return 0;
  866. do {
  867. arg_name = dm_shift_arg(as);
  868. argc--;
  869. if (!strcasecmp(arg_name, "queue_if_no_path")) {
  870. r = queue_if_no_path(m, true, false);
  871. continue;
  872. }
  873. if (!strcasecmp(arg_name, "retain_attached_hw_handler")) {
  874. set_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags);
  875. continue;
  876. }
  877. if (!strcasecmp(arg_name, "pg_init_retries") &&
  878. (argc >= 1)) {
  879. r = dm_read_arg(_args + 1, as, &m->pg_init_retries, &ti->error);
  880. argc--;
  881. continue;
  882. }
  883. if (!strcasecmp(arg_name, "pg_init_delay_msecs") &&
  884. (argc >= 1)) {
  885. r = dm_read_arg(_args + 2, as, &m->pg_init_delay_msecs, &ti->error);
  886. argc--;
  887. continue;
  888. }
  889. if (!strcasecmp(arg_name, "queue_mode") &&
  890. (argc >= 1)) {
  891. const char *queue_mode_name = dm_shift_arg(as);
  892. if (!strcasecmp(queue_mode_name, "bio"))
  893. m->queue_mode = DM_TYPE_BIO_BASED;
  894. else if (!strcasecmp(queue_mode_name, "rq"))
  895. m->queue_mode = DM_TYPE_REQUEST_BASED;
  896. else if (!strcasecmp(queue_mode_name, "mq"))
  897. m->queue_mode = DM_TYPE_MQ_REQUEST_BASED;
  898. else {
  899. ti->error = "Unknown 'queue_mode' requested";
  900. r = -EINVAL;
  901. }
  902. argc--;
  903. continue;
  904. }
  905. ti->error = "Unrecognised multipath feature request";
  906. r = -EINVAL;
  907. } while (argc && !r);
  908. return r;
  909. }
  910. static int multipath_ctr(struct dm_target *ti, unsigned argc, char **argv)
  911. {
  912. /* target arguments */
  913. static struct dm_arg _args[] = {
  914. {0, 1024, "invalid number of priority groups"},
  915. {0, 1024, "invalid initial priority group number"},
  916. };
  917. int r;
  918. struct multipath *m;
  919. struct dm_arg_set as;
  920. unsigned pg_count = 0;
  921. unsigned next_pg_num;
  922. as.argc = argc;
  923. as.argv = argv;
  924. m = alloc_multipath(ti);
  925. if (!m) {
  926. ti->error = "can't allocate multipath";
  927. return -EINVAL;
  928. }
  929. r = parse_features(&as, m);
  930. if (r)
  931. goto bad;
  932. r = alloc_multipath_stage2(ti, m);
  933. if (r)
  934. goto bad;
  935. r = parse_hw_handler(&as, m);
  936. if (r)
  937. goto bad;
  938. r = dm_read_arg(_args, &as, &m->nr_priority_groups, &ti->error);
  939. if (r)
  940. goto bad;
  941. r = dm_read_arg(_args + 1, &as, &next_pg_num, &ti->error);
  942. if (r)
  943. goto bad;
  944. if ((!m->nr_priority_groups && next_pg_num) ||
  945. (m->nr_priority_groups && !next_pg_num)) {
  946. ti->error = "invalid initial priority group";
  947. r = -EINVAL;
  948. goto bad;
  949. }
  950. /* parse the priority groups */
  951. while (as.argc) {
  952. struct priority_group *pg;
  953. unsigned nr_valid_paths = atomic_read(&m->nr_valid_paths);
  954. pg = parse_priority_group(&as, m);
  955. if (IS_ERR(pg)) {
  956. r = PTR_ERR(pg);
  957. goto bad;
  958. }
  959. nr_valid_paths += pg->nr_pgpaths;
  960. atomic_set(&m->nr_valid_paths, nr_valid_paths);
  961. list_add_tail(&pg->list, &m->priority_groups);
  962. pg_count++;
  963. pg->pg_num = pg_count;
  964. if (!--next_pg_num)
  965. m->next_pg = pg;
  966. }
  967. if (pg_count != m->nr_priority_groups) {
  968. ti->error = "priority group count mismatch";
  969. r = -EINVAL;
  970. goto bad;
  971. }
  972. ti->num_flush_bios = 1;
  973. ti->num_discard_bios = 1;
  974. ti->num_write_same_bios = 1;
  975. if (m->queue_mode == DM_TYPE_BIO_BASED)
  976. ti->per_io_data_size = multipath_per_bio_data_size();
  977. else if (m->queue_mode == DM_TYPE_MQ_REQUEST_BASED)
  978. ti->per_io_data_size = sizeof(struct dm_mpath_io);
  979. return 0;
  980. bad:
  981. free_multipath(m);
  982. return r;
  983. }
  984. static void multipath_wait_for_pg_init_completion(struct multipath *m)
  985. {
  986. DEFINE_WAIT(wait);
  987. while (1) {
  988. prepare_to_wait(&m->pg_init_wait, &wait, TASK_UNINTERRUPTIBLE);
  989. if (!atomic_read(&m->pg_init_in_progress))
  990. break;
  991. io_schedule();
  992. }
  993. finish_wait(&m->pg_init_wait, &wait);
  994. }
  995. static void flush_multipath_work(struct multipath *m)
  996. {
  997. set_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
  998. smp_mb__after_atomic();
  999. flush_workqueue(kmpath_handlerd);
  1000. multipath_wait_for_pg_init_completion(m);
  1001. flush_workqueue(kmultipathd);
  1002. flush_work(&m->trigger_event);
  1003. clear_bit(MPATHF_PG_INIT_DISABLED, &m->flags);
  1004. smp_mb__after_atomic();
  1005. }
  1006. static void multipath_dtr(struct dm_target *ti)
  1007. {
  1008. struct multipath *m = ti->private;
  1009. flush_multipath_work(m);
  1010. free_multipath(m);
  1011. }
  1012. /*
  1013. * Take a path out of use.
  1014. */
  1015. static int fail_path(struct pgpath *pgpath)
  1016. {
  1017. unsigned long flags;
  1018. struct multipath *m = pgpath->pg->m;
  1019. spin_lock_irqsave(&m->lock, flags);
  1020. if (!pgpath->is_active)
  1021. goto out;
  1022. DMWARN("Failing path %s.", pgpath->path.dev->name);
  1023. pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
  1024. pgpath->is_active = false;
  1025. pgpath->fail_count++;
  1026. atomic_dec(&m->nr_valid_paths);
  1027. if (pgpath == m->current_pgpath)
  1028. m->current_pgpath = NULL;
  1029. dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
  1030. pgpath->path.dev->name, atomic_read(&m->nr_valid_paths));
  1031. schedule_work(&m->trigger_event);
  1032. out:
  1033. spin_unlock_irqrestore(&m->lock, flags);
  1034. return 0;
  1035. }
  1036. /*
  1037. * Reinstate a previously-failed path
  1038. */
  1039. static int reinstate_path(struct pgpath *pgpath)
  1040. {
  1041. int r = 0, run_queue = 0;
  1042. unsigned long flags;
  1043. struct multipath *m = pgpath->pg->m;
  1044. unsigned nr_valid_paths;
  1045. spin_lock_irqsave(&m->lock, flags);
  1046. if (pgpath->is_active)
  1047. goto out;
  1048. DMWARN("Reinstating path %s.", pgpath->path.dev->name);
  1049. r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
  1050. if (r)
  1051. goto out;
  1052. pgpath->is_active = true;
  1053. nr_valid_paths = atomic_inc_return(&m->nr_valid_paths);
  1054. if (nr_valid_paths == 1) {
  1055. m->current_pgpath = NULL;
  1056. run_queue = 1;
  1057. } else if (m->hw_handler_name && (m->current_pg == pgpath->pg)) {
  1058. if (queue_work(kmpath_handlerd, &pgpath->activate_path.work))
  1059. atomic_inc(&m->pg_init_in_progress);
  1060. }
  1061. dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
  1062. pgpath->path.dev->name, nr_valid_paths);
  1063. schedule_work(&m->trigger_event);
  1064. out:
  1065. spin_unlock_irqrestore(&m->lock, flags);
  1066. if (run_queue) {
  1067. dm_table_run_md_queue_async(m->ti->table);
  1068. process_queued_io_list(m);
  1069. }
  1070. return r;
  1071. }
  1072. /*
  1073. * Fail or reinstate all paths that match the provided struct dm_dev.
  1074. */
  1075. static int action_dev(struct multipath *m, struct dm_dev *dev,
  1076. action_fn action)
  1077. {
  1078. int r = -EINVAL;
  1079. struct pgpath *pgpath;
  1080. struct priority_group *pg;
  1081. list_for_each_entry(pg, &m->priority_groups, list) {
  1082. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  1083. if (pgpath->path.dev == dev)
  1084. r = action(pgpath);
  1085. }
  1086. }
  1087. return r;
  1088. }
  1089. /*
  1090. * Temporarily try to avoid having to use the specified PG
  1091. */
  1092. static void bypass_pg(struct multipath *m, struct priority_group *pg,
  1093. bool bypassed)
  1094. {
  1095. unsigned long flags;
  1096. spin_lock_irqsave(&m->lock, flags);
  1097. pg->bypassed = bypassed;
  1098. m->current_pgpath = NULL;
  1099. m->current_pg = NULL;
  1100. spin_unlock_irqrestore(&m->lock, flags);
  1101. schedule_work(&m->trigger_event);
  1102. }
  1103. /*
  1104. * Switch to using the specified PG from the next I/O that gets mapped
  1105. */
  1106. static int switch_pg_num(struct multipath *m, const char *pgstr)
  1107. {
  1108. struct priority_group *pg;
  1109. unsigned pgnum;
  1110. unsigned long flags;
  1111. char dummy;
  1112. if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
  1113. !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
  1114. DMWARN("invalid PG number supplied to switch_pg_num");
  1115. return -EINVAL;
  1116. }
  1117. spin_lock_irqsave(&m->lock, flags);
  1118. list_for_each_entry(pg, &m->priority_groups, list) {
  1119. pg->bypassed = false;
  1120. if (--pgnum)
  1121. continue;
  1122. m->current_pgpath = NULL;
  1123. m->current_pg = NULL;
  1124. m->next_pg = pg;
  1125. }
  1126. spin_unlock_irqrestore(&m->lock, flags);
  1127. schedule_work(&m->trigger_event);
  1128. return 0;
  1129. }
  1130. /*
  1131. * Set/clear bypassed status of a PG.
  1132. * PGs are numbered upwards from 1 in the order they were declared.
  1133. */
  1134. static int bypass_pg_num(struct multipath *m, const char *pgstr, bool bypassed)
  1135. {
  1136. struct priority_group *pg;
  1137. unsigned pgnum;
  1138. char dummy;
  1139. if (!pgstr || (sscanf(pgstr, "%u%c", &pgnum, &dummy) != 1) || !pgnum ||
  1140. !m->nr_priority_groups || (pgnum > m->nr_priority_groups)) {
  1141. DMWARN("invalid PG number supplied to bypass_pg");
  1142. return -EINVAL;
  1143. }
  1144. list_for_each_entry(pg, &m->priority_groups, list) {
  1145. if (!--pgnum)
  1146. break;
  1147. }
  1148. bypass_pg(m, pg, bypassed);
  1149. return 0;
  1150. }
  1151. /*
  1152. * Should we retry pg_init immediately?
  1153. */
  1154. static bool pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
  1155. {
  1156. unsigned long flags;
  1157. bool limit_reached = false;
  1158. spin_lock_irqsave(&m->lock, flags);
  1159. if (atomic_read(&m->pg_init_count) <= m->pg_init_retries &&
  1160. !test_bit(MPATHF_PG_INIT_DISABLED, &m->flags))
  1161. set_bit(MPATHF_PG_INIT_REQUIRED, &m->flags);
  1162. else
  1163. limit_reached = true;
  1164. spin_unlock_irqrestore(&m->lock, flags);
  1165. return limit_reached;
  1166. }
  1167. static void pg_init_done(void *data, int errors)
  1168. {
  1169. struct pgpath *pgpath = data;
  1170. struct priority_group *pg = pgpath->pg;
  1171. struct multipath *m = pg->m;
  1172. unsigned long flags;
  1173. bool delay_retry = false;
  1174. /* device or driver problems */
  1175. switch (errors) {
  1176. case SCSI_DH_OK:
  1177. break;
  1178. case SCSI_DH_NOSYS:
  1179. if (!m->hw_handler_name) {
  1180. errors = 0;
  1181. break;
  1182. }
  1183. DMERR("Could not failover the device: Handler scsi_dh_%s "
  1184. "Error %d.", m->hw_handler_name, errors);
  1185. /*
  1186. * Fail path for now, so we do not ping pong
  1187. */
  1188. fail_path(pgpath);
  1189. break;
  1190. case SCSI_DH_DEV_TEMP_BUSY:
  1191. /*
  1192. * Probably doing something like FW upgrade on the
  1193. * controller so try the other pg.
  1194. */
  1195. bypass_pg(m, pg, true);
  1196. break;
  1197. case SCSI_DH_RETRY:
  1198. /* Wait before retrying. */
  1199. delay_retry = 1;
  1200. case SCSI_DH_IMM_RETRY:
  1201. case SCSI_DH_RES_TEMP_UNAVAIL:
  1202. if (pg_init_limit_reached(m, pgpath))
  1203. fail_path(pgpath);
  1204. errors = 0;
  1205. break;
  1206. case SCSI_DH_DEV_OFFLINED:
  1207. default:
  1208. /*
  1209. * We probably do not want to fail the path for a device
  1210. * error, but this is what the old dm did. In future
  1211. * patches we can do more advanced handling.
  1212. */
  1213. fail_path(pgpath);
  1214. }
  1215. spin_lock_irqsave(&m->lock, flags);
  1216. if (errors) {
  1217. if (pgpath == m->current_pgpath) {
  1218. DMERR("Could not failover device. Error %d.", errors);
  1219. m->current_pgpath = NULL;
  1220. m->current_pg = NULL;
  1221. }
  1222. } else if (!test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
  1223. pg->bypassed = false;
  1224. if (atomic_dec_return(&m->pg_init_in_progress) > 0)
  1225. /* Activations of other paths are still on going */
  1226. goto out;
  1227. if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags)) {
  1228. if (delay_retry)
  1229. set_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
  1230. else
  1231. clear_bit(MPATHF_PG_INIT_DELAY_RETRY, &m->flags);
  1232. if (__pg_init_all_paths(m))
  1233. goto out;
  1234. }
  1235. clear_bit(MPATHF_QUEUE_IO, &m->flags);
  1236. process_queued_io_list(m);
  1237. /*
  1238. * Wake up any thread waiting to suspend.
  1239. */
  1240. wake_up(&m->pg_init_wait);
  1241. out:
  1242. spin_unlock_irqrestore(&m->lock, flags);
  1243. }
  1244. static void activate_path(struct work_struct *work)
  1245. {
  1246. struct pgpath *pgpath =
  1247. container_of(work, struct pgpath, activate_path.work);
  1248. struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
  1249. if (pgpath->is_active && !blk_queue_dying(q))
  1250. scsi_dh_activate(q, pg_init_done, pgpath);
  1251. else
  1252. pg_init_done(pgpath, SCSI_DH_DEV_OFFLINED);
  1253. }
  1254. static int noretry_error(int error)
  1255. {
  1256. switch (error) {
  1257. case -EBADE:
  1258. /*
  1259. * EBADE signals an reservation conflict.
  1260. * We shouldn't fail the path here as we can communicate with
  1261. * the target. We should failover to the next path, but in
  1262. * doing so we might be causing a ping-pong between paths.
  1263. * So just return the reservation conflict error.
  1264. */
  1265. case -EOPNOTSUPP:
  1266. case -EREMOTEIO:
  1267. case -EILSEQ:
  1268. case -ENODATA:
  1269. case -ENOSPC:
  1270. return 1;
  1271. }
  1272. /* Anything else could be a path failure, so should be retried */
  1273. return 0;
  1274. }
  1275. /*
  1276. * end_io handling
  1277. */
  1278. static int do_end_io(struct multipath *m, struct request *clone,
  1279. int error, struct dm_mpath_io *mpio)
  1280. {
  1281. /*
  1282. * We don't queue any clone request inside the multipath target
  1283. * during end I/O handling, since those clone requests don't have
  1284. * bio clones. If we queue them inside the multipath target,
  1285. * we need to make bio clones, that requires memory allocation.
  1286. * (See drivers/md/dm-rq.c:end_clone_bio() about why the clone requests
  1287. * don't have bio clones.)
  1288. * Instead of queueing the clone request here, we queue the original
  1289. * request into dm core, which will remake a clone request and
  1290. * clone bios for it and resubmit it later.
  1291. */
  1292. int r = DM_ENDIO_REQUEUE;
  1293. if (!error && !clone->errors)
  1294. return 0; /* I/O complete */
  1295. if (noretry_error(error))
  1296. return error;
  1297. if (mpio->pgpath)
  1298. fail_path(mpio->pgpath);
  1299. if (!atomic_read(&m->nr_valid_paths)) {
  1300. if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
  1301. if (!must_push_back_rq(m))
  1302. r = -EIO;
  1303. }
  1304. }
  1305. return r;
  1306. }
  1307. static int multipath_end_io(struct dm_target *ti, struct request *clone,
  1308. int error, union map_info *map_context)
  1309. {
  1310. struct multipath *m = ti->private;
  1311. struct dm_mpath_io *mpio = get_mpio(map_context);
  1312. struct pgpath *pgpath;
  1313. struct path_selector *ps;
  1314. int r;
  1315. BUG_ON(!mpio);
  1316. r = do_end_io(m, clone, error, mpio);
  1317. pgpath = mpio->pgpath;
  1318. if (pgpath) {
  1319. ps = &pgpath->pg->ps;
  1320. if (ps->type->end_io)
  1321. ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
  1322. }
  1323. clear_request_fn_mpio(m, map_context);
  1324. return r;
  1325. }
  1326. static int do_end_io_bio(struct multipath *m, struct bio *clone,
  1327. int error, struct dm_mpath_io *mpio)
  1328. {
  1329. unsigned long flags;
  1330. if (!error)
  1331. return 0; /* I/O complete */
  1332. if (noretry_error(error))
  1333. return error;
  1334. if (mpio->pgpath)
  1335. fail_path(mpio->pgpath);
  1336. if (!atomic_read(&m->nr_valid_paths)) {
  1337. if (!test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags)) {
  1338. if (!must_push_back_bio(m))
  1339. return -EIO;
  1340. return DM_ENDIO_REQUEUE;
  1341. }
  1342. }
  1343. /* Queue for the daemon to resubmit */
  1344. dm_bio_restore(get_bio_details_from_bio(clone), clone);
  1345. spin_lock_irqsave(&m->lock, flags);
  1346. bio_list_add(&m->queued_bios, clone);
  1347. spin_unlock_irqrestore(&m->lock, flags);
  1348. if (!test_bit(MPATHF_QUEUE_IO, &m->flags))
  1349. queue_work(kmultipathd, &m->process_queued_bios);
  1350. return DM_ENDIO_INCOMPLETE;
  1351. }
  1352. static int multipath_end_io_bio(struct dm_target *ti, struct bio *clone, int error)
  1353. {
  1354. struct multipath *m = ti->private;
  1355. struct dm_mpath_io *mpio = get_mpio_from_bio(clone);
  1356. struct pgpath *pgpath;
  1357. struct path_selector *ps;
  1358. int r;
  1359. BUG_ON(!mpio);
  1360. r = do_end_io_bio(m, clone, error, mpio);
  1361. pgpath = mpio->pgpath;
  1362. if (pgpath) {
  1363. ps = &pgpath->pg->ps;
  1364. if (ps->type->end_io)
  1365. ps->type->end_io(ps, &pgpath->path, mpio->nr_bytes);
  1366. }
  1367. return r;
  1368. }
  1369. /*
  1370. * Suspend can't complete until all the I/O is processed so if
  1371. * the last path fails we must error any remaining I/O.
  1372. * Note that if the freeze_bdev fails while suspending, the
  1373. * queue_if_no_path state is lost - userspace should reset it.
  1374. */
  1375. static void multipath_presuspend(struct dm_target *ti)
  1376. {
  1377. struct multipath *m = ti->private;
  1378. queue_if_no_path(m, false, true);
  1379. }
  1380. static void multipath_postsuspend(struct dm_target *ti)
  1381. {
  1382. struct multipath *m = ti->private;
  1383. mutex_lock(&m->work_mutex);
  1384. flush_multipath_work(m);
  1385. mutex_unlock(&m->work_mutex);
  1386. }
  1387. /*
  1388. * Restore the queue_if_no_path setting.
  1389. */
  1390. static void multipath_resume(struct dm_target *ti)
  1391. {
  1392. struct multipath *m = ti->private;
  1393. unsigned long flags;
  1394. spin_lock_irqsave(&m->lock, flags);
  1395. if (test_bit(MPATHF_SAVED_QUEUE_IF_NO_PATH, &m->flags))
  1396. set_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
  1397. else
  1398. clear_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags);
  1399. spin_unlock_irqrestore(&m->lock, flags);
  1400. }
  1401. /*
  1402. * Info output has the following format:
  1403. * num_multipath_feature_args [multipath_feature_args]*
  1404. * num_handler_status_args [handler_status_args]*
  1405. * num_groups init_group_number
  1406. * [A|D|E num_ps_status_args [ps_status_args]*
  1407. * num_paths num_selector_args
  1408. * [path_dev A|F fail_count [selector_args]* ]+ ]+
  1409. *
  1410. * Table output has the following format (identical to the constructor string):
  1411. * num_feature_args [features_args]*
  1412. * num_handler_args hw_handler [hw_handler_args]*
  1413. * num_groups init_group_number
  1414. * [priority selector-name num_ps_args [ps_args]*
  1415. * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
  1416. */
  1417. static void multipath_status(struct dm_target *ti, status_type_t type,
  1418. unsigned status_flags, char *result, unsigned maxlen)
  1419. {
  1420. int sz = 0;
  1421. unsigned long flags;
  1422. struct multipath *m = ti->private;
  1423. struct priority_group *pg;
  1424. struct pgpath *p;
  1425. unsigned pg_num;
  1426. char state;
  1427. spin_lock_irqsave(&m->lock, flags);
  1428. /* Features */
  1429. if (type == STATUSTYPE_INFO)
  1430. DMEMIT("2 %u %u ", test_bit(MPATHF_QUEUE_IO, &m->flags),
  1431. atomic_read(&m->pg_init_count));
  1432. else {
  1433. DMEMIT("%u ", test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags) +
  1434. (m->pg_init_retries > 0) * 2 +
  1435. (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT) * 2 +
  1436. test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags) +
  1437. (m->queue_mode != DM_TYPE_REQUEST_BASED) * 2);
  1438. if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
  1439. DMEMIT("queue_if_no_path ");
  1440. if (m->pg_init_retries)
  1441. DMEMIT("pg_init_retries %u ", m->pg_init_retries);
  1442. if (m->pg_init_delay_msecs != DM_PG_INIT_DELAY_DEFAULT)
  1443. DMEMIT("pg_init_delay_msecs %u ", m->pg_init_delay_msecs);
  1444. if (test_bit(MPATHF_RETAIN_ATTACHED_HW_HANDLER, &m->flags))
  1445. DMEMIT("retain_attached_hw_handler ");
  1446. if (m->queue_mode != DM_TYPE_REQUEST_BASED) {
  1447. switch(m->queue_mode) {
  1448. case DM_TYPE_BIO_BASED:
  1449. DMEMIT("queue_mode bio ");
  1450. break;
  1451. case DM_TYPE_MQ_REQUEST_BASED:
  1452. DMEMIT("queue_mode mq ");
  1453. break;
  1454. }
  1455. }
  1456. }
  1457. if (!m->hw_handler_name || type == STATUSTYPE_INFO)
  1458. DMEMIT("0 ");
  1459. else
  1460. DMEMIT("1 %s ", m->hw_handler_name);
  1461. DMEMIT("%u ", m->nr_priority_groups);
  1462. if (m->next_pg)
  1463. pg_num = m->next_pg->pg_num;
  1464. else if (m->current_pg)
  1465. pg_num = m->current_pg->pg_num;
  1466. else
  1467. pg_num = (m->nr_priority_groups ? 1 : 0);
  1468. DMEMIT("%u ", pg_num);
  1469. switch (type) {
  1470. case STATUSTYPE_INFO:
  1471. list_for_each_entry(pg, &m->priority_groups, list) {
  1472. if (pg->bypassed)
  1473. state = 'D'; /* Disabled */
  1474. else if (pg == m->current_pg)
  1475. state = 'A'; /* Currently Active */
  1476. else
  1477. state = 'E'; /* Enabled */
  1478. DMEMIT("%c ", state);
  1479. if (pg->ps.type->status)
  1480. sz += pg->ps.type->status(&pg->ps, NULL, type,
  1481. result + sz,
  1482. maxlen - sz);
  1483. else
  1484. DMEMIT("0 ");
  1485. DMEMIT("%u %u ", pg->nr_pgpaths,
  1486. pg->ps.type->info_args);
  1487. list_for_each_entry(p, &pg->pgpaths, list) {
  1488. DMEMIT("%s %s %u ", p->path.dev->name,
  1489. p->is_active ? "A" : "F",
  1490. p->fail_count);
  1491. if (pg->ps.type->status)
  1492. sz += pg->ps.type->status(&pg->ps,
  1493. &p->path, type, result + sz,
  1494. maxlen - sz);
  1495. }
  1496. }
  1497. break;
  1498. case STATUSTYPE_TABLE:
  1499. list_for_each_entry(pg, &m->priority_groups, list) {
  1500. DMEMIT("%s ", pg->ps.type->name);
  1501. if (pg->ps.type->status)
  1502. sz += pg->ps.type->status(&pg->ps, NULL, type,
  1503. result + sz,
  1504. maxlen - sz);
  1505. else
  1506. DMEMIT("0 ");
  1507. DMEMIT("%u %u ", pg->nr_pgpaths,
  1508. pg->ps.type->table_args);
  1509. list_for_each_entry(p, &pg->pgpaths, list) {
  1510. DMEMIT("%s ", p->path.dev->name);
  1511. if (pg->ps.type->status)
  1512. sz += pg->ps.type->status(&pg->ps,
  1513. &p->path, type, result + sz,
  1514. maxlen - sz);
  1515. }
  1516. }
  1517. break;
  1518. }
  1519. spin_unlock_irqrestore(&m->lock, flags);
  1520. }
  1521. static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
  1522. {
  1523. int r = -EINVAL;
  1524. struct dm_dev *dev;
  1525. struct multipath *m = ti->private;
  1526. action_fn action;
  1527. mutex_lock(&m->work_mutex);
  1528. if (dm_suspended(ti)) {
  1529. r = -EBUSY;
  1530. goto out;
  1531. }
  1532. if (argc == 1) {
  1533. if (!strcasecmp(argv[0], "queue_if_no_path")) {
  1534. r = queue_if_no_path(m, true, false);
  1535. goto out;
  1536. } else if (!strcasecmp(argv[0], "fail_if_no_path")) {
  1537. r = queue_if_no_path(m, false, false);
  1538. goto out;
  1539. }
  1540. }
  1541. if (argc != 2) {
  1542. DMWARN("Invalid multipath message arguments. Expected 2 arguments, got %d.", argc);
  1543. goto out;
  1544. }
  1545. if (!strcasecmp(argv[0], "disable_group")) {
  1546. r = bypass_pg_num(m, argv[1], true);
  1547. goto out;
  1548. } else if (!strcasecmp(argv[0], "enable_group")) {
  1549. r = bypass_pg_num(m, argv[1], false);
  1550. goto out;
  1551. } else if (!strcasecmp(argv[0], "switch_group")) {
  1552. r = switch_pg_num(m, argv[1]);
  1553. goto out;
  1554. } else if (!strcasecmp(argv[0], "reinstate_path"))
  1555. action = reinstate_path;
  1556. else if (!strcasecmp(argv[0], "fail_path"))
  1557. action = fail_path;
  1558. else {
  1559. DMWARN("Unrecognised multipath message received: %s", argv[0]);
  1560. goto out;
  1561. }
  1562. r = dm_get_device(ti, argv[1], dm_table_get_mode(ti->table), &dev);
  1563. if (r) {
  1564. DMWARN("message: error getting device %s",
  1565. argv[1]);
  1566. goto out;
  1567. }
  1568. r = action_dev(m, dev, action);
  1569. dm_put_device(ti, dev);
  1570. out:
  1571. mutex_unlock(&m->work_mutex);
  1572. return r;
  1573. }
  1574. static int multipath_prepare_ioctl(struct dm_target *ti,
  1575. struct block_device **bdev, fmode_t *mode)
  1576. {
  1577. struct multipath *m = ti->private;
  1578. struct pgpath *current_pgpath;
  1579. int r;
  1580. current_pgpath = lockless_dereference(m->current_pgpath);
  1581. if (!current_pgpath)
  1582. current_pgpath = choose_pgpath(m, 0);
  1583. if (current_pgpath) {
  1584. if (!test_bit(MPATHF_QUEUE_IO, &m->flags)) {
  1585. *bdev = current_pgpath->path.dev->bdev;
  1586. *mode = current_pgpath->path.dev->mode;
  1587. r = 0;
  1588. } else {
  1589. /* pg_init has not started or completed */
  1590. r = -ENOTCONN;
  1591. }
  1592. } else {
  1593. /* No path is available */
  1594. if (test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
  1595. r = -ENOTCONN;
  1596. else
  1597. r = -EIO;
  1598. }
  1599. if (r == -ENOTCONN) {
  1600. if (!lockless_dereference(m->current_pg)) {
  1601. /* Path status changed, redo selection */
  1602. (void) choose_pgpath(m, 0);
  1603. }
  1604. if (test_bit(MPATHF_PG_INIT_REQUIRED, &m->flags))
  1605. pg_init_all_paths(m);
  1606. dm_table_run_md_queue_async(m->ti->table);
  1607. process_queued_io_list(m);
  1608. }
  1609. /*
  1610. * Only pass ioctls through if the device sizes match exactly.
  1611. */
  1612. if (!r && ti->len != i_size_read((*bdev)->bd_inode) >> SECTOR_SHIFT)
  1613. return 1;
  1614. return r;
  1615. }
  1616. static int multipath_iterate_devices(struct dm_target *ti,
  1617. iterate_devices_callout_fn fn, void *data)
  1618. {
  1619. struct multipath *m = ti->private;
  1620. struct priority_group *pg;
  1621. struct pgpath *p;
  1622. int ret = 0;
  1623. list_for_each_entry(pg, &m->priority_groups, list) {
  1624. list_for_each_entry(p, &pg->pgpaths, list) {
  1625. ret = fn(ti, p->path.dev, ti->begin, ti->len, data);
  1626. if (ret)
  1627. goto out;
  1628. }
  1629. }
  1630. out:
  1631. return ret;
  1632. }
  1633. static int pgpath_busy(struct pgpath *pgpath)
  1634. {
  1635. struct request_queue *q = bdev_get_queue(pgpath->path.dev->bdev);
  1636. return blk_lld_busy(q);
  1637. }
  1638. /*
  1639. * We return "busy", only when we can map I/Os but underlying devices
  1640. * are busy (so even if we map I/Os now, the I/Os will wait on
  1641. * the underlying queue).
  1642. * In other words, if we want to kill I/Os or queue them inside us
  1643. * due to map unavailability, we don't return "busy". Otherwise,
  1644. * dm core won't give us the I/Os and we can't do what we want.
  1645. */
  1646. static int multipath_busy(struct dm_target *ti)
  1647. {
  1648. bool busy = false, has_active = false;
  1649. struct multipath *m = ti->private;
  1650. struct priority_group *pg, *next_pg;
  1651. struct pgpath *pgpath;
  1652. /* pg_init in progress */
  1653. if (atomic_read(&m->pg_init_in_progress))
  1654. return true;
  1655. /* no paths available, for blk-mq: rely on IO mapping to delay requeue */
  1656. if (!atomic_read(&m->nr_valid_paths) && test_bit(MPATHF_QUEUE_IF_NO_PATH, &m->flags))
  1657. return (m->queue_mode != DM_TYPE_MQ_REQUEST_BASED);
  1658. /* Guess which priority_group will be used at next mapping time */
  1659. pg = lockless_dereference(m->current_pg);
  1660. next_pg = lockless_dereference(m->next_pg);
  1661. if (unlikely(!lockless_dereference(m->current_pgpath) && next_pg))
  1662. pg = next_pg;
  1663. if (!pg) {
  1664. /*
  1665. * We don't know which pg will be used at next mapping time.
  1666. * We don't call choose_pgpath() here to avoid to trigger
  1667. * pg_init just by busy checking.
  1668. * So we don't know whether underlying devices we will be using
  1669. * at next mapping time are busy or not. Just try mapping.
  1670. */
  1671. return busy;
  1672. }
  1673. /*
  1674. * If there is one non-busy active path at least, the path selector
  1675. * will be able to select it. So we consider such a pg as not busy.
  1676. */
  1677. busy = true;
  1678. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  1679. if (pgpath->is_active) {
  1680. has_active = true;
  1681. if (!pgpath_busy(pgpath)) {
  1682. busy = false;
  1683. break;
  1684. }
  1685. }
  1686. }
  1687. if (!has_active) {
  1688. /*
  1689. * No active path in this pg, so this pg won't be used and
  1690. * the current_pg will be changed at next mapping time.
  1691. * We need to try mapping to determine it.
  1692. */
  1693. busy = false;
  1694. }
  1695. return busy;
  1696. }
  1697. /*-----------------------------------------------------------------
  1698. * Module setup
  1699. *---------------------------------------------------------------*/
  1700. static struct target_type multipath_target = {
  1701. .name = "multipath",
  1702. .version = {1, 12, 0},
  1703. .features = DM_TARGET_SINGLETON | DM_TARGET_IMMUTABLE,
  1704. .module = THIS_MODULE,
  1705. .ctr = multipath_ctr,
  1706. .dtr = multipath_dtr,
  1707. .map_rq = multipath_map,
  1708. .clone_and_map_rq = multipath_clone_and_map,
  1709. .release_clone_rq = multipath_release_clone,
  1710. .rq_end_io = multipath_end_io,
  1711. .map = multipath_map_bio,
  1712. .end_io = multipath_end_io_bio,
  1713. .presuspend = multipath_presuspend,
  1714. .postsuspend = multipath_postsuspend,
  1715. .resume = multipath_resume,
  1716. .status = multipath_status,
  1717. .message = multipath_message,
  1718. .prepare_ioctl = multipath_prepare_ioctl,
  1719. .iterate_devices = multipath_iterate_devices,
  1720. .busy = multipath_busy,
  1721. };
  1722. static int __init dm_multipath_init(void)
  1723. {
  1724. int r;
  1725. /* allocate a slab for the dm_mpath_ios */
  1726. _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
  1727. if (!_mpio_cache)
  1728. return -ENOMEM;
  1729. r = dm_register_target(&multipath_target);
  1730. if (r < 0) {
  1731. DMERR("request-based register failed %d", r);
  1732. r = -EINVAL;
  1733. goto bad_register_target;
  1734. }
  1735. kmultipathd = alloc_workqueue("kmpathd", WQ_MEM_RECLAIM, 0);
  1736. if (!kmultipathd) {
  1737. DMERR("failed to create workqueue kmpathd");
  1738. r = -ENOMEM;
  1739. goto bad_alloc_kmultipathd;
  1740. }
  1741. /*
  1742. * A separate workqueue is used to handle the device handlers
  1743. * to avoid overloading existing workqueue. Overloading the
  1744. * old workqueue would also create a bottleneck in the
  1745. * path of the storage hardware device activation.
  1746. */
  1747. kmpath_handlerd = alloc_ordered_workqueue("kmpath_handlerd",
  1748. WQ_MEM_RECLAIM);
  1749. if (!kmpath_handlerd) {
  1750. DMERR("failed to create workqueue kmpath_handlerd");
  1751. r = -ENOMEM;
  1752. goto bad_alloc_kmpath_handlerd;
  1753. }
  1754. return 0;
  1755. bad_alloc_kmpath_handlerd:
  1756. destroy_workqueue(kmultipathd);
  1757. bad_alloc_kmultipathd:
  1758. dm_unregister_target(&multipath_target);
  1759. bad_register_target:
  1760. kmem_cache_destroy(_mpio_cache);
  1761. return r;
  1762. }
  1763. static void __exit dm_multipath_exit(void)
  1764. {
  1765. destroy_workqueue(kmpath_handlerd);
  1766. destroy_workqueue(kmultipathd);
  1767. dm_unregister_target(&multipath_target);
  1768. kmem_cache_destroy(_mpio_cache);
  1769. }
  1770. module_init(dm_multipath_init);
  1771. module_exit(dm_multipath_exit);
  1772. MODULE_DESCRIPTION(DM_NAME " multipath target");
  1773. MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
  1774. MODULE_LICENSE("GPL");