dm-mpath.c 50 KB

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