dm-mpath.c 34 KB

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