dm-mpath.c 51 KB

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