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