dm-mpath.c 50 KB

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