dm-mpath.c 33 KB

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