dm-mpath.c 33 KB

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