dm-mpath.c 50 KB

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