dm-mpath.c 34 KB

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