dm-mpath.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. /*
  2. * Copyright (C) 2003 Sistina Software Limited.
  3. * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  4. *
  5. * This file is released under the GPL.
  6. */
  7. #include <linux/device-mapper.h>
  8. #include "dm-path-selector.h"
  9. #include "dm-bio-record.h"
  10. #include "dm-uevent.h"
  11. #include <linux/ctype.h>
  12. #include <linux/init.h>
  13. #include <linux/mempool.h>
  14. #include <linux/module.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/slab.h>
  17. #include <linux/time.h>
  18. #include <linux/workqueue.h>
  19. #include <scsi/scsi_dh.h>
  20. #include <asm/atomic.h>
  21. #define DM_MSG_PREFIX "multipath"
  22. #define MESG_STR(x) x, sizeof(x)
  23. /* Path properties */
  24. struct pgpath {
  25. struct list_head list;
  26. struct priority_group *pg; /* Owning PG */
  27. unsigned is_active; /* Path status */
  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->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. if ((pgpath && !m->queue_io) ||
  353. (!pgpath && !m->queue_if_no_path))
  354. must_queue = 0;
  355. if (m->pg_init_required && !m->pg_init_in_progress && pgpath) {
  356. m->pgpath_to_activate = pgpath;
  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 ERR_PTR(-EINVAL);
  465. }
  466. p = alloc_pgpath();
  467. if (!p)
  468. return ERR_PTR(-ENOMEM);
  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 ERR_PTR(r);
  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 arguments";
  507. return ERR_PTR(-EINVAL);
  508. }
  509. pg = alloc_priority_group();
  510. if (!pg) {
  511. ti->error = "couldn't allocate priority group";
  512. return ERR_PTR(-ENOMEM);
  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 (IS_ERR(pgpath)) {
  539. r = PTR_ERR(pgpath);
  540. goto bad;
  541. }
  542. pgpath->pg = pg;
  543. list_add_tail(&pgpath->list, &pg->pgpaths);
  544. consume(as, nr_params);
  545. }
  546. return pg;
  547. bad:
  548. free_priority_group(pg, ti);
  549. return ERR_PTR(r);
  550. }
  551. static int parse_hw_handler(struct arg_set *as, struct multipath *m)
  552. {
  553. unsigned hw_argc;
  554. struct dm_target *ti = m->ti;
  555. static struct param _params[] = {
  556. {0, 1024, "invalid number of hardware handler args"},
  557. };
  558. if (read_param(_params, shift(as), &hw_argc, &ti->error))
  559. return -EINVAL;
  560. if (!hw_argc)
  561. return 0;
  562. m->hw_handler_name = kstrdup(shift(as), GFP_KERNEL);
  563. request_module("scsi_dh_%s", m->hw_handler_name);
  564. if (scsi_dh_handler_exist(m->hw_handler_name) == 0) {
  565. ti->error = "unknown hardware handler type";
  566. kfree(m->hw_handler_name);
  567. m->hw_handler_name = NULL;
  568. return -EINVAL;
  569. }
  570. if (hw_argc > 1)
  571. DMWARN("Ignoring user-specified arguments for "
  572. "hardware handler \"%s\"", m->hw_handler_name);
  573. consume(as, hw_argc - 1);
  574. return 0;
  575. }
  576. static int parse_features(struct arg_set *as, struct multipath *m)
  577. {
  578. int r;
  579. unsigned argc;
  580. struct dm_target *ti = m->ti;
  581. const char *param_name;
  582. static struct param _params[] = {
  583. {0, 3, "invalid number of feature args"},
  584. {1, 50, "pg_init_retries must be between 1 and 50"},
  585. };
  586. r = read_param(_params, shift(as), &argc, &ti->error);
  587. if (r)
  588. return -EINVAL;
  589. if (!argc)
  590. return 0;
  591. do {
  592. param_name = shift(as);
  593. argc--;
  594. if (!strnicmp(param_name, MESG_STR("queue_if_no_path"))) {
  595. r = queue_if_no_path(m, 1, 0);
  596. continue;
  597. }
  598. if (!strnicmp(param_name, MESG_STR("pg_init_retries")) &&
  599. (argc >= 1)) {
  600. r = read_param(_params + 1, shift(as),
  601. &m->pg_init_retries, &ti->error);
  602. argc--;
  603. continue;
  604. }
  605. ti->error = "Unrecognised multipath feature request";
  606. r = -EINVAL;
  607. } while (argc && !r);
  608. return r;
  609. }
  610. static int multipath_ctr(struct dm_target *ti, unsigned int argc,
  611. char **argv)
  612. {
  613. /* target parameters */
  614. static struct param _params[] = {
  615. {1, 1024, "invalid number of priority groups"},
  616. {1, 1024, "invalid initial priority group number"},
  617. };
  618. int r;
  619. struct multipath *m;
  620. struct arg_set as;
  621. unsigned pg_count = 0;
  622. unsigned next_pg_num;
  623. as.argc = argc;
  624. as.argv = argv;
  625. m = alloc_multipath(ti);
  626. if (!m) {
  627. ti->error = "can't allocate multipath";
  628. return -EINVAL;
  629. }
  630. r = parse_features(&as, m);
  631. if (r)
  632. goto bad;
  633. r = parse_hw_handler(&as, m);
  634. if (r)
  635. goto bad;
  636. r = read_param(_params, shift(&as), &m->nr_priority_groups, &ti->error);
  637. if (r)
  638. goto bad;
  639. r = read_param(_params + 1, shift(&as), &next_pg_num, &ti->error);
  640. if (r)
  641. goto bad;
  642. /* parse the priority groups */
  643. while (as.argc) {
  644. struct priority_group *pg;
  645. pg = parse_priority_group(&as, m);
  646. if (IS_ERR(pg)) {
  647. r = PTR_ERR(pg);
  648. goto bad;
  649. }
  650. m->nr_valid_paths += pg->nr_pgpaths;
  651. list_add_tail(&pg->list, &m->priority_groups);
  652. pg_count++;
  653. pg->pg_num = pg_count;
  654. if (!--next_pg_num)
  655. m->next_pg = pg;
  656. }
  657. if (pg_count != m->nr_priority_groups) {
  658. ti->error = "priority group count mismatch";
  659. r = -EINVAL;
  660. goto bad;
  661. }
  662. return 0;
  663. bad:
  664. free_multipath(m);
  665. return r;
  666. }
  667. static void multipath_dtr(struct dm_target *ti)
  668. {
  669. struct multipath *m = (struct multipath *) ti->private;
  670. flush_workqueue(kmpath_handlerd);
  671. flush_workqueue(kmultipathd);
  672. free_multipath(m);
  673. }
  674. /*
  675. * Map bios, recording original fields for later in case we have to resubmit
  676. */
  677. static int multipath_map(struct dm_target *ti, struct bio *bio,
  678. union map_info *map_context)
  679. {
  680. int r;
  681. struct dm_mpath_io *mpio;
  682. struct multipath *m = (struct multipath *) ti->private;
  683. mpio = mempool_alloc(m->mpio_pool, GFP_NOIO);
  684. dm_bio_record(&mpio->details, bio);
  685. map_context->ptr = mpio;
  686. bio->bi_rw |= (1 << BIO_RW_FAILFAST_TRANSPORT);
  687. r = map_io(m, bio, mpio, 0);
  688. if (r < 0 || r == DM_MAPIO_REQUEUE)
  689. mempool_free(mpio, m->mpio_pool);
  690. return r;
  691. }
  692. /*
  693. * Take a path out of use.
  694. */
  695. static int fail_path(struct pgpath *pgpath)
  696. {
  697. unsigned long flags;
  698. struct multipath *m = pgpath->pg->m;
  699. spin_lock_irqsave(&m->lock, flags);
  700. if (!pgpath->is_active)
  701. goto out;
  702. DMWARN("Failing path %s.", pgpath->path.dev->name);
  703. pgpath->pg->ps.type->fail_path(&pgpath->pg->ps, &pgpath->path);
  704. pgpath->is_active = 0;
  705. pgpath->fail_count++;
  706. m->nr_valid_paths--;
  707. if (pgpath == m->current_pgpath)
  708. m->current_pgpath = NULL;
  709. dm_path_uevent(DM_UEVENT_PATH_FAILED, m->ti,
  710. pgpath->path.dev->name, m->nr_valid_paths);
  711. schedule_work(&m->trigger_event);
  712. queue_work(kmultipathd, &pgpath->deactivate_path);
  713. out:
  714. spin_unlock_irqrestore(&m->lock, flags);
  715. return 0;
  716. }
  717. /*
  718. * Reinstate a previously-failed path
  719. */
  720. static int reinstate_path(struct pgpath *pgpath)
  721. {
  722. int r = 0;
  723. unsigned long flags;
  724. struct multipath *m = pgpath->pg->m;
  725. spin_lock_irqsave(&m->lock, flags);
  726. if (pgpath->is_active)
  727. goto out;
  728. if (!pgpath->pg->ps.type->reinstate_path) {
  729. DMWARN("Reinstate path not supported by path selector %s",
  730. pgpath->pg->ps.type->name);
  731. r = -EINVAL;
  732. goto out;
  733. }
  734. r = pgpath->pg->ps.type->reinstate_path(&pgpath->pg->ps, &pgpath->path);
  735. if (r)
  736. goto out;
  737. pgpath->is_active = 1;
  738. m->current_pgpath = NULL;
  739. if (!m->nr_valid_paths++ && m->queue_size)
  740. queue_work(kmultipathd, &m->process_queued_ios);
  741. dm_path_uevent(DM_UEVENT_PATH_REINSTATED, m->ti,
  742. pgpath->path.dev->name, m->nr_valid_paths);
  743. schedule_work(&m->trigger_event);
  744. out:
  745. spin_unlock_irqrestore(&m->lock, flags);
  746. return r;
  747. }
  748. /*
  749. * Fail or reinstate all paths that match the provided struct dm_dev.
  750. */
  751. static int action_dev(struct multipath *m, struct dm_dev *dev,
  752. action_fn action)
  753. {
  754. int r = 0;
  755. struct pgpath *pgpath;
  756. struct priority_group *pg;
  757. list_for_each_entry(pg, &m->priority_groups, list) {
  758. list_for_each_entry(pgpath, &pg->pgpaths, list) {
  759. if (pgpath->path.dev == dev)
  760. r = action(pgpath);
  761. }
  762. }
  763. return r;
  764. }
  765. /*
  766. * Temporarily try to avoid having to use the specified PG
  767. */
  768. static void bypass_pg(struct multipath *m, struct priority_group *pg,
  769. int bypassed)
  770. {
  771. unsigned long flags;
  772. spin_lock_irqsave(&m->lock, flags);
  773. pg->bypassed = bypassed;
  774. m->current_pgpath = NULL;
  775. m->current_pg = NULL;
  776. spin_unlock_irqrestore(&m->lock, flags);
  777. schedule_work(&m->trigger_event);
  778. }
  779. /*
  780. * Switch to using the specified PG from the next I/O that gets mapped
  781. */
  782. static int switch_pg_num(struct multipath *m, const char *pgstr)
  783. {
  784. struct priority_group *pg;
  785. unsigned pgnum;
  786. unsigned long flags;
  787. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  788. (pgnum > m->nr_priority_groups)) {
  789. DMWARN("invalid PG number supplied to switch_pg_num");
  790. return -EINVAL;
  791. }
  792. spin_lock_irqsave(&m->lock, flags);
  793. list_for_each_entry(pg, &m->priority_groups, list) {
  794. pg->bypassed = 0;
  795. if (--pgnum)
  796. continue;
  797. m->current_pgpath = NULL;
  798. m->current_pg = NULL;
  799. m->next_pg = pg;
  800. }
  801. spin_unlock_irqrestore(&m->lock, flags);
  802. schedule_work(&m->trigger_event);
  803. return 0;
  804. }
  805. /*
  806. * Set/clear bypassed status of a PG.
  807. * PGs are numbered upwards from 1 in the order they were declared.
  808. */
  809. static int bypass_pg_num(struct multipath *m, const char *pgstr, int bypassed)
  810. {
  811. struct priority_group *pg;
  812. unsigned pgnum;
  813. if (!pgstr || (sscanf(pgstr, "%u", &pgnum) != 1) || !pgnum ||
  814. (pgnum > m->nr_priority_groups)) {
  815. DMWARN("invalid PG number supplied to bypass_pg");
  816. return -EINVAL;
  817. }
  818. list_for_each_entry(pg, &m->priority_groups, list) {
  819. if (!--pgnum)
  820. break;
  821. }
  822. bypass_pg(m, pg, bypassed);
  823. return 0;
  824. }
  825. /*
  826. * Should we retry pg_init immediately?
  827. */
  828. static int pg_init_limit_reached(struct multipath *m, struct pgpath *pgpath)
  829. {
  830. unsigned long flags;
  831. int limit_reached = 0;
  832. spin_lock_irqsave(&m->lock, flags);
  833. if (m->pg_init_count <= m->pg_init_retries)
  834. m->pg_init_required = 1;
  835. else
  836. limit_reached = 1;
  837. spin_unlock_irqrestore(&m->lock, flags);
  838. return limit_reached;
  839. }
  840. static void pg_init_done(struct dm_path *path, int errors)
  841. {
  842. struct pgpath *pgpath = path_to_pgpath(path);
  843. struct priority_group *pg = pgpath->pg;
  844. struct multipath *m = pg->m;
  845. unsigned long flags;
  846. /* device or driver problems */
  847. switch (errors) {
  848. case SCSI_DH_OK:
  849. break;
  850. case SCSI_DH_NOSYS:
  851. if (!m->hw_handler_name) {
  852. errors = 0;
  853. break;
  854. }
  855. DMERR("Cannot failover device because scsi_dh_%s was not "
  856. "loaded.", m->hw_handler_name);
  857. /*
  858. * Fail path for now, so we do not ping pong
  859. */
  860. fail_path(pgpath);
  861. break;
  862. case SCSI_DH_DEV_TEMP_BUSY:
  863. /*
  864. * Probably doing something like FW upgrade on the
  865. * controller so try the other pg.
  866. */
  867. bypass_pg(m, pg, 1);
  868. break;
  869. /* TODO: For SCSI_DH_RETRY we should wait a couple seconds */
  870. case SCSI_DH_RETRY:
  871. case SCSI_DH_IMM_RETRY:
  872. case SCSI_DH_RES_TEMP_UNAVAIL:
  873. if (pg_init_limit_reached(m, pgpath))
  874. fail_path(pgpath);
  875. errors = 0;
  876. break;
  877. default:
  878. /*
  879. * We probably do not want to fail the path for a device
  880. * error, but this is what the old dm did. In future
  881. * patches we can do more advanced handling.
  882. */
  883. fail_path(pgpath);
  884. }
  885. spin_lock_irqsave(&m->lock, flags);
  886. if (errors) {
  887. DMERR("Could not failover device. Error %d.", errors);
  888. m->current_pgpath = NULL;
  889. m->current_pg = NULL;
  890. } else if (!m->pg_init_required) {
  891. m->queue_io = 0;
  892. pg->bypassed = 0;
  893. }
  894. m->pg_init_in_progress = 0;
  895. queue_work(kmultipathd, &m->process_queued_ios);
  896. spin_unlock_irqrestore(&m->lock, flags);
  897. }
  898. static void activate_path(struct work_struct *work)
  899. {
  900. int ret;
  901. struct multipath *m =
  902. container_of(work, struct multipath, activate_path);
  903. struct dm_path *path;
  904. unsigned long flags;
  905. spin_lock_irqsave(&m->lock, flags);
  906. path = &m->pgpath_to_activate->path;
  907. m->pgpath_to_activate = NULL;
  908. spin_unlock_irqrestore(&m->lock, flags);
  909. if (!path)
  910. return;
  911. ret = scsi_dh_activate(bdev_get_queue(path->dev->bdev));
  912. pg_init_done(path, ret);
  913. }
  914. /*
  915. * end_io handling
  916. */
  917. static int do_end_io(struct multipath *m, struct bio *bio,
  918. int error, struct dm_mpath_io *mpio)
  919. {
  920. unsigned long flags;
  921. if (!error)
  922. return 0; /* I/O complete */
  923. if ((error == -EWOULDBLOCK) && bio_rw_ahead(bio))
  924. return error;
  925. if (error == -EOPNOTSUPP)
  926. return error;
  927. spin_lock_irqsave(&m->lock, flags);
  928. if (!m->nr_valid_paths) {
  929. if (__must_push_back(m)) {
  930. spin_unlock_irqrestore(&m->lock, flags);
  931. return DM_ENDIO_REQUEUE;
  932. } else if (!m->queue_if_no_path) {
  933. spin_unlock_irqrestore(&m->lock, flags);
  934. return -EIO;
  935. } else {
  936. spin_unlock_irqrestore(&m->lock, flags);
  937. goto requeue;
  938. }
  939. }
  940. spin_unlock_irqrestore(&m->lock, flags);
  941. if (mpio->pgpath)
  942. fail_path(mpio->pgpath);
  943. requeue:
  944. dm_bio_restore(&mpio->details, bio);
  945. /* queue for the daemon to resubmit or fail */
  946. spin_lock_irqsave(&m->lock, flags);
  947. bio_list_add(&m->queued_ios, bio);
  948. m->queue_size++;
  949. if (!m->queue_io)
  950. queue_work(kmultipathd, &m->process_queued_ios);
  951. spin_unlock_irqrestore(&m->lock, flags);
  952. return DM_ENDIO_INCOMPLETE; /* io not complete */
  953. }
  954. static int multipath_end_io(struct dm_target *ti, struct bio *bio,
  955. int error, union map_info *map_context)
  956. {
  957. struct multipath *m = ti->private;
  958. struct dm_mpath_io *mpio = map_context->ptr;
  959. struct pgpath *pgpath = mpio->pgpath;
  960. struct path_selector *ps;
  961. int r;
  962. r = do_end_io(m, bio, error, mpio);
  963. if (pgpath) {
  964. ps = &pgpath->pg->ps;
  965. if (ps->type->end_io)
  966. ps->type->end_io(ps, &pgpath->path);
  967. }
  968. if (r != DM_ENDIO_INCOMPLETE)
  969. mempool_free(mpio, m->mpio_pool);
  970. return r;
  971. }
  972. /*
  973. * Suspend can't complete until all the I/O is processed so if
  974. * the last path fails we must error any remaining I/O.
  975. * Note that if the freeze_bdev fails while suspending, the
  976. * queue_if_no_path state is lost - userspace should reset it.
  977. */
  978. static void multipath_presuspend(struct dm_target *ti)
  979. {
  980. struct multipath *m = (struct multipath *) ti->private;
  981. queue_if_no_path(m, 0, 1);
  982. }
  983. /*
  984. * Restore the queue_if_no_path setting.
  985. */
  986. static void multipath_resume(struct dm_target *ti)
  987. {
  988. struct multipath *m = (struct multipath *) ti->private;
  989. unsigned long flags;
  990. spin_lock_irqsave(&m->lock, flags);
  991. m->queue_if_no_path = m->saved_queue_if_no_path;
  992. spin_unlock_irqrestore(&m->lock, flags);
  993. }
  994. /*
  995. * Info output has the following format:
  996. * num_multipath_feature_args [multipath_feature_args]*
  997. * num_handler_status_args [handler_status_args]*
  998. * num_groups init_group_number
  999. * [A|D|E num_ps_status_args [ps_status_args]*
  1000. * num_paths num_selector_args
  1001. * [path_dev A|F fail_count [selector_args]* ]+ ]+
  1002. *
  1003. * Table output has the following format (identical to the constructor string):
  1004. * num_feature_args [features_args]*
  1005. * num_handler_args hw_handler [hw_handler_args]*
  1006. * num_groups init_group_number
  1007. * [priority selector-name num_ps_args [ps_args]*
  1008. * num_paths num_selector_args [path_dev [selector_args]* ]+ ]+
  1009. */
  1010. static int multipath_status(struct dm_target *ti, status_type_t type,
  1011. char *result, unsigned int maxlen)
  1012. {
  1013. int sz = 0;
  1014. unsigned long flags;
  1015. struct multipath *m = (struct multipath *) ti->private;
  1016. struct priority_group *pg;
  1017. struct pgpath *p;
  1018. unsigned pg_num;
  1019. char state;
  1020. spin_lock_irqsave(&m->lock, flags);
  1021. /* Features */
  1022. if (type == STATUSTYPE_INFO)
  1023. DMEMIT("2 %u %u ", m->queue_size, m->pg_init_count);
  1024. else {
  1025. DMEMIT("%u ", m->queue_if_no_path +
  1026. (m->pg_init_retries > 0) * 2);
  1027. if (m->queue_if_no_path)
  1028. DMEMIT("queue_if_no_path ");
  1029. if (m->pg_init_retries)
  1030. DMEMIT("pg_init_retries %u ", m->pg_init_retries);
  1031. }
  1032. if (!m->hw_handler_name || type == STATUSTYPE_INFO)
  1033. DMEMIT("0 ");
  1034. else
  1035. DMEMIT("1 %s ", m->hw_handler_name);
  1036. DMEMIT("%u ", m->nr_priority_groups);
  1037. if (m->next_pg)
  1038. pg_num = m->next_pg->pg_num;
  1039. else if (m->current_pg)
  1040. pg_num = m->current_pg->pg_num;
  1041. else
  1042. pg_num = 1;
  1043. DMEMIT("%u ", pg_num);
  1044. switch (type) {
  1045. case STATUSTYPE_INFO:
  1046. list_for_each_entry(pg, &m->priority_groups, list) {
  1047. if (pg->bypassed)
  1048. state = 'D'; /* Disabled */
  1049. else if (pg == m->current_pg)
  1050. state = 'A'; /* Currently Active */
  1051. else
  1052. state = 'E'; /* Enabled */
  1053. DMEMIT("%c ", state);
  1054. if (pg->ps.type->status)
  1055. sz += pg->ps.type->status(&pg->ps, NULL, type,
  1056. result + sz,
  1057. maxlen - sz);
  1058. else
  1059. DMEMIT("0 ");
  1060. DMEMIT("%u %u ", pg->nr_pgpaths,
  1061. pg->ps.type->info_args);
  1062. list_for_each_entry(p, &pg->pgpaths, list) {
  1063. DMEMIT("%s %s %u ", p->path.dev->name,
  1064. p->is_active ? "A" : "F",
  1065. p->fail_count);
  1066. if (pg->ps.type->status)
  1067. sz += pg->ps.type->status(&pg->ps,
  1068. &p->path, type, result + sz,
  1069. maxlen - sz);
  1070. }
  1071. }
  1072. break;
  1073. case STATUSTYPE_TABLE:
  1074. list_for_each_entry(pg, &m->priority_groups, list) {
  1075. DMEMIT("%s ", pg->ps.type->name);
  1076. if (pg->ps.type->status)
  1077. sz += pg->ps.type->status(&pg->ps, NULL, type,
  1078. result + sz,
  1079. maxlen - sz);
  1080. else
  1081. DMEMIT("0 ");
  1082. DMEMIT("%u %u ", pg->nr_pgpaths,
  1083. pg->ps.type->table_args);
  1084. list_for_each_entry(p, &pg->pgpaths, list) {
  1085. DMEMIT("%s ", p->path.dev->name);
  1086. if (pg->ps.type->status)
  1087. sz += pg->ps.type->status(&pg->ps,
  1088. &p->path, type, result + sz,
  1089. maxlen - sz);
  1090. }
  1091. }
  1092. break;
  1093. }
  1094. spin_unlock_irqrestore(&m->lock, flags);
  1095. return 0;
  1096. }
  1097. static int multipath_message(struct dm_target *ti, unsigned argc, char **argv)
  1098. {
  1099. int r;
  1100. struct dm_dev *dev;
  1101. struct multipath *m = (struct multipath *) ti->private;
  1102. action_fn action;
  1103. if (argc == 1) {
  1104. if (!strnicmp(argv[0], MESG_STR("queue_if_no_path")))
  1105. return queue_if_no_path(m, 1, 0);
  1106. else if (!strnicmp(argv[0], MESG_STR("fail_if_no_path")))
  1107. return queue_if_no_path(m, 0, 0);
  1108. }
  1109. if (argc != 2)
  1110. goto error;
  1111. if (!strnicmp(argv[0], MESG_STR("disable_group")))
  1112. return bypass_pg_num(m, argv[1], 1);
  1113. else if (!strnicmp(argv[0], MESG_STR("enable_group")))
  1114. return bypass_pg_num(m, argv[1], 0);
  1115. else if (!strnicmp(argv[0], MESG_STR("switch_group")))
  1116. return switch_pg_num(m, argv[1]);
  1117. else if (!strnicmp(argv[0], MESG_STR("reinstate_path")))
  1118. action = reinstate_path;
  1119. else if (!strnicmp(argv[0], MESG_STR("fail_path")))
  1120. action = fail_path;
  1121. else
  1122. goto error;
  1123. r = dm_get_device(ti, argv[1], ti->begin, ti->len,
  1124. dm_table_get_mode(ti->table), &dev);
  1125. if (r) {
  1126. DMWARN("message: error getting device %s",
  1127. argv[1]);
  1128. return -EINVAL;
  1129. }
  1130. r = action_dev(m, dev, action);
  1131. dm_put_device(ti, dev);
  1132. return r;
  1133. error:
  1134. DMWARN("Unrecognised multipath message received.");
  1135. return -EINVAL;
  1136. }
  1137. static int multipath_ioctl(struct dm_target *ti, unsigned int cmd,
  1138. unsigned long arg)
  1139. {
  1140. struct multipath *m = (struct multipath *) ti->private;
  1141. struct block_device *bdev = NULL;
  1142. fmode_t mode = 0;
  1143. unsigned long flags;
  1144. int r = 0;
  1145. spin_lock_irqsave(&m->lock, flags);
  1146. if (!m->current_pgpath)
  1147. __choose_pgpath(m);
  1148. if (m->current_pgpath) {
  1149. bdev = m->current_pgpath->path.dev->bdev;
  1150. mode = m->current_pgpath->path.dev->mode;
  1151. }
  1152. if (m->queue_io)
  1153. r = -EAGAIN;
  1154. else if (!bdev)
  1155. r = -EIO;
  1156. spin_unlock_irqrestore(&m->lock, flags);
  1157. return r ? : __blkdev_driver_ioctl(bdev, mode, 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. destroy_workqueue(kmpath_handlerd);
  1218. destroy_workqueue(kmultipathd);
  1219. dm_unregister_target(&multipath_target);
  1220. kmem_cache_destroy(_mpio_cache);
  1221. }
  1222. module_init(dm_multipath_init);
  1223. module_exit(dm_multipath_exit);
  1224. MODULE_DESCRIPTION(DM_NAME " multipath target");
  1225. MODULE_AUTHOR("Sistina Software <dm-devel@redhat.com>");
  1226. MODULE_LICENSE("GPL");