dm-mpath.c 34 KB

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