dm-mpath.c 49 KB

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