dm-mpath.c 34 KB

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