dm-mpath.c 51 KB

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