dm-mpath.c 41 KB

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