dm-mpath.c 49 KB

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