dm-mpath.c 51 KB

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