dm-mpath.c 50 KB

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