dm-mpath.c 34 KB

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