dm-mpath.c 38 KB

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