dm-mpath.c 50 KB

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