dm-mpath.c 40 KB

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