dm-mpath.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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 "dm.h"
  8. #include "dm-path-selector.h"
  9. #include "dm-hw-handler.h"
  10. #include "dm-bio-list.h"
  11. #include "dm-bio-record.h"
  12. #include "dm-uevent.h"
  13. #include <linux/ctype.h>
  14. #include <linux/init.h>
  15. #include <linux/mempool.h>
  16. #include <linux/module.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/slab.h>
  19. #include <linux/time.h>
  20. #include <linux/workqueue.h>
  21. #include <scsi/scsi_dh.h>
  22. #include <asm/atomic.h>
  23. #define DM_MSG_PREFIX "multipath"
  24. #define MESG_STR(x) x, sizeof(x)
  25. /* Path properties */
  26. struct pgpath {
  27. struct list_head list;
  28. struct priority_group *pg; /* Owning PG */
  29. unsigned fail_count; /* Cumulative failure count */
  30. struct dm_path 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. struct work_struct activate_path;
  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 bio_list 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. };
  77. /*
  78. * Context information attached to each bio we process.
  79. */
  80. struct dm_mpath_io {
  81. struct pgpath *pgpath;
  82. struct dm_bio_details details;
  83. };
  84. typedef int (*action_fn) (struct pgpath *pgpath);
  85. #define MIN_IOS 256 /* Mempool size */
  86. static struct kmem_cache *_mpio_cache;
  87. static struct workqueue_struct *kmultipathd, *kmpath_handlerd;
  88. static void process_queued_ios(struct work_struct *work);
  89. static void trigger_event(struct work_struct *work);
  90. static void activate_path(struct work_struct *work);
  91. /*-----------------------------------------------
  92. * Allocation routines
  93. *-----------------------------------------------*/
  94. static struct pgpath *alloc_pgpath(void)
  95. {
  96. struct pgpath *pgpath = kzalloc(sizeof(*pgpath), GFP_KERNEL);
  97. if (pgpath)
  98. pgpath->path.is_active = 1;
  99. return pgpath;
  100. }
  101. static void free_pgpath(struct pgpath *pgpath)
  102. {
  103. kfree(pgpath);
  104. }
  105. static struct priority_group *alloc_priority_group(void)
  106. {
  107. struct priority_group *pg;
  108. pg = kzalloc(sizeof(*pg), GFP_KERNEL);
  109. if (pg)
  110. INIT_LIST_HEAD(&pg->pgpaths);
  111. return pg;
  112. }
  113. static void free_pgpaths(struct list_head *pgpaths, struct dm_target *ti)
  114. {
  115. struct pgpath *pgpath, *tmp;
  116. list_for_each_entry_safe(pgpath, tmp, pgpaths, list) {
  117. list_del(&pgpath->list);
  118. dm_put_device(ti, pgpath->path.dev);
  119. free_pgpath(pgpath);
  120. }
  121. }
  122. static void free_priority_group(struct priority_group *pg,
  123. struct dm_target *ti)
  124. {
  125. struct path_selector *ps = &pg->ps;
  126. if (ps->type) {
  127. ps->type->destroy(ps);
  128. dm_put_path_selector(ps->type);
  129. }
  130. free_pgpaths(&pg->pgpaths, ti);
  131. kfree(pg);
  132. }
  133. static struct multipath *alloc_multipath(struct dm_target *ti)
  134. {
  135. struct multipath *m;
  136. m = kzalloc(sizeof(*m), GFP_KERNEL);
  137. if (m) {
  138. INIT_LIST_HEAD(&m->priority_groups);
  139. spin_lock_init(&m->lock);
  140. m->queue_io = 1;
  141. INIT_WORK(&m->process_queued_ios, process_queued_ios);
  142. INIT_WORK(&m->trigger_event, trigger_event);
  143. INIT_WORK(&m->activate_path, activate_path);
  144. m->mpio_pool = mempool_create_slab_pool(MIN_IOS, _mpio_cache);
  145. if (!m->mpio_pool) {
  146. kfree(m);
  147. return NULL;
  148. }
  149. m->ti = ti;
  150. ti->private = m;
  151. }
  152. return m;
  153. }
  154. static void free_multipath(struct multipath *m)
  155. {
  156. struct priority_group *pg, *tmp;
  157. list_for_each_entry_safe(pg, tmp, &m->priority_groups, list) {
  158. list_del(&pg->list);
  159. free_priority_group(pg, m->ti);
  160. }
  161. kfree(m->hw_handler_name);
  162. mempool_destroy(m->mpio_pool);
  163. kfree(m);
  164. }
  165. /*-----------------------------------------------
  166. * Path selection
  167. *-----------------------------------------------*/
  168. static void __switch_pg(struct multipath *m, struct pgpath *pgpath)
  169. {
  170. m->current_pg = pgpath->pg;
  171. /* Must we initialise the PG first, and queue I/O till it's ready? */
  172. if (m->hw_handler_name) {
  173. m->pg_init_required = 1;
  174. m->queue_io = 1;
  175. } else {
  176. m->pg_init_required = 0;
  177. m->queue_io = 0;
  178. }
  179. m->pg_init_count = 0;
  180. }
  181. static int __choose_path_in_pg(struct multipath *m, struct priority_group *pg)
  182. {
  183. struct dm_path *path;
  184. path = pg->ps.type->select_path(&pg->ps, &m->repeat_count);
  185. if (!path)
  186. return -ENXIO;
  187. m->current_pgpath = path_to_pgpath(path);
  188. if (m->current_pg != pg)
  189. __switch_pg(m, m->current_pgpath);
  190. return 0;
  191. }
  192. static void __choose_pgpath(struct multipath *m)
  193. {
  194. struct priority_group *pg;
  195. unsigned bypassed = 1;
  196. if (!m->nr_valid_paths)
  197. goto failed;
  198. /* Were we instructed to switch PG? */
  199. if (m->next_pg) {
  200. pg = m->next_pg;
  201. m->next_pg = NULL;
  202. if (!__choose_path_in_pg(m, pg))
  203. return;
  204. }
  205. /* Don't change PG until it has no remaining paths */
  206. if (m->current_pg && !__choose_path_in_pg(m, m->current_pg))
  207. return;
  208. /*
  209. * Loop through priority groups until we find a valid path.
  210. * First time we skip PGs marked 'bypassed'.
  211. * Second time we only try the ones we skipped.
  212. */
  213. do {
  214. list_for_each_entry(pg, &m->priority_groups, list) {
  215. if (pg->bypassed == bypassed)
  216. continue;
  217. if (!__choose_path_in_pg(m, pg))
  218. return;
  219. }
  220. } while (bypassed--);
  221. failed:
  222. m->current_pgpath = NULL;
  223. m->current_pg = NULL;
  224. }
  225. /*
  226. * Check whether bios must be queued in the device-mapper core rather
  227. * than here in the target.
  228. *
  229. * m->lock must be held on entry.
  230. *
  231. * If m->queue_if_no_path and m->saved_queue_if_no_path hold the
  232. * same value then we are not between multipath_presuspend()
  233. * and multipath_resume() calls and we have no need to check
  234. * for the DMF_NOFLUSH_SUSPENDING flag.
  235. */
  236. static int __must_push_back(struct multipath *m)
  237. {
  238. return (m->queue_if_no_path != m->saved_queue_if_no_path &&
  239. dm_noflush_suspending(m->ti));
  240. }
  241. static int map_io(struct multipath *m, struct bio *bio,
  242. struct dm_mpath_io *mpio, unsigned was_queued)
  243. {
  244. int r = DM_MAPIO_REMAPPED;
  245. unsigned long flags;
  246. struct pgpath *pgpath;
  247. spin_lock_irqsave(&m->lock, flags);
  248. /* Do we need to select a new pgpath? */
  249. if (!m->current_pgpath ||
  250. (!m->queue_io && (m->repeat_count && --m->repeat_count == 0)))
  251. __choose_pgpath(m);
  252. pgpath = m->current_pgpath;
  253. if (was_queued)
  254. m->queue_size--;
  255. if ((pgpath && m->queue_io) ||
  256. (!pgpath && m->queue_if_no_path)) {
  257. /* Queue for the daemon to resubmit */
  258. bio_list_add(&m->queued_ios, bio);
  259. m->queue_size++;
  260. if ((m->pg_init_required && !m->pg_init_in_progress) ||
  261. !m->queue_io)
  262. queue_work(kmultipathd, &m->process_queued_ios);
  263. pgpath = NULL;
  264. r = DM_MAPIO_SUBMITTED;
  265. } else if (pgpath)
  266. bio->bi_bdev = pgpath->path.dev->bdev;
  267. else if (__must_push_back(m))
  268. r = DM_MAPIO_REQUEUE;
  269. else
  270. r = -EIO; /* Failed */
  271. mpio->pgpath = pgpath;
  272. spin_unlock_irqrestore(&m->lock, flags);
  273. return r;
  274. }
  275. /*
  276. * If we run out of usable paths, should we queue I/O or error it?
  277. */
  278. static int queue_if_no_path(struct multipath *m, unsigned queue_if_no_path,
  279. unsigned save_old_value)
  280. {
  281. unsigned long flags;
  282. spin_lock_irqsave(&m->lock, flags);
  283. if (save_old_value)
  284. m->saved_queue_if_no_path = m->queue_if_no_path;
  285. else
  286. m->saved_queue_if_no_path = queue_if_no_path;
  287. m->queue_if_no_path = queue_if_no_path;
  288. if (!m->queue_if_no_path && m->queue_size)
  289. queue_work(kmultipathd, &m->process_queued_ios);
  290. spin_unlock_irqrestore(&m->lock, flags);
  291. return 0;
  292. }
  293. /*-----------------------------------------------------------------
  294. * The multipath daemon is responsible for resubmitting queued ios.
  295. *---------------------------------------------------------------*/
  296. static void dispatch_queued_ios(struct multipath *m)
  297. {
  298. int r;
  299. unsigned long flags;
  300. struct bio *bio = NULL, *next;
  301. struct dm_mpath_io *mpio;
  302. union map_info *info;
  303. spin_lock_irqsave(&m->lock, flags);
  304. bio = bio_list_get(&m->queued_ios);
  305. spin_unlock_irqrestore(&m->lock, flags);
  306. while (bio) {
  307. next = bio->bi_next;
  308. bio->bi_next = NULL;
  309. info = dm_get_mapinfo(bio);
  310. mpio = info->ptr;
  311. r = map_io(m, bio, mpio, 1);
  312. if (r < 0)
  313. bio_endio(bio, r);
  314. else if (r == DM_MAPIO_REMAPPED)
  315. generic_make_request(bio);
  316. else if (r == DM_MAPIO_REQUEUE)
  317. bio_endio(bio, -EIO);
  318. bio = next;
  319. }
  320. }
  321. static void process_queued_ios(struct work_struct *work)
  322. {
  323. struct multipath *m =
  324. container_of(work, struct multipath, process_queued_ios);
  325. struct pgpath *pgpath = NULL;
  326. unsigned init_required = 0, must_queue = 1;
  327. unsigned long flags;
  328. spin_lock_irqsave(&m->lock, flags);
  329. if (!m->queue_size)
  330. goto out;
  331. if (!m->current_pgpath)
  332. __choose_pgpath(m);
  333. pgpath = m->current_pgpath;
  334. if ((pgpath && !m->queue_io) ||
  335. (!pgpath && !m->queue_if_no_path))
  336. must_queue = 0;
  337. if (m->pg_init_required && !m->pg_init_in_progress) {
  338. m->pg_init_count++;
  339. m->pg_init_required = 0;
  340. m->pg_init_in_progress = 1;
  341. init_required = 1;
  342. }
  343. out:
  344. spin_unlock_irqrestore(&m->lock, flags);
  345. if (init_required)
  346. queue_work(kmpath_handlerd, &m->activate_path);
  347. if (!must_queue)
  348. dispatch_queued_ios(m);
  349. }
  350. /*
  351. * An event is triggered whenever a path is taken out of use.
  352. * Includes path failure and PG bypass.
  353. */
  354. static void trigger_event(struct work_struct *work)
  355. {
  356. struct multipath *m =
  357. container_of(work, struct multipath, trigger_event);
  358. dm_table_event(m->ti->table);
  359. }
  360. /*-----------------------------------------------------------------
  361. * Constructor/argument parsing:
  362. * <#multipath feature args> [<arg>]*
  363. * <#hw_handler args> [hw_handler [<arg>]*]
  364. * <#priority groups>
  365. * <initial priority group>
  366. * [<selector> <#selector args> [<arg>]*
  367. * <#paths> <#per-path selector args>
  368. * [<path> [<arg>]* ]+ ]+
  369. *---------------------------------------------------------------*/
  370. struct param {
  371. unsigned min;
  372. unsigned max;
  373. char *error;
  374. };
  375. static int read_param(struct param *param, char *str, unsigned *v, char **error)
  376. {
  377. if (!str ||
  378. (sscanf(str, "%u", v) != 1) ||
  379. (*v < param->min) ||
  380. (*v > param->max)) {
  381. *error = param->error;
  382. return -EINVAL;
  383. }
  384. return 0;
  385. }
  386. struct arg_set {
  387. unsigned argc;
  388. char **argv;
  389. };
  390. static char *shift(struct arg_set *as)
  391. {
  392. char *r;
  393. if (as->argc) {
  394. as->argc--;
  395. r = *as->argv;
  396. as->argv++;
  397. return r;
  398. }
  399. return NULL;
  400. }
  401. static void consume(struct arg_set *as, unsigned n)
  402. {
  403. BUG_ON (as->argc < n);
  404. as->argc -= n;
  405. as->argv += n;
  406. }
  407. static int parse_path_selector(struct arg_set *as, struct priority_group *pg,
  408. struct dm_target *ti)
  409. {
  410. int r;
  411. struct path_selector_type *pst;
  412. unsigned ps_argc;
  413. static struct param _params[] = {
  414. {0, 1024, "invalid number of path selector args"},
  415. };
  416. pst = dm_get_path_selector(shift(as));
  417. if (!pst) {
  418. ti->error = "unknown path selector type";
  419. return -EINVAL;
  420. }
  421. r = read_param(_params, shift(as), &ps_argc, &ti->error);
  422. if (r)
  423. return -EINVAL;
  424. r = pst->create(&pg->ps, ps_argc, as->argv);
  425. if (r) {
  426. dm_put_path_selector(pst);
  427. ti->error = "path selector constructor failed";
  428. return r;
  429. }
  430. pg->ps.type = pst;
  431. consume(as, ps_argc);
  432. return 0;
  433. }
  434. static struct pgpath *parse_path(struct arg_set *as, struct path_selector *ps,
  435. struct dm_target *ti)
  436. {
  437. int r;
  438. struct pgpath *p;
  439. /* we need at least a path arg */
  440. if (as->argc < 1) {
  441. ti->error = "no device given";
  442. return NULL;
  443. }
  444. p = alloc_pgpath();
  445. if (!p)
  446. return NULL;
  447. r = dm_get_device(ti, shift(as), ti->begin, ti->len,
  448. dm_table_get_mode(ti->table), &p->path.dev);
  449. if (r) {
  450. ti->error = "error getting device";
  451. goto bad;
  452. }
  453. r = ps->type->add_path(ps, &p->path, as->argc, as->argv, &ti->error);
  454. if (r) {
  455. dm_put_device(ti, p->path.dev);
  456. goto bad;
  457. }
  458. return p;
  459. bad:
  460. free_pgpath(p);
  461. return NULL;
  462. }
  463. static struct priority_group *parse_priority_group(struct arg_set *as,
  464. struct multipath *m)
  465. {
  466. static struct param _params[] = {
  467. {1, 1024, "invalid number of paths"},
  468. {0, 1024, "invalid number of selector args"}
  469. };
  470. int r;
  471. unsigned i, nr_selector_args, nr_params;
  472. struct priority_group *pg;
  473. struct dm_target *ti = m->ti;
  474. if (as->argc < 2) {
  475. as->argc = 0;
  476. ti->error = "not enough priority group aruments";
  477. return NULL;
  478. }
  479. pg = alloc_priority_group();
  480. if (!pg) {
  481. ti->error = "couldn't allocate priority group";
  482. return NULL;
  483. }
  484. pg->m = m;
  485. r = parse_path_selector(as, pg, ti);
  486. if (r)
  487. goto bad;
  488. /*
  489. * read the paths
  490. */
  491. r = read_param(_params, shift(as), &pg->nr_pgpaths, &ti->error);
  492. if (r)
  493. goto bad;
  494. r = read_param(_params + 1, shift(as), &nr_selector_args, &ti->error);
  495. if (r)
  496. goto bad;
  497. nr_params = 1 + nr_selector_args;
  498. for (i = 0; i < pg->nr_pgpaths; i++) {
  499. struct pgpath *pgpath;
  500. struct arg_set path_args;
  501. if (as->argc < nr_params)
  502. goto bad;
  503. path_args.argc = nr_params;
  504. path_args.argv = as->argv;
  505. pgpath = parse_path(&path_args, &pg->ps, ti);
  506. if (!pgpath)
  507. goto bad;
  508. pgpath->pg = pg;
  509. list_add_tail(&pgpath->list, &pg->pgpaths);
  510. consume(as, nr_params);
  511. }
  512. return pg;
  513. bad:
  514. free_priority_group(pg, ti);
  515. return NULL;
  516. }
  517. static int parse_hw_handler(struct arg_set *as, struct multipath *m)
  518. {
  519. unsigned hw_argc;
  520. struct dm_target *ti = m->ti;
  521. static struct param _params[] = {
  522. {0, 1024, "invalid number of hardware handler args"},
  523. };
  524. if (read_param(_params, shift(as), &hw_argc, &ti->error))
  525. return -EINVAL;
  526. if (!hw_argc)
  527. return 0;
  528. m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
  529. request_module("scsi_dh_%s", m->hw_handler_name);
  530. if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
  531. ti->error = "unknown hardware handler type";
  532. return -EINVAL;
  533. }
  534. consume(as, hw_argc - 1);
  535. return 0;
  536. }
  537. static int parse_features(struct arg_set *as, struct multipath *m)
  538. {
  539. int r;
  540. unsigned argc;
  541. struct dm_target *ti = m->ti;
  542. const char *param_name;
  543. static struct param _params[] = {
  544. {0, 3, "invalid number of feature args"},
  545. {1, 50, "pg_init_retries must be between 1 and 50"},
  546. };
  547. r = read_param(_params, shift(as), &argc, &ti->error);
  548. if (r)
  549. return -EINVAL;
  550. if (!argc)
  551. return 0;
  552. do {
  553. param_name = shift(as);
  554. argc--;
  555. if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
  556. r = queue_if_no_path(m, 1, 0);
  557. continue;
  558. }
  559. if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
  560. (argc >= 1)) {
  561. r = read_param(_params + 1, shift(as),
  562. &m->pg_init_retries, &ti->error);
  563. argc--;
  564. continue;
  565. }
  566. ti->error = "Unrecognised multipath feature request";
  567. r = -EINVAL;
  568. } while (argc && !r);
  569. return r;
  570. }
  571. static int multipath_ctr(struct dm_target *ti, unsigned int argc,
  572. char **argv)
  573. {
  574. /* target parameters */
  575. static struct param _params[] = {
  576. {1, 1024, "invalid number of priority groups"},
  577. {1, 1024, "invalid initial priority group number"},
  578. };
  579. int r;
  580. struct multipath *m;
  581. struct arg_set as;
  582. unsigned pg_count = 0;
  583. unsigned next_pg_num;
  584. as.argc = argc;
  585. as.argv = argv;
  586. m = alloc_multipath(ti);
  587. if (!m) {
  588. ti->error = "can't allocate multipath";
  589. return -EINVAL;
  590. }
  591. r = parse_features(&as, m);
  592. if (r)
  593. goto bad;
  594. r = parse_hw_handler(&as, m);
  595. if (r)
  596. goto bad;
  597. r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
  598. if (r)
  599. goto bad;
  600. r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
  601. if (r)
  602. goto bad;
  603. /* parse the priority groups */
  604. while (as.argc) {
  605. struct priority_group *pg;
  606. pg = parse_priority_group(&as, m);
  607. if (!pg) {
  608. r = -EINVAL;
  609. goto bad;
  610. }
  611. m->nr_valid_paths += pg->nr_pgpaths;
  612. list_add_tail(&pg->list, &m->priority_groups);
  613. pg_count++;
  614. pg->pg_num = pg_count;
  615. if (!--next_pg_num)
  616. m->next_pg = pg;
  617. }
  618. if (pg_count != m->nr_priority_groups) {
  619. ti->error = "priority group count mismatch";
  620. r = -EINVAL;
  621. goto bad;
  622. }
  623. return 0;
  624. bad:
  625. free_multipath(m);
  626. return r;
  627. }
  628. static void multipath_dtr(struct dm_target *ti)
  629. {
  630. struct multipath *m = (struct multipath *) ti->private;
  631. flush_workqueue(kmpath_handlerd);
  632. flush_workqueue(kmultipathd);
  633. free_multipath(m);
  634. }
  635. /*
  636. * Map bios, recording original fields for later in case we have to resubmit
  637. */
  638. static int multipath_map(struct dm_target *ti, struct bio *bio,
  639. union map_info *map_context)
  640. {
  641. int r;
  642. struct dm_mpath_io *mpio;
  643. struct multipath *m = (struct multipath *) ti->private;
  644. mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
  645. dm_bio_record(&mpio->details, bio);
  646. map_context->ptr = mpio;
  647. bio->bi_rw |= (1 << BIO_RW_FAILFAST);
  648. r = map_io(m, bio, mpio, 0);
  649. if (r < 0 || r == DM_MAPIO_REQUEUE)
  650. mempool_free(mpio, m->mpio_pool);
  651. return r;
  652. }
  653. /*
  654. * Take a path out of use.
  655. */
  656. static int fail_path(struct pgpath *pgpath)
  657. {
  658. unsigned long flags;
  659. struct multipath *m = pgpath->pg->m;
  660. spin_lock_irqsave(&m->lock, flags);
  661. if (!pgpath->path.is_active)
  662. goto out;
  663. DMWARN("Failing path %s.", pgpath->path.dev->name);
  664. pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
  665. pgpath->path.is_active = 0;
  666. pgpath->fail_count++;
  667. m->nr_valid_paths--;
  668. if (pgpath == m->current_pgpath)
  669. m->current_pgpath = NULL;
  670. dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
  671. pgpath->path.dev->name, m->nr_valid_paths);
  672. queue_work(kmultipathd, &m->trigger_event);
  673. out:
  674. spin_unlock_irqrestore(&m->lock, flags);
  675. return 0;
  676. }
  677. /*
  678. * Reinstate a previously-failed path
  679. */
  680. static int reinstate_path(struct pgpath *pgpath)
  681. {
  682. int r = 0;
  683. unsigned long flags;
  684. struct multipath *m = pgpath->pg->m;
  685. spin_lock_irqsave(&m->lock, flags);
  686. if (pgpath->path.is_active)
  687. goto out;
  688. if (!pgpath->pg->ps.type) {
  689. DMWARN("Reinstate path not supported by path selector %s",
  690. pgpath->pg->ps.type->name);
  691. r = -EINVAL;
  692. goto out;
  693. }
  694. r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
  695. if (r)
  696. goto out;
  697. pgpath->path.is_active = 1;
  698. m->current_pgpath = NULL;
  699. if (!m->nr_valid_paths++ && m->queue_size)
  700. queue_work(kmultipathd, &m->process_queued_ios);
  701. dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
  702. pgpath->path.dev->name, m->nr_valid_paths);
  703. queue_work(kmultipathd, &m->trigger_event);
  704. out:
  705. spin_unlock_irqrestore(&m->lock, flags);
  706. return r;
  707. }
  708. /*
  709. * Fail or reinstate all paths that match the provided struct dm_dev.
  710. */
  711. static int action_dev(struct multipath *m, struct dm_dev *dev,
  712. action_fn action)
  713. {
  714. int r = 0;
  715. struct pgpath *pgpath;
  716. struct priority_group *pg;
  717. list_for_each_entry(pg, &m->priority_groups, list) {
  718. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  719. if (pgpath->path.dev == dev)
  720. r = action(pgpath);
  721. }
  722. }
  723. return r;
  724. }
  725. /*
  726. * Temporarily try to avoid having to use the specified PG
  727. */
  728. static void bypass_pg(struct multipath *m, struct priority_group *pg,
  729. int bypassed)
  730. {
  731. unsigned long flags;
  732. spin_lock_irqsave(&m->lock, flags);
  733. pg->bypassed = bypassed;
  734. m->current_pgpath = NULL;
  735. m->current_pg = NULL;
  736. spin_unlock_irqrestore(&m->lock, flags);
  737. queue_work(kmultipathd, &m->trigger_event);
  738. }
  739. /*
  740. * Switch to using the specified PG from the next I/O that gets mapped
  741. */
  742. static int switch_pg_num(struct multipath *m, const char *pgstr)
  743. {
  744. struct priority_group *pg;
  745. unsigned pgnum;
  746. unsigned long flags;
  747. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  748. (pgnum > m->nr_priority_groups)) {
  749. DMWARN("invalid PG number supplied to switch_pg_num");
  750. return -EINVAL;
  751. }
  752. spin_lock_irqsave(&m->lock, flags);
  753. list_for_each_entry(pg, &m->priority_groups, list) {
  754. pg->bypassed = 0;
  755. if (--pgnum)
  756. continue;
  757. m->current_pgpath = NULL;
  758. m->current_pg = NULL;
  759. m->next_pg = pg;
  760. }
  761. spin_unlock_irqrestore(&m->lock, flags);
  762. queue_work(kmultipathd, &m->trigger_event);
  763. return 0;
  764. }
  765. /*
  766. * Set/clear bypassed status of a PG.
  767. * PGs are numbered upwards from 1 in the order they were declared.
  768. */
  769. static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
  770. {
  771. struct priority_group *pg;
  772. unsigned pgnum;
  773. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  774. (pgnum > m->nr_priority_groups)) {
  775. DMWARN("invalid PG number supplied to bypass_pg");
  776. return -EINVAL;
  777. }
  778. list_for_each_entry(pg, &m->priority_groups, list) {
  779. if (!--pgnum)
  780. break;
  781. }
  782. bypass_pg(m, pg, bypassed);
  783. return 0;
  784. }
  785. /*
  786. * Should we retry pg_init immediately?
  787. */
  788. static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
  789. {
  790. unsigned long flags;
  791. int limit_reached = 0;
  792. spin_lock_irqsave(&m->lock, flags);
  793. if (m->pg_init_count <= m->pg_init_retries)
  794. m->pg_init_required = 1;
  795. else
  796. limit_reached = 1;
  797. spin_unlock_irqrestore(&m->lock, flags);
  798. return limit_reached;
  799. }
  800. /*
  801. * pg_init must call this when it has completed its initialisation
  802. */
  803. void dm_pg_init_complete(struct dm_path *path, unsigned err_flags)
  804. {
  805. struct pgpath *pgpath = path_to_pgpath(path);
  806. struct priority_group *pg = pgpath->pg;
  807. struct multipath *m = pg->m;
  808. unsigned long flags;
  809. /*
  810. * If requested, retry pg_init until maximum number of retries exceeded.
  811. * If retry not requested and PG already bypassed, always fail the path.
  812. */
  813. if (err_flags & MP_RETRY) {
  814. if (pg_init_limit_reached(m, pgpath))
  815. err_flags |= MP_FAIL_PATH;
  816. } else if (err_flags && pg->bypassed)
  817. err_flags |= MP_FAIL_PATH;
  818. if (err_flags & MP_FAIL_PATH)
  819. fail_path(pgpath);
  820. if (err_flags & MP_BYPASS_PG)
  821. bypass_pg(m, pg, 1);
  822. spin_lock_irqsave(&m->lock, flags);
  823. if (err_flags & ~MP_RETRY) {
  824. m->current_pgpath = NULL;
  825. m->current_pg = NULL;
  826. } else if (!m->pg_init_required)
  827. m->queue_io = 0;
  828. m->pg_init_in_progress = 0;
  829. queue_work(kmultipathd, &m->process_queued_ios);
  830. spin_unlock_irqrestore(&m->lock, flags);
  831. }
  832. static void pg_init_done(struct dm_path *path, int errors)
  833. {
  834. struct pgpath *pgpath = path_to_pgpath(path);
  835. struct priority_group *pg = pgpath->pg;
  836. struct multipath *m = pg->m;
  837. unsigned long flags;
  838. /* device or driver problems */
  839. switch (errors) {
  840. case SCSI_DH_OK:
  841. break;
  842. case SCSI_DH_NOSYS:
  843. if (!m->hw_handler_name) {
  844. errors = 0;
  845. break;
  846. }
  847. DMERR("Cannot failover device because scsi_dh_%s was not "
  848. "loaded.", m->hw_handler_name);
  849. /*
  850. * Fail path for now, so we do not ping pong
  851. */
  852. fail_path(pgpath);
  853. break;
  854. case SCSI_DH_DEV_TEMP_BUSY:
  855. /*
  856. * Probably doing something like FW upgrade on the
  857. * controller so try the other pg.
  858. */
  859. bypass_pg(m, pg, 1);
  860. break;
  861. /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
  862. case SCSI_DH_RETRY:
  863. case SCSI_DH_IMM_RETRY:
  864. case SCSI_DH_RES_TEMP_UNAVAIL:
  865. if (pg_init_limit_reached(m, pgpath))
  866. fail_path(pgpath);
  867. errors = 0;
  868. break;
  869. default:
  870. /*
  871. * We probably do not want to fail the path for a device
  872. * error, but this is what the old dm did. In future
  873. * patches we can do more advanced handling.
  874. */
  875. fail_path(pgpath);
  876. }
  877. spin_lock_irqsave(&m->lock, flags);
  878. if (errors) {
  879. DMERR("Could not failover device. Error %d.", errors);
  880. m->current_pgpath = NULL;
  881. m->current_pg = NULL;
  882. } else if (!m->pg_init_required) {
  883. m->queue_io = 0;
  884. pg->bypassed = 0;
  885. }
  886. m->pg_init_in_progress = 0;
  887. queue_work(kmultipathd, &m->process_queued_ios);
  888. spin_unlock_irqrestore(&m->lock, flags);
  889. }
  890. static void activate_path(struct work_struct *work)
  891. {
  892. int ret;
  893. struct multipath *m =
  894. container_of(work, struct multipath, activate_path);
  895. struct dm_path *path = &m->current_pgpath->path;
  896. ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
  897. pg_init_done(path, ret);
  898. }
  899. /*
  900. * end_io handling
  901. */
  902. static int do_end_io(struct multipath *m, struct bio *bio,
  903. int error, struct dm_mpath_io *mpio)
  904. {
  905. unsigned long flags;
  906. if (!error)
  907. return 0; /* I/O complete */
  908. if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
  909. return error;
  910. if (error == -EOPNOTSUPP)
  911. return error;
  912. spin_lock_irqsave(&m->lock, flags);
  913. if (!m->nr_valid_paths) {
  914. if (__must_push_back(m)) {
  915. spin_unlock_irqrestore(&m->lock, flags);
  916. return DM_ENDIO_REQUEUE;
  917. } else if (!m->queue_if_no_path) {
  918. spin_unlock_irqrestore(&m->lock, flags);
  919. return -EIO;
  920. } else {
  921. spin_unlock_irqrestore(&m->lock, flags);
  922. goto requeue;
  923. }
  924. }
  925. spin_unlock_irqrestore(&m->lock, flags);
  926. if (mpio->pgpath)
  927. fail_path(mpio->pgpath);
  928. requeue:
  929. dm_bio_restore(&mpio->details, bio);
  930. /* queue for the daemon to resubmit or fail */
  931. spin_lock_irqsave(&m->lock, flags);
  932. bio_list_add(&m->queued_ios, bio);
  933. m->queue_size++;
  934. if (!m->queue_io)
  935. queue_work(kmultipathd, &m->process_queued_ios);
  936. spin_unlock_irqrestore(&m->lock, flags);
  937. return DM_ENDIO_INCOMPLETE; /* io not complete */
  938. }
  939. static int multipath_end_io(struct dm_target *ti, struct bio *bio,
  940. int error, union map_info *map_context)
  941. {
  942. struct multipath *m = ti->private;
  943. struct dm_mpath_io *mpio = map_context->ptr;
  944. struct pgpath *pgpath = mpio->pgpath;
  945. struct path_selector *ps;
  946. int r;
  947. r = do_end_io(m, bio, error, mpio);
  948. if (pgpath) {
  949. ps = &pgpath->pg->ps;
  950. if (ps->type->end_io)
  951. ps->type->end_io(ps, &pgpath->path);
  952. }
  953. if (r != DM_ENDIO_INCOMPLETE)
  954. mempool_free(mpio, m->mpio_pool);
  955. return r;
  956. }
  957. /*
  958. * Suspend can't complete until all the I/O is processed so if
  959. * the last path fails we must error any remaining I/O.
  960. * Note that if the freeze_bdev fails while suspending, the
  961. * queue_if_no_path state is lost - userspace should reset it.
  962. */
  963. static void multipath_presuspend(struct dm_target *ti)
  964. {
  965. struct multipath *m = (struct multipath *) ti->private;
  966. queue_if_no_path(m, 0, 1);
  967. }
  968. /*
  969. * Restore the queue_if_no_path setting.
  970. */
  971. static void multipath_resume(struct dm_target *ti)
  972. {
  973. struct multipath *m = (struct multipath *) ti->private;
  974. unsigned long flags;
  975. spin_lock_irqsave(&m->lock, flags);
  976. m->queue_if_no_path = m->saved_queue_if_no_path;
  977. spin_unlock_irqrestore(&m->lock, flags);
  978. }
  979. /*
  980. * Info output has the following format:
  981. * num_multipath_feature_args [multipath_feature_args]*
  982. * num_handler_status_args [handler_status_args]*
  983. * num_groups init_group_number
  984. * [A|D|E num_ps_status_args [ps_status_args]*
  985. * num_paths num_selector_args
  986. * [path_dev A|F fail_count [selector_args]* ]+ ]+
  987. *
  988. * Table output has the following format (identical to the constructor string):
  989. * num_feature_args [features_args]*
  990. * num_handler_args hw_handler [hw_handler_args]*
  991. * num_groups init_group_number
  992. * [priority selector-name num_ps_args [ps_args]*
  993. * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
  994. */
  995. static int multipath_status(struct dm_target *ti, status_type_t type,
  996. char *result, unsigned int maxlen)
  997. {
  998. int sz = 0;
  999. unsigned long flags;
  1000. struct multipath *m = (struct multipath *) ti->private;
  1001. struct priority_group *pg;
  1002. struct pgpath *p;
  1003. unsigned pg_num;
  1004. char state;
  1005. spin_lock_irqsave(&m->lock, flags);
  1006. /* Features */
  1007. if (type == STATUSTYPE_INFO)
  1008. DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
  1009. else {
  1010. DMEMIT("%u ", m->queue_if_no_path +
  1011. (m->pg_init_retries > 0) * 2);
  1012. if (m->queue_if_no_path)
  1013. DMEMIT("queue_if_no_path ");
  1014. if (m->pg_init_retries)
  1015. DMEMIT("pg_init_retries %u ", m->pg_init_retries);
  1016. }
  1017. if (!m->hw_handler_name || type == STATUSTYPE_INFO)
  1018. DMEMIT("0 ");
  1019. else
  1020. DMEMIT("1 %s ", m->hw_handler_name);
  1021. DMEMIT("%u ", m->nr_priority_groups);
  1022. if (m->next_pg)
  1023. pg_num = m->next_pg->pg_num;
  1024. else if (m->current_pg)
  1025. pg_num = m->current_pg->pg_num;
  1026. else
  1027. pg_num = 1;
  1028. DMEMIT("%u ", pg_num);
  1029. switch (type) {
  1030. case STATUSTYPE_INFO:
  1031. list_for_each_entry(pg, &m->priority_groups, list) {
  1032. if (pg->bypassed)
  1033. state = 'D'; /* Disabled */
  1034. else if (pg == m->current_pg)
  1035. state = 'A'; /* Currently Active */
  1036. else
  1037. state = 'E'; /* Enabled */
  1038. DMEMIT("%c ", state);
  1039. if (pg->ps.type->status)
  1040. sz += pg->ps.type->status(&pg->ps, NULL, type,
  1041. result + sz,
  1042. maxlen - sz);
  1043. else
  1044. DMEMIT("0 ");
  1045. DMEMIT("%u %u ", pg->nr_pgpaths,
  1046. pg->ps.type->info_args);
  1047. list_for_each_entry(p, &pg->pgpaths, list) {
  1048. DMEMIT("%s %s %u ", p->path.dev->name,
  1049. p->path.is_active ? "A" : "F",
  1050. p->fail_count);
  1051. if (pg->ps.type->status)
  1052. sz += pg->ps.type->status(&pg->ps,
  1053. &p->path, type, result + sz,
  1054. maxlen - sz);
  1055. }
  1056. }
  1057. break;
  1058. case STATUSTYPE_TABLE:
  1059. list_for_each_entry(pg, &m->priority_groups, list) {
  1060. DMEMIT("%s ", pg->ps.type->name);
  1061. if (pg->ps.type->status)
  1062. sz += pg->ps.type->status(&pg->ps, NULL, type,
  1063. result + sz,
  1064. maxlen - sz);
  1065. else
  1066. DMEMIT("0 ");
  1067. DMEMIT("%u %u ", pg->nr_pgpaths,
  1068. pg->ps.type->table_args);
  1069. list_for_each_entry(p, &pg->pgpaths, list) {
  1070. DMEMIT("%s ", p->path.dev->name);
  1071. if (pg->ps.type->status)
  1072. sz += pg->ps.type->status(&pg->ps,
  1073. &p->path, type, result + sz,
  1074. maxlen - sz);
  1075. }
  1076. }
  1077. break;
  1078. }
  1079. spin_unlock_irqrestore(&m->lock, flags);
  1080. return 0;
  1081. }
  1082. static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
  1083. {
  1084. int r;
  1085. struct dm_dev *dev;
  1086. struct multipath *m = (struct multipath *) ti->private;
  1087. action_fn action;
  1088. if (argc == 1) {
  1089. if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
  1090. return queue_if_no_path(m, 1, 0);
  1091. else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
  1092. return queue_if_no_path(m, 0, 0);
  1093. }
  1094. if (argc != 2)
  1095. goto error;
  1096. if (!strnicmp(argv[0], MESG_STR("disable_group")))
  1097. return bypass_pg_num(m, argv[1], 1);
  1098. else if (!strnicmp(argv[0], MESG_STR("enable_group")))
  1099. return bypass_pg_num(m, argv[1], 0);
  1100. else if (!strnicmp(argv[0], MESG_STR("switch_group")))
  1101. return switch_pg_num(m, argv[1]);
  1102. else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
  1103. action = reinstate_path;
  1104. else if (!strnicmp(argv[0], MESG_STR("fail_path")))
  1105. action = fail_path;
  1106. else
  1107. goto error;
  1108. r = dm_get_device(ti, argv[1], ti->begin, ti->len,
  1109. dm_table_get_mode(ti->table), &dev);
  1110. if (r) {
  1111. DMWARN("message: error getting device %s",
  1112. argv[1]);
  1113. return -EINVAL;
  1114. }
  1115. r = action_dev(m, dev, action);
  1116. dm_put_device(ti, dev);
  1117. return r;
  1118. error:
  1119. DMWARN("Unrecognised multipath message received.");
  1120. return -EINVAL;
  1121. }
  1122. static int multipath_ioctl(struct dm_target *ti, struct inode *inode,
  1123. struct file *filp, unsigned int cmd,
  1124. unsigned long arg)
  1125. {
  1126. struct multipath *m = (struct multipath *) ti->private;
  1127. struct block_device *bdev = NULL;
  1128. unsigned long flags;
  1129. struct file fake_file = {};
  1130. struct dentry fake_dentry = {};
  1131. int r = 0;
  1132. fake_file.f_path.dentry = &fake_dentry;
  1133. spin_lock_irqsave(&m->lock, flags);
  1134. if (!m->current_pgpath)
  1135. __choose_pgpath(m);
  1136. if (m->current_pgpath) {
  1137. bdev = m->current_pgpath->path.dev->bdev;
  1138. fake_dentry.d_inode = bdev->bd_inode;
  1139. fake_file.f_mode = m->current_pgpath->path.dev->mode;
  1140. }
  1141. if (m->queue_io)
  1142. r = -EAGAIN;
  1143. else if (!bdev)
  1144. r = -EIO;
  1145. spin_unlock_irqrestore(&m->lock, flags);
  1146. return r ? : blkdev_driver_ioctl(bdev->bd_inode, &fake_file,
  1147. bdev->bd_disk, cmd, arg);
  1148. }
  1149. /*-----------------------------------------------------------------
  1150. * Module setup
  1151. *---------------------------------------------------------------*/
  1152. static struct target_type multipath_target = {
  1153. .name = "multipath",
  1154. .version = {1, 0, 5},
  1155. .module = THIS_MODULE,
  1156. .ctr = multipath_ctr,
  1157. .dtr = multipath_dtr,
  1158. .map = multipath_map,
  1159. .end_io = multipath_end_io,
  1160. .presuspend = multipath_presuspend,
  1161. .resume = multipath_resume,
  1162. .status = multipath_status,
  1163. .message = multipath_message,
  1164. .ioctl = multipath_ioctl,
  1165. };
  1166. static int __init dm_multipath_init(void)
  1167. {
  1168. int r;
  1169. /* allocate a slab for the dm_ios */
  1170. _mpio_cache = KMEM_CACHE(dm_mpath_io, 0);
  1171. if (!_mpio_cache)
  1172. return -ENOMEM;
  1173. r = dm_register_target(&multipath_target);
  1174. if (r < 0) {
  1175. DMERR("register failed %d", r);
  1176. kmem_cache_destroy(_mpio_cache);
  1177. return -EINVAL;
  1178. }
  1179. kmultipathd = create_workqueue("kmpathd");
  1180. if (!kmultipathd) {
  1181. DMERR("failed to create workqueue kmpathd");
  1182. dm_unregister_target(&multipath_target);
  1183. kmem_cache_destroy(_mpio_cache);
  1184. return -ENOMEM;
  1185. }
  1186. /*
  1187. * A separate workqueue is used to handle the device handlers
  1188. * to avoid overloading existing workqueue. Overloading the
  1189. * old workqueue would also create a bottleneck in the
  1190. * path of the storage hardware device activation.
  1191. */
  1192. kmpath_handlerd = create_singlethread_workqueue("kmpath_handlerd");
  1193. if (!kmpath_handlerd) {
  1194. DMERR("failed to create workqueue kmpath_handlerd");
  1195. destroy_workqueue(kmultipathd);
  1196. dm_unregister_target(&multipath_target);
  1197. kmem_cache_destroy(_mpio_cache);
  1198. return -ENOMEM;
  1199. }
  1200. DMINFO("version %u.%u.%u loaded",
  1201. multipath_target.version[0], multipath_target.version[1],
  1202. multipath_target.version[2]);
  1203. return r;
  1204. }
  1205. static void __exit dm_multipath_exit(void)
  1206. {
  1207. int r;
  1208. destroy_workqueue(kmpath_handlerd);
  1209. destroy_workqueue(kmultipathd);
  1210. r = dm_unregister_target(&multipath_target);
  1211. if (r < 0)
  1212. DMERR("target unregister failed %d", r);
  1213. kmem_cache_destroy(_mpio_cache);
  1214. }
  1215. EXPORT_SYMBOL_GPL(dm_pg_init_complete);
  1216. module_init(dm_multipath_init);
  1217. module_exit(dm_multipath_exit);
  1218. MODULE_DESCRIPTION(DM_NAME " multipath target");
  1219. MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
  1220. MODULE_LICENSE("GPL");