dm-mpath.c 49 KB

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