dm-mpath.c 43 KB

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