dm-mpath.c 34 KB

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