dm-mpath.c 39 KB

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