dm-mpath.c 48 KB

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