dm-mpath.c 59 KB

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