dm-mpath.c 50 KB

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