dm-mpath.c 56 KB

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