dm-mpath.c 37 KB

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