dm-mpath.c 34 KB

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