dm-mpath.c 33 KB

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